pull/643/head^2
Travis Fischer 2024-06-02 18:13:24 -05:00
rodzic 4b4e7e0510
commit 65e2e73a00
4 zmienionych plików z 50 dodań i 40 usunięć

Wyświetl plik

@ -18,40 +18,46 @@ async function main() {
{ role: 'user', content: 'What is the weather in San Francisco?' } { role: 'user', content: 'What is the weather in San Francisco?' }
] ]
const res0 = await openai.chat.completions.create({ const tools = weather.tools
messages,
model: 'gpt-4o',
temperature: 0,
tools: weather.tools.specs,
tool_choice: 'required'
})
const message0 = res0.choices[0]?.message!
console.log(JSON.stringify(message0, null, 2))
assert(message0.role === 'assistant')
assert(message0.tool_calls?.[0]?.function?.name === 'get_current_weather')
const getCurrentWeather = weather.tools.get('get_current_weather')!.function {
assert(getCurrentWeather) // First call to OpenAI to invoke the weather tool
const res = await openai.chat.completions.create({
messages,
model: 'gpt-4o',
temperature: 0,
tools: tools.specs,
tool_choice: 'required'
})
const message = res.choices[0]?.message!
console.log(JSON.stringify(message, null, 2))
assert(message.tool_calls?.[0]?.function?.name === 'get_current_weather')
const toolParams = message0.tool_calls[0].function.arguments const fn = tools.get('get_current_weather')!.function
assert(typeof toolParams === 'string') assert(fn)
const toolResult = await getCurrentWeather(toolParams)
messages.push(message0) const toolParams = message.tool_calls[0].function.arguments
messages.push({ const toolResult = await fn(toolParams)
role: 'tool',
tool_call_id: message0.tool_calls[0].id,
content: JSON.stringify(toolResult)
})
const res1 = await openai.chat.completions.create({ messages.push(message)
messages, messages.push({
model: 'gpt-4o', role: 'tool',
temperature: 0, tool_call_id: message.tool_calls[0].id,
tools: weather.tools.specs content: JSON.stringify(toolResult)
}) })
const message1 = res1.choices[0].message }
console.log(JSON.stringify(message1, null, 2))
{
// Second call to OpenAI to generate a text response
const res = await openai.chat.completions.create({
messages,
model: 'gpt-4o',
temperature: 0,
tools: tools.specs
})
const message = res.choices[0].message
console.log(JSON.stringify(message, null, 2))
}
} }
await main() await main()

Wyświetl plik

@ -26,12 +26,15 @@ export function createAIFunction<InputSchema extends z.ZodObject<any>, Return>(
/** Implementation of the function to call with the parsed arguments. */ /** Implementation of the function to call with the parsed arguments. */
implementation: (params: z.infer<InputSchema>) => types.MaybePromise<Return> implementation: (params: z.infer<InputSchema>) => types.MaybePromise<Return>
): types.AIFunction<InputSchema, Return> { ): types.AIFunction<InputSchema, Return> {
assert(spec.name, 'Missing required AIFunction "spec.name"') assert(spec.name, 'createAIFunction missing required "spec.name"')
assert(spec.inputSchema, 'Missing required AIFunction "spec.inputSchema"') assert(
assert(implementation, 'Missing required AIFunction "implementation"') spec.inputSchema,
'createAIFunction missing required "spec.inputSchema"'
)
assert(implementation, 'createAIFunction missing required "implementation"')
assert( assert(
typeof implementation === 'function', typeof implementation === 'function',
'Required AIFunction "implementation" must be a function' 'createAIFunction "implementation" must be a function'
) )
/** Parse the arguments string, optionally reading from a message. */ /** Parse the arguments string, optionally reading from a message. */

Wyświetl plik

@ -3,9 +3,9 @@ import './symbol-polyfill.js'
import type { z } from 'zod' import type { z } from 'zod'
import type * as types from './types.js' import type * as types from './types.js'
import { createAIFunction } from './ai-function.js'
import { AIFunctionSet } from './ai-function-set.js' import { AIFunctionSet } from './ai-function-set.js'
import { AIToolSet } from './ai-tool-set.js' import { AIToolSet } from './ai-tool-set.js'
import { createAIFunction } from './create-ai-function.js'
import { assert } from './utils.js' import { assert } from './utils.js'
export interface Invocable { export interface Invocable {
@ -35,7 +35,7 @@ export abstract class AIToolsProvider {
// console.log({ metadata, invocables }) // console.log({ metadata, invocables })
const aiFunctions = invocables.map((invocable) => { const aiFunctions = invocables.map((invocable) => {
const impl = (this as any)[invocable.methodName]?.bind(this) const impl = (this as any)[invocable.methodName]
assert(impl) assert(impl)
return createAIFunction(invocable, impl) return createAIFunction(invocable, impl)
@ -87,14 +87,15 @@ export function aiFunction<
inputSchema, inputSchema,
methodName methodName
}) })
// console.log({ // console.log({
// name, // name,
// methodName, // methodName,
// context // context
// }) // })
// context.addInitializer(function () { context.addInitializer(function () {
// ;(this as any)[methodName] = (this as any)[methodName].bind(this) ;(this as any)[methodName] = (this as any)[methodName].bind(this)
// }) })
} }
} }

Wyświetl plik

@ -1,6 +1,6 @@
export * from './ai-function.js'
export * from './ai-function-set.js' export * from './ai-function-set.js'
export * from './ai-tool-set.js' export * from './ai-tool-set.js'
export * from './create-ai-function.js'
export * from './errors.js' export * from './errors.js'
export * from './fns.js' export * from './fns.js'
export * from './parse-structured-output.js' export * from './parse-structured-output.js'