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

fix(openai): Support o1 streaming #7229

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ToolMessageChunk,
OpenAIToolCall,
isAIMessage,
convertToChunk,
UsageMetadata,
} from "@langchain/core/messages";
import {
Expand Down Expand Up @@ -1360,19 +1359,6 @@ export class ChatOpenAI<
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
if (this.model.includes("o1-")) {
console.warn(
"[WARNING]: OpenAI o1 models do not yet support token-level streaming. Streaming will yield single chunk."
);
const result = await this._generate(messages, options, runManager);
const messageChunk = convertToChunk(result.generations[0].message);
yield new ChatGenerationChunk({
message: messageChunk,
text:
typeof messageChunk.content === "string" ? messageChunk.content : "",
});
return;
}
const messagesMapped: OpenAICompletionParam[] =
_convertMessagesToOpenAIParams(messages);
const params = {
Expand Down
28 changes: 28 additions & 0 deletions libs/langchain-openai/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,3 +1166,31 @@ describe("Audio output", () => {
).toBeGreaterThan(1);
});
});

test("Can stream o1 requests", async () => {
const model = new ChatOpenAI({
model: "o1-mini",
});
const stream = await model.stream(
"Write me a very simple hello world program in Python. Ensure it is wrapped in a function called 'hello_world' and has descriptive comments."
);
let finalMsg: AIMessageChunk | undefined;
let numChunks = 0;
for await (const chunk of stream) {
finalMsg = finalMsg ? concat(finalMsg, chunk) : chunk;
numChunks += 1;
}

expect(finalMsg).toBeTruthy();
if (!finalMsg) {
throw new Error("No final message found");
}
if (typeof finalMsg.content === "string") {
expect(finalMsg.content.length).toBeGreaterThan(10);
} else {
expect(finalMsg.content.length).toBeGreaterThanOrEqual(1);
}

// A
expect(numChunks).toBeGreaterThan(3);
});
Loading