From aab3d13b423cfb905fefd2a48edff7a83a3900d2 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 4 Aug 2024 07:06:02 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 52 ++++++++++++++-------------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/readme.md b/readme.md index 6597c5a..2750ac1 100644 --- a/readme.md +++ b/readme.md @@ -243,52 +243,28 @@ const messages: OpenAI.ChatCompletionMessageParam[] = [ { role: 'user', content: 'What is the weather in San Francisco?' } ] -{ - // First call to OpenAI to invoke the weather tool - const res = await openai.chat.completions.create({ - messages, - model: 'gpt-4o-mini', - temperature: 0, - tools: weather.functions.toolSpecs, - 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 res = await openai.chat.completions.create({ + messages, + model: 'gpt-4o-mini', + temperature: 0, + tools: weather.functions.toolSpecs, + 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 fn = weather.functions.get('get_current_weather')! - assert(fn) +const fn = weather.functions.get('get_current_weather')! - const toolParams = message.tool_calls[0].function.arguments - const toolResult = await fn(toolParams) - - messages.push(message) - messages.push({ - role: 'tool', - tool_call_id: message.tool_calls[0].id, - content: JSON.stringify(toolResult) - }) -} - -{ - // Second call to OpenAI to generate a text response - const res = await openai.chat.completions.create({ - messages, - model: 'gpt-4o-mini', - temperature: 0, - tools: weather.functions.toolSpecs - }) - const message = res.choices?.[0]?.message - console.log(JSON.stringify(message, null, 2)) -} +const toolParams = message.tool_calls[0].function.arguments +const toolResult = await fn(toolParams) +console.log(JSON.stringify(toolResult, null, 2)) ``` See [examples/openai](./examples/openai) for a full example. -See the [examples](./examples) directory for working examples of how to use each of these adapters. - ### Optimized Imports `@agentic/stdlib` is just a convenience wrapper which re-exports all of the built-in AI tool packages. If you want to optimize your imports, you can replace `@agentic/stdlib` with the specific AI tools you want. For example: