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

Request to stream a chat completion results in buffered response instead of stream #290

Open
au-re opened this issue Apr 6, 2024 · 1 comment
Labels
bug Something isn't working triage

Comments

@au-re
Copy link

au-re commented Apr 6, 2024

What Happened?

I tried running the gateway locally using npx @portkey-ai/gateway, node run dev:node and node build/start-server.js (Node 18). I noticed that even though I was specifying the "stream" parameter in the config the response was buffered and returned all at once to the client instead of streamed back. I also noticed that this is somehow related to the "compress" middleware step. After removing the following middleware, the response was streamed correctly:

// app.use('*', (c, next) => {
//   const runtime = getRuntimeKey();
//   if (runtime !== 'lagon' && runtime !== 'workerd') {
//     return compress()(c, next);
//   }
//   return next();
// });

What Should Have Happened?

The response should be streamed to the client.

Relevant Code Snippet

code on the client side


const run = async () => {

    const client = new OpenAI({
        apiKey: "xxx",
        baseURL: "http://127.0.0.1:8787/v1",
        defaultHeaders: {
            'x-portkey-config': JSON.stringify({
                "provider": "openai",
                "api_key": "xxx",
            })
        },
    });

    const stream = await client.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: [{ role: "system", content: "You are a helpful assistant." }, { role: "user", content: "Write a short story about Mumin" }],
        stream: true,
        max_tokens: 254,
    });

    for await (const part of stream) {
        console.log(part.choices[0]?.delta?.content || "", new Date());
    }
}

run()

Your Twitter/LinkedIn

No response

@au-re au-re added the bug Something isn't working label Apr 6, 2024
@github-actions github-actions bot added the triage label Apr 6, 2024
@mohankumarelec
Copy link

mohankumarelec commented Apr 22, 2024

I am also facing same issue here. Removing

// index.ts
app.use('*', (c, next) => {
    const runtime = getRuntimeKey();
    if (runtime !== 'lagon' && runtime !== 'workerd') {
      return compress()(c, next);
    }
    return next();
});

and removing content-encoding by adding below in updateResponseHeaders

// handlerUtils.ts
response.headers.delete('content-encoding')

Solved it. Any permanent fix available ? @VisargD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

2 participants