Skip to content

Commit

Permalink
openai[patch]: Adds OpenAI system_fingerprint to response_metadata (#…
Browse files Browse the repository at this point in the history
…6107)

* Adds OpenAI system_fingerprint to response_metadata

* Fix lint
  • Loading branch information
jacoblee93 authored Jul 17, 2024
1 parent 5633484 commit 7e2e254
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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

0 comments on commit 7e2e254

Please sign in to comment.