AI SDKs
OpenAI
How to use Agentic with the OpenAI TS SDK directly.
There’s no need for an adapter with the OpenAI SDK since all agentic tools are
compatible with OpenAI by default. You can use AIFunctionSet.specs
for
function calling or AIFunctionSet.toolSpecs
for parallel tool calling.
Install
Usage
import { WeatherClient } from '@agentic/stdlib'
import OpenAI from 'openai'
const weather = new WeatherClient()
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?' }
]
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')!
const toolParams = message.tool_calls[0].function.arguments
const toolResult = await fn(toolParams)
console.log(JSON.stringify(toolResult, null, 2))
Running this example
You’ll need a free API key from weatherapi.com
to run this example. Store it in a local .env
file as WEATHER_API_KEY
.
You’ll need an OpenAI API key
to run this example. Store it in a local .env
file as OPENAI_API_KEY
.
git clone git@github.com:transitive-bullshit/agentic.git
cd agentic
pnpm install
echo 'WEATHER_API_KEY=your-key' >> .env
echo 'OPENAI_API_KEY=your-key' >> .env
npx tsx examples/openai/bin/weather.ts