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

incapable to work against openai #10

Open
Pavel-Husakouski opened this issue Jan 24, 2025 · 0 comments
Open

incapable to work against openai #10

Pavel-Husakouski opened this issue Jan 24, 2025 · 0 comments

Comments

@Pavel-Husakouski
Copy link

Pavel-Husakouski commented Jan 24, 2025

The done value should be used to close the stream

      const { done, value } = await reader.read();

Looks like this code is a bit weak to handle the invalid stream

 // HTTP 204 means "close the connection, no more data will be sent"
    if (status === 204) { 
      onDisconnect()
      close()
      return
    }

code to reproduce

async function main() {
  const es = createEventSource({
    url: 'https://api.openai.com/v1/chat/completions',
    method: 'POST',
    body: JSON.stringify({
      model: 'gpt-4o-mini',
      messages: [
        {role: 'system', content: 'You are a helpful assistant.'},
        {role: 'user', content: 'create some longest possible esseay on what is the purpose of life?'},
      ],
      max_tokens: 5000,
      temperature: 0.5,
      stream: true,
    }),
    headers: {
      'content-type': 'application/json',
      authorization: 'Bearer ' + 'sk-xxx',
    }
  })

  let seenMessages = 0
  for await (const { data, event, id } of es) {
    console.log(data)
  }

// IMPORTANT: EventSource is _not_ closed automatically when breaking out of
// loop. You must manually call `close()` to close the connection.
  es.close()
}
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

1 participant