Skip to content

Commit

Permalink
Update completion example.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Mar 19, 2024
1 parent e66c33b commit e6d90c2
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions examples/next-openai/app/api/completion/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import OpenAI from 'openai';
import {
OpenAIStream,
StreamingTextResponse,
experimental_StreamData,
} from 'ai';
import { StreamingTextResponse, experimental_StreamData } from 'ai';
import { streamText } from 'ai/core';
import { openai } from 'ai/openai';

// Create an OpenAI API client (that's edge friendly!)
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
});

// IMPORTANT! Set the runtime to edge
export const runtime = 'edge';

export async function POST(req: Request) {
// Extract the `prompt` from the body of the request
const { prompt } = await req.json();

// Ask OpenAI for a streaming completion given the prompt
const response = await openai.completions.create({
model: 'gpt-3.5-turbo-instruct',
max_tokens: 2000,
stream: true,
const result = await streamText({
model: openai.completion('gpt-3.5-turbo-instruct'),
maxTokens: 2000,
prompt,
});

Expand All @@ -31,7 +20,7 @@ export async function POST(req: Request) {
data.append({ test: 'value' });

// Convert the response into a friendly text-stream
const stream = OpenAIStream(response, {
const stream = result.toAIStream({
onFinal(completion) {
data.close();
},
Expand Down

0 comments on commit e6d90c2

Please sign in to comment.