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

Chat Completion Readable Stream #31

Open
stephenjason89 opened this issue Oct 19, 2023 · 1 comment
Open

Chat Completion Readable Stream #31

stephenjason89 opened this issue Oct 19, 2023 · 1 comment

Comments

@stephenjason89
Copy link

Is this function already implemented?

@kyr0
Copy link

kyr0 commented Feb 16, 2024

Example code:

// https://github.com/dexaai/openai-fetch
import { OpenAIClient, type ChatParams } from 'openai-fetch';

const client = new OpenAIClient({ apiKey: 'YOUR_KEY' });

let partialResponseText = ''

async function readStreamChunks(stream: ReadableStream, cb: Function) {
    const reader = stream.getReader();
  
    // A function to handle reading each chunk
    async function read() {
      const { done, value } = await reader.read();
      if (done) {
        cb();
        return;
      }
      
      console.log('delta chunk response', value.choices[0].delta.content)
      partialResponseText += value.choices[0].delta.content
      console.log('concat partial response', partialResponseText)
  
      // Recursively read the next chunk
      read();
    }
  
    // Start reading
    read();
  }
  
const readableStream: ChatStreamResponse = await client.streamChatCompletion({
    model: "gpt-4-turbo-preview", // make sure on platform.openai.com that you have access to that model or choose another
    messages: [
        {
            role: "system",
            content: "Tell me a story about openai-fetch, a library that can stream OpenAI responses."
        },
    ],
    temperature: 0.7,
    n: 1,
} as ChatParams);

// start reading the stream
readStreamChunks(readableStream, () => {
    console.log('full response ready', partialResponseText)
})

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

No branches or pull requests

2 participants