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

openai[patch]: Adds OpenAI system_fingerprint to response_metadata #6107

Merged
merged 2 commits into from
Jul 17, 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
10 changes: 10 additions & 0 deletions libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,18 @@ function openAIResponseToChatMessage(
if (includeRawResponse !== undefined) {
additional_kwargs.__raw_response = rawResponse;
}
let response_metadata: Record<string, unknown> | undefined;
if (rawResponse.system_fingerprint) {
response_metadata = {
system_fingerprint: rawResponse.system_fingerprint,
};
}
return new AIMessage({
content: message.content || "",
tool_calls: toolCalls,
invalid_tool_calls: invalidToolCalls,
additional_kwargs,
response_metadata,
id: rawResponse.id,
});
}
Expand Down Expand Up @@ -681,6 +688,9 @@ export class ChatOpenAI<
const generationInfo: Record<string, any> = { ...newTokenIndices };
if (choice.finish_reason !== undefined) {
generationInfo.finish_reason = choice.finish_reason;
// Only include system fingerprint in the last chunk for now
// to avoid concatenation issues
generationInfo.system_fingerprint = data.system_fingerprint;
}
if (this.logprobs) {
generationInfo.logprobs = choice.logprobs;
Expand Down
11 changes: 9 additions & 2 deletions libs/langchain-openai/src/tests/chat_models-extended.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ test("Test ChatOpenAI seed", async () => {
seed: 123454930394983,
});
const message = new HumanMessage("Say something random!");

const res = await chat.invoke([message]);
console.log(JSON.stringify(res));

const res2 = await chat.invoke([message]);

expect(res.response_metadata.system_fingerprint).toBeDefined();
expect(res2.response_metadata.system_fingerprint).toBeDefined();

// These are unfortunately not consistently the same
delete res.response_metadata.system_fingerprint;
delete res2.response_metadata.system_fingerprint;

const resAsObject = {
...res,
id: undefined,
Expand All @@ -40,7 +48,6 @@ test("Test ChatOpenAI seed", async () => {
id: undefined,
lc_kwargs: { ...res2.lc_kwargs, id: undefined },
};

expect(resAsObject).toEqual(res2AsObject);
});

Expand Down
Loading