pull/659/head
Travis Fischer 2024-08-04 07:06:02 -05:00
rodzic 58a1da40e8
commit aab3d13b42
1 zmienionych plików z 14 dodań i 38 usunięć

Wyświetl plik

@ -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.
</details>
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: