chatgpt-api/docs/marketplace/ts-sdks/ai-sdk.mdx

112 wiersze
2.7 KiB
Markdown

---
title: Vercel AI SDK
description: How to use Agentic tools with the Vercel AI SDK.
---
## Install
<CodeGroup>
```bash npm
npm install ai @agentic/ai-sdk @agentic/platform-tool-client
```
```bash pnpm
pnpm add ai @agentic/ai-sdk @agentic/platform-tool-client
```
```bash bun
bun add ai @agentic/ai-sdk @agentic/platform-tool-client
```
```bash yarn
yarn add ai @agentic/ai-sdk @agentic/platform-tool-client
```
</CodeGroup>
## Usage
This example uses the [`@agentic/search`](https://agentic.so/marketplace/projects/@agentic/search) tool.
```ts
import 'dotenv/config'
import { createAISDKTools } from '@agentic/ai-sdk'
import { AgenticToolClient } from '@agentic/platform-tool-client'
import { createOpenAI } from '@ai-sdk/openai'
import { generateText } from 'ai'
async function main() {
const searchTool = await AgenticToolClient.fromIdentifier('@agentic/search')
const openai = createOpenAI({ compatibility: 'strict' })
const result = await generateText({
model: openai('gpt-4o-mini'),
tools: createAISDKTools(searchTool),
toolChoice: 'required',
temperature: 0,
system: 'You are a helpful assistant. Be as concise as possible.',
prompt: 'What is the weather in San Francisco?'
})
console.log(JSON.stringify(result.toolResults[0], null, 2))
}
await main()
```
<Expandable title="Additional dependencies">
This example also uses the [@ai-sdk/openai](https://ai-sdk.dev/providers/ai-sdk-providers/openai) provider, which adds OpenAI support to the Vercel AI SDK.
_Note that OpenAI is not necessary to use Agentic; this is just an example._
<CodeGroup>
```bash npm
npm install @ai-sdk/openai dotenv
```
```bash pnpm
pnpm add @ai-sdk/openai dotenv
```
```bash bun
bun add @ai-sdk/openai dotenv
```
```bash yarn
yarn add @ai-sdk/openai dotenv
```
</CodeGroup>
</Expandable>
## Running this example
You can view the full source for this example here: https://github.com/transitive-bullshit/agentic/tree/main/examples/ts-sdks/ai-sdk
<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>
<Info>
The
[`@agentic/search`](https://agentic.so/marketplace/projects/@agentic/search)
tool comes with a generous free tier, but once that runs out, you'll need to
sign up for a paid plan and add an `AGENTIC_API_KEY` to your `.env` file.
</Info>
```sh
git clone git@github.com:transitive-bullshit/agentic.git
cd agentic
pnpm install
pnpm build
echo 'OPENAI_API_KEY=your-key' >> .env
npx tsx examples/ts-sdks/ai-sdk/bin/weather.ts
```
## Additional resources
- [`@agentic/ai-sdk` source](https://github.com/transitive-bullshit/agentic/blob/main/stdlib/ai-sdk/src/ai-sdk.ts)
- [Vercel AI SDK docs](https://ai-sdk.dev)