@langchain/openai 0.6.9 → 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/azure/chat_models.cjs +661 -808
- package/dist/azure/chat_models.cjs.map +1 -0
- package/dist/azure/chat_models.d.cts +449 -0
- package/dist/azure/chat_models.d.cts.map +1 -0
- package/dist/azure/chat_models.d.ts +32 -28
- package/dist/azure/chat_models.d.ts.map +1 -0
- package/dist/azure/chat_models.js +660 -804
- package/dist/azure/chat_models.js.map +1 -0
- package/dist/azure/embeddings.cjs +81 -130
- package/dist/azure/embeddings.cjs.map +1 -0
- package/dist/azure/embeddings.d.cts +27 -0
- package/dist/azure/embeddings.d.cts.map +1 -0
- package/dist/azure/embeddings.d.ts +25 -20
- package/dist/azure/embeddings.d.ts.map +1 -0
- package/dist/azure/embeddings.js +80 -126
- package/dist/azure/embeddings.js.map +1 -0
- package/dist/azure/llms.cjs +109 -168
- package/dist/azure/llms.cjs.map +1 -0
- package/dist/azure/llms.d.cts +33 -0
- package/dist/azure/llms.d.cts.map +1 -0
- package/dist/azure/llms.d.ts +31 -25
- package/dist/azure/llms.d.ts.map +1 -0
- package/dist/azure/llms.js +108 -164
- package/dist/azure/llms.js.map +1 -0
- package/dist/chat_models.cjs +1951 -2774
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +930 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +365 -348
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +1946 -2764
- package/dist/chat_models.js.map +1 -0
- package/dist/embeddings.cjs +136 -193
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +115 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +88 -81
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +134 -188
- package/dist/embeddings.js.map +1 -0
- package/dist/index.cjs +47 -34
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -13
- package/dist/index.js +17 -13
- package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
- package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
- package/dist/llms.cjs +312 -468
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +123 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +93 -87
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +309 -462
- package/dist/llms.js.map +1 -0
- package/dist/tools/custom.cjs +25 -26
- package/dist/tools/custom.cjs.map +1 -0
- package/dist/tools/custom.d.cts +10 -0
- package/dist/tools/custom.d.cts.map +1 -0
- package/dist/tools/custom.d.ts +8 -3
- package/dist/tools/custom.d.ts.map +1 -0
- package/dist/tools/custom.js +24 -23
- package/dist/tools/custom.js.map +1 -0
- package/dist/tools/dalle.cjs +104 -194
- package/dist/tools/dalle.cjs.map +1 -0
- package/dist/tools/dalle.d.cts +123 -0
- package/dist/tools/dalle.d.cts.map +1 -0
- package/dist/tools/dalle.d.ts +111 -104
- package/dist/tools/dalle.d.ts.map +1 -0
- package/dist/tools/dalle.js +102 -189
- package/dist/tools/dalle.js.map +1 -0
- package/dist/tools/index.cjs +1 -17
- package/dist/tools/index.js +1 -1
- package/dist/types.d.cts +263 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +244 -237
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/azure.cjs +86 -47
- package/dist/utils/azure.cjs.map +1 -0
- package/dist/utils/azure.d.cts +76 -0
- package/dist/utils/azure.d.cts.map +1 -0
- package/dist/utils/azure.d.ts +51 -9
- package/dist/utils/azure.d.ts.map +1 -0
- package/dist/utils/azure.js +85 -45
- package/dist/utils/azure.js.map +1 -0
- package/dist/utils/client.cjs +25 -0
- package/dist/utils/client.cjs.map +1 -0
- package/dist/utils/client.d.cts +5 -0
- package/dist/utils/client.d.cts.map +1 -0
- package/dist/utils/client.d.ts +5 -0
- package/dist/utils/client.d.ts.map +1 -0
- package/dist/utils/client.js +24 -0
- package/dist/utils/client.js.map +1 -0
- package/dist/utils/errors.cjs +9 -8
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/errors.js +9 -6
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/message_inputs.cjs +139 -0
- package/dist/utils/message_inputs.cjs.map +1 -0
- package/dist/utils/message_inputs.d.cts +9 -0
- package/dist/utils/message_inputs.d.cts.map +1 -0
- package/dist/utils/message_inputs.d.ts +9 -0
- package/dist/utils/message_inputs.d.ts.map +1 -0
- package/dist/utils/message_inputs.js +137 -0
- package/dist/utils/message_inputs.js.map +1 -0
- package/dist/utils/misc.cjs +30 -0
- package/dist/utils/misc.cjs.map +1 -0
- package/dist/utils/misc.d.cts +9 -0
- package/dist/utils/misc.d.cts.map +1 -0
- package/dist/utils/misc.d.ts +9 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +27 -0
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/output.cjs +74 -0
- package/dist/utils/output.cjs.map +1 -0
- package/dist/utils/output.js +72 -0
- package/dist/utils/output.js.map +1 -0
- package/dist/utils/prompts.cjs +39 -38
- package/dist/utils/prompts.cjs.map +1 -0
- package/dist/utils/prompts.d.cts +42 -0
- package/dist/utils/prompts.d.cts.map +1 -0
- package/dist/utils/prompts.d.ts +10 -4
- package/dist/utils/prompts.d.ts.map +1 -0
- package/dist/utils/prompts.js +40 -36
- package/dist/utils/prompts.js.map +1 -0
- package/dist/utils/standard.cjs +113 -0
- package/dist/utils/standard.cjs.map +1 -0
- package/dist/utils/standard.js +111 -0
- package/dist/utils/standard.js.map +1 -0
- package/dist/utils/tools.cjs +150 -127
- package/dist/utils/tools.cjs.map +1 -0
- package/dist/utils/tools.d.cts +13 -0
- package/dist/utils/tools.d.cts.map +1 -0
- package/dist/utils/tools.d.ts +12 -42
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +146 -123
- package/dist/utils/tools.js.map +1 -0
- package/package.json +32 -40
- package/dist/tools/index.d.ts +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -1
- package/dist/utils/errors.d.ts +0 -2
- package/dist/utils/headers.cjs +0 -39
- package/dist/utils/headers.d.ts +0 -8
- package/dist/utils/headers.js +0 -35
- package/dist/utils/openai-format-fndef.cjs +0 -80
- package/dist/utils/openai-format-fndef.d.ts +0 -44
- package/dist/utils/openai-format-fndef.js +0 -77
- package/dist/utils/openai.cjs +0 -127
- package/dist/utils/openai.d.ts +0 -15
- package/dist/utils/openai.js +0 -119
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/chat_models.js
CHANGED
|
@@ -1,2771 +1,1953 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { iife, isReasoningModel, messageToOpenAIRole } from "./utils/misc.js";
|
|
2
|
+
import { getEndpoint } from "./utils/azure.js";
|
|
3
|
+
import { wrapOpenAIClientError } from "./utils/client.js";
|
|
4
|
+
import { _convertToOpenAITool, convertCompletionsCustomTool, convertResponsesCustomTool, formatFunctionDefinitions, formatToOpenAIToolChoice, isBuiltInTool, isBuiltInToolChoice, isCustomTool, isCustomToolCall, isOpenAICustomTool, parseCustomToolCall } from "./utils/tools.js";
|
|
5
|
+
import { getStructuredOutputMethod, interopZodResponseFormat } from "./utils/output.js";
|
|
6
|
+
import { _convertToResponsesMessageFromV1 } from "./utils/standard.js";
|
|
7
|
+
import { _convertMessagesToOpenAIParams, completionsApiContentBlockConverter } from "./utils/message_inputs.js";
|
|
8
|
+
import { OpenAI as OpenAI$1 } from "openai";
|
|
9
|
+
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, convertToProviderContentBlock, isAIMessage, isDataContentBlock } from "@langchain/core/messages";
|
|
10
|
+
import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
4
11
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
5
|
-
import { BaseChatModel
|
|
6
|
-
import { isOpenAITool
|
|
7
|
-
import { RunnableLambda, RunnablePassthrough, RunnableSequence
|
|
8
|
-
import { JsonOutputParser, StructuredOutputParser
|
|
9
|
-
import { JsonOutputKeyToolsParser,
|
|
10
|
-
import { getSchemaDescription, isInteropZodSchema
|
|
12
|
+
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
13
|
+
import { isOpenAITool } from "@langchain/core/language_models/base";
|
|
14
|
+
import { RunnableLambda, RunnablePassthrough, RunnableSequence } from "@langchain/core/runnables";
|
|
15
|
+
import { JsonOutputParser, StructuredOutputParser } from "@langchain/core/output_parsers";
|
|
16
|
+
import { JsonOutputKeyToolsParser, makeInvalidToolCall, parseToolCall } from "@langchain/core/output_parsers/openai_tools";
|
|
17
|
+
import { getSchemaDescription, isInteropZodSchema } from "@langchain/core/utils/types";
|
|
11
18
|
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import { formatFunctionDefinitions, } from "./utils/openai-format-fndef.js";
|
|
15
|
-
import { _convertToOpenAITool, convertCompletionsCustomTool, convertResponsesCustomTool, isBuiltInTool, isBuiltInToolChoice, isCustomTool, isCustomToolCall, isOpenAICustomTool, parseCustomToolCall, } from "./utils/tools.js";
|
|
19
|
+
|
|
20
|
+
//#region src/chat_models.ts
|
|
16
21
|
const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
|
|
17
|
-
function isReasoningModel(model) {
|
|
18
|
-
if (!model)
|
|
19
|
-
return false;
|
|
20
|
-
if (/^o\d/.test(model ?? ""))
|
|
21
|
-
return true;
|
|
22
|
-
if (model.startsWith("gpt-5") && !model.startsWith("gpt-5-chat"))
|
|
23
|
-
return true;
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
function isStructuredOutputMethodParams(x
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
) {
|
|
29
|
-
return (x !== undefined &&
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
-
typeof x.schema ===
|
|
32
|
-
"object");
|
|
33
|
-
}
|
|
34
|
-
function extractGenericMessageCustomRole(message) {
|
|
35
|
-
if (message.role !== "system" &&
|
|
36
|
-
message.role !== "developer" &&
|
|
37
|
-
message.role !== "assistant" &&
|
|
38
|
-
message.role !== "user" &&
|
|
39
|
-
message.role !== "function" &&
|
|
40
|
-
message.role !== "tool") {
|
|
41
|
-
console.warn(`Unknown message role: ${message.role}`);
|
|
42
|
-
}
|
|
43
|
-
return message.role;
|
|
44
|
-
}
|
|
45
|
-
export function messageToOpenAIRole(message) {
|
|
46
|
-
const type = message._getType();
|
|
47
|
-
switch (type) {
|
|
48
|
-
case "system":
|
|
49
|
-
return "system";
|
|
50
|
-
case "ai":
|
|
51
|
-
return "assistant";
|
|
52
|
-
case "human":
|
|
53
|
-
return "user";
|
|
54
|
-
case "function":
|
|
55
|
-
return "function";
|
|
56
|
-
case "tool":
|
|
57
|
-
return "tool";
|
|
58
|
-
case "generic": {
|
|
59
|
-
if (!ChatMessage.isInstance(message))
|
|
60
|
-
throw new Error("Invalid generic chat message");
|
|
61
|
-
return extractGenericMessageCustomRole(message);
|
|
62
|
-
}
|
|
63
|
-
default:
|
|
64
|
-
throw new Error(`Unknown message type: ${type}`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const completionsApiContentBlockConverter = {
|
|
68
|
-
providerName: "ChatOpenAI",
|
|
69
|
-
fromStandardTextBlock(block) {
|
|
70
|
-
return { type: "text", text: block.text };
|
|
71
|
-
},
|
|
72
|
-
fromStandardImageBlock(block) {
|
|
73
|
-
if (block.source_type === "url") {
|
|
74
|
-
return {
|
|
75
|
-
type: "image_url",
|
|
76
|
-
image_url: {
|
|
77
|
-
url: block.url,
|
|
78
|
-
...(block.metadata?.detail
|
|
79
|
-
? { detail: block.metadata.detail }
|
|
80
|
-
: {}),
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (block.source_type === "base64") {
|
|
85
|
-
const url = `data:${block.mime_type ?? ""};base64,${block.data}`;
|
|
86
|
-
return {
|
|
87
|
-
type: "image_url",
|
|
88
|
-
image_url: {
|
|
89
|
-
url,
|
|
90
|
-
...(block.metadata?.detail
|
|
91
|
-
? { detail: block.metadata.detail }
|
|
92
|
-
: {}),
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
throw new Error(`Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
|
|
97
|
-
},
|
|
98
|
-
fromStandardAudioBlock(block) {
|
|
99
|
-
if (block.source_type === "url") {
|
|
100
|
-
const data = parseBase64DataUrl({ dataUrl: block.url });
|
|
101
|
-
if (!data) {
|
|
102
|
-
throw new Error(`URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
|
|
103
|
-
}
|
|
104
|
-
const rawMimeType = data.mime_type || block.mime_type || "";
|
|
105
|
-
let mimeType;
|
|
106
|
-
try {
|
|
107
|
-
mimeType = parseMimeType(rawMimeType);
|
|
108
|
-
}
|
|
109
|
-
catch {
|
|
110
|
-
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
111
|
-
}
|
|
112
|
-
if (mimeType.type !== "audio" ||
|
|
113
|
-
(mimeType.subtype !== "wav" && mimeType.subtype !== "mp3")) {
|
|
114
|
-
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
115
|
-
}
|
|
116
|
-
return {
|
|
117
|
-
type: "input_audio",
|
|
118
|
-
input_audio: {
|
|
119
|
-
format: mimeType.subtype,
|
|
120
|
-
data: data.data,
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
if (block.source_type === "base64") {
|
|
125
|
-
let mimeType;
|
|
126
|
-
try {
|
|
127
|
-
mimeType = parseMimeType(block.mime_type ?? "");
|
|
128
|
-
}
|
|
129
|
-
catch {
|
|
130
|
-
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
131
|
-
}
|
|
132
|
-
if (mimeType.type !== "audio" ||
|
|
133
|
-
(mimeType.subtype !== "wav" && mimeType.subtype !== "mp3")) {
|
|
134
|
-
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
135
|
-
}
|
|
136
|
-
return {
|
|
137
|
-
type: "input_audio",
|
|
138
|
-
input_audio: {
|
|
139
|
-
format: mimeType.subtype,
|
|
140
|
-
data: block.data,
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
throw new Error(`Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
|
|
145
|
-
},
|
|
146
|
-
fromStandardFileBlock(block) {
|
|
147
|
-
if (block.source_type === "url") {
|
|
148
|
-
const data = parseBase64DataUrl({ dataUrl: block.url });
|
|
149
|
-
if (!data) {
|
|
150
|
-
throw new Error(`URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
|
|
151
|
-
}
|
|
152
|
-
return {
|
|
153
|
-
type: "file",
|
|
154
|
-
file: {
|
|
155
|
-
file_data: block.url, // formatted as base64 data URL
|
|
156
|
-
...(block.metadata?.filename || block.metadata?.name
|
|
157
|
-
? {
|
|
158
|
-
filename: (block.metadata?.filename ||
|
|
159
|
-
block.metadata?.name),
|
|
160
|
-
}
|
|
161
|
-
: {}),
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
if (block.source_type === "base64") {
|
|
166
|
-
return {
|
|
167
|
-
type: "file",
|
|
168
|
-
file: {
|
|
169
|
-
file_data: `data:${block.mime_type ?? ""};base64,${block.data}`,
|
|
170
|
-
...(block.metadata?.filename ||
|
|
171
|
-
block.metadata?.name ||
|
|
172
|
-
block.metadata?.title
|
|
173
|
-
? {
|
|
174
|
-
filename: (block.metadata?.filename ||
|
|
175
|
-
block.metadata?.name ||
|
|
176
|
-
block.metadata?.title),
|
|
177
|
-
}
|
|
178
|
-
: {}),
|
|
179
|
-
},
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
if (block.source_type === "id") {
|
|
183
|
-
return {
|
|
184
|
-
type: "file",
|
|
185
|
-
file: {
|
|
186
|
-
file_id: block.id,
|
|
187
|
-
},
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
throw new Error(`File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
// Used in LangSmith, export is important here
|
|
194
|
-
// TODO: put this conversion elsewhere
|
|
195
|
-
export function _convertMessagesToOpenAIParams(messages, model) {
|
|
196
|
-
// TODO: Function messages do not support array content, fix cast
|
|
197
|
-
return messages.flatMap((message) => {
|
|
198
|
-
let role = messageToOpenAIRole(message);
|
|
199
|
-
if (role === "system" && isReasoningModel(model)) {
|
|
200
|
-
role = "developer";
|
|
201
|
-
}
|
|
202
|
-
const content = typeof message.content === "string"
|
|
203
|
-
? message.content
|
|
204
|
-
: message.content.map((m) => {
|
|
205
|
-
if (isDataContentBlock(m)) {
|
|
206
|
-
return convertToProviderContentBlock(m, completionsApiContentBlockConverter);
|
|
207
|
-
}
|
|
208
|
-
return m;
|
|
209
|
-
});
|
|
210
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
211
|
-
const completionParam = {
|
|
212
|
-
role,
|
|
213
|
-
content,
|
|
214
|
-
};
|
|
215
|
-
if (message.name != null) {
|
|
216
|
-
completionParam.name = message.name;
|
|
217
|
-
}
|
|
218
|
-
if (message.additional_kwargs.function_call != null) {
|
|
219
|
-
completionParam.function_call = message.additional_kwargs.function_call;
|
|
220
|
-
completionParam.content = "";
|
|
221
|
-
}
|
|
222
|
-
if (isAIMessage(message) && !!message.tool_calls?.length) {
|
|
223
|
-
completionParam.tool_calls = message.tool_calls.map(convertLangChainToolCallToOpenAI);
|
|
224
|
-
completionParam.content = "";
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
if (message.additional_kwargs.tool_calls != null) {
|
|
228
|
-
completionParam.tool_calls = message.additional_kwargs.tool_calls;
|
|
229
|
-
}
|
|
230
|
-
if (message.tool_call_id != null) {
|
|
231
|
-
completionParam.tool_call_id = message.tool_call_id;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (message.additional_kwargs.audio &&
|
|
235
|
-
typeof message.additional_kwargs.audio === "object" &&
|
|
236
|
-
"id" in message.additional_kwargs.audio) {
|
|
237
|
-
const audioMessage = {
|
|
238
|
-
role: "assistant",
|
|
239
|
-
audio: {
|
|
240
|
-
id: message.additional_kwargs.audio.id,
|
|
241
|
-
},
|
|
242
|
-
};
|
|
243
|
-
return [
|
|
244
|
-
completionParam,
|
|
245
|
-
audioMessage,
|
|
246
|
-
];
|
|
247
|
-
}
|
|
248
|
-
return completionParam;
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
22
|
/** @internal */
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
tokensPerMessage = 4;
|
|
759
|
-
tokensPerName = -1;
|
|
760
|
-
}
|
|
761
|
-
else {
|
|
762
|
-
tokensPerMessage = 3;
|
|
763
|
-
tokensPerName = 1;
|
|
764
|
-
}
|
|
765
|
-
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
766
|
-
const textCount = await this.getNumTokens(message.content);
|
|
767
|
-
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
768
|
-
const nameCount = message.name !== undefined
|
|
769
|
-
? tokensPerName + (await this.getNumTokens(message.name))
|
|
770
|
-
: 0;
|
|
771
|
-
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
772
|
-
// From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
|
|
773
|
-
const openAIMessage = message;
|
|
774
|
-
if (openAIMessage._getType() === "function") {
|
|
775
|
-
count -= 2;
|
|
776
|
-
}
|
|
777
|
-
if (openAIMessage.additional_kwargs?.function_call) {
|
|
778
|
-
count += 3;
|
|
779
|
-
}
|
|
780
|
-
if (openAIMessage?.additional_kwargs.function_call?.name) {
|
|
781
|
-
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
782
|
-
}
|
|
783
|
-
if (openAIMessage.additional_kwargs.function_call?.arguments) {
|
|
784
|
-
try {
|
|
785
|
-
count += await this.getNumTokens(
|
|
786
|
-
// Remove newlines and spaces
|
|
787
|
-
JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
788
|
-
}
|
|
789
|
-
catch (error) {
|
|
790
|
-
console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
|
|
791
|
-
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
totalCount += count;
|
|
795
|
-
return count;
|
|
796
|
-
}));
|
|
797
|
-
totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
|
|
798
|
-
return { totalCount, countPerMessage };
|
|
799
|
-
}
|
|
800
|
-
/** @internal */
|
|
801
|
-
async _getNumTokensFromGenerations(generations) {
|
|
802
|
-
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
803
|
-
if (generation.message.additional_kwargs?.function_call) {
|
|
804
|
-
return (await this.getNumTokensFromMessages([generation.message]))
|
|
805
|
-
.countPerMessage[0];
|
|
806
|
-
}
|
|
807
|
-
else {
|
|
808
|
-
return await this.getNumTokens(generation.message.content);
|
|
809
|
-
}
|
|
810
|
-
}));
|
|
811
|
-
return generationUsages.reduce((a, b) => a + b, 0);
|
|
812
|
-
}
|
|
813
|
-
/** @internal */
|
|
814
|
-
async _getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
815
|
-
// It appears that if functions are present, the first system message is padded with a trailing newline. This
|
|
816
|
-
// was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
|
|
817
|
-
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
818
|
-
// If there are functions, add the function definitions as they count towards token usage
|
|
819
|
-
if (functions && function_call !== "auto") {
|
|
820
|
-
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
821
|
-
tokens += await this.getNumTokens(promptDefinitions);
|
|
822
|
-
tokens += 9; // Add nine per completion
|
|
823
|
-
}
|
|
824
|
-
// If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
|
|
825
|
-
// functions typically add a system message, but reuse the first one if it's already there. This offsets
|
|
826
|
-
// the extra 9 tokens added by the function definitions.
|
|
827
|
-
if (functions && messages.find((m) => m._getType() === "system")) {
|
|
828
|
-
tokens -= 4;
|
|
829
|
-
}
|
|
830
|
-
// If function_call is 'none', add one token.
|
|
831
|
-
// If it's a FunctionCall object, add 4 + the number of tokens in the function name.
|
|
832
|
-
// If it's undefined or 'auto', don't add anything.
|
|
833
|
-
if (function_call === "none") {
|
|
834
|
-
tokens += 1;
|
|
835
|
-
}
|
|
836
|
-
else if (typeof function_call === "object") {
|
|
837
|
-
tokens += (await this.getNumTokens(function_call.name)) + 4;
|
|
838
|
-
}
|
|
839
|
-
return tokens;
|
|
840
|
-
}
|
|
841
|
-
/** @internal */
|
|
842
|
-
_getStructuredOutputMethod(config) {
|
|
843
|
-
const ensuredConfig = { ...config };
|
|
844
|
-
if (!this.model.startsWith("gpt-3") &&
|
|
845
|
-
!this.model.startsWith("gpt-4-") &&
|
|
846
|
-
this.model !== "gpt-4") {
|
|
847
|
-
if (ensuredConfig?.method === undefined) {
|
|
848
|
-
return "jsonSchema";
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
else if (ensuredConfig.method === "jsonSchema") {
|
|
852
|
-
console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
853
|
-
}
|
|
854
|
-
return ensuredConfig.method;
|
|
855
|
-
}
|
|
856
|
-
withStructuredOutput(outputSchema, config) {
|
|
857
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
858
|
-
let schema;
|
|
859
|
-
let name;
|
|
860
|
-
let method;
|
|
861
|
-
let includeRaw;
|
|
862
|
-
if (isStructuredOutputMethodParams(outputSchema)) {
|
|
863
|
-
schema = outputSchema.schema;
|
|
864
|
-
name = outputSchema.name;
|
|
865
|
-
method = outputSchema.method;
|
|
866
|
-
includeRaw = outputSchema.includeRaw;
|
|
867
|
-
}
|
|
868
|
-
else {
|
|
869
|
-
schema = outputSchema;
|
|
870
|
-
name = config?.name;
|
|
871
|
-
method = config?.method;
|
|
872
|
-
includeRaw = config?.includeRaw;
|
|
873
|
-
}
|
|
874
|
-
let llm;
|
|
875
|
-
let outputParser;
|
|
876
|
-
if (config?.strict !== undefined && method === "jsonMode") {
|
|
877
|
-
throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
878
|
-
}
|
|
879
|
-
method = this._getStructuredOutputMethod({ ...config, method });
|
|
880
|
-
if (method === "jsonMode") {
|
|
881
|
-
if (isInteropZodSchema(schema)) {
|
|
882
|
-
outputParser = StructuredOutputParser.fromZodSchema(schema);
|
|
883
|
-
}
|
|
884
|
-
else {
|
|
885
|
-
outputParser = new JsonOutputParser();
|
|
886
|
-
}
|
|
887
|
-
const asJsonSchema = toJsonSchema(schema);
|
|
888
|
-
llm = this.withConfig({
|
|
889
|
-
response_format: { type: "json_object" },
|
|
890
|
-
ls_structured_output_format: {
|
|
891
|
-
kwargs: { method: "json_mode" },
|
|
892
|
-
schema: { title: name ?? "extract", ...asJsonSchema },
|
|
893
|
-
},
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
else if (method === "jsonSchema") {
|
|
897
|
-
const openaiJsonSchemaParams = {
|
|
898
|
-
name: name ?? "extract",
|
|
899
|
-
description: getSchemaDescription(schema),
|
|
900
|
-
schema,
|
|
901
|
-
strict: config?.strict,
|
|
902
|
-
};
|
|
903
|
-
const asJsonSchema = toJsonSchema(openaiJsonSchemaParams.schema);
|
|
904
|
-
llm = this.withConfig({
|
|
905
|
-
response_format: {
|
|
906
|
-
type: "json_schema",
|
|
907
|
-
json_schema: openaiJsonSchemaParams,
|
|
908
|
-
},
|
|
909
|
-
ls_structured_output_format: {
|
|
910
|
-
kwargs: { method: "json_schema" },
|
|
911
|
-
schema: {
|
|
912
|
-
title: openaiJsonSchemaParams.name,
|
|
913
|
-
description: openaiJsonSchemaParams.description,
|
|
914
|
-
...asJsonSchema,
|
|
915
|
-
},
|
|
916
|
-
},
|
|
917
|
-
});
|
|
918
|
-
if (isInteropZodSchema(schema)) {
|
|
919
|
-
const altParser = StructuredOutputParser.fromZodSchema(schema);
|
|
920
|
-
outputParser = RunnableLambda.from((aiMessage) => {
|
|
921
|
-
if ("parsed" in aiMessage.additional_kwargs) {
|
|
922
|
-
return aiMessage.additional_kwargs.parsed;
|
|
923
|
-
}
|
|
924
|
-
return altParser;
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
else {
|
|
928
|
-
outputParser = new JsonOutputParser();
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
else {
|
|
932
|
-
let functionName = name ?? "extract";
|
|
933
|
-
// Is function calling
|
|
934
|
-
if (isInteropZodSchema(schema)) {
|
|
935
|
-
const asJsonSchema = toJsonSchema(schema);
|
|
936
|
-
llm = this.withConfig({
|
|
937
|
-
tools: [
|
|
938
|
-
{
|
|
939
|
-
type: "function",
|
|
940
|
-
function: {
|
|
941
|
-
name: functionName,
|
|
942
|
-
description: asJsonSchema.description,
|
|
943
|
-
parameters: asJsonSchema,
|
|
944
|
-
},
|
|
945
|
-
},
|
|
946
|
-
],
|
|
947
|
-
tool_choice: {
|
|
948
|
-
type: "function",
|
|
949
|
-
function: {
|
|
950
|
-
name: functionName,
|
|
951
|
-
},
|
|
952
|
-
},
|
|
953
|
-
ls_structured_output_format: {
|
|
954
|
-
kwargs: { method: "function_calling" },
|
|
955
|
-
schema: { title: functionName, ...asJsonSchema },
|
|
956
|
-
},
|
|
957
|
-
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
958
|
-
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
959
|
-
});
|
|
960
|
-
outputParser = new JsonOutputKeyToolsParser({
|
|
961
|
-
returnSingle: true,
|
|
962
|
-
keyName: functionName,
|
|
963
|
-
zodSchema: schema,
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
else {
|
|
967
|
-
let openAIFunctionDefinition;
|
|
968
|
-
if (typeof schema.name === "string" &&
|
|
969
|
-
typeof schema.parameters === "object" &&
|
|
970
|
-
schema.parameters != null) {
|
|
971
|
-
openAIFunctionDefinition = schema;
|
|
972
|
-
functionName = schema.name;
|
|
973
|
-
}
|
|
974
|
-
else {
|
|
975
|
-
functionName = schema.title ?? functionName;
|
|
976
|
-
openAIFunctionDefinition = {
|
|
977
|
-
name: functionName,
|
|
978
|
-
description: schema.description ?? "",
|
|
979
|
-
parameters: schema,
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
const asJsonSchema = toJsonSchema(schema);
|
|
983
|
-
llm = this.withConfig({
|
|
984
|
-
tools: [
|
|
985
|
-
{
|
|
986
|
-
type: "function",
|
|
987
|
-
function: openAIFunctionDefinition,
|
|
988
|
-
},
|
|
989
|
-
],
|
|
990
|
-
tool_choice: {
|
|
991
|
-
type: "function",
|
|
992
|
-
function: {
|
|
993
|
-
name: functionName,
|
|
994
|
-
},
|
|
995
|
-
},
|
|
996
|
-
ls_structured_output_format: {
|
|
997
|
-
kwargs: { method: "function_calling" },
|
|
998
|
-
schema: { title: functionName, ...asJsonSchema },
|
|
999
|
-
},
|
|
1000
|
-
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
|
|
1001
|
-
...(config?.strict !== undefined ? { strict: config.strict } : {}),
|
|
1002
|
-
});
|
|
1003
|
-
outputParser = new JsonOutputKeyToolsParser({
|
|
1004
|
-
returnSingle: true,
|
|
1005
|
-
keyName: functionName,
|
|
1006
|
-
});
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
if (!includeRaw) {
|
|
1010
|
-
return llm.pipe(outputParser);
|
|
1011
|
-
}
|
|
1012
|
-
const parserAssign = RunnablePassthrough.assign({
|
|
1013
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1014
|
-
parsed: (input, config) => outputParser.invoke(input.raw, config),
|
|
1015
|
-
});
|
|
1016
|
-
const parserNone = RunnablePassthrough.assign({
|
|
1017
|
-
parsed: () => null,
|
|
1018
|
-
});
|
|
1019
|
-
const parsedWithFallback = parserAssign.withFallbacks({
|
|
1020
|
-
fallbacks: [parserNone],
|
|
1021
|
-
});
|
|
1022
|
-
return RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
23
|
+
var BaseChatOpenAI = class extends BaseChatModel {
|
|
24
|
+
temperature;
|
|
25
|
+
topP;
|
|
26
|
+
frequencyPenalty;
|
|
27
|
+
presencePenalty;
|
|
28
|
+
n;
|
|
29
|
+
logitBias;
|
|
30
|
+
model = "gpt-3.5-turbo";
|
|
31
|
+
modelKwargs;
|
|
32
|
+
stop;
|
|
33
|
+
stopSequences;
|
|
34
|
+
user;
|
|
35
|
+
timeout;
|
|
36
|
+
streaming = false;
|
|
37
|
+
streamUsage = true;
|
|
38
|
+
maxTokens;
|
|
39
|
+
logprobs;
|
|
40
|
+
topLogprobs;
|
|
41
|
+
apiKey;
|
|
42
|
+
organization;
|
|
43
|
+
__includeRawResponse;
|
|
44
|
+
/** @internal */
|
|
45
|
+
client;
|
|
46
|
+
/** @internal */
|
|
47
|
+
clientConfig;
|
|
48
|
+
/**
|
|
49
|
+
* Whether the model supports the `strict` argument when passing in tools.
|
|
50
|
+
* If `undefined` the `strict` argument will not be passed to OpenAI.
|
|
51
|
+
*/
|
|
52
|
+
supportsStrictToolCalling;
|
|
53
|
+
audio;
|
|
54
|
+
modalities;
|
|
55
|
+
reasoning;
|
|
56
|
+
/**
|
|
57
|
+
* Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
|
|
58
|
+
* output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
|
|
59
|
+
* OpenAI organization or project. This must be configured directly with OpenAI.
|
|
60
|
+
*
|
|
61
|
+
* See:
|
|
62
|
+
* https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
|
|
63
|
+
* https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
|
|
64
|
+
*
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
zdrEnabled;
|
|
68
|
+
/**
|
|
69
|
+
* Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
|
|
70
|
+
* Specifies the service tier for prioritization and latency optimization.
|
|
71
|
+
*/
|
|
72
|
+
service_tier;
|
|
73
|
+
/**
|
|
74
|
+
* Used by OpenAI to cache responses for similar requests to optimize your cache
|
|
75
|
+
* hit rates.
|
|
76
|
+
* [Learn more](https://platform.openai.com/docs/guides/prompt-caching).
|
|
77
|
+
*/
|
|
78
|
+
promptCacheKey;
|
|
79
|
+
/**
|
|
80
|
+
* The verbosity of the model's response.
|
|
81
|
+
*/
|
|
82
|
+
verbosity;
|
|
83
|
+
defaultOptions;
|
|
84
|
+
_llmType() {
|
|
85
|
+
return "openai";
|
|
86
|
+
}
|
|
87
|
+
static lc_name() {
|
|
88
|
+
return "ChatOpenAI";
|
|
89
|
+
}
|
|
90
|
+
get callKeys() {
|
|
91
|
+
return [
|
|
92
|
+
...super.callKeys,
|
|
93
|
+
"options",
|
|
94
|
+
"function_call",
|
|
95
|
+
"functions",
|
|
96
|
+
"tools",
|
|
97
|
+
"tool_choice",
|
|
98
|
+
"promptIndex",
|
|
99
|
+
"response_format",
|
|
100
|
+
"seed",
|
|
101
|
+
"reasoning",
|
|
102
|
+
"service_tier"
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
lc_serializable = true;
|
|
106
|
+
get lc_secrets() {
|
|
107
|
+
return {
|
|
108
|
+
apiKey: "OPENAI_API_KEY",
|
|
109
|
+
organization: "OPENAI_ORGANIZATION"
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
get lc_aliases() {
|
|
113
|
+
return {
|
|
114
|
+
apiKey: "openai_api_key",
|
|
115
|
+
modelName: "model"
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
get lc_serializable_keys() {
|
|
119
|
+
return [
|
|
120
|
+
"configuration",
|
|
121
|
+
"logprobs",
|
|
122
|
+
"topLogprobs",
|
|
123
|
+
"prefixMessages",
|
|
124
|
+
"supportsStrictToolCalling",
|
|
125
|
+
"modalities",
|
|
126
|
+
"audio",
|
|
127
|
+
"temperature",
|
|
128
|
+
"maxTokens",
|
|
129
|
+
"topP",
|
|
130
|
+
"frequencyPenalty",
|
|
131
|
+
"presencePenalty",
|
|
132
|
+
"n",
|
|
133
|
+
"logitBias",
|
|
134
|
+
"user",
|
|
135
|
+
"streaming",
|
|
136
|
+
"streamUsage",
|
|
137
|
+
"model",
|
|
138
|
+
"modelName",
|
|
139
|
+
"modelKwargs",
|
|
140
|
+
"stop",
|
|
141
|
+
"stopSequences",
|
|
142
|
+
"timeout",
|
|
143
|
+
"apiKey",
|
|
144
|
+
"cache",
|
|
145
|
+
"maxConcurrency",
|
|
146
|
+
"maxRetries",
|
|
147
|
+
"verbose",
|
|
148
|
+
"callbacks",
|
|
149
|
+
"tags",
|
|
150
|
+
"metadata",
|
|
151
|
+
"disableStreaming",
|
|
152
|
+
"zdrEnabled",
|
|
153
|
+
"reasoning",
|
|
154
|
+
"promptCacheKey",
|
|
155
|
+
"verbosity"
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
getLsParams(options) {
|
|
159
|
+
const params = this.invocationParams(options);
|
|
160
|
+
return {
|
|
161
|
+
ls_provider: "openai",
|
|
162
|
+
ls_model_name: this.model,
|
|
163
|
+
ls_model_type: "chat",
|
|
164
|
+
ls_temperature: params.temperature ?? void 0,
|
|
165
|
+
ls_max_tokens: params.max_tokens ?? void 0,
|
|
166
|
+
ls_stop: options.stop
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** @ignore */
|
|
170
|
+
_identifyingParams() {
|
|
171
|
+
return {
|
|
172
|
+
model_name: this.model,
|
|
173
|
+
...this.invocationParams(),
|
|
174
|
+
...this.clientConfig
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get the identifying parameters for the model
|
|
179
|
+
*/
|
|
180
|
+
identifyingParams() {
|
|
181
|
+
return this._identifyingParams();
|
|
182
|
+
}
|
|
183
|
+
constructor(fields) {
|
|
184
|
+
super(fields ?? {});
|
|
185
|
+
this.apiKey = fields?.apiKey ?? fields?.configuration?.apiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
|
|
186
|
+
this.organization = fields?.configuration?.organization ?? getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
187
|
+
this.model = fields?.model ?? fields?.modelName ?? this.model;
|
|
188
|
+
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
189
|
+
this.timeout = fields?.timeout;
|
|
190
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
191
|
+
this.topP = fields?.topP ?? this.topP;
|
|
192
|
+
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
193
|
+
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
194
|
+
this.logprobs = fields?.logprobs;
|
|
195
|
+
this.topLogprobs = fields?.topLogprobs;
|
|
196
|
+
this.n = fields?.n ?? this.n;
|
|
197
|
+
this.logitBias = fields?.logitBias;
|
|
198
|
+
this.stop = fields?.stopSequences ?? fields?.stop;
|
|
199
|
+
this.stopSequences = this.stop;
|
|
200
|
+
this.user = fields?.user;
|
|
201
|
+
this.__includeRawResponse = fields?.__includeRawResponse;
|
|
202
|
+
this.audio = fields?.audio;
|
|
203
|
+
this.modalities = fields?.modalities;
|
|
204
|
+
this.reasoning = fields?.reasoning;
|
|
205
|
+
this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
|
|
206
|
+
this.disableStreaming = fields?.disableStreaming ?? this.disableStreaming;
|
|
207
|
+
this.promptCacheKey = fields?.promptCacheKey ?? this.promptCacheKey;
|
|
208
|
+
this.verbosity = fields?.verbosity ?? this.verbosity;
|
|
209
|
+
this.streaming = fields?.streaming ?? false;
|
|
210
|
+
if (this.disableStreaming) this.streaming = false;
|
|
211
|
+
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
|
|
212
|
+
if (this.disableStreaming) this.streamUsage = false;
|
|
213
|
+
this.clientConfig = {
|
|
214
|
+
apiKey: this.apiKey,
|
|
215
|
+
organization: this.organization,
|
|
216
|
+
dangerouslyAllowBrowser: true,
|
|
217
|
+
...fields?.configuration
|
|
218
|
+
};
|
|
219
|
+
if (fields?.supportsStrictToolCalling !== void 0) this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
|
|
220
|
+
if (fields?.service_tier !== void 0) this.service_tier = fields.service_tier;
|
|
221
|
+
this.zdrEnabled = fields?.zdrEnabled ?? false;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Returns backwards compatible reasoning parameters from constructor params and call options
|
|
225
|
+
* @internal
|
|
226
|
+
*/
|
|
227
|
+
_getReasoningParams(options) {
|
|
228
|
+
if (!isReasoningModel(this.model)) return;
|
|
229
|
+
let reasoning;
|
|
230
|
+
if (this.reasoning !== void 0) reasoning = {
|
|
231
|
+
...reasoning,
|
|
232
|
+
...this.reasoning
|
|
233
|
+
};
|
|
234
|
+
if (options?.reasoning !== void 0) reasoning = {
|
|
235
|
+
...reasoning,
|
|
236
|
+
...options.reasoning
|
|
237
|
+
};
|
|
238
|
+
return reasoning;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Returns an openai compatible response format from a set of options
|
|
242
|
+
* @internal
|
|
243
|
+
*/
|
|
244
|
+
_getResponseFormat(resFormat) {
|
|
245
|
+
if (resFormat && resFormat.type === "json_schema" && resFormat.json_schema.schema && isInteropZodSchema(resFormat.json_schema.schema)) return interopZodResponseFormat(resFormat.json_schema.schema, resFormat.json_schema.name, { description: resFormat.json_schema.description });
|
|
246
|
+
return resFormat;
|
|
247
|
+
}
|
|
248
|
+
_combineCallOptions(additionalOptions) {
|
|
249
|
+
return {
|
|
250
|
+
...this.defaultOptions,
|
|
251
|
+
...additionalOptions ?? {}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
/** @internal */
|
|
255
|
+
_getClientOptions(options) {
|
|
256
|
+
if (!this.client) {
|
|
257
|
+
const openAIEndpointConfig = { baseURL: this.clientConfig.baseURL };
|
|
258
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
259
|
+
const params = {
|
|
260
|
+
...this.clientConfig,
|
|
261
|
+
baseURL: endpoint,
|
|
262
|
+
timeout: this.timeout,
|
|
263
|
+
maxRetries: 0
|
|
264
|
+
};
|
|
265
|
+
if (!params.baseURL) delete params.baseURL;
|
|
266
|
+
this.client = new OpenAI$1(params);
|
|
267
|
+
}
|
|
268
|
+
const requestOptions = {
|
|
269
|
+
...this.clientConfig,
|
|
270
|
+
...options
|
|
271
|
+
};
|
|
272
|
+
return requestOptions;
|
|
273
|
+
}
|
|
274
|
+
_convertChatOpenAIToolToCompletionsTool(tool, fields) {
|
|
275
|
+
if (isCustomTool(tool)) return convertResponsesCustomTool(tool.metadata.customTool);
|
|
276
|
+
if (isOpenAITool(tool)) {
|
|
277
|
+
if (fields?.strict !== void 0) return {
|
|
278
|
+
...tool,
|
|
279
|
+
function: {
|
|
280
|
+
...tool.function,
|
|
281
|
+
strict: fields.strict
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
return tool;
|
|
285
|
+
}
|
|
286
|
+
return _convertToOpenAITool(tool, fields);
|
|
287
|
+
}
|
|
288
|
+
bindTools(tools, kwargs) {
|
|
289
|
+
let strict;
|
|
290
|
+
if (kwargs?.strict !== void 0) strict = kwargs.strict;
|
|
291
|
+
else if (this.supportsStrictToolCalling !== void 0) strict = this.supportsStrictToolCalling;
|
|
292
|
+
return this.withConfig({
|
|
293
|
+
tools: tools.map((tool) => isBuiltInTool(tool) ? tool : this._convertChatOpenAIToolToCompletionsTool(tool, { strict })),
|
|
294
|
+
...kwargs
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
async stream(input, options) {
|
|
298
|
+
return super.stream(input, this._combineCallOptions(options));
|
|
299
|
+
}
|
|
300
|
+
async invoke(input, options) {
|
|
301
|
+
return super.invoke(input, this._combineCallOptions(options));
|
|
302
|
+
}
|
|
303
|
+
/** @ignore */
|
|
304
|
+
_combineLLMOutput(...llmOutputs) {
|
|
305
|
+
return llmOutputs.reduce((acc, llmOutput) => {
|
|
306
|
+
if (llmOutput && llmOutput.tokenUsage) {
|
|
307
|
+
acc.tokenUsage.completionTokens += llmOutput.tokenUsage.completionTokens ?? 0;
|
|
308
|
+
acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
|
|
309
|
+
acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
|
|
310
|
+
}
|
|
311
|
+
return acc;
|
|
312
|
+
}, { tokenUsage: {
|
|
313
|
+
completionTokens: 0,
|
|
314
|
+
promptTokens: 0,
|
|
315
|
+
totalTokens: 0
|
|
316
|
+
} });
|
|
317
|
+
}
|
|
318
|
+
async getNumTokensFromMessages(messages) {
|
|
319
|
+
let totalCount = 0;
|
|
320
|
+
let tokensPerMessage = 0;
|
|
321
|
+
let tokensPerName = 0;
|
|
322
|
+
if (this.model === "gpt-3.5-turbo-0301") {
|
|
323
|
+
tokensPerMessage = 4;
|
|
324
|
+
tokensPerName = -1;
|
|
325
|
+
} else {
|
|
326
|
+
tokensPerMessage = 3;
|
|
327
|
+
tokensPerName = 1;
|
|
328
|
+
}
|
|
329
|
+
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
330
|
+
const textCount = await this.getNumTokens(message.content);
|
|
331
|
+
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
332
|
+
const nameCount = message.name !== void 0 ? tokensPerName + await this.getNumTokens(message.name) : 0;
|
|
333
|
+
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
334
|
+
const openAIMessage = message;
|
|
335
|
+
if (openAIMessage._getType() === "function") count -= 2;
|
|
336
|
+
if (openAIMessage.additional_kwargs?.function_call) count += 3;
|
|
337
|
+
if (openAIMessage?.additional_kwargs.function_call?.name) count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
338
|
+
if (openAIMessage.additional_kwargs.function_call?.arguments) try {
|
|
339
|
+
count += await this.getNumTokens(JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
340
|
+
} catch (error) {
|
|
341
|
+
console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
|
|
342
|
+
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
|
|
343
|
+
}
|
|
344
|
+
totalCount += count;
|
|
345
|
+
return count;
|
|
346
|
+
}));
|
|
347
|
+
totalCount += 3;
|
|
348
|
+
return {
|
|
349
|
+
totalCount,
|
|
350
|
+
countPerMessage
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
/** @internal */
|
|
354
|
+
async _getNumTokensFromGenerations(generations) {
|
|
355
|
+
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
356
|
+
if (generation.message.additional_kwargs?.function_call) return (await this.getNumTokensFromMessages([generation.message])).countPerMessage[0];
|
|
357
|
+
else return await this.getNumTokens(generation.message.content);
|
|
358
|
+
}));
|
|
359
|
+
return generationUsages.reduce((a, b) => a + b, 0);
|
|
360
|
+
}
|
|
361
|
+
/** @internal */
|
|
362
|
+
async _getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
363
|
+
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
364
|
+
if (functions && function_call !== "auto") {
|
|
365
|
+
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
366
|
+
tokens += await this.getNumTokens(promptDefinitions);
|
|
367
|
+
tokens += 9;
|
|
368
|
+
}
|
|
369
|
+
if (functions && messages.find((m) => m._getType() === "system")) tokens -= 4;
|
|
370
|
+
if (function_call === "none") tokens += 1;
|
|
371
|
+
else if (typeof function_call === "object") tokens += await this.getNumTokens(function_call.name) + 4;
|
|
372
|
+
return tokens;
|
|
373
|
+
}
|
|
374
|
+
/** @internal */
|
|
375
|
+
_getStructuredOutputMethod(config) {
|
|
376
|
+
const ensuredConfig = { ...config };
|
|
377
|
+
if (!this.model.startsWith("gpt-3") && !this.model.startsWith("gpt-4-") && this.model !== "gpt-4") {
|
|
378
|
+
if (ensuredConfig?.method === void 0) return "jsonSchema";
|
|
379
|
+
} else if (ensuredConfig.method === "jsonSchema") console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
|
|
380
|
+
return ensuredConfig.method;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Add structured output to the model.
|
|
384
|
+
*
|
|
385
|
+
* The OpenAI model family supports the following structured output methods:
|
|
386
|
+
* - `jsonSchema`: Use the `response_format` field in the response to return a JSON schema. Only supported with the `gpt-4o-mini`,
|
|
387
|
+
* `gpt-4o-mini-2024-07-18`, and `gpt-4o-2024-08-06` model snapshots and later.
|
|
388
|
+
* - `functionCalling`: Function calling is useful when you are building an application that bridges the models and functionality
|
|
389
|
+
* of your application.
|
|
390
|
+
* - `jsonMode`: JSON mode is a more basic version of the Structured Outputs feature. While JSON mode ensures that model
|
|
391
|
+
* output is valid JSON, Structured Outputs reliably matches the model's output to the schema you specify.
|
|
392
|
+
* We recommend you use `functionCalling` or `jsonSchema` if it is supported for your use case.
|
|
393
|
+
*
|
|
394
|
+
* The default method is `functionCalling`.
|
|
395
|
+
*
|
|
396
|
+
* @see https://platform.openai.com/docs/guides/structured-outputs
|
|
397
|
+
* @param outputSchema - The schema to use for structured output.
|
|
398
|
+
* @param config - The structured output method options.
|
|
399
|
+
* @returns The model with structured output.
|
|
400
|
+
*/
|
|
401
|
+
withStructuredOutput(outputSchema, config) {
|
|
402
|
+
let llm;
|
|
403
|
+
let outputParser;
|
|
404
|
+
const { schema, name, includeRaw } = {
|
|
405
|
+
...config,
|
|
406
|
+
schema: outputSchema
|
|
407
|
+
};
|
|
408
|
+
if (config?.strict !== void 0 && config.method === "jsonMode") throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
|
|
409
|
+
const method = getStructuredOutputMethod(this.model, config?.method);
|
|
410
|
+
if (method === "jsonMode") {
|
|
411
|
+
if (isInteropZodSchema(schema)) outputParser = StructuredOutputParser.fromZodSchema(schema);
|
|
412
|
+
else outputParser = new JsonOutputParser();
|
|
413
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
414
|
+
llm = this.withConfig({
|
|
415
|
+
response_format: { type: "json_object" },
|
|
416
|
+
ls_structured_output_format: {
|
|
417
|
+
kwargs: { method: "json_mode" },
|
|
418
|
+
schema: {
|
|
419
|
+
title: name ?? "extract",
|
|
420
|
+
...asJsonSchema
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
} else if (method === "jsonSchema") {
|
|
425
|
+
const openaiJsonSchemaParams = {
|
|
426
|
+
name: name ?? "extract",
|
|
427
|
+
description: getSchemaDescription(schema),
|
|
428
|
+
schema,
|
|
429
|
+
strict: config?.strict
|
|
430
|
+
};
|
|
431
|
+
const asJsonSchema = toJsonSchema(openaiJsonSchemaParams.schema);
|
|
432
|
+
llm = this.withConfig({
|
|
433
|
+
response_format: {
|
|
434
|
+
type: "json_schema",
|
|
435
|
+
json_schema: openaiJsonSchemaParams
|
|
436
|
+
},
|
|
437
|
+
ls_structured_output_format: {
|
|
438
|
+
kwargs: { method: "json_schema" },
|
|
439
|
+
schema: {
|
|
440
|
+
title: openaiJsonSchemaParams.name,
|
|
441
|
+
description: openaiJsonSchemaParams.description,
|
|
442
|
+
...asJsonSchema
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
if (isInteropZodSchema(schema)) {
|
|
447
|
+
const altParser = StructuredOutputParser.fromZodSchema(schema);
|
|
448
|
+
outputParser = RunnableLambda.from((aiMessage) => {
|
|
449
|
+
if ("parsed" in aiMessage.additional_kwargs) return aiMessage.additional_kwargs.parsed;
|
|
450
|
+
return altParser;
|
|
451
|
+
});
|
|
452
|
+
} else outputParser = new JsonOutputParser();
|
|
453
|
+
} else {
|
|
454
|
+
let functionName = name ?? "extract";
|
|
455
|
+
if (isInteropZodSchema(schema)) {
|
|
456
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
457
|
+
llm = this.withConfig({
|
|
458
|
+
tools: [{
|
|
459
|
+
type: "function",
|
|
460
|
+
function: {
|
|
461
|
+
name: functionName,
|
|
462
|
+
description: asJsonSchema.description,
|
|
463
|
+
parameters: asJsonSchema
|
|
464
|
+
}
|
|
465
|
+
}],
|
|
466
|
+
tool_choice: {
|
|
467
|
+
type: "function",
|
|
468
|
+
function: { name: functionName }
|
|
469
|
+
},
|
|
470
|
+
ls_structured_output_format: {
|
|
471
|
+
kwargs: { method: "function_calling" },
|
|
472
|
+
schema: {
|
|
473
|
+
title: functionName,
|
|
474
|
+
...asJsonSchema
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
...config?.strict !== void 0 ? { strict: config.strict } : {}
|
|
478
|
+
});
|
|
479
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
480
|
+
returnSingle: true,
|
|
481
|
+
keyName: functionName,
|
|
482
|
+
zodSchema: schema
|
|
483
|
+
});
|
|
484
|
+
} else {
|
|
485
|
+
let openAIFunctionDefinition;
|
|
486
|
+
if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
|
|
487
|
+
openAIFunctionDefinition = schema;
|
|
488
|
+
functionName = schema.name;
|
|
489
|
+
} else {
|
|
490
|
+
functionName = schema.title ?? functionName;
|
|
491
|
+
openAIFunctionDefinition = {
|
|
492
|
+
name: functionName,
|
|
493
|
+
description: schema.description ?? "",
|
|
494
|
+
parameters: schema
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
const asJsonSchema = toJsonSchema(schema);
|
|
498
|
+
llm = this.withConfig({
|
|
499
|
+
tools: [{
|
|
500
|
+
type: "function",
|
|
501
|
+
function: openAIFunctionDefinition
|
|
502
|
+
}],
|
|
503
|
+
tool_choice: {
|
|
504
|
+
type: "function",
|
|
505
|
+
function: { name: functionName }
|
|
506
|
+
},
|
|
507
|
+
ls_structured_output_format: {
|
|
508
|
+
kwargs: { method: "function_calling" },
|
|
509
|
+
schema: {
|
|
510
|
+
title: functionName,
|
|
511
|
+
...asJsonSchema
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
...config?.strict !== void 0 ? { strict: config.strict } : {}
|
|
515
|
+
});
|
|
516
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
517
|
+
returnSingle: true,
|
|
518
|
+
keyName: functionName
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!includeRaw) return llm.pipe(outputParser);
|
|
523
|
+
const parserAssign = RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
|
|
524
|
+
const parserNone = RunnablePassthrough.assign({ parsed: () => null });
|
|
525
|
+
const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
|
|
526
|
+
return RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
1025
529
|
/**
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
text: item.text,
|
|
1543
|
-
// @ts-expect-error TODO: add types for `annotations`
|
|
1544
|
-
annotations: item.annotations ?? [],
|
|
1545
|
-
};
|
|
1546
|
-
}
|
|
1547
|
-
if (item.type === "output_text" ||
|
|
1548
|
-
item.type === "refusal") {
|
|
1549
|
-
return item;
|
|
1550
|
-
}
|
|
1551
|
-
return [];
|
|
1552
|
-
}),
|
|
1553
|
-
});
|
|
1554
|
-
}
|
|
1555
|
-
const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
|
|
1556
|
-
if (isAIMessage(lcMsg) && !!lcMsg.tool_calls?.length) {
|
|
1557
|
-
input.push(...lcMsg.tool_calls.map((toolCall) => {
|
|
1558
|
-
if (isCustomToolCall(toolCall)) {
|
|
1559
|
-
return {
|
|
1560
|
-
type: "custom_tool_call",
|
|
1561
|
-
id: toolCall.call_id,
|
|
1562
|
-
call_id: toolCall.id ?? "",
|
|
1563
|
-
input: toolCall.args.input,
|
|
1564
|
-
name: toolCall.name,
|
|
1565
|
-
};
|
|
1566
|
-
}
|
|
1567
|
-
return {
|
|
1568
|
-
type: "function_call",
|
|
1569
|
-
name: toolCall.name,
|
|
1570
|
-
arguments: JSON.stringify(toolCall.args),
|
|
1571
|
-
call_id: toolCall.id,
|
|
1572
|
-
...(this.zdrEnabled
|
|
1573
|
-
? { id: functionCallIds?.[toolCall.id] }
|
|
1574
|
-
: {}),
|
|
1575
|
-
};
|
|
1576
|
-
}));
|
|
1577
|
-
}
|
|
1578
|
-
else if (additional_kwargs?.tool_calls) {
|
|
1579
|
-
input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
|
|
1580
|
-
type: "function_call",
|
|
1581
|
-
name: toolCall.function.name,
|
|
1582
|
-
call_id: toolCall.id,
|
|
1583
|
-
arguments: toolCall.function.arguments,
|
|
1584
|
-
...(this.zdrEnabled
|
|
1585
|
-
? { id: functionCallIds?.[toolCall.id] }
|
|
1586
|
-
: {}),
|
|
1587
|
-
})));
|
|
1588
|
-
}
|
|
1589
|
-
const toolOutputs = lcMsg.response_metadata.output?.length
|
|
1590
|
-
? lcMsg.response_metadata.output
|
|
1591
|
-
: additional_kwargs.tool_outputs;
|
|
1592
|
-
const fallthroughCallTypes = [
|
|
1593
|
-
"computer_call",
|
|
1594
|
-
"mcp_call",
|
|
1595
|
-
"code_interpreter_call",
|
|
1596
|
-
"image_generation_call",
|
|
1597
|
-
];
|
|
1598
|
-
if (toolOutputs != null) {
|
|
1599
|
-
const castToolOutputs = toolOutputs;
|
|
1600
|
-
const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
|
|
1601
|
-
if (fallthroughCalls.length > 0)
|
|
1602
|
-
input.push(...fallthroughCalls);
|
|
1603
|
-
}
|
|
1604
|
-
return input;
|
|
1605
|
-
}
|
|
1606
|
-
if (role === "user" || role === "system" || role === "developer") {
|
|
1607
|
-
if (typeof lcMsg.content === "string") {
|
|
1608
|
-
return { type: "message", role, content: lcMsg.content };
|
|
1609
|
-
}
|
|
1610
|
-
const messages = [];
|
|
1611
|
-
const content = lcMsg.content.flatMap((item) => {
|
|
1612
|
-
if (item.type === "mcp_approval_response") {
|
|
1613
|
-
messages.push({
|
|
1614
|
-
type: "mcp_approval_response",
|
|
1615
|
-
approval_request_id: item.approval_request_id,
|
|
1616
|
-
approve: item.approve,
|
|
1617
|
-
});
|
|
1618
|
-
}
|
|
1619
|
-
if (isDataContentBlock(item)) {
|
|
1620
|
-
return convertToProviderContentBlock(item, completionsApiContentBlockConverter);
|
|
1621
|
-
}
|
|
1622
|
-
if (item.type === "text") {
|
|
1623
|
-
return {
|
|
1624
|
-
type: "input_text",
|
|
1625
|
-
text: item.text,
|
|
1626
|
-
};
|
|
1627
|
-
}
|
|
1628
|
-
if (item.type === "image_url") {
|
|
1629
|
-
return {
|
|
1630
|
-
type: "input_image",
|
|
1631
|
-
image_url: typeof item.image_url === "string"
|
|
1632
|
-
? item.image_url
|
|
1633
|
-
: item.image_url.url,
|
|
1634
|
-
detail: typeof item.image_url === "string"
|
|
1635
|
-
? "auto"
|
|
1636
|
-
: item.image_url.detail,
|
|
1637
|
-
};
|
|
1638
|
-
}
|
|
1639
|
-
if (item.type === "input_text" ||
|
|
1640
|
-
item.type === "input_image" ||
|
|
1641
|
-
item.type === "input_file") {
|
|
1642
|
-
return item;
|
|
1643
|
-
}
|
|
1644
|
-
return [];
|
|
1645
|
-
});
|
|
1646
|
-
if (content.length > 0) {
|
|
1647
|
-
messages.push({ type: "message", role, content });
|
|
1648
|
-
}
|
|
1649
|
-
return messages;
|
|
1650
|
-
}
|
|
1651
|
-
console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
|
|
1652
|
-
return [];
|
|
1653
|
-
});
|
|
1654
|
-
}
|
|
1655
|
-
/** @internal */
|
|
1656
|
-
_convertReasoningSummary(reasoning) {
|
|
1657
|
-
// combine summary parts that have the the same index and then remove the indexes
|
|
1658
|
-
const summary = (reasoning.summary.length > 1
|
|
1659
|
-
? reasoning.summary.reduce((acc, curr) => {
|
|
1660
|
-
const last = acc.at(-1);
|
|
1661
|
-
if (last.index === curr.index) {
|
|
1662
|
-
last.text += curr.text;
|
|
1663
|
-
}
|
|
1664
|
-
else {
|
|
1665
|
-
acc.push(curr);
|
|
1666
|
-
}
|
|
1667
|
-
return acc;
|
|
1668
|
-
}, [{ ...reasoning.summary[0] }])
|
|
1669
|
-
: reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
|
|
1670
|
-
return {
|
|
1671
|
-
...reasoning,
|
|
1672
|
-
summary,
|
|
1673
|
-
};
|
|
1674
|
-
}
|
|
1675
|
-
/** @internal */
|
|
1676
|
-
_reduceChatOpenAITools(tools, fields) {
|
|
1677
|
-
const reducedTools = [];
|
|
1678
|
-
for (const tool of tools) {
|
|
1679
|
-
if (isBuiltInTool(tool)) {
|
|
1680
|
-
if (tool.type === "image_generation" && fields?.stream) {
|
|
1681
|
-
// OpenAI sends a 400 error if partial_images is not set and we want to stream.
|
|
1682
|
-
// We also set it to 1 since we don't support partial images yet.
|
|
1683
|
-
tool.partial_images = 1;
|
|
1684
|
-
}
|
|
1685
|
-
reducedTools.push(tool);
|
|
1686
|
-
}
|
|
1687
|
-
else if (isOpenAIFunctionTool(tool)) {
|
|
1688
|
-
reducedTools.push({
|
|
1689
|
-
type: "function",
|
|
1690
|
-
name: tool.function.name,
|
|
1691
|
-
parameters: tool.function.parameters,
|
|
1692
|
-
description: tool.function.description,
|
|
1693
|
-
strict: fields?.strict ?? null,
|
|
1694
|
-
});
|
|
1695
|
-
}
|
|
1696
|
-
else if (isOpenAICustomTool(tool)) {
|
|
1697
|
-
reducedTools.push(convertCompletionsCustomTool(tool));
|
|
1698
|
-
}
|
|
1699
|
-
}
|
|
1700
|
-
return reducedTools;
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
530
|
+
* OpenAI Responses API implementation.
|
|
531
|
+
*
|
|
532
|
+
* Will be exported in a later version of @langchain/openai.
|
|
533
|
+
*
|
|
534
|
+
* @internal
|
|
535
|
+
*/
|
|
536
|
+
var ChatOpenAIResponses = class extends BaseChatOpenAI {
|
|
537
|
+
invocationParams(options) {
|
|
538
|
+
let strict;
|
|
539
|
+
if (options?.strict !== void 0) strict = options.strict;
|
|
540
|
+
else if (this.supportsStrictToolCalling !== void 0) strict = this.supportsStrictToolCalling;
|
|
541
|
+
const params = {
|
|
542
|
+
model: this.model,
|
|
543
|
+
temperature: this.temperature,
|
|
544
|
+
top_p: this.topP,
|
|
545
|
+
user: this.user,
|
|
546
|
+
stream: this.streaming,
|
|
547
|
+
previous_response_id: options?.previous_response_id,
|
|
548
|
+
truncation: options?.truncation,
|
|
549
|
+
include: options?.include,
|
|
550
|
+
tools: options?.tools?.length ? this._reduceChatOpenAITools(options.tools, {
|
|
551
|
+
stream: this.streaming,
|
|
552
|
+
strict
|
|
553
|
+
}) : void 0,
|
|
554
|
+
tool_choice: isBuiltInToolChoice(options?.tool_choice) ? options?.tool_choice : (() => {
|
|
555
|
+
const formatted = formatToOpenAIToolChoice(options?.tool_choice);
|
|
556
|
+
if (typeof formatted === "object" && "type" in formatted) {
|
|
557
|
+
if (formatted.type === "function") return {
|
|
558
|
+
type: "function",
|
|
559
|
+
name: formatted.function.name
|
|
560
|
+
};
|
|
561
|
+
else if (formatted.type === "allowed_tools") return {
|
|
562
|
+
type: "allowed_tools",
|
|
563
|
+
mode: formatted.allowed_tools.mode,
|
|
564
|
+
tools: formatted.allowed_tools.tools
|
|
565
|
+
};
|
|
566
|
+
else if (formatted.type === "custom") return {
|
|
567
|
+
type: "custom",
|
|
568
|
+
name: formatted.custom.name
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
return void 0;
|
|
572
|
+
})(),
|
|
573
|
+
text: (() => {
|
|
574
|
+
if (options?.text) return options.text;
|
|
575
|
+
const format = this._getResponseFormat(options?.response_format);
|
|
576
|
+
if (format?.type === "json_schema") {
|
|
577
|
+
if (format.json_schema.schema != null) return {
|
|
578
|
+
format: {
|
|
579
|
+
type: "json_schema",
|
|
580
|
+
schema: format.json_schema.schema,
|
|
581
|
+
description: format.json_schema.description,
|
|
582
|
+
name: format.json_schema.name,
|
|
583
|
+
strict: format.json_schema.strict
|
|
584
|
+
},
|
|
585
|
+
verbosity: options?.verbosity
|
|
586
|
+
};
|
|
587
|
+
return void 0;
|
|
588
|
+
}
|
|
589
|
+
return {
|
|
590
|
+
format,
|
|
591
|
+
verbosity: options?.verbosity
|
|
592
|
+
};
|
|
593
|
+
})(),
|
|
594
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
595
|
+
max_output_tokens: this.maxTokens === -1 ? void 0 : this.maxTokens,
|
|
596
|
+
prompt_cache_key: options?.promptCacheKey ?? this.promptCacheKey,
|
|
597
|
+
...this.zdrEnabled ? { store: false } : {},
|
|
598
|
+
...this.modelKwargs
|
|
599
|
+
};
|
|
600
|
+
const reasoning = this._getReasoningParams(options);
|
|
601
|
+
if (reasoning !== void 0) params.reasoning = reasoning;
|
|
602
|
+
return params;
|
|
603
|
+
}
|
|
604
|
+
async _generate(messages, options) {
|
|
605
|
+
const invocationParams = this.invocationParams(options);
|
|
606
|
+
if (invocationParams.stream) {
|
|
607
|
+
const stream = this._streamResponseChunks(messages, options);
|
|
608
|
+
let finalChunk;
|
|
609
|
+
for await (const chunk of stream) {
|
|
610
|
+
chunk.message.response_metadata = {
|
|
611
|
+
...chunk.generationInfo,
|
|
612
|
+
...chunk.message.response_metadata
|
|
613
|
+
};
|
|
614
|
+
finalChunk = finalChunk?.concat(chunk) ?? chunk;
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
generations: finalChunk ? [finalChunk] : [],
|
|
618
|
+
llmOutput: { estimatedTokenUsage: (finalChunk?.message)?.usage_metadata }
|
|
619
|
+
};
|
|
620
|
+
} else {
|
|
621
|
+
const input = this._convertMessagesToResponsesParams(messages);
|
|
622
|
+
const data = await this.completionWithRetry({
|
|
623
|
+
input,
|
|
624
|
+
...invocationParams,
|
|
625
|
+
stream: false
|
|
626
|
+
}, {
|
|
627
|
+
signal: options?.signal,
|
|
628
|
+
...options?.options
|
|
629
|
+
});
|
|
630
|
+
return {
|
|
631
|
+
generations: [{
|
|
632
|
+
text: data.output_text,
|
|
633
|
+
message: this._convertResponsesMessageToBaseMessage(data)
|
|
634
|
+
}],
|
|
635
|
+
llmOutput: {
|
|
636
|
+
id: data.id,
|
|
637
|
+
estimatedTokenUsage: data.usage ? {
|
|
638
|
+
promptTokens: data.usage.input_tokens,
|
|
639
|
+
completionTokens: data.usage.output_tokens,
|
|
640
|
+
totalTokens: data.usage.total_tokens
|
|
641
|
+
} : void 0
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
647
|
+
const streamIterable = await this.completionWithRetry({
|
|
648
|
+
...this.invocationParams(options),
|
|
649
|
+
input: this._convertMessagesToResponsesParams(messages),
|
|
650
|
+
stream: true
|
|
651
|
+
}, options);
|
|
652
|
+
for await (const data of streamIterable) {
|
|
653
|
+
const chunk = this._convertResponsesDeltaToBaseMessageChunk(data);
|
|
654
|
+
if (chunk == null) continue;
|
|
655
|
+
yield chunk;
|
|
656
|
+
await runManager?.handleLLMNewToken(chunk.text || "", {
|
|
657
|
+
prompt: options.promptIndex ?? 0,
|
|
658
|
+
completion: 0
|
|
659
|
+
}, void 0, void 0, void 0, { chunk });
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
async completionWithRetry(request, requestOptions) {
|
|
663
|
+
return this.caller.call(async () => {
|
|
664
|
+
const clientOptions = this._getClientOptions(requestOptions);
|
|
665
|
+
try {
|
|
666
|
+
if (request.text?.format?.type === "json_schema" && !request.stream) return await this.client.responses.parse(request, clientOptions);
|
|
667
|
+
return await this.client.responses.create(request, clientOptions);
|
|
668
|
+
} catch (e) {
|
|
669
|
+
const error = wrapOpenAIClientError(e);
|
|
670
|
+
throw error;
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
/** @internal */
|
|
675
|
+
_convertResponsesMessageToBaseMessage(response) {
|
|
676
|
+
if (response.error) {
|
|
677
|
+
const error = new Error(response.error.message);
|
|
678
|
+
error.name = response.error.code;
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
let messageId;
|
|
682
|
+
const content = [];
|
|
683
|
+
const tool_calls = [];
|
|
684
|
+
const invalid_tool_calls = [];
|
|
685
|
+
const response_metadata = {
|
|
686
|
+
model_provider: "openai",
|
|
687
|
+
model: response.model,
|
|
688
|
+
created_at: response.created_at,
|
|
689
|
+
id: response.id,
|
|
690
|
+
incomplete_details: response.incomplete_details,
|
|
691
|
+
metadata: response.metadata,
|
|
692
|
+
object: response.object,
|
|
693
|
+
status: response.status,
|
|
694
|
+
user: response.user,
|
|
695
|
+
service_tier: response.service_tier,
|
|
696
|
+
model_name: response.model
|
|
697
|
+
};
|
|
698
|
+
const additional_kwargs = {};
|
|
699
|
+
for (const item of response.output) if (item.type === "message") {
|
|
700
|
+
messageId = item.id;
|
|
701
|
+
content.push(...item.content.flatMap((part) => {
|
|
702
|
+
if (part.type === "output_text") {
|
|
703
|
+
if ("parsed" in part && part.parsed != null) additional_kwargs.parsed = part.parsed;
|
|
704
|
+
return {
|
|
705
|
+
type: "text",
|
|
706
|
+
text: part.text,
|
|
707
|
+
annotations: part.annotations
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
if (part.type === "refusal") {
|
|
711
|
+
additional_kwargs.refusal = part.refusal;
|
|
712
|
+
return [];
|
|
713
|
+
}
|
|
714
|
+
return part;
|
|
715
|
+
}));
|
|
716
|
+
} else if (item.type === "function_call") {
|
|
717
|
+
const fnAdapter = {
|
|
718
|
+
function: {
|
|
719
|
+
name: item.name,
|
|
720
|
+
arguments: item.arguments
|
|
721
|
+
},
|
|
722
|
+
id: item.call_id
|
|
723
|
+
};
|
|
724
|
+
try {
|
|
725
|
+
tool_calls.push(parseToolCall(fnAdapter, { returnId: true }));
|
|
726
|
+
} catch (e) {
|
|
727
|
+
let errMessage;
|
|
728
|
+
if (typeof e === "object" && e != null && "message" in e && typeof e.message === "string") errMessage = e.message;
|
|
729
|
+
invalid_tool_calls.push(makeInvalidToolCall(fnAdapter, errMessage));
|
|
730
|
+
}
|
|
731
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
|
|
732
|
+
if (item.id) additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
|
|
733
|
+
} else if (item.type === "reasoning") additional_kwargs.reasoning = item;
|
|
734
|
+
else if (item.type === "custom_tool_call") {
|
|
735
|
+
const parsed = parseCustomToolCall(item);
|
|
736
|
+
if (parsed) tool_calls.push(parsed);
|
|
737
|
+
else invalid_tool_calls.push(makeInvalidToolCall(item, "Malformed custom tool call"));
|
|
738
|
+
} else {
|
|
739
|
+
additional_kwargs.tool_outputs ??= [];
|
|
740
|
+
additional_kwargs.tool_outputs.push(item);
|
|
741
|
+
}
|
|
742
|
+
return new AIMessage({
|
|
743
|
+
id: messageId,
|
|
744
|
+
content,
|
|
745
|
+
tool_calls,
|
|
746
|
+
invalid_tool_calls,
|
|
747
|
+
usage_metadata: response.usage,
|
|
748
|
+
additional_kwargs,
|
|
749
|
+
response_metadata
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
/** @internal */
|
|
753
|
+
_convertResponsesDeltaToBaseMessageChunk(chunk) {
|
|
754
|
+
const content = [];
|
|
755
|
+
let generationInfo = {};
|
|
756
|
+
let usage_metadata;
|
|
757
|
+
const tool_call_chunks = [];
|
|
758
|
+
const response_metadata = { model_provider: "openai" };
|
|
759
|
+
const additional_kwargs = {};
|
|
760
|
+
let id;
|
|
761
|
+
if (chunk.type === "response.output_text.delta") content.push({
|
|
762
|
+
type: "text",
|
|
763
|
+
text: chunk.delta,
|
|
764
|
+
index: chunk.content_index
|
|
765
|
+
});
|
|
766
|
+
else if (chunk.type === "response.output_text.annotation.added") content.push({
|
|
767
|
+
type: "text",
|
|
768
|
+
text: "",
|
|
769
|
+
annotations: [chunk.annotation],
|
|
770
|
+
index: chunk.content_index
|
|
771
|
+
});
|
|
772
|
+
else if (chunk.type === "response.output_item.added" && chunk.item.type === "message") id = chunk.item.id;
|
|
773
|
+
else if (chunk.type === "response.output_item.added" && chunk.item.type === "function_call") {
|
|
774
|
+
tool_call_chunks.push({
|
|
775
|
+
type: "tool_call_chunk",
|
|
776
|
+
name: chunk.item.name,
|
|
777
|
+
args: chunk.item.arguments,
|
|
778
|
+
id: chunk.item.call_id,
|
|
779
|
+
index: chunk.output_index
|
|
780
|
+
});
|
|
781
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = { [chunk.item.call_id]: chunk.item.id };
|
|
782
|
+
} else if (chunk.type === "response.output_item.done" && [
|
|
783
|
+
"web_search_call",
|
|
784
|
+
"file_search_call",
|
|
785
|
+
"computer_call",
|
|
786
|
+
"code_interpreter_call",
|
|
787
|
+
"mcp_call",
|
|
788
|
+
"mcp_list_tools",
|
|
789
|
+
"mcp_approval_request",
|
|
790
|
+
"image_generation_call",
|
|
791
|
+
"custom_tool_call"
|
|
792
|
+
].includes(chunk.item.type)) additional_kwargs.tool_outputs = [chunk.item];
|
|
793
|
+
else if (chunk.type === "response.created") {
|
|
794
|
+
response_metadata.id = chunk.response.id;
|
|
795
|
+
response_metadata.model_name = chunk.response.model;
|
|
796
|
+
response_metadata.model = chunk.response.model;
|
|
797
|
+
} else if (chunk.type === "response.completed") {
|
|
798
|
+
const msg = this._convertResponsesMessageToBaseMessage(chunk.response);
|
|
799
|
+
usage_metadata = chunk.response.usage;
|
|
800
|
+
if (chunk.response.text?.format?.type === "json_schema") additional_kwargs.parsed ??= JSON.parse(msg.text);
|
|
801
|
+
for (const [key, value] of Object.entries(chunk.response)) if (key !== "id") response_metadata[key] = value;
|
|
802
|
+
} else if (chunk.type === "response.function_call_arguments.delta") tool_call_chunks.push({
|
|
803
|
+
type: "tool_call_chunk",
|
|
804
|
+
args: chunk.delta,
|
|
805
|
+
index: chunk.output_index
|
|
806
|
+
});
|
|
807
|
+
else if (chunk.type === "response.web_search_call.completed" || chunk.type === "response.file_search_call.completed") generationInfo = { tool_outputs: {
|
|
808
|
+
id: chunk.item_id,
|
|
809
|
+
type: chunk.type.replace("response.", "").replace(".completed", ""),
|
|
810
|
+
status: "completed"
|
|
811
|
+
} };
|
|
812
|
+
else if (chunk.type === "response.refusal.done") additional_kwargs.refusal = chunk.refusal;
|
|
813
|
+
else if (chunk.type === "response.output_item.added" && "item" in chunk && chunk.item.type === "reasoning") {
|
|
814
|
+
const summary = chunk.item.summary ? chunk.item.summary.map((s, index) => ({
|
|
815
|
+
...s,
|
|
816
|
+
index
|
|
817
|
+
})) : void 0;
|
|
818
|
+
additional_kwargs.reasoning = {
|
|
819
|
+
id: chunk.item.id,
|
|
820
|
+
type: chunk.item.type,
|
|
821
|
+
...summary ? { summary } : {}
|
|
822
|
+
};
|
|
823
|
+
} else if (chunk.type === "response.reasoning_summary_part.added") additional_kwargs.reasoning = {
|
|
824
|
+
type: "reasoning",
|
|
825
|
+
summary: [{
|
|
826
|
+
...chunk.part,
|
|
827
|
+
index: chunk.summary_index
|
|
828
|
+
}]
|
|
829
|
+
};
|
|
830
|
+
else if (chunk.type === "response.reasoning_summary_text.delta") additional_kwargs.reasoning = {
|
|
831
|
+
type: "reasoning",
|
|
832
|
+
summary: [{
|
|
833
|
+
text: chunk.delta,
|
|
834
|
+
type: "summary_text",
|
|
835
|
+
index: chunk.summary_index
|
|
836
|
+
}]
|
|
837
|
+
};
|
|
838
|
+
else if (chunk.type === "response.image_generation_call.partial_image") return null;
|
|
839
|
+
else return null;
|
|
840
|
+
return new ChatGenerationChunk({
|
|
841
|
+
text: content.map((part) => part.text).join(""),
|
|
842
|
+
message: new AIMessageChunk({
|
|
843
|
+
id,
|
|
844
|
+
content,
|
|
845
|
+
tool_call_chunks,
|
|
846
|
+
usage_metadata,
|
|
847
|
+
additional_kwargs,
|
|
848
|
+
response_metadata
|
|
849
|
+
}),
|
|
850
|
+
generationInfo
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
/** @internal */
|
|
854
|
+
_convertMessagesToResponsesParams(messages) {
|
|
855
|
+
return messages.flatMap((lcMsg) => {
|
|
856
|
+
if (isAIMessage(lcMsg) && lcMsg.response_metadata?.output_version === "v1") return _convertToResponsesMessageFromV1(lcMsg);
|
|
857
|
+
const additional_kwargs = lcMsg.additional_kwargs;
|
|
858
|
+
let role = messageToOpenAIRole(lcMsg);
|
|
859
|
+
if (role === "system" && isReasoningModel(this.model)) role = "developer";
|
|
860
|
+
if (role === "function") throw new Error("Function messages are not supported in Responses API");
|
|
861
|
+
if (role === "tool") {
|
|
862
|
+
const toolMessage = lcMsg;
|
|
863
|
+
if (additional_kwargs?.type === "computer_call_output") {
|
|
864
|
+
const output = (() => {
|
|
865
|
+
if (typeof toolMessage.content === "string") return {
|
|
866
|
+
type: "computer_screenshot",
|
|
867
|
+
image_url: toolMessage.content
|
|
868
|
+
};
|
|
869
|
+
if (Array.isArray(toolMessage.content)) {
|
|
870
|
+
const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
|
|
871
|
+
if (oaiScreenshot) return oaiScreenshot;
|
|
872
|
+
const lcImage = toolMessage.content.find((i) => i.type === "image_url");
|
|
873
|
+
if (lcImage) return {
|
|
874
|
+
type: "computer_screenshot",
|
|
875
|
+
image_url: typeof lcImage.image_url === "string" ? lcImage.image_url : lcImage.image_url.url
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
throw new Error("Invalid computer call output");
|
|
879
|
+
})();
|
|
880
|
+
return {
|
|
881
|
+
type: "computer_call_output",
|
|
882
|
+
output,
|
|
883
|
+
call_id: toolMessage.tool_call_id
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
return {
|
|
887
|
+
type: "function_call_output",
|
|
888
|
+
call_id: toolMessage.tool_call_id,
|
|
889
|
+
id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : void 0,
|
|
890
|
+
output: typeof toolMessage.content !== "string" ? JSON.stringify(toolMessage.content) : toolMessage.content
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
if (role === "assistant") {
|
|
894
|
+
if (!this.zdrEnabled && lcMsg.response_metadata.output != null && Array.isArray(lcMsg.response_metadata.output) && lcMsg.response_metadata.output.length > 0 && lcMsg.response_metadata.output.every((item) => "type" in item)) return lcMsg.response_metadata.output;
|
|
895
|
+
const input = [];
|
|
896
|
+
if (additional_kwargs?.reasoning && !this.zdrEnabled) {
|
|
897
|
+
const reasoningItem = this._convertReasoningSummary(additional_kwargs.reasoning);
|
|
898
|
+
input.push(reasoningItem);
|
|
899
|
+
}
|
|
900
|
+
let { content } = lcMsg;
|
|
901
|
+
if (additional_kwargs?.refusal) {
|
|
902
|
+
if (typeof content === "string") content = [{
|
|
903
|
+
type: "output_text",
|
|
904
|
+
text: content,
|
|
905
|
+
annotations: []
|
|
906
|
+
}];
|
|
907
|
+
content = [...content, {
|
|
908
|
+
type: "refusal",
|
|
909
|
+
refusal: additional_kwargs.refusal
|
|
910
|
+
}];
|
|
911
|
+
}
|
|
912
|
+
if (typeof content === "string" || content.length > 0) input.push({
|
|
913
|
+
type: "message",
|
|
914
|
+
role: "assistant",
|
|
915
|
+
...lcMsg.id && !this.zdrEnabled && lcMsg.id.startsWith("msg_") ? { id: lcMsg.id } : {},
|
|
916
|
+
content: iife(() => {
|
|
917
|
+
if (typeof content === "string") return content;
|
|
918
|
+
return content.flatMap((item) => {
|
|
919
|
+
if (item.type === "text") return {
|
|
920
|
+
type: "output_text",
|
|
921
|
+
text: item.text,
|
|
922
|
+
annotations: item.annotations ?? []
|
|
923
|
+
};
|
|
924
|
+
if (item.type === "output_text" || item.type === "refusal") return item;
|
|
925
|
+
return [];
|
|
926
|
+
});
|
|
927
|
+
})
|
|
928
|
+
});
|
|
929
|
+
const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
|
|
930
|
+
if (isAIMessage(lcMsg) && !!lcMsg.tool_calls?.length) input.push(...lcMsg.tool_calls.map((toolCall) => {
|
|
931
|
+
if (isCustomToolCall(toolCall)) return {
|
|
932
|
+
type: "custom_tool_call",
|
|
933
|
+
id: toolCall.call_id,
|
|
934
|
+
call_id: toolCall.id ?? "",
|
|
935
|
+
input: toolCall.args.input,
|
|
936
|
+
name: toolCall.name
|
|
937
|
+
};
|
|
938
|
+
return {
|
|
939
|
+
type: "function_call",
|
|
940
|
+
name: toolCall.name,
|
|
941
|
+
arguments: JSON.stringify(toolCall.args),
|
|
942
|
+
call_id: toolCall.id,
|
|
943
|
+
...this.zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}
|
|
944
|
+
};
|
|
945
|
+
}));
|
|
946
|
+
else if (additional_kwargs?.tool_calls) input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
|
|
947
|
+
type: "function_call",
|
|
948
|
+
name: toolCall.function.name,
|
|
949
|
+
call_id: toolCall.id,
|
|
950
|
+
arguments: toolCall.function.arguments,
|
|
951
|
+
...this.zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}
|
|
952
|
+
})));
|
|
953
|
+
const toolOutputs = lcMsg.response_metadata.output?.length ? lcMsg.response_metadata.output : additional_kwargs.tool_outputs;
|
|
954
|
+
const fallthroughCallTypes = [
|
|
955
|
+
"computer_call",
|
|
956
|
+
"mcp_call",
|
|
957
|
+
"code_interpreter_call",
|
|
958
|
+
"image_generation_call"
|
|
959
|
+
];
|
|
960
|
+
if (toolOutputs != null) {
|
|
961
|
+
const castToolOutputs = toolOutputs;
|
|
962
|
+
const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
|
|
963
|
+
if (fallthroughCalls.length > 0) input.push(...fallthroughCalls);
|
|
964
|
+
}
|
|
965
|
+
return input;
|
|
966
|
+
}
|
|
967
|
+
if (role === "user" || role === "system" || role === "developer") {
|
|
968
|
+
if (typeof lcMsg.content === "string") return {
|
|
969
|
+
type: "message",
|
|
970
|
+
role,
|
|
971
|
+
content: lcMsg.content
|
|
972
|
+
};
|
|
973
|
+
const messages$1 = [];
|
|
974
|
+
const content = lcMsg.content.flatMap((item) => {
|
|
975
|
+
if (item.type === "mcp_approval_response") messages$1.push({
|
|
976
|
+
type: "mcp_approval_response",
|
|
977
|
+
approval_request_id: item.approval_request_id,
|
|
978
|
+
approve: item.approve
|
|
979
|
+
});
|
|
980
|
+
if (isDataContentBlock(item)) return convertToProviderContentBlock(item, completionsApiContentBlockConverter);
|
|
981
|
+
if (item.type === "text") return {
|
|
982
|
+
type: "input_text",
|
|
983
|
+
text: item.text
|
|
984
|
+
};
|
|
985
|
+
if (item.type === "image_url") {
|
|
986
|
+
const imageUrl = iife(() => {
|
|
987
|
+
if (typeof item.image_url === "string") return item.image_url;
|
|
988
|
+
else if (typeof item.image_url === "object" && item.image_url !== null && "url" in item.image_url) return item.image_url.url;
|
|
989
|
+
return void 0;
|
|
990
|
+
});
|
|
991
|
+
const detail = iife(() => {
|
|
992
|
+
if (typeof item.image_url === "string") return "auto";
|
|
993
|
+
else if (typeof item.image_url === "object" && item.image_url !== null && "detail" in item.image_url) return item.image_url.detail;
|
|
994
|
+
return void 0;
|
|
995
|
+
});
|
|
996
|
+
return {
|
|
997
|
+
type: "input_image",
|
|
998
|
+
image_url: imageUrl,
|
|
999
|
+
detail
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
if (item.type === "input_text" || item.type === "input_image" || item.type === "input_file") return item;
|
|
1003
|
+
return [];
|
|
1004
|
+
});
|
|
1005
|
+
if (content.length > 0) messages$1.push({
|
|
1006
|
+
type: "message",
|
|
1007
|
+
role,
|
|
1008
|
+
content
|
|
1009
|
+
});
|
|
1010
|
+
return messages$1;
|
|
1011
|
+
}
|
|
1012
|
+
console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
|
|
1013
|
+
return [];
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
/** @internal */
|
|
1017
|
+
_convertReasoningSummary(reasoning) {
|
|
1018
|
+
const summary = (reasoning.summary.length > 1 ? reasoning.summary.reduce((acc, curr) => {
|
|
1019
|
+
const last = acc.at(-1);
|
|
1020
|
+
if (last.index === curr.index) last.text += curr.text;
|
|
1021
|
+
else acc.push(curr);
|
|
1022
|
+
return acc;
|
|
1023
|
+
}, [{ ...reasoning.summary[0] }]) : reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
|
|
1024
|
+
return {
|
|
1025
|
+
...reasoning,
|
|
1026
|
+
summary
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
/** @internal */
|
|
1030
|
+
_reduceChatOpenAITools(tools, fields) {
|
|
1031
|
+
const reducedTools = [];
|
|
1032
|
+
for (const tool of tools) if (isBuiltInTool(tool)) {
|
|
1033
|
+
if (tool.type === "image_generation" && fields?.stream) tool.partial_images = 1;
|
|
1034
|
+
reducedTools.push(tool);
|
|
1035
|
+
} else if (isOpenAITool(tool)) reducedTools.push({
|
|
1036
|
+
type: "function",
|
|
1037
|
+
name: tool.function.name,
|
|
1038
|
+
parameters: tool.function.parameters,
|
|
1039
|
+
description: tool.function.description,
|
|
1040
|
+
strict: fields?.strict ?? null
|
|
1041
|
+
});
|
|
1042
|
+
else if (isOpenAICustomTool(tool)) reducedTools.push(convertCompletionsCustomTool(tool));
|
|
1043
|
+
return reducedTools;
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1703
1046
|
/**
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
_convertCompletionsMessageToBaseMessage(message, rawResponse) {
|
|
2015
|
-
const rawToolCalls = message.tool_calls;
|
|
2016
|
-
switch (message.role) {
|
|
2017
|
-
case "assistant": {
|
|
2018
|
-
const toolCalls = [];
|
|
2019
|
-
const invalidToolCalls = [];
|
|
2020
|
-
for (const rawToolCall of rawToolCalls ?? []) {
|
|
2021
|
-
try {
|
|
2022
|
-
toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
|
|
2023
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2024
|
-
}
|
|
2025
|
-
catch (e) {
|
|
2026
|
-
invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
|
|
2027
|
-
}
|
|
2028
|
-
}
|
|
2029
|
-
const additional_kwargs = {
|
|
2030
|
-
function_call: message.function_call,
|
|
2031
|
-
tool_calls: rawToolCalls,
|
|
2032
|
-
};
|
|
2033
|
-
if (this.__includeRawResponse !== undefined) {
|
|
2034
|
-
additional_kwargs.__raw_response = rawResponse;
|
|
2035
|
-
}
|
|
2036
|
-
const response_metadata = {
|
|
2037
|
-
model_name: rawResponse.model,
|
|
2038
|
-
...(rawResponse.system_fingerprint
|
|
2039
|
-
? {
|
|
2040
|
-
usage: { ...rawResponse.usage },
|
|
2041
|
-
system_fingerprint: rawResponse.system_fingerprint,
|
|
2042
|
-
}
|
|
2043
|
-
: {}),
|
|
2044
|
-
};
|
|
2045
|
-
if (message.audio) {
|
|
2046
|
-
additional_kwargs.audio = message.audio;
|
|
2047
|
-
}
|
|
2048
|
-
return new AIMessage({
|
|
2049
|
-
content: message.content || "",
|
|
2050
|
-
tool_calls: toolCalls,
|
|
2051
|
-
invalid_tool_calls: invalidToolCalls,
|
|
2052
|
-
additional_kwargs,
|
|
2053
|
-
response_metadata,
|
|
2054
|
-
id: rawResponse.id,
|
|
2055
|
-
});
|
|
2056
|
-
}
|
|
2057
|
-
default:
|
|
2058
|
-
return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
2059
|
-
}
|
|
2060
|
-
}
|
|
2061
|
-
/** @internal */
|
|
2062
|
-
_convertCompletionsDeltaToBaseMessageChunk(
|
|
2063
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2064
|
-
delta, rawResponse, defaultRole) {
|
|
2065
|
-
const role = delta.role ?? defaultRole;
|
|
2066
|
-
const content = delta.content ?? "";
|
|
2067
|
-
let additional_kwargs;
|
|
2068
|
-
if (delta.function_call) {
|
|
2069
|
-
additional_kwargs = {
|
|
2070
|
-
function_call: delta.function_call,
|
|
2071
|
-
};
|
|
2072
|
-
}
|
|
2073
|
-
else if (delta.tool_calls) {
|
|
2074
|
-
additional_kwargs = {
|
|
2075
|
-
tool_calls: delta.tool_calls,
|
|
2076
|
-
};
|
|
2077
|
-
}
|
|
2078
|
-
else {
|
|
2079
|
-
additional_kwargs = {};
|
|
2080
|
-
}
|
|
2081
|
-
if (this.__includeRawResponse) {
|
|
2082
|
-
additional_kwargs.__raw_response = rawResponse;
|
|
2083
|
-
}
|
|
2084
|
-
if (delta.audio) {
|
|
2085
|
-
additional_kwargs.audio = {
|
|
2086
|
-
...delta.audio,
|
|
2087
|
-
index: rawResponse.choices[0].index,
|
|
2088
|
-
};
|
|
2089
|
-
}
|
|
2090
|
-
const response_metadata = { usage: { ...rawResponse.usage } };
|
|
2091
|
-
if (role === "user") {
|
|
2092
|
-
return new HumanMessageChunk({ content, response_metadata });
|
|
2093
|
-
}
|
|
2094
|
-
else if (role === "assistant") {
|
|
2095
|
-
const toolCallChunks = [];
|
|
2096
|
-
if (Array.isArray(delta.tool_calls)) {
|
|
2097
|
-
for (const rawToolCall of delta.tool_calls) {
|
|
2098
|
-
toolCallChunks.push({
|
|
2099
|
-
name: rawToolCall.function?.name,
|
|
2100
|
-
args: rawToolCall.function?.arguments,
|
|
2101
|
-
id: rawToolCall.id,
|
|
2102
|
-
index: rawToolCall.index,
|
|
2103
|
-
type: "tool_call_chunk",
|
|
2104
|
-
});
|
|
2105
|
-
}
|
|
2106
|
-
}
|
|
2107
|
-
return new AIMessageChunk({
|
|
2108
|
-
content,
|
|
2109
|
-
tool_call_chunks: toolCallChunks,
|
|
2110
|
-
additional_kwargs,
|
|
2111
|
-
id: rawResponse.id,
|
|
2112
|
-
response_metadata,
|
|
2113
|
-
});
|
|
2114
|
-
}
|
|
2115
|
-
else if (role === "system") {
|
|
2116
|
-
return new SystemMessageChunk({ content, response_metadata });
|
|
2117
|
-
}
|
|
2118
|
-
else if (role === "developer") {
|
|
2119
|
-
return new SystemMessageChunk({
|
|
2120
|
-
content,
|
|
2121
|
-
response_metadata,
|
|
2122
|
-
additional_kwargs: {
|
|
2123
|
-
__openai_role__: "developer",
|
|
2124
|
-
},
|
|
2125
|
-
});
|
|
2126
|
-
}
|
|
2127
|
-
else if (role === "function") {
|
|
2128
|
-
return new FunctionMessageChunk({
|
|
2129
|
-
content,
|
|
2130
|
-
additional_kwargs,
|
|
2131
|
-
name: delta.name,
|
|
2132
|
-
response_metadata,
|
|
2133
|
-
});
|
|
2134
|
-
}
|
|
2135
|
-
else if (role === "tool") {
|
|
2136
|
-
return new ToolMessageChunk({
|
|
2137
|
-
content,
|
|
2138
|
-
additional_kwargs,
|
|
2139
|
-
tool_call_id: delta.tool_call_id,
|
|
2140
|
-
response_metadata,
|
|
2141
|
-
});
|
|
2142
|
-
}
|
|
2143
|
-
else {
|
|
2144
|
-
return new ChatMessageChunk({ content, role, response_metadata });
|
|
2145
|
-
}
|
|
2146
|
-
}
|
|
2147
|
-
}
|
|
1047
|
+
* OpenAI Completions API implementation.
|
|
1048
|
+
* @internal
|
|
1049
|
+
*/
|
|
1050
|
+
var ChatOpenAICompletions = class extends BaseChatOpenAI {
|
|
1051
|
+
/** @internal */
|
|
1052
|
+
invocationParams(options, extra) {
|
|
1053
|
+
let strict;
|
|
1054
|
+
if (options?.strict !== void 0) strict = options.strict;
|
|
1055
|
+
else if (this.supportsStrictToolCalling !== void 0) strict = this.supportsStrictToolCalling;
|
|
1056
|
+
let streamOptionsConfig = {};
|
|
1057
|
+
if (options?.stream_options !== void 0) streamOptionsConfig = { stream_options: options.stream_options };
|
|
1058
|
+
else if (this.streamUsage && (this.streaming || extra?.streaming)) streamOptionsConfig = { stream_options: { include_usage: true } };
|
|
1059
|
+
const params = {
|
|
1060
|
+
model: this.model,
|
|
1061
|
+
temperature: this.temperature,
|
|
1062
|
+
top_p: this.topP,
|
|
1063
|
+
frequency_penalty: this.frequencyPenalty,
|
|
1064
|
+
presence_penalty: this.presencePenalty,
|
|
1065
|
+
logprobs: this.logprobs,
|
|
1066
|
+
top_logprobs: this.topLogprobs,
|
|
1067
|
+
n: this.n,
|
|
1068
|
+
logit_bias: this.logitBias,
|
|
1069
|
+
stop: options?.stop ?? this.stopSequences,
|
|
1070
|
+
user: this.user,
|
|
1071
|
+
stream: this.streaming,
|
|
1072
|
+
functions: options?.functions,
|
|
1073
|
+
function_call: options?.function_call,
|
|
1074
|
+
tools: options?.tools?.length ? options.tools.map((tool) => this._convertChatOpenAIToolToCompletionsTool(tool, { strict })) : void 0,
|
|
1075
|
+
tool_choice: formatToOpenAIToolChoice(options?.tool_choice),
|
|
1076
|
+
response_format: this._getResponseFormat(options?.response_format),
|
|
1077
|
+
seed: options?.seed,
|
|
1078
|
+
...streamOptionsConfig,
|
|
1079
|
+
parallel_tool_calls: options?.parallel_tool_calls,
|
|
1080
|
+
...this.audio || options?.audio ? { audio: this.audio || options?.audio } : {},
|
|
1081
|
+
...this.modalities || options?.modalities ? { modalities: this.modalities || options?.modalities } : {},
|
|
1082
|
+
...this.modelKwargs,
|
|
1083
|
+
prompt_cache_key: options?.promptCacheKey ?? this.promptCacheKey,
|
|
1084
|
+
verbosity: options?.verbosity ?? this.verbosity
|
|
1085
|
+
};
|
|
1086
|
+
if (options?.prediction !== void 0) params.prediction = options.prediction;
|
|
1087
|
+
if (this.service_tier !== void 0) params.service_tier = this.service_tier;
|
|
1088
|
+
if (options?.service_tier !== void 0) params.service_tier = options.service_tier;
|
|
1089
|
+
const reasoning = this._getReasoningParams(options);
|
|
1090
|
+
if (reasoning !== void 0 && reasoning.effort !== void 0) params.reasoning_effort = reasoning.effort;
|
|
1091
|
+
if (isReasoningModel(params.model)) params.max_completion_tokens = this.maxTokens === -1 ? void 0 : this.maxTokens;
|
|
1092
|
+
else params.max_tokens = this.maxTokens === -1 ? void 0 : this.maxTokens;
|
|
1093
|
+
return params;
|
|
1094
|
+
}
|
|
1095
|
+
async _generate(messages, options, runManager) {
|
|
1096
|
+
const usageMetadata = {};
|
|
1097
|
+
const params = this.invocationParams(options);
|
|
1098
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
1099
|
+
if (params.stream) {
|
|
1100
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
1101
|
+
const finalChunks = {};
|
|
1102
|
+
for await (const chunk of stream) {
|
|
1103
|
+
chunk.message.response_metadata = {
|
|
1104
|
+
...chunk.generationInfo,
|
|
1105
|
+
...chunk.message.response_metadata
|
|
1106
|
+
};
|
|
1107
|
+
const index = chunk.generationInfo?.completion ?? 0;
|
|
1108
|
+
if (finalChunks[index] === void 0) finalChunks[index] = chunk;
|
|
1109
|
+
else finalChunks[index] = finalChunks[index].concat(chunk);
|
|
1110
|
+
}
|
|
1111
|
+
const generations = Object.entries(finalChunks).sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10)).map(([_, value]) => value);
|
|
1112
|
+
const { functions, function_call } = this.invocationParams(options);
|
|
1113
|
+
const promptTokenUsage = await this._getEstimatedTokenCountFromPrompt(messages, functions, function_call);
|
|
1114
|
+
const completionTokenUsage = await this._getNumTokensFromGenerations(generations);
|
|
1115
|
+
usageMetadata.input_tokens = promptTokenUsage;
|
|
1116
|
+
usageMetadata.output_tokens = completionTokenUsage;
|
|
1117
|
+
usageMetadata.total_tokens = promptTokenUsage + completionTokenUsage;
|
|
1118
|
+
return {
|
|
1119
|
+
generations,
|
|
1120
|
+
llmOutput: { estimatedTokenUsage: {
|
|
1121
|
+
promptTokens: usageMetadata.input_tokens,
|
|
1122
|
+
completionTokens: usageMetadata.output_tokens,
|
|
1123
|
+
totalTokens: usageMetadata.total_tokens
|
|
1124
|
+
} }
|
|
1125
|
+
};
|
|
1126
|
+
} else {
|
|
1127
|
+
const data = await this.completionWithRetry({
|
|
1128
|
+
...params,
|
|
1129
|
+
stream: false,
|
|
1130
|
+
messages: messagesMapped
|
|
1131
|
+
}, {
|
|
1132
|
+
signal: options?.signal,
|
|
1133
|
+
...options?.options
|
|
1134
|
+
});
|
|
1135
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, prompt_tokens_details: promptTokensDetails, completion_tokens_details: completionTokensDetails } = data?.usage ?? {};
|
|
1136
|
+
if (completionTokens) usageMetadata.output_tokens = (usageMetadata.output_tokens ?? 0) + completionTokens;
|
|
1137
|
+
if (promptTokens) usageMetadata.input_tokens = (usageMetadata.input_tokens ?? 0) + promptTokens;
|
|
1138
|
+
if (totalTokens) usageMetadata.total_tokens = (usageMetadata.total_tokens ?? 0) + totalTokens;
|
|
1139
|
+
if (promptTokensDetails?.audio_tokens !== null || promptTokensDetails?.cached_tokens !== null) usageMetadata.input_token_details = {
|
|
1140
|
+
...promptTokensDetails?.audio_tokens !== null && { audio: promptTokensDetails?.audio_tokens },
|
|
1141
|
+
...promptTokensDetails?.cached_tokens !== null && { cache_read: promptTokensDetails?.cached_tokens }
|
|
1142
|
+
};
|
|
1143
|
+
if (completionTokensDetails?.audio_tokens !== null || completionTokensDetails?.reasoning_tokens !== null) usageMetadata.output_token_details = {
|
|
1144
|
+
...completionTokensDetails?.audio_tokens !== null && { audio: completionTokensDetails?.audio_tokens },
|
|
1145
|
+
...completionTokensDetails?.reasoning_tokens !== null && { reasoning: completionTokensDetails?.reasoning_tokens }
|
|
1146
|
+
};
|
|
1147
|
+
const generations = [];
|
|
1148
|
+
for (const part of data?.choices ?? []) {
|
|
1149
|
+
const text = part.message?.content ?? "";
|
|
1150
|
+
const generation = {
|
|
1151
|
+
text,
|
|
1152
|
+
message: this._convertCompletionsMessageToBaseMessage(part.message ?? { role: "assistant" }, data)
|
|
1153
|
+
};
|
|
1154
|
+
generation.generationInfo = {
|
|
1155
|
+
...part.finish_reason ? { finish_reason: part.finish_reason } : {},
|
|
1156
|
+
...part.logprobs ? { logprobs: part.logprobs } : {}
|
|
1157
|
+
};
|
|
1158
|
+
if (isAIMessage(generation.message)) generation.message.usage_metadata = usageMetadata;
|
|
1159
|
+
generation.message = new AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith("lc_"))));
|
|
1160
|
+
generations.push(generation);
|
|
1161
|
+
}
|
|
1162
|
+
return {
|
|
1163
|
+
generations,
|
|
1164
|
+
llmOutput: { tokenUsage: {
|
|
1165
|
+
promptTokens: usageMetadata.input_tokens,
|
|
1166
|
+
completionTokens: usageMetadata.output_tokens,
|
|
1167
|
+
totalTokens: usageMetadata.total_tokens
|
|
1168
|
+
} }
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
1173
|
+
const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
|
|
1174
|
+
const params = {
|
|
1175
|
+
...this.invocationParams(options, { streaming: true }),
|
|
1176
|
+
messages: messagesMapped,
|
|
1177
|
+
stream: true
|
|
1178
|
+
};
|
|
1179
|
+
let defaultRole;
|
|
1180
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
1181
|
+
let usage;
|
|
1182
|
+
for await (const data of streamIterable) {
|
|
1183
|
+
const choice = data?.choices?.[0];
|
|
1184
|
+
if (data.usage) usage = data.usage;
|
|
1185
|
+
if (!choice) continue;
|
|
1186
|
+
const { delta } = choice;
|
|
1187
|
+
if (!delta) continue;
|
|
1188
|
+
const chunk = this._convertCompletionsDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
1189
|
+
defaultRole = delta.role ?? defaultRole;
|
|
1190
|
+
const newTokenIndices = {
|
|
1191
|
+
prompt: options.promptIndex ?? 0,
|
|
1192
|
+
completion: choice.index ?? 0
|
|
1193
|
+
};
|
|
1194
|
+
if (typeof chunk.content !== "string") {
|
|
1195
|
+
console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
|
|
1196
|
+
continue;
|
|
1197
|
+
}
|
|
1198
|
+
const generationInfo = { ...newTokenIndices };
|
|
1199
|
+
if (choice.finish_reason != null) {
|
|
1200
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
1201
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
1202
|
+
generationInfo.model_name = data.model;
|
|
1203
|
+
generationInfo.service_tier = data.service_tier;
|
|
1204
|
+
}
|
|
1205
|
+
if (this.logprobs) generationInfo.logprobs = choice.logprobs;
|
|
1206
|
+
const generationChunk = new ChatGenerationChunk({
|
|
1207
|
+
message: chunk,
|
|
1208
|
+
text: chunk.content,
|
|
1209
|
+
generationInfo
|
|
1210
|
+
});
|
|
1211
|
+
yield generationChunk;
|
|
1212
|
+
await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, void 0, void 0, void 0, { chunk: generationChunk });
|
|
1213
|
+
}
|
|
1214
|
+
if (usage) {
|
|
1215
|
+
const inputTokenDetails = {
|
|
1216
|
+
...usage.prompt_tokens_details?.audio_tokens !== null && { audio: usage.prompt_tokens_details?.audio_tokens },
|
|
1217
|
+
...usage.prompt_tokens_details?.cached_tokens !== null && { cache_read: usage.prompt_tokens_details?.cached_tokens }
|
|
1218
|
+
};
|
|
1219
|
+
const outputTokenDetails = {
|
|
1220
|
+
...usage.completion_tokens_details?.audio_tokens !== null && { audio: usage.completion_tokens_details?.audio_tokens },
|
|
1221
|
+
...usage.completion_tokens_details?.reasoning_tokens !== null && { reasoning: usage.completion_tokens_details?.reasoning_tokens }
|
|
1222
|
+
};
|
|
1223
|
+
const generationChunk = new ChatGenerationChunk({
|
|
1224
|
+
message: new AIMessageChunk({
|
|
1225
|
+
content: "",
|
|
1226
|
+
response_metadata: { usage: { ...usage } },
|
|
1227
|
+
usage_metadata: {
|
|
1228
|
+
input_tokens: usage.prompt_tokens,
|
|
1229
|
+
output_tokens: usage.completion_tokens,
|
|
1230
|
+
total_tokens: usage.total_tokens,
|
|
1231
|
+
...Object.keys(inputTokenDetails).length > 0 && { input_token_details: inputTokenDetails },
|
|
1232
|
+
...Object.keys(outputTokenDetails).length > 0 && { output_token_details: outputTokenDetails }
|
|
1233
|
+
}
|
|
1234
|
+
}),
|
|
1235
|
+
text: ""
|
|
1236
|
+
});
|
|
1237
|
+
yield generationChunk;
|
|
1238
|
+
}
|
|
1239
|
+
if (options.signal?.aborted) throw new Error("AbortError");
|
|
1240
|
+
}
|
|
1241
|
+
async completionWithRetry(request, requestOptions) {
|
|
1242
|
+
const clientOptions = this._getClientOptions(requestOptions);
|
|
1243
|
+
const isParseableFormat = request.response_format && request.response_format.type === "json_schema";
|
|
1244
|
+
return this.caller.call(async () => {
|
|
1245
|
+
try {
|
|
1246
|
+
if (isParseableFormat && !request.stream) return await this.client.chat.completions.parse(request, clientOptions);
|
|
1247
|
+
else return await this.client.chat.completions.create(request, clientOptions);
|
|
1248
|
+
} catch (e) {
|
|
1249
|
+
const error = wrapOpenAIClientError(e);
|
|
1250
|
+
throw error;
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
/** @internal */
|
|
1255
|
+
_convertCompletionsMessageToBaseMessage(message, rawResponse) {
|
|
1256
|
+
const rawToolCalls = message.tool_calls;
|
|
1257
|
+
switch (message.role) {
|
|
1258
|
+
case "assistant": {
|
|
1259
|
+
const toolCalls = [];
|
|
1260
|
+
const invalidToolCalls = [];
|
|
1261
|
+
for (const rawToolCall of rawToolCalls ?? []) try {
|
|
1262
|
+
toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
|
|
1263
|
+
} catch (e) {
|
|
1264
|
+
invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
|
|
1265
|
+
}
|
|
1266
|
+
const additional_kwargs = {
|
|
1267
|
+
function_call: message.function_call,
|
|
1268
|
+
tool_calls: rawToolCalls
|
|
1269
|
+
};
|
|
1270
|
+
if (this.__includeRawResponse !== void 0) additional_kwargs.__raw_response = rawResponse;
|
|
1271
|
+
const response_metadata = {
|
|
1272
|
+
model_provider: "openai",
|
|
1273
|
+
model_name: rawResponse.model,
|
|
1274
|
+
...rawResponse.system_fingerprint ? {
|
|
1275
|
+
usage: { ...rawResponse.usage },
|
|
1276
|
+
system_fingerprint: rawResponse.system_fingerprint
|
|
1277
|
+
} : {}
|
|
1278
|
+
};
|
|
1279
|
+
if (message.audio) additional_kwargs.audio = message.audio;
|
|
1280
|
+
return new AIMessage({
|
|
1281
|
+
content: message.content || "",
|
|
1282
|
+
tool_calls: toolCalls,
|
|
1283
|
+
invalid_tool_calls: invalidToolCalls,
|
|
1284
|
+
additional_kwargs,
|
|
1285
|
+
response_metadata,
|
|
1286
|
+
id: rawResponse.id
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
default: return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
/** @internal */
|
|
1293
|
+
_convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
|
|
1294
|
+
const role = delta.role ?? defaultRole;
|
|
1295
|
+
const content = delta.content ?? "";
|
|
1296
|
+
let additional_kwargs;
|
|
1297
|
+
if (delta.function_call) additional_kwargs = { function_call: delta.function_call };
|
|
1298
|
+
else if (delta.tool_calls) additional_kwargs = { tool_calls: delta.tool_calls };
|
|
1299
|
+
else additional_kwargs = {};
|
|
1300
|
+
if (this.__includeRawResponse) additional_kwargs.__raw_response = rawResponse;
|
|
1301
|
+
if (delta.audio) additional_kwargs.audio = {
|
|
1302
|
+
...delta.audio,
|
|
1303
|
+
index: rawResponse.choices[0].index
|
|
1304
|
+
};
|
|
1305
|
+
const response_metadata = {
|
|
1306
|
+
model_provider: "openai",
|
|
1307
|
+
usage: { ...rawResponse.usage }
|
|
1308
|
+
};
|
|
1309
|
+
if (role === "user") return new HumanMessageChunk({
|
|
1310
|
+
content,
|
|
1311
|
+
response_metadata
|
|
1312
|
+
});
|
|
1313
|
+
else if (role === "assistant") {
|
|
1314
|
+
const toolCallChunks = [];
|
|
1315
|
+
if (Array.isArray(delta.tool_calls)) for (const rawToolCall of delta.tool_calls) toolCallChunks.push({
|
|
1316
|
+
name: rawToolCall.function?.name,
|
|
1317
|
+
args: rawToolCall.function?.arguments,
|
|
1318
|
+
id: rawToolCall.id,
|
|
1319
|
+
index: rawToolCall.index,
|
|
1320
|
+
type: "tool_call_chunk"
|
|
1321
|
+
});
|
|
1322
|
+
return new AIMessageChunk({
|
|
1323
|
+
content,
|
|
1324
|
+
tool_call_chunks: toolCallChunks,
|
|
1325
|
+
additional_kwargs,
|
|
1326
|
+
id: rawResponse.id,
|
|
1327
|
+
response_metadata
|
|
1328
|
+
});
|
|
1329
|
+
} else if (role === "system") return new SystemMessageChunk({
|
|
1330
|
+
content,
|
|
1331
|
+
response_metadata
|
|
1332
|
+
});
|
|
1333
|
+
else if (role === "developer") return new SystemMessageChunk({
|
|
1334
|
+
content,
|
|
1335
|
+
response_metadata,
|
|
1336
|
+
additional_kwargs: { __openai_role__: "developer" }
|
|
1337
|
+
});
|
|
1338
|
+
else if (role === "function") return new FunctionMessageChunk({
|
|
1339
|
+
content,
|
|
1340
|
+
additional_kwargs,
|
|
1341
|
+
name: delta.name,
|
|
1342
|
+
response_metadata
|
|
1343
|
+
});
|
|
1344
|
+
else if (role === "tool") return new ToolMessageChunk({
|
|
1345
|
+
content,
|
|
1346
|
+
additional_kwargs,
|
|
1347
|
+
tool_call_id: delta.tool_call_id,
|
|
1348
|
+
response_metadata
|
|
1349
|
+
});
|
|
1350
|
+
else return new ChatMessageChunk({
|
|
1351
|
+
content,
|
|
1352
|
+
role,
|
|
1353
|
+
response_metadata
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
};
|
|
2148
1357
|
/**
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
invocationParams(options) {
|
|
2746
|
-
const optionsWithDefaults = this._combineCallOptions(options);
|
|
2747
|
-
if (this._useResponsesApi(options)) {
|
|
2748
|
-
return this.responses.invocationParams(optionsWithDefaults);
|
|
2749
|
-
}
|
|
2750
|
-
return this.completions.invocationParams(optionsWithDefaults);
|
|
2751
|
-
}
|
|
2752
|
-
/** @ignore */
|
|
2753
|
-
async _generate(messages, options, runManager) {
|
|
2754
|
-
if (this._useResponsesApi(options)) {
|
|
2755
|
-
return this.responses._generate(messages, options);
|
|
2756
|
-
}
|
|
2757
|
-
return this.completions._generate(messages, options, runManager);
|
|
2758
|
-
}
|
|
2759
|
-
async *_streamResponseChunks(messages, options, runManager) {
|
|
2760
|
-
if (this._useResponsesApi(options)) {
|
|
2761
|
-
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2762
|
-
return;
|
|
2763
|
-
}
|
|
2764
|
-
yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
2765
|
-
}
|
|
2766
|
-
withConfig(config) {
|
|
2767
|
-
const newModel = new ChatOpenAI(this.fields);
|
|
2768
|
-
newModel.defaultOptions = { ...this.defaultOptions, ...config };
|
|
2769
|
-
return newModel;
|
|
2770
|
-
}
|
|
2771
|
-
}
|
|
1358
|
+
* OpenAI chat model integration.
|
|
1359
|
+
*
|
|
1360
|
+
* To use with Azure, import the `AzureChatOpenAI` class.
|
|
1361
|
+
*
|
|
1362
|
+
* Setup:
|
|
1363
|
+
* Install `@langchain/openai` and set an environment variable named `OPENAI_API_KEY`.
|
|
1364
|
+
*
|
|
1365
|
+
* ```bash
|
|
1366
|
+
* npm install @langchain/openai
|
|
1367
|
+
* export OPENAI_API_KEY="your-api-key"
|
|
1368
|
+
* ```
|
|
1369
|
+
*
|
|
1370
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html#constructor)
|
|
1371
|
+
*
|
|
1372
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_openai.ChatOpenAICallOptions.html)
|
|
1373
|
+
*
|
|
1374
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
1375
|
+
* They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
1376
|
+
*
|
|
1377
|
+
* ```typescript
|
|
1378
|
+
* // When calling `.withConfig`, call options should be passed via the first argument
|
|
1379
|
+
* const llmWithArgsBound = llm.withConfig({
|
|
1380
|
+
* stop: ["\n"],
|
|
1381
|
+
* tools: [...],
|
|
1382
|
+
* });
|
|
1383
|
+
*
|
|
1384
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
1385
|
+
* const llmWithTools = llm.bindTools(
|
|
1386
|
+
* [...],
|
|
1387
|
+
* {
|
|
1388
|
+
* tool_choice: "auto",
|
|
1389
|
+
* }
|
|
1390
|
+
* );
|
|
1391
|
+
* ```
|
|
1392
|
+
*
|
|
1393
|
+
* ## Examples
|
|
1394
|
+
*
|
|
1395
|
+
* <details open>
|
|
1396
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
1397
|
+
*
|
|
1398
|
+
* ```typescript
|
|
1399
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
1400
|
+
*
|
|
1401
|
+
* const llm = new ChatOpenAI({
|
|
1402
|
+
* model: "gpt-4o-mini",
|
|
1403
|
+
* temperature: 0,
|
|
1404
|
+
* maxTokens: undefined,
|
|
1405
|
+
* timeout: undefined,
|
|
1406
|
+
* maxRetries: 2,
|
|
1407
|
+
* // apiKey: "...",
|
|
1408
|
+
* // baseUrl: "...",
|
|
1409
|
+
* // organization: "...",
|
|
1410
|
+
* // other params...
|
|
1411
|
+
* });
|
|
1412
|
+
* ```
|
|
1413
|
+
* </details>
|
|
1414
|
+
*
|
|
1415
|
+
* <br />
|
|
1416
|
+
*
|
|
1417
|
+
* <details>
|
|
1418
|
+
* <summary><strong>Invoking</strong></summary>
|
|
1419
|
+
*
|
|
1420
|
+
* ```typescript
|
|
1421
|
+
* const input = `Translate "I love programming" into French.`;
|
|
1422
|
+
*
|
|
1423
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
1424
|
+
* const result = await llm.invoke(input);
|
|
1425
|
+
* console.log(result);
|
|
1426
|
+
* ```
|
|
1427
|
+
*
|
|
1428
|
+
* ```txt
|
|
1429
|
+
* AIMessage {
|
|
1430
|
+
* "id": "chatcmpl-9u4Mpu44CbPjwYFkTbeoZgvzB00Tz",
|
|
1431
|
+
* "content": "J'adore la programmation.",
|
|
1432
|
+
* "response_metadata": {
|
|
1433
|
+
* "tokenUsage": {
|
|
1434
|
+
* "completionTokens": 5,
|
|
1435
|
+
* "promptTokens": 28,
|
|
1436
|
+
* "totalTokens": 33
|
|
1437
|
+
* },
|
|
1438
|
+
* "finish_reason": "stop",
|
|
1439
|
+
* "system_fingerprint": "fp_3aa7262c27"
|
|
1440
|
+
* },
|
|
1441
|
+
* "usage_metadata": {
|
|
1442
|
+
* "input_tokens": 28,
|
|
1443
|
+
* "output_tokens": 5,
|
|
1444
|
+
* "total_tokens": 33
|
|
1445
|
+
* }
|
|
1446
|
+
* }
|
|
1447
|
+
* ```
|
|
1448
|
+
* </details>
|
|
1449
|
+
*
|
|
1450
|
+
* <br />
|
|
1451
|
+
*
|
|
1452
|
+
* <details>
|
|
1453
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
1454
|
+
*
|
|
1455
|
+
* ```typescript
|
|
1456
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
1457
|
+
* console.log(chunk);
|
|
1458
|
+
* }
|
|
1459
|
+
* ```
|
|
1460
|
+
*
|
|
1461
|
+
* ```txt
|
|
1462
|
+
* AIMessageChunk {
|
|
1463
|
+
* "id": "chatcmpl-9u4NWB7yUeHCKdLr6jP3HpaOYHTqs",
|
|
1464
|
+
* "content": ""
|
|
1465
|
+
* }
|
|
1466
|
+
* AIMessageChunk {
|
|
1467
|
+
* "content": "J"
|
|
1468
|
+
* }
|
|
1469
|
+
* AIMessageChunk {
|
|
1470
|
+
* "content": "'adore"
|
|
1471
|
+
* }
|
|
1472
|
+
* AIMessageChunk {
|
|
1473
|
+
* "content": " la"
|
|
1474
|
+
* }
|
|
1475
|
+
* AIMessageChunk {
|
|
1476
|
+
* "content": " programmation",,
|
|
1477
|
+
* }
|
|
1478
|
+
* AIMessageChunk {
|
|
1479
|
+
* "content": ".",,
|
|
1480
|
+
* }
|
|
1481
|
+
* AIMessageChunk {
|
|
1482
|
+
* "content": "",
|
|
1483
|
+
* "response_metadata": {
|
|
1484
|
+
* "finish_reason": "stop",
|
|
1485
|
+
* "system_fingerprint": "fp_c9aa9c0491"
|
|
1486
|
+
* },
|
|
1487
|
+
* }
|
|
1488
|
+
* AIMessageChunk {
|
|
1489
|
+
* "content": "",
|
|
1490
|
+
* "usage_metadata": {
|
|
1491
|
+
* "input_tokens": 28,
|
|
1492
|
+
* "output_tokens": 5,
|
|
1493
|
+
* "total_tokens": 33
|
|
1494
|
+
* }
|
|
1495
|
+
* }
|
|
1496
|
+
* ```
|
|
1497
|
+
* </details>
|
|
1498
|
+
*
|
|
1499
|
+
* <br />
|
|
1500
|
+
*
|
|
1501
|
+
* <details>
|
|
1502
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
1503
|
+
*
|
|
1504
|
+
* ```typescript
|
|
1505
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
1506
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
1507
|
+
*
|
|
1508
|
+
* const stream = await llm.stream(input);
|
|
1509
|
+
* let full: AIMessageChunk | undefined;
|
|
1510
|
+
* for await (const chunk of stream) {
|
|
1511
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
1512
|
+
* }
|
|
1513
|
+
* console.log(full);
|
|
1514
|
+
* ```
|
|
1515
|
+
*
|
|
1516
|
+
* ```txt
|
|
1517
|
+
* AIMessageChunk {
|
|
1518
|
+
* "id": "chatcmpl-9u4PnX6Fy7OmK46DASy0bH6cxn5Xu",
|
|
1519
|
+
* "content": "J'adore la programmation.",
|
|
1520
|
+
* "response_metadata": {
|
|
1521
|
+
* "prompt": 0,
|
|
1522
|
+
* "completion": 0,
|
|
1523
|
+
* "finish_reason": "stop",
|
|
1524
|
+
* },
|
|
1525
|
+
* "usage_metadata": {
|
|
1526
|
+
* "input_tokens": 28,
|
|
1527
|
+
* "output_tokens": 5,
|
|
1528
|
+
* "total_tokens": 33
|
|
1529
|
+
* }
|
|
1530
|
+
* }
|
|
1531
|
+
* ```
|
|
1532
|
+
* </details>
|
|
1533
|
+
*
|
|
1534
|
+
* <br />
|
|
1535
|
+
*
|
|
1536
|
+
* <details>
|
|
1537
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
1538
|
+
*
|
|
1539
|
+
* ```typescript
|
|
1540
|
+
* import { z } from 'zod';
|
|
1541
|
+
*
|
|
1542
|
+
* const GetWeather = {
|
|
1543
|
+
* name: "GetWeather",
|
|
1544
|
+
* description: "Get the current weather in a given location",
|
|
1545
|
+
* schema: z.object({
|
|
1546
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
1547
|
+
* }),
|
|
1548
|
+
* }
|
|
1549
|
+
*
|
|
1550
|
+
* const GetPopulation = {
|
|
1551
|
+
* name: "GetPopulation",
|
|
1552
|
+
* description: "Get the current population in a given location",
|
|
1553
|
+
* schema: z.object({
|
|
1554
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
1555
|
+
* }),
|
|
1556
|
+
* }
|
|
1557
|
+
*
|
|
1558
|
+
* const llmWithTools = llm.bindTools(
|
|
1559
|
+
* [GetWeather, GetPopulation],
|
|
1560
|
+
* {
|
|
1561
|
+
* // strict: true // enforce tool args schema is respected
|
|
1562
|
+
* }
|
|
1563
|
+
* );
|
|
1564
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
1565
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
1566
|
+
* );
|
|
1567
|
+
* console.log(aiMsg.tool_calls);
|
|
1568
|
+
* ```
|
|
1569
|
+
*
|
|
1570
|
+
* ```txt
|
|
1571
|
+
* [
|
|
1572
|
+
* {
|
|
1573
|
+
* name: 'GetWeather',
|
|
1574
|
+
* args: { location: 'Los Angeles, CA' },
|
|
1575
|
+
* type: 'tool_call',
|
|
1576
|
+
* id: 'call_uPU4FiFzoKAtMxfmPnfQL6UK'
|
|
1577
|
+
* },
|
|
1578
|
+
* {
|
|
1579
|
+
* name: 'GetWeather',
|
|
1580
|
+
* args: { location: 'New York, NY' },
|
|
1581
|
+
* type: 'tool_call',
|
|
1582
|
+
* id: 'call_UNkEwuQsHrGYqgDQuH9nPAtX'
|
|
1583
|
+
* },
|
|
1584
|
+
* {
|
|
1585
|
+
* name: 'GetPopulation',
|
|
1586
|
+
* args: { location: 'Los Angeles, CA' },
|
|
1587
|
+
* type: 'tool_call',
|
|
1588
|
+
* id: 'call_kL3OXxaq9OjIKqRTpvjaCH14'
|
|
1589
|
+
* },
|
|
1590
|
+
* {
|
|
1591
|
+
* name: 'GetPopulation',
|
|
1592
|
+
* args: { location: 'New York, NY' },
|
|
1593
|
+
* type: 'tool_call',
|
|
1594
|
+
* id: 'call_s9KQB1UWj45LLGaEnjz0179q'
|
|
1595
|
+
* }
|
|
1596
|
+
* ]
|
|
1597
|
+
* ```
|
|
1598
|
+
* </details>
|
|
1599
|
+
*
|
|
1600
|
+
* <br />
|
|
1601
|
+
*
|
|
1602
|
+
* <details>
|
|
1603
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
1604
|
+
*
|
|
1605
|
+
* ```typescript
|
|
1606
|
+
* import { z } from 'zod';
|
|
1607
|
+
*
|
|
1608
|
+
* const Joke = z.object({
|
|
1609
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
1610
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
1611
|
+
* rating: z.number().nullable().describe("How funny the joke is, from 1 to 10")
|
|
1612
|
+
* }).describe('Joke to tell user.');
|
|
1613
|
+
*
|
|
1614
|
+
* const structuredLlm = llm.withStructuredOutput(Joke, {
|
|
1615
|
+
* name: "Joke",
|
|
1616
|
+
* strict: true, // Optionally enable OpenAI structured outputs
|
|
1617
|
+
* });
|
|
1618
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
1619
|
+
* console.log(jokeResult);
|
|
1620
|
+
* ```
|
|
1621
|
+
*
|
|
1622
|
+
* ```txt
|
|
1623
|
+
* {
|
|
1624
|
+
* setup: 'Why was the cat sitting on the computer?',
|
|
1625
|
+
* punchline: 'Because it wanted to keep an eye on the mouse!',
|
|
1626
|
+
* rating: 7
|
|
1627
|
+
* }
|
|
1628
|
+
* ```
|
|
1629
|
+
* </details>
|
|
1630
|
+
*
|
|
1631
|
+
* <br />
|
|
1632
|
+
*
|
|
1633
|
+
* <details>
|
|
1634
|
+
* <summary><strong>JSON Object Response Format</strong></summary>
|
|
1635
|
+
*
|
|
1636
|
+
* ```typescript
|
|
1637
|
+
* const jsonLlm = llm.withConfig({ response_format: { type: "json_object" } });
|
|
1638
|
+
* const jsonLlmAiMsg = await jsonLlm.invoke(
|
|
1639
|
+
* "Return a JSON object with key 'randomInts' and a value of 10 random ints in [0-99]"
|
|
1640
|
+
* );
|
|
1641
|
+
* console.log(jsonLlmAiMsg.content);
|
|
1642
|
+
* ```
|
|
1643
|
+
*
|
|
1644
|
+
* ```txt
|
|
1645
|
+
* {
|
|
1646
|
+
* "randomInts": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]
|
|
1647
|
+
* }
|
|
1648
|
+
* ```
|
|
1649
|
+
* </details>
|
|
1650
|
+
*
|
|
1651
|
+
* <br />
|
|
1652
|
+
*
|
|
1653
|
+
* <details>
|
|
1654
|
+
* <summary><strong>Multimodal</strong></summary>
|
|
1655
|
+
*
|
|
1656
|
+
* ```typescript
|
|
1657
|
+
* import { HumanMessage } from '@langchain/core/messages';
|
|
1658
|
+
*
|
|
1659
|
+
* const imageUrl = "https://example.com/image.jpg";
|
|
1660
|
+
* const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
|
|
1661
|
+
* const base64Image = Buffer.from(imageData).toString('base64');
|
|
1662
|
+
*
|
|
1663
|
+
* const message = new HumanMessage({
|
|
1664
|
+
* content: [
|
|
1665
|
+
* { type: "text", text: "describe the weather in this image" },
|
|
1666
|
+
* {
|
|
1667
|
+
* type: "image_url",
|
|
1668
|
+
* image_url: { url: `data:image/jpeg;base64,${base64Image}` },
|
|
1669
|
+
* },
|
|
1670
|
+
* ]
|
|
1671
|
+
* });
|
|
1672
|
+
*
|
|
1673
|
+
* const imageDescriptionAiMsg = await llm.invoke([message]);
|
|
1674
|
+
* console.log(imageDescriptionAiMsg.content);
|
|
1675
|
+
* ```
|
|
1676
|
+
*
|
|
1677
|
+
* ```txt
|
|
1678
|
+
* The weather in the image appears to be clear and sunny. The sky is mostly blue with a few scattered white clouds, indicating fair weather. The bright sunlight is casting shadows on the green, grassy hill, suggesting it is a pleasant day with good visibility. There are no signs of rain or stormy conditions.
|
|
1679
|
+
* ```
|
|
1680
|
+
* </details>
|
|
1681
|
+
*
|
|
1682
|
+
* <br />
|
|
1683
|
+
*
|
|
1684
|
+
* <details>
|
|
1685
|
+
* <summary><strong>Usage Metadata</strong></summary>
|
|
1686
|
+
*
|
|
1687
|
+
* ```typescript
|
|
1688
|
+
* const aiMsgForMetadata = await llm.invoke(input);
|
|
1689
|
+
* console.log(aiMsgForMetadata.usage_metadata);
|
|
1690
|
+
* ```
|
|
1691
|
+
*
|
|
1692
|
+
* ```txt
|
|
1693
|
+
* { input_tokens: 28, output_tokens: 5, total_tokens: 33 }
|
|
1694
|
+
* ```
|
|
1695
|
+
* </details>
|
|
1696
|
+
*
|
|
1697
|
+
* <br />
|
|
1698
|
+
*
|
|
1699
|
+
* <details>
|
|
1700
|
+
* <summary><strong>Logprobs</strong></summary>
|
|
1701
|
+
*
|
|
1702
|
+
* ```typescript
|
|
1703
|
+
* const logprobsLlm = new ChatOpenAI({ model: "gpt-4o-mini", logprobs: true });
|
|
1704
|
+
* const aiMsgForLogprobs = await logprobsLlm.invoke(input);
|
|
1705
|
+
* console.log(aiMsgForLogprobs.response_metadata.logprobs);
|
|
1706
|
+
* ```
|
|
1707
|
+
*
|
|
1708
|
+
* ```txt
|
|
1709
|
+
* {
|
|
1710
|
+
* content: [
|
|
1711
|
+
* {
|
|
1712
|
+
* token: 'J',
|
|
1713
|
+
* logprob: -0.000050616763,
|
|
1714
|
+
* bytes: [Array],
|
|
1715
|
+
* top_logprobs: []
|
|
1716
|
+
* },
|
|
1717
|
+
* {
|
|
1718
|
+
* token: "'",
|
|
1719
|
+
* logprob: -0.01868736,
|
|
1720
|
+
* bytes: [Array],
|
|
1721
|
+
* top_logprobs: []
|
|
1722
|
+
* },
|
|
1723
|
+
* {
|
|
1724
|
+
* token: 'ad',
|
|
1725
|
+
* logprob: -0.0000030545007,
|
|
1726
|
+
* bytes: [Array],
|
|
1727
|
+
* top_logprobs: []
|
|
1728
|
+
* },
|
|
1729
|
+
* { token: 'ore', logprob: 0, bytes: [Array], top_logprobs: [] },
|
|
1730
|
+
* {
|
|
1731
|
+
* token: ' la',
|
|
1732
|
+
* logprob: -0.515404,
|
|
1733
|
+
* bytes: [Array],
|
|
1734
|
+
* top_logprobs: []
|
|
1735
|
+
* },
|
|
1736
|
+
* {
|
|
1737
|
+
* token: ' programm',
|
|
1738
|
+
* logprob: -0.0000118755715,
|
|
1739
|
+
* bytes: [Array],
|
|
1740
|
+
* top_logprobs: []
|
|
1741
|
+
* },
|
|
1742
|
+
* { token: 'ation', logprob: 0, bytes: [Array], top_logprobs: [] },
|
|
1743
|
+
* {
|
|
1744
|
+
* token: '.',
|
|
1745
|
+
* logprob: -0.0000037697225,
|
|
1746
|
+
* bytes: [Array],
|
|
1747
|
+
* top_logprobs: []
|
|
1748
|
+
* }
|
|
1749
|
+
* ],
|
|
1750
|
+
* refusal: null
|
|
1751
|
+
* }
|
|
1752
|
+
* ```
|
|
1753
|
+
* </details>
|
|
1754
|
+
*
|
|
1755
|
+
* <br />
|
|
1756
|
+
*
|
|
1757
|
+
* <details>
|
|
1758
|
+
* <summary><strong>Response Metadata</strong></summary>
|
|
1759
|
+
*
|
|
1760
|
+
* ```typescript
|
|
1761
|
+
* const aiMsgForResponseMetadata = await llm.invoke(input);
|
|
1762
|
+
* console.log(aiMsgForResponseMetadata.response_metadata);
|
|
1763
|
+
* ```
|
|
1764
|
+
*
|
|
1765
|
+
* ```txt
|
|
1766
|
+
* {
|
|
1767
|
+
* tokenUsage: { completionTokens: 5, promptTokens: 28, totalTokens: 33 },
|
|
1768
|
+
* finish_reason: 'stop',
|
|
1769
|
+
* system_fingerprint: 'fp_3aa7262c27'
|
|
1770
|
+
* }
|
|
1771
|
+
* ```
|
|
1772
|
+
* </details>
|
|
1773
|
+
*
|
|
1774
|
+
* <br />
|
|
1775
|
+
*
|
|
1776
|
+
* <details>
|
|
1777
|
+
* <summary><strong>JSON Schema Structured Output</strong></summary>
|
|
1778
|
+
*
|
|
1779
|
+
* ```typescript
|
|
1780
|
+
* const llmForJsonSchema = new ChatOpenAI({
|
|
1781
|
+
* model: "gpt-4o-2024-08-06",
|
|
1782
|
+
* }).withStructuredOutput(
|
|
1783
|
+
* z.object({
|
|
1784
|
+
* command: z.string().describe("The command to execute"),
|
|
1785
|
+
* expectedOutput: z.string().describe("The expected output of the command"),
|
|
1786
|
+
* options: z
|
|
1787
|
+
* .array(z.string())
|
|
1788
|
+
* .describe("The options you can pass to the command"),
|
|
1789
|
+
* }),
|
|
1790
|
+
* {
|
|
1791
|
+
* method: "jsonSchema",
|
|
1792
|
+
* strict: true, // Optional when using the `jsonSchema` method
|
|
1793
|
+
* }
|
|
1794
|
+
* );
|
|
1795
|
+
*
|
|
1796
|
+
* const jsonSchemaRes = await llmForJsonSchema.invoke(
|
|
1797
|
+
* "What is the command to list files in a directory?"
|
|
1798
|
+
* );
|
|
1799
|
+
* console.log(jsonSchemaRes);
|
|
1800
|
+
* ```
|
|
1801
|
+
*
|
|
1802
|
+
* ```txt
|
|
1803
|
+
* {
|
|
1804
|
+
* command: 'ls',
|
|
1805
|
+
* expectedOutput: 'A list of files and subdirectories within the specified directory.',
|
|
1806
|
+
* options: [
|
|
1807
|
+
* '-a: include directory entries whose names begin with a dot (.).',
|
|
1808
|
+
* '-l: use a long listing format.',
|
|
1809
|
+
* '-h: with -l, print sizes in human readable format (e.g., 1K, 234M, 2G).',
|
|
1810
|
+
* '-t: sort by time, newest first.',
|
|
1811
|
+
* '-r: reverse order while sorting.',
|
|
1812
|
+
* '-S: sort by file size, largest first.',
|
|
1813
|
+
* '-R: list subdirectories recursively.'
|
|
1814
|
+
* ]
|
|
1815
|
+
* }
|
|
1816
|
+
* ```
|
|
1817
|
+
* </details>
|
|
1818
|
+
*
|
|
1819
|
+
* <br />
|
|
1820
|
+
*
|
|
1821
|
+
* <details>
|
|
1822
|
+
* <summary><strong>Audio Outputs</strong></summary>
|
|
1823
|
+
*
|
|
1824
|
+
* ```typescript
|
|
1825
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
1826
|
+
*
|
|
1827
|
+
* const modelWithAudioOutput = new ChatOpenAI({
|
|
1828
|
+
* model: "gpt-4o-audio-preview",
|
|
1829
|
+
* // You may also pass these fields to `.withConfig` as a call argument.
|
|
1830
|
+
* modalities: ["text", "audio"], // Specifies that the model should output audio.
|
|
1831
|
+
* audio: {
|
|
1832
|
+
* voice: "alloy",
|
|
1833
|
+
* format: "wav",
|
|
1834
|
+
* },
|
|
1835
|
+
* });
|
|
1836
|
+
*
|
|
1837
|
+
* const audioOutputResult = await modelWithAudioOutput.invoke("Tell me a joke about cats.");
|
|
1838
|
+
* const castMessageContent = audioOutputResult.content[0] as Record<string, any>;
|
|
1839
|
+
*
|
|
1840
|
+
* console.log({
|
|
1841
|
+
* ...castMessageContent,
|
|
1842
|
+
* data: castMessageContent.data.slice(0, 100) // Sliced for brevity
|
|
1843
|
+
* })
|
|
1844
|
+
* ```
|
|
1845
|
+
*
|
|
1846
|
+
* ```txt
|
|
1847
|
+
* {
|
|
1848
|
+
* id: 'audio_67117718c6008190a3afad3e3054b9b6',
|
|
1849
|
+
* data: 'UklGRqYwBgBXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAATElTVBoAAABJTkZPSVNGVA4AAABMYXZmNTguMjkuMTAwAGRhdGFg',
|
|
1850
|
+
* expires_at: 1729201448,
|
|
1851
|
+
* transcript: 'Sure! Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'
|
|
1852
|
+
* }
|
|
1853
|
+
* ```
|
|
1854
|
+
* </details>
|
|
1855
|
+
*
|
|
1856
|
+
* <br />
|
|
1857
|
+
*
|
|
1858
|
+
* <details>
|
|
1859
|
+
* <summary><strong>Audio Outputs</strong></summary>
|
|
1860
|
+
*
|
|
1861
|
+
* ```typescript
|
|
1862
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
1863
|
+
*
|
|
1864
|
+
* const modelWithAudioOutput = new ChatOpenAI({
|
|
1865
|
+
* model: "gpt-4o-audio-preview",
|
|
1866
|
+
* // You may also pass these fields to `.withConfig` as a call argument.
|
|
1867
|
+
* modalities: ["text", "audio"], // Specifies that the model should output audio.
|
|
1868
|
+
* audio: {
|
|
1869
|
+
* voice: "alloy",
|
|
1870
|
+
* format: "wav",
|
|
1871
|
+
* },
|
|
1872
|
+
* });
|
|
1873
|
+
*
|
|
1874
|
+
* const audioOutputResult = await modelWithAudioOutput.invoke("Tell me a joke about cats.");
|
|
1875
|
+
* const castAudioContent = audioOutputResult.additional_kwargs.audio as Record<string, any>;
|
|
1876
|
+
*
|
|
1877
|
+
* console.log({
|
|
1878
|
+
* ...castAudioContent,
|
|
1879
|
+
* data: castAudioContent.data.slice(0, 100) // Sliced for brevity
|
|
1880
|
+
* })
|
|
1881
|
+
* ```
|
|
1882
|
+
*
|
|
1883
|
+
* ```txt
|
|
1884
|
+
* {
|
|
1885
|
+
* id: 'audio_67117718c6008190a3afad3e3054b9b6',
|
|
1886
|
+
* data: 'UklGRqYwBgBXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAATElTVBoAAABJTkZPSVNGVA4AAABMYXZmNTguMjkuMTAwAGRhdGFg',
|
|
1887
|
+
* expires_at: 1729201448,
|
|
1888
|
+
* transcript: 'Sure! Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'
|
|
1889
|
+
* }
|
|
1890
|
+
* ```
|
|
1891
|
+
* </details>
|
|
1892
|
+
*
|
|
1893
|
+
* <br />
|
|
1894
|
+
*/
|
|
1895
|
+
var ChatOpenAI = class ChatOpenAI extends BaseChatOpenAI {
|
|
1896
|
+
/**
|
|
1897
|
+
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
1898
|
+
* only when required in order to fulfill the request.
|
|
1899
|
+
*/
|
|
1900
|
+
useResponsesApi = false;
|
|
1901
|
+
responses;
|
|
1902
|
+
completions;
|
|
1903
|
+
get lc_serializable_keys() {
|
|
1904
|
+
return [...super.lc_serializable_keys, "useResponsesApi"];
|
|
1905
|
+
}
|
|
1906
|
+
constructor(fields) {
|
|
1907
|
+
super(fields);
|
|
1908
|
+
this.fields = fields;
|
|
1909
|
+
this.useResponsesApi = fields?.useResponsesApi ?? false;
|
|
1910
|
+
this.responses = fields?.responses ?? new ChatOpenAIResponses(fields);
|
|
1911
|
+
this.completions = fields?.completions ?? new ChatOpenAICompletions(fields);
|
|
1912
|
+
}
|
|
1913
|
+
_useResponsesApi(options) {
|
|
1914
|
+
const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
|
|
1915
|
+
const hasResponsesOnlyKwargs = options?.previous_response_id != null || options?.text != null || options?.truncation != null || options?.include != null || options?.reasoning?.summary != null || this.reasoning?.summary != null;
|
|
1916
|
+
const hasCustomTools = options?.tools?.some(isOpenAICustomTool);
|
|
1917
|
+
return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs || hasCustomTools;
|
|
1918
|
+
}
|
|
1919
|
+
getLsParams(options) {
|
|
1920
|
+
const optionsWithDefaults = this._combineCallOptions(options);
|
|
1921
|
+
if (this._useResponsesApi(options)) return this.responses.getLsParams(optionsWithDefaults);
|
|
1922
|
+
return this.completions.getLsParams(optionsWithDefaults);
|
|
1923
|
+
}
|
|
1924
|
+
invocationParams(options) {
|
|
1925
|
+
const optionsWithDefaults = this._combineCallOptions(options);
|
|
1926
|
+
if (this._useResponsesApi(options)) return this.responses.invocationParams(optionsWithDefaults);
|
|
1927
|
+
return this.completions.invocationParams(optionsWithDefaults);
|
|
1928
|
+
}
|
|
1929
|
+
/** @ignore */
|
|
1930
|
+
async _generate(messages, options, runManager) {
|
|
1931
|
+
if (this._useResponsesApi(options)) return this.responses._generate(messages, options);
|
|
1932
|
+
return this.completions._generate(messages, options, runManager);
|
|
1933
|
+
}
|
|
1934
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
1935
|
+
if (this._useResponsesApi(options)) {
|
|
1936
|
+
yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
|
|
1940
|
+
}
|
|
1941
|
+
withConfig(config) {
|
|
1942
|
+
const newModel = new ChatOpenAI(this.fields);
|
|
1943
|
+
newModel.defaultOptions = {
|
|
1944
|
+
...this.defaultOptions,
|
|
1945
|
+
...config
|
|
1946
|
+
};
|
|
1947
|
+
return newModel;
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
|
|
1951
|
+
//#endregion
|
|
1952
|
+
export { BaseChatOpenAI, ChatOpenAI, ChatOpenAICompletions, ChatOpenAIResponses };
|
|
1953
|
+
//# sourceMappingURL=chat_models.js.map
|