Integrates OpenAI language and embedding models into applications using Vercel's AI SDK. Supports chat, completion, and embeddings APIs with the @ai-sdk/openai provider.
This skill enables integration of OpenAI's language and embedding models into applications using Vercel's AI SDK. It provides type-safe access to OpenAI's chat, completion, and embeddings APIs through the `@ai-sdk/openai` provider.
Install the `@ai-sdk/openai` package and the core AI SDK:
```bash
npm i @ai-sdk/openai ai
```
Add your OpenAI API key to your environment:
```bash
OPENAI_API_KEY=sk-...
```
Create a provider instance by importing from `@ai-sdk/openai`:
```typescript
import { openai } from '@ai-sdk/openai';
```
You can optionally configure the provider with custom settings:
```typescript
import { createOpenAI } from '@ai-sdk/openai';
const customOpenAI = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: 'https://api.openai.com/v1', // optional
});
```
Use the `generateText` function from the AI SDK with OpenAI models:
```typescript
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const { text } = await generateText({
model: openai('gpt-4-turbo'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(text);
```
For streaming text generation:
```typescript
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
const { textStream } = await streamText({
model: openai('gpt-4-turbo'),
prompt: 'Explain quantum computing in simple terms.',
});
for await (const chunk of textStream) {
process.stdout.write(chunk);
}
```
For generating text embeddings:
```typescript
import { openai } from '@ai-sdk/openai';
import { embed } from 'ai';
const { embedding } = await embed({
model: openai.embedding('text-embedding-3-small'),
value: 'This is a sample text to embed.',
});
console.log(embedding);
```
Common OpenAI models accessible through this provider:
Refer to OpenAI's model documentation for the latest available models.
```typescript
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const result = await generateText({
model: openai('gpt-4-turbo'),
prompt: 'What are the benefits of TypeScript?',
});
console.log(result.text);
```
```typescript
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const result = await generateText({
model: openai('gpt-4-turbo'),
messages: [
{ role: 'user', content: 'What is the capital of France?' },
{ role: 'assistant', content: 'The capital of France is Paris.' },
{ role: 'user', content: 'What is its population?' },
],
});
console.log(result.text);
```
```typescript
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { z } from 'zod';
const result = await generateText({
model: openai('gpt-4-turbo'),
tools: {
weather: {
description: 'Get the weather for a location',
parameters: z.object({
location: z.string().describe('The city name'),
}),
},
},
prompt: 'What is the weather in San Francisco?',
});
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/openai-ai-sdk-integration/raw