chatgpt-api/docs/sdks/openai.mdx

157 wiersze
3.9 KiB
Markdown

---
title: OpenAI
description: How to use Agentic with the OpenAI TS SDK directly.
---
<Note>
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.
</Note>
## Install
<CodeGroup>
```bash npm
npm install @agentic/stdlib openai
```
```bash yarn
yarn add @agentic/stdlib openai
```
```bash pnpm
pnpm add @agentic/stdlib openai
```
</CodeGroup>
## Usage
```ts
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 toolArgs = message.tool_calls[0].function.arguments
const toolResult = await fn(toolArgs)
console.log(JSON.stringify(toolResult, null, 2))
```
### Running this example
<Info>
You'll need a free API key from [weatherapi.com](https://www.weatherapi.com)
to run this example. Store it in a local `.env` file as `WEATHER_API_KEY`.
</Info>
<Info>
You'll need an [OpenAI API key](https://platform.openai.com/docs/quickstart)
to run this example. Store it in a local `.env` file as `OPENAI_API_KEY`.
</Info>
```sh
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
```
## OpenAI Responses API
Agentic also supports the [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses) by referencing `AIFunctionSet.responsesToolSpecs` as in this example:
```ts
import 'dotenv/config'
import { WeatherClient } from '@agentic/stdlib'
import OpenAI from 'openai'
async function main() {
const weather = new WeatherClient()
const openai = new OpenAI()
const messages: OpenAI.Responses.ResponseInput = [
{
role: 'system',
content: 'You are a helpful assistant. Be as concise as possible.'
},
{ role: 'user', content: 'What is the weather in San Francisco?' }
]
{
// Call to OpenAI to invoke the weather tool
const res = await openai.responses.create({
model: 'gpt-4o-mini',
temperature: 0,
tools: weather.functions.responsesToolSpecs,
tool_choice: 'required',
input: messages
})
const message = res.output[0]
console.log(JSON.stringify(message, null, 2))
assert(message?.type === 'function_call')
assert(message.name === 'get_current_weather')
const fn = weather.functions.get('get_current_weather')!
const toolResult = await fn(message.arguments)
messages.push(message)
messages.push({
type: 'function_call_output',
call_id: message.call_id,
output: JSON.stringify(toolResult)
})
}
}
await main()
```
### Running this example
<Info>
You'll need a free API key from [weatherapi.com](https://www.weatherapi.com)
to run this example. Store it in a local `.env` file as `WEATHER_API_KEY`.
</Info>
<Info>
You'll need an [OpenAI API key](https://platform.openai.com/docs/quickstart)
to run this example. Store it in a local `.env` file as `OPENAI_API_KEY`.
</Info>
```sh
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-responses.ts
```