mirror of
https://github.com/run-llama/tool.git
synced 2026-06-30 21:17:57 -04:00
fix: lint
This commit is contained in:
@@ -3,7 +3,7 @@ import { getWeather } from './utils'
|
||||
/**
|
||||
* Get current location
|
||||
*/
|
||||
export function getCurrentLocation() {
|
||||
export function getCurrentLocation () {
|
||||
console.log('Getting current location')
|
||||
return 'London'
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as Tools from './index.llama'
|
||||
import { getTools, registerTools } from '@llamaindex/tool'
|
||||
import { convertTools, registerTools } from '@llamaindex/tool'
|
||||
import { OpenAI } from 'openai'
|
||||
import { inspect } from 'node:util'
|
||||
import { OpenAIAgent } from 'llamaindex'
|
||||
@@ -14,7 +14,7 @@ const openai = new OpenAI()
|
||||
role: 'user',
|
||||
content: 'What\'s my current weather?'
|
||||
}],
|
||||
tools: getTools('openai')
|
||||
tools: convertTools('openai')
|
||||
})
|
||||
|
||||
console.log('response:',
|
||||
@@ -28,7 +28,7 @@ const openai = new OpenAI()
|
||||
role: 'user',
|
||||
content: 'What\'s the weather in London?'
|
||||
}],
|
||||
tools: getTools('openai')
|
||||
tools: convertTools('openai')
|
||||
})
|
||||
|
||||
console.log('response:',
|
||||
@@ -36,7 +36,7 @@ const openai = new OpenAI()
|
||||
}
|
||||
{
|
||||
const agent = new OpenAIAgent({
|
||||
tools: getTools('llamaindex')
|
||||
tools: convertTools('llamaindex')
|
||||
})
|
||||
const response = await agent.chat({
|
||||
message: 'What\'s my current weather?'
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
* @param city The city to get the weather for
|
||||
* @returns The weather for the city, e.g. "Sunny", "Rainy", etc.
|
||||
*/
|
||||
export function getWeather(
|
||||
city: string,
|
||||
export function getWeather (
|
||||
city: string
|
||||
) {
|
||||
return `The weather in ${city} is sunny!`;
|
||||
}
|
||||
return `The weather in ${city} is sunny!`
|
||||
}
|
||||
|
||||
+42
-32
@@ -1,6 +1,7 @@
|
||||
import { ChatCompletionTool } from 'openai/resources/chat/completions'
|
||||
import { Info, store, toolMetadataAtom, toolsAtom } from './internal'
|
||||
import { BaseTool, ToolMetadata } from 'llamaindex'
|
||||
import type { ChatCompletionTool } from 'openai/resources/chat/completions'
|
||||
import { type Info, store, toolMetadataAtom, toolsAtom } from './internal'
|
||||
import type { BaseTool, ToolMetadata } from 'llamaindex'
|
||||
import { atom } from 'jotai/vanilla'
|
||||
|
||||
export type {
|
||||
Info
|
||||
@@ -25,45 +26,54 @@ export function registerTools (
|
||||
)
|
||||
}
|
||||
|
||||
export function getTools (
|
||||
const openaiToolsAtom = atom<ChatCompletionTool[]>(get => {
|
||||
const metadata = get(toolMetadataAtom)
|
||||
return metadata.map(([metadata]) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
parameters: metadata.parameters,
|
||||
name: metadata.name,
|
||||
description: metadata.description
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
const llamaindexToolsAtom = atom<BaseTool[]>(get => {
|
||||
const metadata = get(toolMetadataAtom)
|
||||
const fns = get(toolsAtom)
|
||||
return metadata.map(([metadata, info]) => ({
|
||||
call: (input: Record<string, unknown>) => {
|
||||
const args = Object.entries(info.parameterMapping).
|
||||
reduce((arr, [name, idx]) => {
|
||||
arr[idx] = input[name]
|
||||
return arr
|
||||
}, [] as unknown[])
|
||||
console.debug('find function:', metadata.name, args)
|
||||
const fn = fns[metadata.name]
|
||||
if (!fn) {
|
||||
throw new Error(`Cannot find function to call: ${metadata.name}`)
|
||||
}
|
||||
return fn(...args)
|
||||
},
|
||||
metadata
|
||||
}))
|
||||
})
|
||||
|
||||
export function convertTools (
|
||||
format: 'openai'
|
||||
): ChatCompletionTool[];
|
||||
export function getTools (
|
||||
export function convertTools (
|
||||
format: 'llamaindex'
|
||||
): BaseTool[];
|
||||
export function getTools (
|
||||
export function convertTools (
|
||||
format: string
|
||||
): ChatCompletionTool[] | BaseTool[] {
|
||||
switch (format) {
|
||||
case 'openai': {
|
||||
return store.get(toolMetadataAtom).
|
||||
map<ChatCompletionTool>(([metadata]) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
parameters: metadata.parameters,
|
||||
name: metadata.name,
|
||||
description: metadata.description
|
||||
}
|
||||
}))
|
||||
return store.get(openaiToolsAtom)
|
||||
}
|
||||
case 'llamaindex': {
|
||||
const fns = store.get(toolsAtom)
|
||||
return store.get(toolMetadataAtom).map(([metadata, info]) => ({
|
||||
call: (input: Record<string, unknown>) => {
|
||||
const args = Object.entries(info.parameterMapping).
|
||||
reduce((arr, [name, idx]) => {
|
||||
arr[idx] = input[name]
|
||||
return arr
|
||||
}, [] as unknown[])
|
||||
console.debug('find function:', metadata.name, args)
|
||||
const fn = fns[metadata.name]
|
||||
if (!fn) {
|
||||
throw new Error(`Cannot find function to call: ${metadata.name}`)
|
||||
}
|
||||
return fn(...args)
|
||||
},
|
||||
metadata
|
||||
}))
|
||||
return store.get(llamaindexToolsAtom)
|
||||
}
|
||||
}
|
||||
throw new Error(`Unknown format: ${format}`)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { atom, createStore } from 'jotai/vanilla'
|
||||
import { ToolMetadata } from 'llamaindex'
|
||||
import type { ToolMetadata } from 'llamaindex'
|
||||
|
||||
export type Info = {
|
||||
parameterMapping: Record<string, number>
|
||||
@@ -7,4 +7,4 @@ export type Info = {
|
||||
|
||||
export const store = createStore()
|
||||
export const toolMetadataAtom = atom<[ToolMetadata, Info][]>([])
|
||||
export const toolsAtom = atom<Record<string, (...args: any[]) => any>>({})
|
||||
export const toolsAtom = atom<Record<string, (...args: any[]) => any>>({})
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import type { LoadHook } from 'node:module'
|
||||
import {
|
||||
import type {
|
||||
JSONSchema7,
|
||||
JSONSchema7Definition,
|
||||
JSONSchema7TypeName
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"strictNullChecks": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user