Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got '0:" !"\n' out of very strange characters from the stream since the last update, and the document is not updated. #1370

Closed
Melvynx opened this issue Apr 17, 2024 · 1 comment

Comments

@Melvynx
Copy link

Melvynx commented Apr 17, 2024

Description

Since the last update, the AI SDK now fully supports custom file reading.

CleanShot 2024-04-17 at 14 34 24@2x

Code example

I use ReadStream to read stream from OpenAI :

export const readStream = async (
  stream: ReadableStream,
  onChunkValue: (chunk: string) => void
) => {
  const reader = stream.getReader();
  const decoder = new TextDecoder();
  let done = false;

  let result = '';

  while (!done) {
    // eslint-disable-next-line no-await-in-loop
    const { value, done: doneReading } = await reader.read();
    done = doneReading;
    const chunkValue = decoder.decode(value);

    result += chunkValue;

    onChunkValue(result);
  }
};

And follow the documentation for the app router :

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
 
// Optional, but recommended: run on the edge runtime.
// See https://vercel.com/docs/concepts/functions/edge-functions
export const runtime = 'edge';
 
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY!,
});
 
export async function POST(req: Request) {
  // Extract the `messages` from the body of the request
  const { messages } = await req.json();
 
  // Request the OpenAI API for the response based on the prompt
  const response = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    stream: true,
    messages: messages,
  });
 
  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);
 
  // Respond with the stream
  return new StreamingTextResponse(stream);
}

Additional context

In this file from the Next.js example, I see that the documentation seems to not be updated: https://github.com/vercel/ai/blob/main/examples/next-openai/app/api/chat/route.ts

@lgrammel
Copy link
Collaborator

Duplicate of #1316

See #1316 (comment) for solutions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants