2024-06-02 22:30:39 +00:00
|
|
|
import 'dotenv/config'
|
|
|
|
|
2025-06-29 07:40:38 +00:00
|
|
|
import { AgenticToolClient } from '@agentic/platform-tool-client'
|
2024-06-02 22:30:39 +00:00
|
|
|
import OpenAI from 'openai'
|
|
|
|
|
|
|
|
async function main() {
|
2025-06-29 07:40:38 +00:00
|
|
|
const searchTool = await AgenticToolClient.fromIdentifier('@agentic/search')
|
2024-06-02 22:30:39 +00:00
|
|
|
const openai = new OpenAI()
|
|
|
|
|
|
|
|
const messages: OpenAI.ChatCompletionMessageParam[] = [
|
|
|
|
{
|
|
|
|
role: 'system',
|
|
|
|
content: 'You are a helpful assistant. Be as concise as possible.'
|
|
|
|
},
|
|
|
|
{ role: 'user', content: 'What is the weather in San Francisco?' }
|
|
|
|
]
|
|
|
|
|
2024-06-02 23:13:24 +00:00
|
|
|
{
|
2025-06-29 07:40:38 +00:00
|
|
|
// First call to OpenAI to invoke the tool
|
2024-06-02 23:13:24 +00:00
|
|
|
const res = await openai.chat.completions.create({
|
|
|
|
messages,
|
2024-08-04 10:55:47 +00:00
|
|
|
model: 'gpt-4o-mini',
|
2024-06-02 23:13:24 +00:00
|
|
|
temperature: 0,
|
2025-06-29 07:40:38 +00:00
|
|
|
tools: searchTool.functions.toolSpecs,
|
2024-06-02 23:13:24 +00:00
|
|
|
tool_choice: 'required'
|
|
|
|
})
|
|
|
|
|
2025-06-29 07:40:38 +00:00
|
|
|
const message = res.choices[0]!.message!
|
|
|
|
const toolCall = message.tool_calls![0]!.function!
|
|
|
|
const toolResult = await searchTool.callTool(
|
|
|
|
toolCall.name,
|
|
|
|
toolCall.arguments
|
|
|
|
)
|
2024-06-02 23:13:24 +00:00
|
|
|
|
|
|
|
messages.push(message)
|
|
|
|
messages.push({
|
|
|
|
role: 'tool',
|
2025-06-29 07:40:38 +00:00
|
|
|
tool_call_id: message.tool_calls![0]!.id,
|
2024-06-02 23:13:24 +00:00
|
|
|
content: JSON.stringify(toolResult)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Second call to OpenAI to generate a text response
|
|
|
|
const res = await openai.chat.completions.create({
|
|
|
|
messages,
|
2024-08-04 10:55:47 +00:00
|
|
|
model: 'gpt-4o-mini',
|
2024-06-02 23:13:24 +00:00
|
|
|
temperature: 0,
|
2025-06-29 07:40:38 +00:00
|
|
|
tools: searchTool.functions.toolSpecs
|
2024-06-02 23:13:24 +00:00
|
|
|
})
|
2024-08-04 06:32:30 +00:00
|
|
|
const message = res.choices?.[0]?.message
|
2025-06-29 10:42:43 +00:00
|
|
|
console.log(message?.content)
|
2024-06-02 23:13:24 +00:00
|
|
|
}
|
2024-06-02 22:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await main()
|