-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Streaming broken for ChatOpenAI in version 0.0.132 (and before) #2365
Comments
🤖 Hello, Based on the information you've provided and the context from the LangChain repository, it seems like the issue you're experiencing is related to how streaming is handled in the LangChain framework. In the If you want to handle tokens as they are generated, you would need to modify this code to resolve the Promise or handle the data as soon as each token is received, rather than waiting for the Here is the relevant code: const data = params.stream
? await new Promise<CreateCompletionResponse>((resolve, reject) => {
const choices: CreateCompletionResponseChoicesInner[] = [];
let response: Omit<CreateCompletionResponse, "usage">;
let rejected = false;
let resolved = false;
this.completionWithRetry(
{
...params,
prompt: subPrompts[i],
},
{
signal: options.signal,
...options.options,
adapter: fetchAdapter, // default adapter doesn't do streaming
responseType: "stream",
onmessage: (event) => {
if (event.data?.trim?.() === "[DONE]") {
if (resolved || rejected) {
return;
}
resolved = true;
resolve({
...response,
choices,
});
} else {
// ... rest of the code
}
},
}
).catch((error) => {
if (!rejected) {
rejected = true;
reject(error);
}
});
})
: await this.completionWithRetry(
{
...params,
prompt: subPrompts[i],
},
{
signal: options.signal,
...options.options,
}
); In the context shared, the I hope this helps! If you have any other questions, feel free to ask. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
I can confirm the same issue with When working with a const model = new ChatOpenAI({
openAIApiKey: env.OPENAI_API_KEY,
streaming: true,
});
const chain = new ConversationChain({ llm: model });
const response = await chain.stream({ input: "Generate 500 words of Lorem Ipsum" });
console.log(response); // ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
let reader = response.getReader();
let data = await reader.read();
console.log(data.done); // false
while (!data.done) {
console.log(data.value.response); // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce euismod tellus non tortor ultricies, sit amet porta arcu feugiat. Curabitur hendrerit bibendum odio, a convallis velit molestie id. Nulla quis congue turpis. Sed eu urna quis ligula venenatis laoreet. Sed placerat felis sed ligula eleifend sagittis. Maecenas aliquet sodales neque, non lobortis massa tristique nec. Fusce sagittis bibendum lorem vel imperdiet. Phasellus fringilla molestie nisl id aliquam. Nam at erat ut justo bibendum sagittis. Aenean at finibus turpis. Donec justo dui, dapibus id ligula varius, ultricies feugiat est. Integer luctus lorem at interdum mattis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Etiam sollicitudin urna eget velit cursus consequat. Vivamus vitae tincidunt arcu. Vivamus lacinia hendrerit scelerisque. Suspendisse a fermentum ipsum. Aenean et volutpat quam. Pellentesque nec tortor at mi vestibulum sollicitudin et eu tortor. Nam felis eros, rhoncus non ex nec, varius interdum mi. Praesent a tincidunt nunc, ut scelerisque neque. Phasellus sed orci pulvinar, tristique dui et, ultrices odio. Aliquam erat volutpat. Nulla finibus porta interdum. Quisque fringilla elit ac sem fermentum, a posuere enim ultrices. Nulla mollis vehicula sem, quis efficitur elit pulvinar eget. Fusce in pulvinar leo, vitae porttitor justo. Etiam lacinia, tortor in aliquam vulputate, velit sapien faucibus turpis, sed posuere eros nunc a augue. Ut vitae accumsan risus. Morbi dapibus malesuada justo, sit amet vestibulum nulla mattis a. Proin id fermentum urna. Sed dignissim tortor velit, ut euismod metus ultricies non. Quisque a nulla sodales, lacinia massa in, sagittis ante. Aenean congue, elit sit amet ornare porttitor, nunc tellus tristique nulla, non hendrerit lorem massa a mi. Suspendisse vitae posuere mi. Nullam tincidunt malesuada augue, id tempus quam malesuada in. Mauris aliquet metus at purus vehicula porta. Vestibulum ut mattis mauris. Suspendisse mattis sem eget ante hendrerit iaculis. Aenean ligula turpis, venenatis id ullamcorper ac, dapibus ac neque. Aenean pulvinar pulvinar leo. Sed feugiat, nulla nec iaculis tristique, ante libero auctor ipsum, at aliquam arcu ex sed mauris. Etiam faucibus egestas mauris, id sollicitudin ligula. In dignissim, dolor at pellentesque mattis, sem sem vestibulum felis, vitae dapibus nulla magna sit amet nulla. Donec sed porttitor enim. Sed sed magna ultricies, congue tellus ac, varius nibh.
data = await reader.read();
console.log(data.done); // true
}
console.log(data.done); // true Oddly, I have conducted this test in the simpler fashion by cutting out the |
This integration test seems to log tokens alright for me - can you share anything else about your setup?
|
@msmith-techempower yeah unfortunately that's expected at the moment - the |
We should definitely update the ConversationChain docs with an expression language variant and mark the existing one as soft-deprecated/only if you need a more exotic type of memory. |
I am using Bun by @Jarred-Sumner, and apparently that was the issue. It is working perfectly now in Bun 0.8! |
Ah, that's an important detail! Glad it's resolved |
I am trying to use streaming messages from ChatOpenAI, but streaming seems to have broken at some point. I had it working, and have all the eventemitter+websocket things set up. The problem is that handleLLMNewToken is not receiving the tokens as they are generated, instead all tokens come in at once when the response is finished (at the same time as call() returns the response).
This is the code I am using:
The text was updated successfully, but these errors were encountered: