OpenAI

Use GPT-4o, GPT-4, and GPT-3.5 models

OpenAI

GPT-4o, GPT-4, GPT-3.5 Turbo

The most popular LLM provider. Access GPT-4o, GPT-4 Turbo, and GPT-3.5 models.


Setup

1. Install Packages

npm install @yourgpt/copilot-sdk @yourgpt/llm-sdk openai

2. Get API Key

Get your API key from platform.openai.com

3. Add Environment Variable

.env.local
OPENAI_API_KEY=sk-...

4. Usage

import { generateText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

const result = await generateText({
  model: openai('gpt-4o'),
  prompt: 'Explain quantum computing simply.',
});

console.log(result.text);

5. Streaming (API Route)

app/api/chat/route.ts
import { streamText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: openai('gpt-4o'),
    system: 'You are a helpful assistant.',
    messages,
  });

  return result.toTextStreamResponse();
}

Available Models

// Latest and best
openai('gpt-4o')              // Fastest GPT-4 class
openai('gpt-4o-mini')         // Cost-effective, very capable

// GPT-4 Turbo
openai('gpt-4-turbo')         // 128K context

// GPT-3.5
openai('gpt-3.5-turbo')       // Fast and cheap

Configuration Options

import { openai } from '@yourgpt/llm-sdk/openai';

// Custom API key
const model = openai('gpt-4o', {
  apiKey: 'sk-custom-key',
});

// With generation options
const result = await generateText({
  model: openai('gpt-4o'),
  prompt: 'Hello',
  temperature: 0.7,        // 0-2, default 1
  maxTokens: 4096,         // Max response length
});

Tool Calling

OpenAI has excellent tool/function calling support:

import { generateText, tool } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';
import { z } from 'zod';

const result = await generateText({
  model: openai('gpt-4o'),
  prompt: 'What is the weather in Tokyo?',
  tools: {
    getWeather: tool({
      description: 'Get weather for a city',
      parameters: z.object({
        city: z.string().describe('City name'),
      }),
      execute: async ({ city }) => {
        // Your weather API call
        return { temperature: 22, condition: 'sunny' };
      },
    }),
  },
  maxSteps: 5, // Allow multiple tool calls
});

Vision (Image Input)

GPT-4o supports image analysis. Send images in your messages:

const result = await generateText({
  model: openai('gpt-4o'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: "What's in this image?" },
        { type: 'image', image: base64ImageData },
      ],
    },
  ],
});

With Copilot UI

Use with the Copilot React components:

app/providers.tsx
'use client';

import { CopilotProvider } from '@yourgpt/copilot-sdk/react';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <CopilotProvider runtimeUrl="/api/chat">
      {children}
    </CopilotProvider>
  );
}

Pricing

ModelInputOutput
gpt-4o$2.50/1M tokens$10/1M tokens
gpt-4o-mini$0.15/1M tokens$0.60/1M tokens
gpt-3.5-turbo$0.50/1M tokens$1.50/1M tokens

Prices as of late 2024. Check OpenAI pricing for current rates.


Next Steps

On this page