@langchain/openai
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -116,2 +116,3 @@ import { type ClientOptions, OpenAI as OpenAIClient } from "openai"; | ||
organization?: string; | ||
__includeRawResponse?: boolean; | ||
protected client: OpenAIClient; | ||
@@ -118,0 +119,0 @@ protected clientConfig: ClientOptions; |
@@ -46,3 +46,3 @@ import { OpenAI as OpenAIClient } from "openai"; | ||
} | ||
function openAIResponseToChatMessage(message, messageId) { | ||
function openAIResponseToChatMessage(message, rawResponse, includeRawResponse) { | ||
const rawToolCalls = message.tool_calls; | ||
@@ -62,2 +62,15 @@ switch (message.role) { | ||
} | ||
const additional_kwargs = { | ||
function_call: message.function_call, | ||
tool_calls: rawToolCalls, | ||
}; | ||
if (includeRawResponse !== undefined) { | ||
additional_kwargs.__raw_response = rawResponse; | ||
} | ||
let response_metadata; | ||
if (rawResponse.system_fingerprint) { | ||
response_metadata = { | ||
system_fingerprint: rawResponse.system_fingerprint, | ||
}; | ||
} | ||
return new AIMessage({ | ||
@@ -67,7 +80,5 @@ content: message.content || "", | ||
invalid_tool_calls: invalidToolCalls, | ||
additional_kwargs: { | ||
function_call: message.function_call, | ||
tool_calls: rawToolCalls, | ||
}, | ||
id: messageId, | ||
additional_kwargs, | ||
response_metadata, | ||
id: rawResponse.id, | ||
}); | ||
@@ -81,3 +92,3 @@ } | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delta, messageId, defaultRole) { | ||
delta, rawResponse, defaultRole, includeRawResponse) { | ||
const role = delta.role ?? defaultRole; | ||
@@ -99,2 +110,5 @@ const content = delta.content ?? ""; | ||
} | ||
if (includeRawResponse) { | ||
additional_kwargs.__raw_response = rawResponse; | ||
} | ||
if (role === "user") { | ||
@@ -120,3 +134,3 @@ return new HumanMessageChunk({ content }); | ||
additional_kwargs, | ||
id: messageId, | ||
id: rawResponse.id, | ||
}); | ||
@@ -416,2 +430,8 @@ } | ||
}); | ||
Object.defineProperty(this, "__includeRawResponse", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "client", { | ||
@@ -472,2 +492,3 @@ enumerable: true, | ||
this.user = fields?.user; | ||
this.__includeRawResponse = fields?.__includeRawResponse; | ||
if (this.azureOpenAIApiKey || this.azureADTokenProvider) { | ||
@@ -595,3 +616,3 @@ if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) { | ||
} | ||
const chunk = _convertDeltaToMessageChunk(delta, data.id, defaultRole); | ||
const chunk = _convertDeltaToMessageChunk(delta, data, defaultRole, this.__includeRawResponse); | ||
defaultRole = delta.role ?? defaultRole; | ||
@@ -610,2 +631,5 @@ const newTokenIndices = { | ||
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; | ||
} | ||
@@ -707,3 +731,3 @@ if (this.logprobs) { | ||
text, | ||
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data.id), | ||
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data, this.__includeRawResponse), | ||
}; | ||
@@ -710,0 +734,0 @@ generation.generationInfo = { |
@@ -118,3 +118,8 @@ import type { OpenAI as OpenAIClient } from "openai"; | ||
/** ChatGPT messages to pass as a prefix to the prompt */ | ||
prefixMessages?: OpenAIClient.Chat.CreateChatCompletionRequestMessage[]; | ||
prefixMessages?: OpenAIClient.Chat.ChatCompletionMessageParam[]; | ||
/** | ||
* Whether to include the raw OpenAI response in the output message's "additional_kwargs" field. | ||
* Currently in experimental beta. | ||
*/ | ||
__includeRawResponse?: boolean; | ||
} | ||
@@ -121,0 +126,0 @@ export declare interface AzureOpenAIInput { |
{ | ||
"name": "@langchain/openai", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "OpenAI integrations for LangChain.js", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
270439
6839