Skip to content

Commit

Permalink
fix(openai): Avoid thrown error on o1 stream calls (langchain-ai#6747)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored and FilipZmijewski committed Sep 27, 2024
1 parent 842324c commit a608eeb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ToolMessageChunk,
OpenAIToolCall,
isAIMessage,
convertToChunk,
} from "@langchain/core/messages";
import {
type ChatGeneration,
Expand Down Expand Up @@ -1185,6 +1186,19 @@ 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
11 changes: 11 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 @@ -940,3 +940,14 @@ test("populates ID field on AIMessage", async () => {
expect(finalChunk?.id?.length).toBeGreaterThan(1);
expect(finalChunk?.id?.startsWith("chatcmpl-")).toBe(true);
});

test("Test ChatOpenAI stream method", async () => {
const model = new ChatOpenAI({ model: "o1-mini" });
const stream = await model.stream("Print hello world.");
const chunks = [];
for await (const chunk of stream) {
console.log(chunk);
chunks.push(chunk);
}
expect(chunks.length).toEqual(1);
});

0 comments on commit a608eeb

Please sign in to comment.