@langchain/openai 1.2.6 → 1.2.8-dev-1771365493298
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/CHANGELOG.md +8 -0
- package/dist/azure/chat_models/common.cjs +9 -10
- package/dist/azure/chat_models/common.cjs.map +1 -1
- package/dist/azure/chat_models/common.d.cts +0 -1
- package/dist/azure/chat_models/common.d.cts.map +1 -1
- package/dist/azure/chat_models/common.d.ts +0 -1
- package/dist/azure/chat_models/common.d.ts.map +1 -1
- package/dist/azure/chat_models/common.js +1 -1
- package/dist/azure/chat_models/common.js.map +1 -1
- package/dist/azure/chat_models/completions.cjs.map +1 -1
- package/dist/azure/chat_models/completions.d.cts.map +1 -1
- package/dist/azure/chat_models/completions.d.ts.map +1 -1
- package/dist/azure/chat_models/completions.js.map +1 -1
- package/dist/azure/chat_models/index.cjs.map +1 -1
- package/dist/azure/chat_models/index.d.cts +0 -1
- package/dist/azure/chat_models/index.d.cts.map +1 -1
- package/dist/azure/chat_models/index.d.ts +0 -1
- package/dist/azure/chat_models/index.d.ts.map +1 -1
- package/dist/azure/chat_models/index.js.map +1 -1
- package/dist/azure/chat_models/responses.cjs.map +1 -1
- package/dist/azure/chat_models/responses.d.cts.map +1 -1
- package/dist/azure/chat_models/responses.d.ts.map +1 -1
- package/dist/azure/chat_models/responses.js.map +1 -1
- package/dist/azure/embeddings.cjs +10 -13
- package/dist/azure/embeddings.cjs.map +1 -1
- package/dist/azure/embeddings.d.cts +1 -2
- package/dist/azure/embeddings.d.cts.map +1 -1
- package/dist/azure/embeddings.d.ts +1 -2
- package/dist/azure/embeddings.d.ts.map +1 -1
- package/dist/azure/embeddings.js +3 -5
- package/dist/azure/embeddings.js.map +1 -1
- package/dist/azure/llms.cjs +9 -10
- package/dist/azure/llms.cjs.map +1 -1
- package/dist/azure/llms.d.cts +2 -2
- package/dist/azure/llms.d.cts.map +1 -1
- package/dist/azure/llms.d.ts +2 -2
- package/dist/azure/llms.d.ts.map +1 -1
- package/dist/azure/llms.js +3 -3
- package/dist/azure/llms.js.map +1 -1
- package/dist/chat_models/base.cjs +38 -45
- package/dist/chat_models/base.cjs.map +1 -1
- package/dist/chat_models/base.d.cts.map +1 -1
- package/dist/chat_models/base.d.ts.map +1 -1
- package/dist/chat_models/base.js +10 -16
- package/dist/chat_models/base.js.map +1 -1
- package/dist/chat_models/completions.cjs +9 -13
- package/dist/chat_models/completions.cjs.map +1 -1
- package/dist/chat_models/completions.d.cts.map +1 -1
- package/dist/chat_models/completions.d.ts.map +1 -1
- package/dist/chat_models/completions.js +3 -6
- package/dist/chat_models/completions.js.map +1 -1
- package/dist/chat_models/index.cjs.map +1 -1
- package/dist/chat_models/index.d.cts.map +1 -1
- package/dist/chat_models/index.d.ts.map +1 -1
- package/dist/chat_models/index.js.map +1 -1
- package/dist/chat_models/profiles.cjs +1 -2
- package/dist/chat_models/profiles.cjs.map +1 -1
- package/dist/chat_models/profiles.js +1 -2
- package/dist/chat_models/profiles.js.map +1 -1
- package/dist/chat_models/responses.cjs +5 -7
- package/dist/chat_models/responses.cjs.map +1 -1
- package/dist/chat_models/responses.d.cts.map +1 -1
- package/dist/chat_models/responses.d.ts.map +1 -1
- package/dist/chat_models/responses.js +3 -4
- package/dist/chat_models/responses.js.map +1 -1
- package/dist/converters/completions.cjs +36 -45
- package/dist/converters/completions.cjs.map +1 -1
- package/dist/converters/completions.d.cts +0 -1
- package/dist/converters/completions.d.cts.map +1 -1
- package/dist/converters/completions.d.ts +0 -1
- package/dist/converters/completions.d.ts.map +1 -1
- package/dist/converters/completions.js +14 -22
- package/dist/converters/completions.js.map +1 -1
- package/dist/converters/index.js +2 -0
- package/dist/converters/responses.cjs +53 -55
- package/dist/converters/responses.cjs.map +1 -1
- package/dist/converters/responses.d.cts.map +1 -1
- package/dist/converters/responses.d.ts.map +1 -1
- package/dist/converters/responses.js +45 -46
- package/dist/converters/responses.js.map +1 -1
- package/dist/embeddings.cjs +11 -15
- package/dist/embeddings.cjs.map +1 -1
- package/dist/embeddings.d.cts +1 -3
- package/dist/embeddings.d.cts.map +1 -1
- package/dist/embeddings.d.ts +1 -3
- package/dist/embeddings.d.ts.map +1 -1
- package/dist/embeddings.js +3 -6
- package/dist/embeddings.js.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/llms.cjs +27 -33
- package/dist/llms.cjs.map +1 -1
- package/dist/llms.d.cts +0 -1
- package/dist/llms.d.cts.map +1 -1
- package/dist/llms.d.ts +0 -1
- package/dist/llms.d.ts.map +1 -1
- package/dist/llms.js +15 -20
- package/dist/llms.js.map +1 -1
- package/dist/tools/applyPatch.cjs +3 -4
- package/dist/tools/applyPatch.cjs.map +1 -1
- package/dist/tools/applyPatch.d.cts +0 -1
- package/dist/tools/applyPatch.d.cts.map +1 -1
- package/dist/tools/applyPatch.d.ts +0 -1
- package/dist/tools/applyPatch.d.ts.map +1 -1
- package/dist/tools/applyPatch.js.map +1 -1
- package/dist/tools/codeInterpreter.cjs.map +1 -1
- package/dist/tools/codeInterpreter.d.cts +0 -1
- package/dist/tools/codeInterpreter.d.cts.map +1 -1
- package/dist/tools/codeInterpreter.d.ts +0 -1
- package/dist/tools/codeInterpreter.d.ts.map +1 -1
- package/dist/tools/codeInterpreter.js.map +1 -1
- package/dist/tools/computerUse.cjs +7 -13
- package/dist/tools/computerUse.cjs.map +1 -1
- package/dist/tools/computerUse.d.cts +0 -1
- package/dist/tools/computerUse.d.cts.map +1 -1
- package/dist/tools/computerUse.d.ts +0 -1
- package/dist/tools/computerUse.d.ts.map +1 -1
- package/dist/tools/computerUse.js +1 -6
- package/dist/tools/computerUse.js.map +1 -1
- package/dist/tools/custom.cjs +6 -7
- package/dist/tools/custom.cjs.map +1 -1
- package/dist/tools/custom.d.cts.map +1 -1
- package/dist/tools/custom.d.ts.map +1 -1
- package/dist/tools/custom.js.map +1 -1
- package/dist/tools/dalle.cjs +10 -16
- package/dist/tools/dalle.cjs.map +1 -1
- package/dist/tools/dalle.d.cts +0 -1
- package/dist/tools/dalle.d.cts.map +1 -1
- package/dist/tools/dalle.d.ts +0 -1
- package/dist/tools/dalle.d.ts.map +1 -1
- package/dist/tools/dalle.js +6 -11
- package/dist/tools/dalle.js.map +1 -1
- package/dist/tools/fileSearch.cjs.map +1 -1
- package/dist/tools/fileSearch.d.cts +0 -1
- package/dist/tools/fileSearch.d.cts.map +1 -1
- package/dist/tools/fileSearch.d.ts +0 -1
- package/dist/tools/fileSearch.d.ts.map +1 -1
- package/dist/tools/fileSearch.js.map +1 -1
- package/dist/tools/imageGeneration.cjs.map +1 -1
- package/dist/tools/imageGeneration.d.cts +0 -1
- package/dist/tools/imageGeneration.d.cts.map +1 -1
- package/dist/tools/imageGeneration.d.ts +0 -1
- package/dist/tools/imageGeneration.d.ts.map +1 -1
- package/dist/tools/imageGeneration.js.map +1 -1
- package/dist/tools/index.cjs.map +1 -1
- package/dist/tools/index.d.cts +0 -1
- package/dist/tools/index.d.cts.map +1 -1
- package/dist/tools/index.d.ts +0 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/localShell.cjs +3 -4
- package/dist/tools/localShell.cjs.map +1 -1
- package/dist/tools/localShell.d.cts +0 -1
- package/dist/tools/localShell.d.cts.map +1 -1
- package/dist/tools/localShell.d.ts +0 -1
- package/dist/tools/localShell.d.ts.map +1 -1
- package/dist/tools/localShell.js.map +1 -1
- package/dist/tools/mcp.cjs.map +1 -1
- package/dist/tools/mcp.d.cts +0 -1
- package/dist/tools/mcp.d.cts.map +1 -1
- package/dist/tools/mcp.d.ts +0 -1
- package/dist/tools/mcp.d.ts.map +1 -1
- package/dist/tools/mcp.js.map +1 -1
- package/dist/tools/shell.cjs +3 -4
- package/dist/tools/shell.cjs.map +1 -1
- package/dist/tools/shell.d.cts +0 -1
- package/dist/tools/shell.d.cts.map +1 -1
- package/dist/tools/shell.d.ts +0 -1
- package/dist/tools/shell.d.ts.map +1 -1
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/webSearch.cjs.map +1 -1
- package/dist/tools/webSearch.d.cts +0 -1
- package/dist/tools/webSearch.d.cts.map +1 -1
- package/dist/tools/webSearch.d.ts +0 -1
- package/dist/tools/webSearch.d.ts.map +1 -1
- package/dist/tools/webSearch.js.map +1 -1
- package/dist/types.d.cts +0 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/azure.cjs +2 -3
- package/dist/utils/azure.cjs.map +1 -1
- package/dist/utils/azure.d.cts.map +1 -1
- package/dist/utils/azure.d.ts.map +1 -1
- package/dist/utils/azure.js.map +1 -1
- package/dist/utils/client.cjs +9 -3
- package/dist/utils/client.cjs.map +1 -1
- package/dist/utils/client.d.cts.map +1 -1
- package/dist/utils/client.d.ts.map +1 -1
- package/dist/utils/client.js +8 -1
- package/dist/utils/client.js.map +1 -1
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/misc.cjs +2 -3
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts +0 -1
- package/dist/utils/misc.d.cts.map +1 -1
- package/dist/utils/misc.d.ts +0 -1
- package/dist/utils/misc.d.ts.map +1 -1
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/output.cjs +7 -8
- package/dist/utils/output.cjs.map +1 -1
- package/dist/utils/output.js.map +1 -1
- package/dist/utils/prompts.cjs +1 -2
- package/dist/utils/prompts.cjs.map +1 -1
- package/dist/utils/prompts.d.cts +0 -1
- package/dist/utils/prompts.d.cts.map +1 -1
- package/dist/utils/prompts.d.ts +0 -1
- package/dist/utils/prompts.d.ts.map +1 -1
- package/dist/utils/prompts.js +1 -2
- package/dist/utils/prompts.js.map +1 -1
- package/dist/utils/tools.cjs +9 -12
- package/dist/utils/tools.cjs.map +1 -1
- package/dist/utils/tools.d.cts +0 -1
- package/dist/utils/tools.d.cts.map +1 -1
- package/dist/utils/tools.d.ts +0 -1
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js +5 -7
- package/dist/utils/tools.js.map +1 -1
- package/package.json +5 -5
- package/dist/_virtual/rolldown_runtime.cjs +0 -25
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
1
|
const require_misc = require('../utils/misc.cjs');
|
|
3
2
|
const require_output = require('../utils/output.cjs');
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
let _langchain_core_messages = require("@langchain/core/messages");
|
|
4
|
+
let _langchain_core_output_parsers_openai_tools = require("@langchain/core/output_parsers/openai_tools");
|
|
6
5
|
|
|
7
6
|
//#region src/converters/completions.ts
|
|
8
7
|
/**
|
|
@@ -24,26 +23,23 @@ const completionsApiContentBlockConverter = {
|
|
|
24
23
|
...block.metadata?.detail ? { detail: block.metadata.detail } : {}
|
|
25
24
|
}
|
|
26
25
|
};
|
|
27
|
-
if (block.source_type === "base64") {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
26
|
+
if (block.source_type === "base64") return {
|
|
27
|
+
type: "image_url",
|
|
28
|
+
image_url: {
|
|
29
|
+
url: `data:${block.mime_type ?? ""};base64,${block.data}`,
|
|
30
|
+
...block.metadata?.detail ? { detail: block.metadata.detail } : {}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
37
33
|
throw new Error(`Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
|
|
38
34
|
},
|
|
39
35
|
fromStandardAudioBlock(block) {
|
|
40
36
|
if (block.source_type === "url") {
|
|
41
|
-
const data = (0,
|
|
37
|
+
const data = (0, _langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
|
|
42
38
|
if (!data) throw new Error(`URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
|
|
43
39
|
const rawMimeType = data.mime_type || block.mime_type || "";
|
|
44
40
|
let mimeType;
|
|
45
41
|
try {
|
|
46
|
-
mimeType = (0,
|
|
42
|
+
mimeType = (0, _langchain_core_messages.parseMimeType)(rawMimeType);
|
|
47
43
|
} catch {
|
|
48
44
|
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
49
45
|
}
|
|
@@ -59,7 +55,7 @@ const completionsApiContentBlockConverter = {
|
|
|
59
55
|
if (block.source_type === "base64") {
|
|
60
56
|
let mimeType;
|
|
61
57
|
try {
|
|
62
|
-
mimeType = (0,
|
|
58
|
+
mimeType = (0, _langchain_core_messages.parseMimeType)(block.mime_type ?? "");
|
|
63
59
|
} catch {
|
|
64
60
|
throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
|
|
65
61
|
}
|
|
@@ -76,7 +72,7 @@ const completionsApiContentBlockConverter = {
|
|
|
76
72
|
},
|
|
77
73
|
fromStandardFileBlock(block) {
|
|
78
74
|
if (block.source_type === "url") {
|
|
79
|
-
const data = (0,
|
|
75
|
+
const data = (0, _langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
|
|
80
76
|
const filename = require_misc.getRequiredFilenameFromMetadata(block);
|
|
81
77
|
if (!data) throw new Error(`URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
|
|
82
78
|
return {
|
|
@@ -167,9 +163,9 @@ const convertCompletionsMessageToBaseMessage = ({ message, rawResponse, includeR
|
|
|
167
163
|
const toolCalls = [];
|
|
168
164
|
const invalidToolCalls = [];
|
|
169
165
|
for (const rawToolCall of rawToolCalls ?? []) try {
|
|
170
|
-
toolCalls.push((0,
|
|
166
|
+
toolCalls.push((0, _langchain_core_output_parsers_openai_tools.parseToolCall)(rawToolCall, { returnId: true }));
|
|
171
167
|
} catch (e) {
|
|
172
|
-
invalidToolCalls.push((0,
|
|
168
|
+
invalidToolCalls.push((0, _langchain_core_output_parsers_openai_tools.makeInvalidToolCall)(rawToolCall, e.message));
|
|
173
169
|
}
|
|
174
170
|
const additional_kwargs = {
|
|
175
171
|
function_call: message.function_call,
|
|
@@ -185,9 +181,8 @@ const convertCompletionsMessageToBaseMessage = ({ message, rawResponse, includeR
|
|
|
185
181
|
} : {}
|
|
186
182
|
};
|
|
187
183
|
if (message.audio) additional_kwargs.audio = message.audio;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
content,
|
|
184
|
+
return new _langchain_core_messages.AIMessage({
|
|
185
|
+
content: require_output.handleMultiModalOutput(message.content || "", rawResponse.choices?.[0]?.message),
|
|
191
186
|
tool_calls: toolCalls,
|
|
192
187
|
invalid_tool_calls: invalidToolCalls,
|
|
193
188
|
additional_kwargs,
|
|
@@ -195,7 +190,7 @@ const convertCompletionsMessageToBaseMessage = ({ message, rawResponse, includeR
|
|
|
195
190
|
id: rawResponse.id
|
|
196
191
|
});
|
|
197
192
|
}
|
|
198
|
-
default: return new
|
|
193
|
+
default: return new _langchain_core_messages.ChatMessage(message.content || "", message.role ?? "unknown");
|
|
199
194
|
}
|
|
200
195
|
};
|
|
201
196
|
/**
|
|
@@ -273,7 +268,7 @@ const convertCompletionsDeltaToBaseMessageChunk = ({ delta, rawResponse, include
|
|
|
273
268
|
model_provider: "openai",
|
|
274
269
|
usage: { ...rawResponse.usage }
|
|
275
270
|
};
|
|
276
|
-
if (role === "user") return new
|
|
271
|
+
if (role === "user") return new _langchain_core_messages.HumanMessageChunk({
|
|
277
272
|
content,
|
|
278
273
|
response_metadata
|
|
279
274
|
});
|
|
@@ -286,35 +281,35 @@ const convertCompletionsDeltaToBaseMessageChunk = ({ delta, rawResponse, include
|
|
|
286
281
|
index: rawToolCall.index,
|
|
287
282
|
type: "tool_call_chunk"
|
|
288
283
|
});
|
|
289
|
-
return new
|
|
284
|
+
return new _langchain_core_messages.AIMessageChunk({
|
|
290
285
|
content,
|
|
291
286
|
tool_call_chunks: toolCallChunks,
|
|
292
287
|
additional_kwargs,
|
|
293
288
|
id: rawResponse.id,
|
|
294
289
|
response_metadata
|
|
295
290
|
});
|
|
296
|
-
} else if (role === "system") return new
|
|
291
|
+
} else if (role === "system") return new _langchain_core_messages.SystemMessageChunk({
|
|
297
292
|
content,
|
|
298
293
|
response_metadata
|
|
299
294
|
});
|
|
300
|
-
else if (role === "developer") return new
|
|
295
|
+
else if (role === "developer") return new _langchain_core_messages.SystemMessageChunk({
|
|
301
296
|
content,
|
|
302
297
|
response_metadata,
|
|
303
298
|
additional_kwargs: { __openai_role__: "developer" }
|
|
304
299
|
});
|
|
305
|
-
else if (role === "function") return new
|
|
300
|
+
else if (role === "function") return new _langchain_core_messages.FunctionMessageChunk({
|
|
306
301
|
content,
|
|
307
302
|
additional_kwargs,
|
|
308
303
|
name: delta.name,
|
|
309
304
|
response_metadata
|
|
310
305
|
});
|
|
311
|
-
else if (role === "tool") return new
|
|
306
|
+
else if (role === "tool") return new _langchain_core_messages.ToolMessageChunk({
|
|
312
307
|
content,
|
|
313
308
|
additional_kwargs,
|
|
314
309
|
tool_call_id: delta.tool_call_id,
|
|
315
310
|
response_metadata
|
|
316
311
|
});
|
|
317
|
-
else return new
|
|
312
|
+
else return new _langchain_core_messages.ChatMessageChunk({
|
|
318
313
|
content,
|
|
319
314
|
role,
|
|
320
315
|
response_metadata
|
|
@@ -361,9 +356,9 @@ const convertStandardContentBlockToCompletionsContentPart = (block) => {
|
|
|
361
356
|
}
|
|
362
357
|
if (block.type === "audio") {
|
|
363
358
|
if (block.data) {
|
|
364
|
-
const format = (0,
|
|
365
|
-
const [, format
|
|
366
|
-
if (format
|
|
359
|
+
const format = (0, _langchain_core_messages.iife)(() => {
|
|
360
|
+
const [, format] = block.mimeType.split("/");
|
|
361
|
+
if (format === "wav" || format === "mp3") return format;
|
|
367
362
|
return "wav";
|
|
368
363
|
});
|
|
369
364
|
return {
|
|
@@ -391,7 +386,6 @@ const convertStandardContentBlockToCompletionsContentPart = (block) => {
|
|
|
391
386
|
file: { file_id: block.fileId }
|
|
392
387
|
};
|
|
393
388
|
}
|
|
394
|
-
return void 0;
|
|
395
389
|
};
|
|
396
390
|
/**
|
|
397
391
|
* Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.
|
|
@@ -473,7 +467,7 @@ const convertStandardContentMessageToCompletionsMessage = ({ message, model }) =
|
|
|
473
467
|
role: "assistant",
|
|
474
468
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
475
469
|
};
|
|
476
|
-
else if (role === "tool" &&
|
|
470
|
+
else if (role === "tool" && _langchain_core_messages.ToolMessage.isInstance(message)) return {
|
|
477
471
|
role: "tool",
|
|
478
472
|
tool_call_id: message.tool_call_id,
|
|
479
473
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
@@ -575,7 +569,7 @@ const convertMessagesToCompletionsMessageParams = ({ messages, model }) => {
|
|
|
575
569
|
let role = require_misc.messageToOpenAIRole(message);
|
|
576
570
|
if (role === "system" && require_misc.isReasoningModel(model)) role = "developer";
|
|
577
571
|
const content = typeof message.content === "string" ? message.content : message.content.flatMap((m) => {
|
|
578
|
-
if ((0,
|
|
572
|
+
if ((0, _langchain_core_messages.isDataContentBlock)(m)) return (0, _langchain_core_messages.convertToProviderContentBlock)(m, completionsApiContentBlockConverter);
|
|
579
573
|
if (typeof m === "object" && m !== null && "type" in m && m.type === "tool_use") return [];
|
|
580
574
|
return m;
|
|
581
575
|
});
|
|
@@ -585,18 +579,15 @@ const convertMessagesToCompletionsMessageParams = ({ messages, model }) => {
|
|
|
585
579
|
};
|
|
586
580
|
if (message.name != null) completionParam.name = message.name;
|
|
587
581
|
if (message.additional_kwargs.function_call != null) completionParam.function_call = message.additional_kwargs.function_call;
|
|
588
|
-
if (
|
|
582
|
+
if (_langchain_core_messages.AIMessage.isInstance(message) && !!message.tool_calls?.length) completionParam.tool_calls = message.tool_calls.map(_langchain_core_output_parsers_openai_tools.convertLangChainToolCallToOpenAI);
|
|
589
583
|
else {
|
|
590
584
|
if (message.additional_kwargs.tool_calls != null) completionParam.tool_calls = message.additional_kwargs.tool_calls;
|
|
591
|
-
if (
|
|
592
|
-
}
|
|
593
|
-
if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) {
|
|
594
|
-
const audioMessage = {
|
|
595
|
-
role: "assistant",
|
|
596
|
-
audio: { id: message.additional_kwargs.audio.id }
|
|
597
|
-
};
|
|
598
|
-
return [completionParam, audioMessage];
|
|
585
|
+
if (_langchain_core_messages.ToolMessage.isInstance(message) && message.tool_call_id != null) completionParam.tool_call_id = message.tool_call_id;
|
|
599
586
|
}
|
|
587
|
+
if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) return [completionParam, {
|
|
588
|
+
role: "assistant",
|
|
589
|
+
audio: { id: message.additional_kwargs.audio.id }
|
|
590
|
+
}];
|
|
600
591
|
return completionParam;
|
|
601
592
|
});
|
|
602
593
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"completions.cjs","names":["completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>","mimeType: { type: string; subtype: string }","getRequiredFilenameFromMetadata","convertCompletionsMessageToBaseMessage: Converter<\n {\n message: OpenAIClient.Chat.Completions.ChatCompletionMessage;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;\n includeRawResponse?: boolean;\n },\n BaseMessage\n>","rawToolCalls: OpenAIToolCall[] | undefined","e: any","additional_kwargs: Record<string, unknown>","response_metadata: Record<string, unknown> | undefined","handleMultiModalOutput","AIMessage","ChatMessage","convertCompletionsDeltaToBaseMessageChunk: Converter<\n {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delta: Record<string, any>;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk;\n includeRawResponse?: boolean;\n defaultRole?: OpenAIClient.Chat.ChatCompletionRole;\n },\n BaseMessageChunk\n>","HumanMessageChunk","toolCallChunks: ToolCallChunk[]","AIMessageChunk","SystemMessageChunk","FunctionMessageChunk","ToolMessageChunk","ChatMessageChunk","convertStandardContentBlockToCompletionsContentPart: Converter<\n ContentBlock.Standard,\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartImage\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio\n | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File\n | undefined\n>","format","convertStandardContentMessageToCompletionsMessage: Converter<\n { message: BaseMessage; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam\n>","messageToOpenAIRole","isReasoningModel","ToolMessage","blocks: ContentBlock.Standard[]","convertMessagesToCompletionsMessageParams: Converter<\n { messages: BaseMessage[]; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam[]\n>","completionParam: Record<string, any>","convertLangChainToolCallToOpenAI"],"sources":["../../src/converters/completions.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageChunk,\n ChatMessage,\n ChatMessageChunk,\n FunctionMessageChunk,\n HumanMessageChunk,\n OpenAIToolCall,\n SystemMessageChunk,\n ToolCallChunk,\n ToolMessage,\n ToolMessageChunk,\n parseBase64DataUrl,\n parseMimeType,\n StandardContentBlockConverter,\n isDataContentBlock,\n ContentBlock,\n iife,\n convertToProviderContentBlock,\n} from \"@langchain/core/messages\";\nimport {\n convertLangChainToolCallToOpenAI,\n makeInvalidToolCall,\n parseToolCall,\n} from \"@langchain/core/output_parsers/openai_tools\";\nimport { Converter } from \"@langchain/core/utils/format\";\nimport type {\n ChatCompletionContentPartText,\n ChatCompletionContentPartImage,\n ChatCompletionContentPartInputAudio,\n ChatCompletionContentPart,\n} from \"openai/resources/chat/completions\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { handleMultiModalOutput } from \"../utils/output.js\";\nimport {\n getRequiredFilenameFromMetadata,\n isReasoningModel,\n messageToOpenAIRole,\n} from \"../utils/misc.js\";\n\n/**\n * @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.\n */\nexport const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}> = {\n providerName: \"ChatOpenAI\",\n\n fromStandardTextBlock(block): ChatCompletionContentPartText {\n return { type: \"text\", text: block.text };\n },\n\n fromStandardImageBlock(block): ChatCompletionContentPartImage {\n if (block.source_type === \"url\") {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const url = `data:${block.mime_type ?? \"\"};base64,${block.data}`;\n return {\n type: \"image_url\",\n image_url: {\n url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n throw new Error(\n `Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardAudioBlock(block): ChatCompletionContentPartInputAudio {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n const rawMimeType = data.mime_type || block.mime_type || \"\";\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(rawMimeType);\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: data.data,\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(block.mime_type ?? \"\");\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: block.data,\n },\n };\n }\n\n throw new Error(\n `Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardFileBlock(block): ChatCompletionContentPart.File {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n\n const filename = getRequiredFilenameFromMetadata(block);\n\n if (!data) {\n throw new Error(\n `URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n return {\n type: \"file\",\n file: {\n file_data: block.url, // formatted as base64 data URL\n ...(block.metadata?.filename || block.metadata?.name\n ? {\n filename,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const filename = getRequiredFilenameFromMetadata(block);\n\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mime_type ?? \"\"};base64,${block.data}`,\n ...(block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title\n ? {\n filename,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"id\") {\n return {\n type: \"file\",\n file: {\n file_id: block.id,\n },\n };\n }\n\n throw new Error(\n `File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n};\n\n/**\n * Converts an OpenAI Chat Completions API message to a LangChain BaseMessage.\n *\n * This converter transforms messages from OpenAI's Chat Completions API format into\n * LangChain's internal message representation, handling various message types and\n * preserving metadata, tool calls, and other relevant information.\n *\n * @remarks\n * The converter handles the following message roles:\n * - `assistant`: Converted to {@link AIMessage} with support for tool calls, function calls,\n * audio content, and multi-modal outputs\n * - Other roles: Converted to generic {@link ChatMessage}\n *\n * For assistant messages, the converter:\n * - Parses and validates tool calls, separating valid and invalid calls\n * - Preserves function call information in additional_kwargs\n * - Includes usage statistics and system fingerprint in response_metadata\n * - Handles multi-modal content (text, images, audio)\n * - Optionally includes the raw API response for debugging\n *\n * @param params - Conversion parameters\n * @param params.message - The OpenAI chat completion message to convert\n * @param params.rawResponse - The complete raw response from OpenAI's API, used to extract\n * metadata like model name, usage statistics, and system fingerprint\n * @param params.includeRawResponse - If true, includes the raw OpenAI response in the\n * message's additional_kwargs under the `__raw_response` key. Useful for debugging\n * or accessing provider-specific fields. Defaults to false.\n *\n * @returns A LangChain BaseMessage instance:\n * - {@link AIMessage} for assistant messages with tool calls, metadata, and content\n * - {@link ChatMessage} for all other message types\n *\n * @example\n * ```typescript\n * const baseMessage = convertCompletionsMessageToBaseMessage({\n * message: {\n * role: \"assistant\",\n * content: \"Hello! How can I help you?\",\n * tool_calls: [\n * {\n * id: \"call_123\",\n * type: \"function\",\n * function: { name: \"get_weather\", arguments: '{\"location\":\"NYC\"}' }\n * }\n * ]\n * },\n * rawResponse: completionResponse,\n * includeRawResponse: true\n * });\n * // Returns an AIMessage with parsed tool calls and metadata\n * ```\n *\n * @throws {Error} If tool call parsing fails, the invalid tool call is captured in\n * the `invalid_tool_calls` array rather than throwing an error\n *\n */\nexport const convertCompletionsMessageToBaseMessage: Converter<\n {\n message: OpenAIClient.Chat.Completions.ChatCompletionMessage;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;\n includeRawResponse?: boolean;\n },\n BaseMessage\n> = ({ message, rawResponse, includeRawResponse }) => {\n const rawToolCalls: OpenAIToolCall[] | undefined = message.tool_calls as\n | OpenAIToolCall[]\n | undefined;\n switch (message.role) {\n case \"assistant\": {\n const toolCalls = [];\n const invalidToolCalls = [];\n for (const rawToolCall of rawToolCalls ?? []) {\n try {\n toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));\n }\n }\n const additional_kwargs: Record<string, unknown> = {\n function_call: message.function_call,\n tool_calls: rawToolCalls,\n };\n if (includeRawResponse !== undefined) {\n additional_kwargs.__raw_response = rawResponse;\n }\n const response_metadata: Record<string, unknown> | undefined = {\n model_provider: \"openai\",\n model_name: rawResponse.model,\n ...(rawResponse.system_fingerprint\n ? {\n usage: { ...rawResponse.usage },\n system_fingerprint: rawResponse.system_fingerprint,\n }\n : {}),\n };\n\n if (message.audio) {\n additional_kwargs.audio = message.audio;\n }\n\n const content = handleMultiModalOutput(\n message.content || \"\",\n rawResponse.choices?.[0]?.message\n );\n return new AIMessage({\n content,\n tool_calls: toolCalls,\n invalid_tool_calls: invalidToolCalls,\n additional_kwargs,\n response_metadata,\n id: rawResponse.id,\n });\n }\n default:\n return new ChatMessage(message.content || \"\", message.role ?? \"unknown\");\n }\n};\n\n/**\n * Converts an OpenAI Chat Completions API delta (streaming chunk) to a LangChain BaseMessageChunk.\n *\n * This converter is used during streaming responses to transform incremental updates from OpenAI's\n * Chat Completions API into LangChain message chunks. It handles various message types, tool calls,\n * function calls, audio content, and role-specific message chunk creation.\n *\n * @param params - Conversion parameters\n * @param params.delta - The delta object from an OpenAI streaming chunk containing incremental\n * message updates. May include content, role, tool_calls, function_call, audio, etc.\n * @param params.rawResponse - The complete raw ChatCompletionChunk response from OpenAI,\n * containing metadata like model info, usage stats, and the delta\n * @param params.includeRawResponse - Optional flag to include the raw OpenAI response in the\n * message chunk's additional_kwargs. Useful for debugging or accessing provider-specific data\n * @param params.defaultRole - Optional default role to use if the delta doesn't specify one.\n * Typically used to maintain role consistency across chunks in a streaming response\n *\n * @returns A BaseMessageChunk subclass appropriate for the message role:\n * - HumanMessageChunk for \"user\" role\n * - AIMessageChunk for \"assistant\" role (includes tool call chunks)\n * - SystemMessageChunk for \"system\" or \"developer\" roles\n * - FunctionMessageChunk for \"function\" role\n * - ToolMessageChunk for \"tool\" role\n * - ChatMessageChunk for any other role\n *\n * @example\n * Basic streaming text chunk:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: { role: \"assistant\", content: \"Hello\" },\n * rawResponse: { id: \"chatcmpl-123\", model: \"gpt-4\", ... }\n * });\n * // Returns: AIMessageChunk with content \"Hello\"\n * ```\n *\n * @example\n * Streaming chunk with tool call:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: {\n * role: \"assistant\",\n * tool_calls: [{\n * index: 0,\n * id: \"call_123\",\n * function: { name: \"get_weather\", arguments: '{\"location\":' }\n * }]\n * },\n * rawResponse: { id: \"chatcmpl-123\", ... }\n * });\n * // Returns: AIMessageChunk with tool_call_chunks containing partial tool call data\n * ```\n *\n * @remarks\n * - Tool calls are converted to ToolCallChunk objects with incremental data\n * - Audio content includes the chunk index from the raw response\n * - The \"developer\" role is mapped to SystemMessageChunk with a special marker\n * - Response metadata includes model provider info and usage statistics\n * - Function calls and tool calls are stored in additional_kwargs for compatibility\n */\nexport const convertCompletionsDeltaToBaseMessageChunk: Converter<\n {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delta: Record<string, any>;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk;\n includeRawResponse?: boolean;\n defaultRole?: OpenAIClient.Chat.ChatCompletionRole;\n },\n BaseMessageChunk\n> = ({ delta, rawResponse, includeRawResponse, defaultRole }) => {\n const role = delta.role ?? defaultRole;\n const content = delta.content ?? \"\";\n let additional_kwargs: Record<string, unknown>;\n if (delta.function_call) {\n additional_kwargs = {\n function_call: delta.function_call,\n };\n } else if (delta.tool_calls) {\n additional_kwargs = {\n tool_calls: delta.tool_calls,\n };\n } else {\n additional_kwargs = {};\n }\n if (includeRawResponse) {\n additional_kwargs.__raw_response = rawResponse;\n }\n\n if (delta.audio) {\n additional_kwargs.audio = {\n ...delta.audio,\n index: rawResponse.choices[0].index,\n };\n }\n\n const response_metadata = {\n model_provider: \"openai\",\n usage: { ...rawResponse.usage },\n };\n if (role === \"user\") {\n return new HumanMessageChunk({ content, response_metadata });\n } else if (role === \"assistant\") {\n const toolCallChunks: ToolCallChunk[] = [];\n if (Array.isArray(delta.tool_calls)) {\n for (const rawToolCall of delta.tool_calls) {\n toolCallChunks.push({\n name: rawToolCall.function?.name,\n args: rawToolCall.function?.arguments,\n id: rawToolCall.id,\n index: rawToolCall.index,\n type: \"tool_call_chunk\",\n });\n }\n }\n return new AIMessageChunk({\n content,\n tool_call_chunks: toolCallChunks,\n additional_kwargs,\n id: rawResponse.id,\n response_metadata,\n });\n } else if (role === \"system\") {\n return new SystemMessageChunk({ content, response_metadata });\n } else if (role === \"developer\") {\n return new SystemMessageChunk({\n content,\n response_metadata,\n additional_kwargs: {\n __openai_role__: \"developer\",\n },\n });\n } else if (role === \"function\") {\n return new FunctionMessageChunk({\n content,\n additional_kwargs,\n name: delta.name,\n response_metadata,\n });\n } else if (role === \"tool\") {\n return new ToolMessageChunk({\n content,\n additional_kwargs,\n tool_call_id: delta.tool_call_id,\n response_metadata,\n });\n } else {\n return new ChatMessageChunk({ content, role, response_metadata });\n }\n};\n\n/**\n * Converts a standard LangChain content block to an OpenAI Completions API content part.\n *\n * This converter transforms LangChain's standardized content blocks (image, audio, file)\n * into the format expected by OpenAI's Chat Completions API. It handles various content\n * types including images (URL or base64), audio (base64), and files (data or file ID).\n *\n * @param block - The standard content block to convert. Can be an image, audio, or file block.\n *\n * @returns An OpenAI Chat Completions content part object, or undefined if the block\n * cannot be converted (e.g., missing required data).\n *\n * @example\n * Image with URL:\n * ```typescript\n * const block = { type: \"image\", url: \"https://example.com/image.jpg\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * ```\n *\n * @example\n * Image with base64 data:\n * ```typescript\n * const block = { type: \"image\", data: \"iVBORw0KGgo...\", mimeType: \"image/png\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"data:image/png;base64,iVBORw0KGgo...\" } }\n * ```\n */\nexport const convertStandardContentBlockToCompletionsContentPart: Converter<\n ContentBlock.Standard,\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartImage\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio\n | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File\n | undefined\n> = (block) => {\n if (block.type === \"image\") {\n if (block.url) {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n },\n };\n } else if (block.data) {\n return {\n type: \"image_url\",\n image_url: {\n url: `data:${block.mimeType};base64,${block.data}`,\n },\n };\n }\n }\n if (block.type === \"audio\") {\n if (block.data) {\n const format = iife(() => {\n const [, format] = block.mimeType.split(\"/\");\n if (format === \"wav\" || format === \"mp3\") {\n return format;\n }\n return \"wav\";\n });\n return {\n type: \"input_audio\",\n input_audio: {\n data: block.data.toString(),\n format,\n },\n };\n }\n }\n if (block.type === \"file\") {\n if (block.data) {\n const filename = getRequiredFilenameFromMetadata(block);\n\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mimeType};base64,${block.data}`,\n filename: filename,\n },\n };\n }\n if (block.fileId) {\n return {\n type: \"file\",\n file: {\n file_id: block.fileId,\n },\n };\n }\n }\n return undefined;\n};\n\n/**\n * Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.\n *\n * This converter transforms LangChain's standardized message format (using contentBlocks) into the format\n * expected by OpenAI's Chat Completions API. It handles role mapping, content filtering, and multi-modal\n * content conversion for various message types.\n *\n * @remarks\n * The converter performs the following transformations:\n * - Maps LangChain message roles to OpenAI API roles (user, assistant, system, developer, tool, function)\n * - For reasoning models, automatically converts \"system\" role to \"developer\" role\n * - Filters content blocks based on message role (most roles only include text blocks)\n * - For user messages, converts multi-modal content blocks (images, audio, files) to OpenAI format\n * - Preserves tool call IDs for tool messages and function names for function messages\n *\n * Role-specific behavior:\n * - **developer**: Returns only text content blocks (used for reasoning models)\n * - **system**: Returns only text content blocks\n * - **assistant**: Returns only text content blocks\n * - **tool**: Returns only text content blocks with tool_call_id preserved\n * - **function**: Returns text content blocks joined as a single string with function name\n * - **user** (default): Returns multi-modal content including text, images, audio, and files\n *\n * @param params - Conversion parameters\n * @param params.message - The LangChain BaseMessage to convert. Must have contentBlocks property\n * containing an array of standard content blocks (text, image, audio, file, etc.)\n * @param params.model - Optional model name. Used to determine if special role mapping is needed\n * (e.g., \"system\" -> \"developer\" for reasoning models like o1)\n *\n * @returns An OpenAI ChatCompletionMessageParam object formatted for the Chat Completions API.\n * The structure varies by role:\n * - Developer/System/Assistant: `{ role, content: TextBlock[] }`\n * - Tool: `{ role: \"tool\", tool_call_id, content: TextBlock[] }`\n * - Function: `{ role: \"function\", name, content: string }`\n * - User: `{ role: \"user\", content: Array<TextPart | ImagePart | AudioPart | FilePart> }`\n *\n * @example\n * Simple text message:\n * ```typescript\n * const message = new HumanMessage({\n * content: [{ type: \"text\", text: \"Hello!\" }]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: { role: \"user\", content: [{ type: \"text\", text: \"Hello!\" }] }\n * ```\n *\n * @example\n * Multi-modal user message with image:\n * ```typescript\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"What's in this image?\" },\n * { type: \"image\", url: \"https://example.com/image.jpg\" }\n * ]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: {\n * // role: \"user\",\n * // content: [\n * // { type: \"text\", text: \"What's in this image?\" },\n * // { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * // ]\n * // }\n * ```\n */\nexport const convertStandardContentMessageToCompletionsMessage: Converter<\n { message: BaseMessage; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam\n> = ({ message, model }) => {\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n if (role === \"developer\") {\n return {\n role: \"developer\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"system\") {\n return {\n role: \"system\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"assistant\") {\n return {\n role: \"assistant\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"tool\" && ToolMessage.isInstance(message)) {\n return {\n role: \"tool\",\n tool_call_id: message.tool_call_id,\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"function\") {\n return {\n role: \"function\",\n name: message.name ?? \"\",\n content: message.contentBlocks\n .filter((block) => block.type === \"text\")\n .join(\"\"),\n };\n }\n // Default to user message handling\n function* iterateUserContent(blocks: ContentBlock.Standard[]) {\n for (const block of blocks) {\n if (block.type === \"text\") {\n yield {\n type: \"text\" as const,\n text: block.text,\n };\n }\n const data = convertStandardContentBlockToCompletionsContentPart(block);\n if (data) {\n yield data;\n }\n }\n }\n return {\n role: \"user\",\n content: Array.from(iterateUserContent(message.contentBlocks)),\n };\n};\n\n/**\n * Converts an array of LangChain BaseMessages to OpenAI Chat Completions API message parameters.\n *\n * This converter transforms LangChain's internal message representation into the format required\n * by OpenAI's Chat Completions API. It handles various message types, roles, content formats,\n * tool calls, function calls, audio messages, and special model-specific requirements.\n *\n * @remarks\n * The converter performs several key transformations:\n * - Maps LangChain message types to OpenAI roles (user, assistant, system, tool, function, developer)\n * - Converts standard content blocks (v1 format) using a specialized converter\n * - Handles multimodal content including text, images, audio, and data blocks\n * - Preserves tool calls and function calls with proper formatting\n * - Applies model-specific role mappings (e.g., \"system\" → \"developer\" for reasoning models)\n * - Splits audio messages into separate message parameters when needed\n *\n * @param params - Conversion parameters\n * @param params.messages - Array of LangChain BaseMessages to convert. Can include any message\n * type: HumanMessage, AIMessage, SystemMessage, ToolMessage, FunctionMessage, etc.\n * @param params.model - Optional model name used to determine if special role mapping is needed.\n * For reasoning models (o1, o3, etc.), \"system\" role is converted to \"developer\" role.\n *\n * @returns Array of ChatCompletionMessageParam objects formatted for OpenAI's Chat Completions API.\n * Some messages may be split into multiple parameters (e.g., audio messages).\n *\n * @example\n * Basic message conversion:\n * ```typescript\n * const messages = [\n * new HumanMessage(\"What's the weather like?\"),\n * new AIMessage(\"Let me check that for you.\")\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({\n * messages,\n * model: \"gpt-4\"\n * });\n * // Returns:\n * // [\n * // { role: \"user\", content: \"What's the weather like?\" },\n * // { role: \"assistant\", content: \"Let me check that for you.\" }\n * // ]\n * ```\n *\n * @example\n * Message with tool calls:\n * ```typescript\n * const messages = [\n * new AIMessage({\n * content: \"\",\n * tool_calls: [{\n * id: \"call_123\",\n * name: \"get_weather\",\n * args: { location: \"San Francisco\" }\n * }]\n * })\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({ messages });\n * // Returns:\n * // [{\n * // role: \"assistant\",\n * // content: \"\",\n * // tool_calls: [{\n * // id: \"call_123\",\n * // type: \"function\",\n * // function: { name: \"get_weather\", arguments: '{\"location\":\"San Francisco\"}' }\n * // }]\n * // }]\n * ```\n */\nexport const convertMessagesToCompletionsMessageParams: Converter<\n { messages: BaseMessage[]; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam[]\n> = ({ messages, model }) => {\n return messages.flatMap((message) => {\n if (\n \"output_version\" in message.response_metadata &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return convertStandardContentMessageToCompletionsMessage({ message });\n }\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n\n const content =\n typeof message.content === \"string\"\n ? message.content\n : message.content.flatMap((m) => {\n if (isDataContentBlock(m)) {\n return convertToProviderContentBlock(\n m,\n completionsApiContentBlockConverter\n );\n }\n // Drop Anthropic tool_use blocks from content — these are\n // already represented in message.tool_calls and would cause\n // an API error if passed through to OpenAI.\n if (\n typeof m === \"object\" &&\n m !== null &&\n \"type\" in m &&\n m.type === \"tool_use\"\n ) {\n return [];\n }\n return m;\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const completionParam: Record<string, any> = {\n role,\n content,\n };\n if (message.name != null) {\n completionParam.name = message.name;\n }\n if (message.additional_kwargs.function_call != null) {\n completionParam.function_call = message.additional_kwargs.function_call;\n }\n if (AIMessage.isInstance(message) && !!message.tool_calls?.length) {\n completionParam.tool_calls = message.tool_calls.map(\n convertLangChainToolCallToOpenAI\n );\n } else {\n if (message.additional_kwargs.tool_calls != null) {\n completionParam.tool_calls = message.additional_kwargs.tool_calls;\n }\n if (ToolMessage.isInstance(message) && message.tool_call_id != null) {\n completionParam.tool_call_id = message.tool_call_id;\n }\n }\n\n if (\n message.additional_kwargs.audio &&\n typeof message.additional_kwargs.audio === \"object\" &&\n \"id\" in message.additional_kwargs.audio\n ) {\n const audioMessage = {\n role: \"assistant\",\n audio: {\n id: message.additional_kwargs.audio.id,\n },\n };\n return [\n completionParam,\n audioMessage,\n ] as OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n }\n\n return completionParam as OpenAIClient.Chat.Completions.ChatCompletionMessageParam;\n });\n};\n"],"mappings":";;;;;;;;;;AA6CA,MAAaA,sCAKR;CACH,cAAc;CAEd,sBAAsB,OAAsC;AAC1D,SAAO;GAAE,MAAM;GAAQ,MAAM,MAAM;EAAM;CAC1C;CAED,uBAAuB,OAAuC;AAC5D,MAAI,MAAM,gBAAgB,MACxB,QAAO;GACL,MAAM;GACN,WAAW;IACT,KAAK,MAAM;IACX,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,OAAmC,IAC5D,CAAE;GACP;EACF;AAGH,MAAI,MAAM,gBAAgB,UAAU;GAClC,MAAM,MAAM,CAAC,KAAK,EAAE,MAAM,aAAa,GAAG,QAAQ,EAAE,MAAM,MAAM;AAChE,UAAO;IACL,MAAM;IACN,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AAQ,kBAAkB,UAAU,YAC3C,QAAQ,QAAQ,kBAAkB,OAClC;GACA,MAAM,eAAe;IACnB,MAAM;IACN,OAAO,EACL,IAAI,QAAQ,kBAAkB,MAAM,GACrC;GACF;AACD,UAAO,CACL,iBACA,YACD;EACF;AAED,SAAO;CACR,EAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"completions.cjs","names":["getRequiredFilenameFromMetadata","AIMessage","handleMultiModalOutput","ChatMessage","HumanMessageChunk","AIMessageChunk","SystemMessageChunk","FunctionMessageChunk","ToolMessageChunk","ChatMessageChunk","messageToOpenAIRole","isReasoningModel","ToolMessage","convertLangChainToolCallToOpenAI"],"sources":["../../src/converters/completions.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageChunk,\n ChatMessage,\n ChatMessageChunk,\n FunctionMessageChunk,\n HumanMessageChunk,\n OpenAIToolCall,\n SystemMessageChunk,\n ToolCallChunk,\n ToolMessage,\n ToolMessageChunk,\n parseBase64DataUrl,\n parseMimeType,\n StandardContentBlockConverter,\n isDataContentBlock,\n ContentBlock,\n iife,\n convertToProviderContentBlock,\n} from \"@langchain/core/messages\";\nimport {\n convertLangChainToolCallToOpenAI,\n makeInvalidToolCall,\n parseToolCall,\n} from \"@langchain/core/output_parsers/openai_tools\";\nimport { Converter } from \"@langchain/core/utils/format\";\nimport type {\n ChatCompletionContentPartText,\n ChatCompletionContentPartImage,\n ChatCompletionContentPartInputAudio,\n ChatCompletionContentPart,\n} from \"openai/resources/chat/completions\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { handleMultiModalOutput } from \"../utils/output.js\";\nimport {\n getRequiredFilenameFromMetadata,\n isReasoningModel,\n messageToOpenAIRole,\n} from \"../utils/misc.js\";\n\n/**\n * @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.\n */\nexport const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}> = {\n providerName: \"ChatOpenAI\",\n\n fromStandardTextBlock(block): ChatCompletionContentPartText {\n return { type: \"text\", text: block.text };\n },\n\n fromStandardImageBlock(block): ChatCompletionContentPartImage {\n if (block.source_type === \"url\") {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const url = `data:${block.mime_type ?? \"\"};base64,${block.data}`;\n return {\n type: \"image_url\",\n image_url: {\n url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n throw new Error(\n `Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardAudioBlock(block): ChatCompletionContentPartInputAudio {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n const rawMimeType = data.mime_type || block.mime_type || \"\";\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(rawMimeType);\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: data.data,\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(block.mime_type ?? \"\");\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: block.data,\n },\n };\n }\n\n throw new Error(\n `Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardFileBlock(block): ChatCompletionContentPart.File {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n\n const filename = getRequiredFilenameFromMetadata(block);\n\n if (!data) {\n throw new Error(\n `URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n return {\n type: \"file\",\n file: {\n file_data: block.url, // formatted as base64 data URL\n ...(block.metadata?.filename || block.metadata?.name\n ? {\n filename,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const filename = getRequiredFilenameFromMetadata(block);\n\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mime_type ?? \"\"};base64,${block.data}`,\n ...(block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title\n ? {\n filename,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"id\") {\n return {\n type: \"file\",\n file: {\n file_id: block.id,\n },\n };\n }\n\n throw new Error(\n `File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n};\n\n/**\n * Converts an OpenAI Chat Completions API message to a LangChain BaseMessage.\n *\n * This converter transforms messages from OpenAI's Chat Completions API format into\n * LangChain's internal message representation, handling various message types and\n * preserving metadata, tool calls, and other relevant information.\n *\n * @remarks\n * The converter handles the following message roles:\n * - `assistant`: Converted to {@link AIMessage} with support for tool calls, function calls,\n * audio content, and multi-modal outputs\n * - Other roles: Converted to generic {@link ChatMessage}\n *\n * For assistant messages, the converter:\n * - Parses and validates tool calls, separating valid and invalid calls\n * - Preserves function call information in additional_kwargs\n * - Includes usage statistics and system fingerprint in response_metadata\n * - Handles multi-modal content (text, images, audio)\n * - Optionally includes the raw API response for debugging\n *\n * @param params - Conversion parameters\n * @param params.message - The OpenAI chat completion message to convert\n * @param params.rawResponse - The complete raw response from OpenAI's API, used to extract\n * metadata like model name, usage statistics, and system fingerprint\n * @param params.includeRawResponse - If true, includes the raw OpenAI response in the\n * message's additional_kwargs under the `__raw_response` key. Useful for debugging\n * or accessing provider-specific fields. Defaults to false.\n *\n * @returns A LangChain BaseMessage instance:\n * - {@link AIMessage} for assistant messages with tool calls, metadata, and content\n * - {@link ChatMessage} for all other message types\n *\n * @example\n * ```typescript\n * const baseMessage = convertCompletionsMessageToBaseMessage({\n * message: {\n * role: \"assistant\",\n * content: \"Hello! How can I help you?\",\n * tool_calls: [\n * {\n * id: \"call_123\",\n * type: \"function\",\n * function: { name: \"get_weather\", arguments: '{\"location\":\"NYC\"}' }\n * }\n * ]\n * },\n * rawResponse: completionResponse,\n * includeRawResponse: true\n * });\n * // Returns an AIMessage with parsed tool calls and metadata\n * ```\n *\n * @throws {Error} If tool call parsing fails, the invalid tool call is captured in\n * the `invalid_tool_calls` array rather than throwing an error\n *\n */\nexport const convertCompletionsMessageToBaseMessage: Converter<\n {\n message: OpenAIClient.Chat.Completions.ChatCompletionMessage;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;\n includeRawResponse?: boolean;\n },\n BaseMessage\n> = ({ message, rawResponse, includeRawResponse }) => {\n const rawToolCalls: OpenAIToolCall[] | undefined = message.tool_calls as\n | OpenAIToolCall[]\n | undefined;\n switch (message.role) {\n case \"assistant\": {\n const toolCalls = [];\n const invalidToolCalls = [];\n for (const rawToolCall of rawToolCalls ?? []) {\n try {\n toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));\n }\n }\n const additional_kwargs: Record<string, unknown> = {\n function_call: message.function_call,\n tool_calls: rawToolCalls,\n };\n if (includeRawResponse !== undefined) {\n additional_kwargs.__raw_response = rawResponse;\n }\n const response_metadata: Record<string, unknown> | undefined = {\n model_provider: \"openai\",\n model_name: rawResponse.model,\n ...(rawResponse.system_fingerprint\n ? {\n usage: { ...rawResponse.usage },\n system_fingerprint: rawResponse.system_fingerprint,\n }\n : {}),\n };\n\n if (message.audio) {\n additional_kwargs.audio = message.audio;\n }\n\n const content = handleMultiModalOutput(\n message.content || \"\",\n rawResponse.choices?.[0]?.message\n );\n return new AIMessage({\n content,\n tool_calls: toolCalls,\n invalid_tool_calls: invalidToolCalls,\n additional_kwargs,\n response_metadata,\n id: rawResponse.id,\n });\n }\n default:\n return new ChatMessage(message.content || \"\", message.role ?? \"unknown\");\n }\n};\n\n/**\n * Converts an OpenAI Chat Completions API delta (streaming chunk) to a LangChain BaseMessageChunk.\n *\n * This converter is used during streaming responses to transform incremental updates from OpenAI's\n * Chat Completions API into LangChain message chunks. It handles various message types, tool calls,\n * function calls, audio content, and role-specific message chunk creation.\n *\n * @param params - Conversion parameters\n * @param params.delta - The delta object from an OpenAI streaming chunk containing incremental\n * message updates. May include content, role, tool_calls, function_call, audio, etc.\n * @param params.rawResponse - The complete raw ChatCompletionChunk response from OpenAI,\n * containing metadata like model info, usage stats, and the delta\n * @param params.includeRawResponse - Optional flag to include the raw OpenAI response in the\n * message chunk's additional_kwargs. Useful for debugging or accessing provider-specific data\n * @param params.defaultRole - Optional default role to use if the delta doesn't specify one.\n * Typically used to maintain role consistency across chunks in a streaming response\n *\n * @returns A BaseMessageChunk subclass appropriate for the message role:\n * - HumanMessageChunk for \"user\" role\n * - AIMessageChunk for \"assistant\" role (includes tool call chunks)\n * - SystemMessageChunk for \"system\" or \"developer\" roles\n * - FunctionMessageChunk for \"function\" role\n * - ToolMessageChunk for \"tool\" role\n * - ChatMessageChunk for any other role\n *\n * @example\n * Basic streaming text chunk:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: { role: \"assistant\", content: \"Hello\" },\n * rawResponse: { id: \"chatcmpl-123\", model: \"gpt-4\", ... }\n * });\n * // Returns: AIMessageChunk with content \"Hello\"\n * ```\n *\n * @example\n * Streaming chunk with tool call:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: {\n * role: \"assistant\",\n * tool_calls: [{\n * index: 0,\n * id: \"call_123\",\n * function: { name: \"get_weather\", arguments: '{\"location\":' }\n * }]\n * },\n * rawResponse: { id: \"chatcmpl-123\", ... }\n * });\n * // Returns: AIMessageChunk with tool_call_chunks containing partial tool call data\n * ```\n *\n * @remarks\n * - Tool calls are converted to ToolCallChunk objects with incremental data\n * - Audio content includes the chunk index from the raw response\n * - The \"developer\" role is mapped to SystemMessageChunk with a special marker\n * - Response metadata includes model provider info and usage statistics\n * - Function calls and tool calls are stored in additional_kwargs for compatibility\n */\nexport const convertCompletionsDeltaToBaseMessageChunk: Converter<\n {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delta: Record<string, any>;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk;\n includeRawResponse?: boolean;\n defaultRole?: OpenAIClient.Chat.ChatCompletionRole;\n },\n BaseMessageChunk\n> = ({ delta, rawResponse, includeRawResponse, defaultRole }) => {\n const role = delta.role ?? defaultRole;\n const content = delta.content ?? \"\";\n let additional_kwargs: Record<string, unknown>;\n if (delta.function_call) {\n additional_kwargs = {\n function_call: delta.function_call,\n };\n } else if (delta.tool_calls) {\n additional_kwargs = {\n tool_calls: delta.tool_calls,\n };\n } else {\n additional_kwargs = {};\n }\n if (includeRawResponse) {\n additional_kwargs.__raw_response = rawResponse;\n }\n\n if (delta.audio) {\n additional_kwargs.audio = {\n ...delta.audio,\n index: rawResponse.choices[0].index,\n };\n }\n\n const response_metadata = {\n model_provider: \"openai\",\n usage: { ...rawResponse.usage },\n };\n if (role === \"user\") {\n return new HumanMessageChunk({ content, response_metadata });\n } else if (role === \"assistant\") {\n const toolCallChunks: ToolCallChunk[] = [];\n if (Array.isArray(delta.tool_calls)) {\n for (const rawToolCall of delta.tool_calls) {\n toolCallChunks.push({\n name: rawToolCall.function?.name,\n args: rawToolCall.function?.arguments,\n id: rawToolCall.id,\n index: rawToolCall.index,\n type: \"tool_call_chunk\",\n });\n }\n }\n return new AIMessageChunk({\n content,\n tool_call_chunks: toolCallChunks,\n additional_kwargs,\n id: rawResponse.id,\n response_metadata,\n });\n } else if (role === \"system\") {\n return new SystemMessageChunk({ content, response_metadata });\n } else if (role === \"developer\") {\n return new SystemMessageChunk({\n content,\n response_metadata,\n additional_kwargs: {\n __openai_role__: \"developer\",\n },\n });\n } else if (role === \"function\") {\n return new FunctionMessageChunk({\n content,\n additional_kwargs,\n name: delta.name,\n response_metadata,\n });\n } else if (role === \"tool\") {\n return new ToolMessageChunk({\n content,\n additional_kwargs,\n tool_call_id: delta.tool_call_id,\n response_metadata,\n });\n } else {\n return new ChatMessageChunk({ content, role, response_metadata });\n }\n};\n\n/**\n * Converts a standard LangChain content block to an OpenAI Completions API content part.\n *\n * This converter transforms LangChain's standardized content blocks (image, audio, file)\n * into the format expected by OpenAI's Chat Completions API. It handles various content\n * types including images (URL or base64), audio (base64), and files (data or file ID).\n *\n * @param block - The standard content block to convert. Can be an image, audio, or file block.\n *\n * @returns An OpenAI Chat Completions content part object, or undefined if the block\n * cannot be converted (e.g., missing required data).\n *\n * @example\n * Image with URL:\n * ```typescript\n * const block = { type: \"image\", url: \"https://example.com/image.jpg\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * ```\n *\n * @example\n * Image with base64 data:\n * ```typescript\n * const block = { type: \"image\", data: \"iVBORw0KGgo...\", mimeType: \"image/png\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"data:image/png;base64,iVBORw0KGgo...\" } }\n * ```\n */\nexport const convertStandardContentBlockToCompletionsContentPart: Converter<\n ContentBlock.Standard,\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartImage\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio\n | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File\n | undefined\n> = (block) => {\n if (block.type === \"image\") {\n if (block.url) {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n },\n };\n } else if (block.data) {\n return {\n type: \"image_url\",\n image_url: {\n url: `data:${block.mimeType};base64,${block.data}`,\n },\n };\n }\n }\n if (block.type === \"audio\") {\n if (block.data) {\n const format = iife(() => {\n const [, format] = block.mimeType.split(\"/\");\n if (format === \"wav\" || format === \"mp3\") {\n return format;\n }\n return \"wav\";\n });\n return {\n type: \"input_audio\",\n input_audio: {\n data: block.data.toString(),\n format,\n },\n };\n }\n }\n if (block.type === \"file\") {\n if (block.data) {\n const filename = getRequiredFilenameFromMetadata(block);\n\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mimeType};base64,${block.data}`,\n filename: filename,\n },\n };\n }\n if (block.fileId) {\n return {\n type: \"file\",\n file: {\n file_id: block.fileId,\n },\n };\n }\n }\n return undefined;\n};\n\n/**\n * Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.\n *\n * This converter transforms LangChain's standardized message format (using contentBlocks) into the format\n * expected by OpenAI's Chat Completions API. It handles role mapping, content filtering, and multi-modal\n * content conversion for various message types.\n *\n * @remarks\n * The converter performs the following transformations:\n * - Maps LangChain message roles to OpenAI API roles (user, assistant, system, developer, tool, function)\n * - For reasoning models, automatically converts \"system\" role to \"developer\" role\n * - Filters content blocks based on message role (most roles only include text blocks)\n * - For user messages, converts multi-modal content blocks (images, audio, files) to OpenAI format\n * - Preserves tool call IDs for tool messages and function names for function messages\n *\n * Role-specific behavior:\n * - **developer**: Returns only text content blocks (used for reasoning models)\n * - **system**: Returns only text content blocks\n * - **assistant**: Returns only text content blocks\n * - **tool**: Returns only text content blocks with tool_call_id preserved\n * - **function**: Returns text content blocks joined as a single string with function name\n * - **user** (default): Returns multi-modal content including text, images, audio, and files\n *\n * @param params - Conversion parameters\n * @param params.message - The LangChain BaseMessage to convert. Must have contentBlocks property\n * containing an array of standard content blocks (text, image, audio, file, etc.)\n * @param params.model - Optional model name. Used to determine if special role mapping is needed\n * (e.g., \"system\" -> \"developer\" for reasoning models like o1)\n *\n * @returns An OpenAI ChatCompletionMessageParam object formatted for the Chat Completions API.\n * The structure varies by role:\n * - Developer/System/Assistant: `{ role, content: TextBlock[] }`\n * - Tool: `{ role: \"tool\", tool_call_id, content: TextBlock[] }`\n * - Function: `{ role: \"function\", name, content: string }`\n * - User: `{ role: \"user\", content: Array<TextPart | ImagePart | AudioPart | FilePart> }`\n *\n * @example\n * Simple text message:\n * ```typescript\n * const message = new HumanMessage({\n * content: [{ type: \"text\", text: \"Hello!\" }]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: { role: \"user\", content: [{ type: \"text\", text: \"Hello!\" }] }\n * ```\n *\n * @example\n * Multi-modal user message with image:\n * ```typescript\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"What's in this image?\" },\n * { type: \"image\", url: \"https://example.com/image.jpg\" }\n * ]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: {\n * // role: \"user\",\n * // content: [\n * // { type: \"text\", text: \"What's in this image?\" },\n * // { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * // ]\n * // }\n * ```\n */\nexport const convertStandardContentMessageToCompletionsMessage: Converter<\n { message: BaseMessage; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam\n> = ({ message, model }) => {\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n if (role === \"developer\") {\n return {\n role: \"developer\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"system\") {\n return {\n role: \"system\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"assistant\") {\n return {\n role: \"assistant\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"tool\" && ToolMessage.isInstance(message)) {\n return {\n role: \"tool\",\n tool_call_id: message.tool_call_id,\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"function\") {\n return {\n role: \"function\",\n name: message.name ?? \"\",\n content: message.contentBlocks\n .filter((block) => block.type === \"text\")\n .join(\"\"),\n };\n }\n // Default to user message handling\n function* iterateUserContent(blocks: ContentBlock.Standard[]) {\n for (const block of blocks) {\n if (block.type === \"text\") {\n yield {\n type: \"text\" as const,\n text: block.text,\n };\n }\n const data = convertStandardContentBlockToCompletionsContentPart(block);\n if (data) {\n yield data;\n }\n }\n }\n return {\n role: \"user\",\n content: Array.from(iterateUserContent(message.contentBlocks)),\n };\n};\n\n/**\n * Converts an array of LangChain BaseMessages to OpenAI Chat Completions API message parameters.\n *\n * This converter transforms LangChain's internal message representation into the format required\n * by OpenAI's Chat Completions API. It handles various message types, roles, content formats,\n * tool calls, function calls, audio messages, and special model-specific requirements.\n *\n * @remarks\n * The converter performs several key transformations:\n * - Maps LangChain message types to OpenAI roles (user, assistant, system, tool, function, developer)\n * - Converts standard content blocks (v1 format) using a specialized converter\n * - Handles multimodal content including text, images, audio, and data blocks\n * - Preserves tool calls and function calls with proper formatting\n * - Applies model-specific role mappings (e.g., \"system\" → \"developer\" for reasoning models)\n * - Splits audio messages into separate message parameters when needed\n *\n * @param params - Conversion parameters\n * @param params.messages - Array of LangChain BaseMessages to convert. Can include any message\n * type: HumanMessage, AIMessage, SystemMessage, ToolMessage, FunctionMessage, etc.\n * @param params.model - Optional model name used to determine if special role mapping is needed.\n * For reasoning models (o1, o3, etc.), \"system\" role is converted to \"developer\" role.\n *\n * @returns Array of ChatCompletionMessageParam objects formatted for OpenAI's Chat Completions API.\n * Some messages may be split into multiple parameters (e.g., audio messages).\n *\n * @example\n * Basic message conversion:\n * ```typescript\n * const messages = [\n * new HumanMessage(\"What's the weather like?\"),\n * new AIMessage(\"Let me check that for you.\")\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({\n * messages,\n * model: \"gpt-4\"\n * });\n * // Returns:\n * // [\n * // { role: \"user\", content: \"What's the weather like?\" },\n * // { role: \"assistant\", content: \"Let me check that for you.\" }\n * // ]\n * ```\n *\n * @example\n * Message with tool calls:\n * ```typescript\n * const messages = [\n * new AIMessage({\n * content: \"\",\n * tool_calls: [{\n * id: \"call_123\",\n * name: \"get_weather\",\n * args: { location: \"San Francisco\" }\n * }]\n * })\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({ messages });\n * // Returns:\n * // [{\n * // role: \"assistant\",\n * // content: \"\",\n * // tool_calls: [{\n * // id: \"call_123\",\n * // type: \"function\",\n * // function: { name: \"get_weather\", arguments: '{\"location\":\"San Francisco\"}' }\n * // }]\n * // }]\n * ```\n */\nexport const convertMessagesToCompletionsMessageParams: Converter<\n { messages: BaseMessage[]; model?: string },\n OpenAIClient.Chat.Completions.ChatCompletionMessageParam[]\n> = ({ messages, model }) => {\n return messages.flatMap((message) => {\n if (\n \"output_version\" in message.response_metadata &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return convertStandardContentMessageToCompletionsMessage({ message });\n }\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n\n const content =\n typeof message.content === \"string\"\n ? message.content\n : message.content.flatMap((m) => {\n if (isDataContentBlock(m)) {\n return convertToProviderContentBlock(\n m,\n completionsApiContentBlockConverter\n );\n }\n // Drop Anthropic tool_use blocks from content — these are\n // already represented in message.tool_calls and would cause\n // an API error if passed through to OpenAI.\n if (\n typeof m === \"object\" &&\n m !== null &&\n \"type\" in m &&\n m.type === \"tool_use\"\n ) {\n return [];\n }\n return m;\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const completionParam: Record<string, any> = {\n role,\n content,\n };\n if (message.name != null) {\n completionParam.name = message.name;\n }\n if (message.additional_kwargs.function_call != null) {\n completionParam.function_call = message.additional_kwargs.function_call;\n }\n if (AIMessage.isInstance(message) && !!message.tool_calls?.length) {\n completionParam.tool_calls = message.tool_calls.map(\n convertLangChainToolCallToOpenAI\n );\n } else {\n if (message.additional_kwargs.tool_calls != null) {\n completionParam.tool_calls = message.additional_kwargs.tool_calls;\n }\n if (ToolMessage.isInstance(message) && message.tool_call_id != null) {\n completionParam.tool_call_id = message.tool_call_id;\n }\n }\n\n if (\n message.additional_kwargs.audio &&\n typeof message.additional_kwargs.audio === \"object\" &&\n \"id\" in message.additional_kwargs.audio\n ) {\n const audioMessage = {\n role: \"assistant\",\n audio: {\n id: message.additional_kwargs.audio.id,\n },\n };\n return [\n completionParam,\n audioMessage,\n ] as OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n }\n\n return completionParam as OpenAIClient.Chat.Completions.ChatCompletionMessageParam;\n });\n};\n"],"mappings":";;;;;;;;;AA6CA,MAAa,sCAKR;CACH,cAAc;CAEd,sBAAsB,OAAsC;AAC1D,SAAO;GAAE,MAAM;GAAQ,MAAM,MAAM;GAAM;;CAG3C,uBAAuB,OAAuC;AAC5D,MAAI,MAAM,gBAAgB,MACxB,QAAO;GACL,MAAM;GACN,WAAW;IACT,KAAK,MAAM;IACX,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,QAAmC,GAC5D,EAAE;IACP;GACF;AAGH,MAAI,MAAM,gBAAgB,SAExB,QAAO;GACL,MAAM;GACN,WAAW;IACT,KAJQ,QAAQ,MAAM,aAAa,GAAG,UAAU,MAAM;IAKtD,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,QAAmC,GAC5D,EAAE;IACP;GACF;AAGH,QAAM,IAAI,MACR,yCAAyC,MAAM,YAAY,mCAC5D;;CAGH,uBAAuB,OAA4C;AACjE,MAAI,MAAM,gBAAgB,OAAO;GAC/B,MAA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MAAM,YACb,MAAM,QACN,UAAU,KACV,EAAE,SAAS,WAEX,QAAO,EAAE;AAEX,UAAO;IACP;EAER,MAAM,kBAAuC;GAC3C;GACA;GACD;AACD,MAAI,QAAQ,QAAQ,KAClB,iBAAgB,OAAO,QAAQ;AAEjC,MAAI,QAAQ,kBAAkB,iBAAiB,KAC7C,iBAAgB,gBAAgB,QAAQ,kBAAkB;AAE5D,MAAIV,mCAAU,WAAW,QAAQ,IAAI,CAAC,CAAC,QAAQ,YAAY,OACzD,iBAAgB,aAAa,QAAQ,WAAW,IAC9CY,6EACD;OACI;AACL,OAAI,QAAQ,kBAAkB,cAAc,KAC1C,iBAAgB,aAAa,QAAQ,kBAAkB;AAEzD,OAAID,qCAAY,WAAW,QAAQ,IAAI,QAAQ,gBAAgB,KAC7D,iBAAgB,eAAe,QAAQ;;AAI3C,MACE,QAAQ,kBAAkB,SAC1B,OAAO,QAAQ,kBAAkB,UAAU,YAC3C,QAAQ,QAAQ,kBAAkB,MAQlC,QAAO,CACL,iBAPmB;GACnB,MAAM;GACN,OAAO,EACL,IAAI,QAAQ,kBAAkB,MAAM,IACrC;GACF,CAIA;AAGH,SAAO;GACP"}
|
|
@@ -4,7 +4,6 @@ import { Converter } from "@langchain/core/utils/format";
|
|
|
4
4
|
import { ChatCompletionContentPart, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPartText } from "openai/resources/chat/completions";
|
|
5
5
|
|
|
6
6
|
//#region src/converters/completions.d.ts
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.
|
|
10
9
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"completions.d.cts","names":["BaseMessage","BaseMessageChunk","StandardContentBlockConverter","ContentBlock","Converter","ChatCompletionContentPartText","ChatCompletionContentPartImage","ChatCompletionContentPartInputAudio","ChatCompletionContentPart","OpenAI","OpenAIClient","completionsApiContentBlockConverter","File","convertCompletionsMessageToBaseMessage","Chat","Completions","ChatCompletionMessage","ChatCompletion","convertCompletionsDeltaToBaseMessageChunk","Record","ChatCompletionChunk","ChatCompletionRole","convertStandardContentBlockToCompletionsContentPart","Standard","convertStandardContentMessageToCompletionsMessage","ChatCompletionMessageParam","convertMessagesToCompletionsMessageParams"],"sources":["../../src/converters/completions.d.ts"],"sourcesContent":["import { BaseMessage, BaseMessageChunk, StandardContentBlockConverter, ContentBlock } from \"@langchain/core/messages\";\nimport { Converter } from \"@langchain/core/utils/format\";\nimport type { ChatCompletionContentPartText, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPart } from \"openai/resources/chat/completions\";\nimport { OpenAI as OpenAIClient } from \"openai\";\n/**\n * @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.\n */\nexport declare const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>;\n/**\n * Converts an OpenAI Chat Completions API message to a LangChain BaseMessage.\n *\n * This converter transforms messages from OpenAI's Chat Completions API format into\n * LangChain's internal message representation, handling various message types and\n * preserving metadata, tool calls, and other relevant information.\n *\n * @remarks\n * The converter handles the following message roles:\n * - `assistant`: Converted to {@link AIMessage} with support for tool calls, function calls,\n * audio content, and multi-modal outputs\n * - Other roles: Converted to generic {@link ChatMessage}\n *\n * For assistant messages, the converter:\n * - Parses and validates tool calls, separating valid and invalid calls\n * - Preserves function call information in additional_kwargs\n * - Includes usage statistics and system fingerprint in response_metadata\n * - Handles multi-modal content (text, images, audio)\n * - Optionally includes the raw API response for debugging\n *\n * @param params - Conversion parameters\n * @param params.message - The OpenAI chat completion message to convert\n * @param params.rawResponse - The complete raw response from OpenAI's API, used to extract\n * metadata like model name, usage statistics, and system fingerprint\n * @param params.includeRawResponse - If true, includes the raw OpenAI response in the\n * message's additional_kwargs under the `__raw_response` key. Useful for debugging\n * or accessing provider-specific fields. Defaults to false.\n *\n * @returns A LangChain BaseMessage instance:\n * - {@link AIMessage} for assistant messages with tool calls, metadata, and content\n * - {@link ChatMessage} for all other message types\n *\n * @example\n * ```typescript\n * const baseMessage = convertCompletionsMessageToBaseMessage({\n * message: {\n * role: \"assistant\",\n * content: \"Hello! How can I help you?\",\n * tool_calls: [\n * {\n * id: \"call_123\",\n * type: \"function\",\n * function: { name: \"get_weather\", arguments: '{\"location\":\"NYC\"}' }\n * }\n * ]\n * },\n * rawResponse: completionResponse,\n * includeRawResponse: true\n * });\n * // Returns an AIMessage with parsed tool calls and metadata\n * ```\n *\n * @throws {Error} If tool call parsing fails, the invalid tool call is captured in\n * the `invalid_tool_calls` array rather than throwing an error\n *\n */\nexport declare const convertCompletionsMessageToBaseMessage: Converter<{\n message: OpenAIClient.Chat.Completions.ChatCompletionMessage;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;\n includeRawResponse?: boolean;\n}, BaseMessage>;\n/**\n * Converts an OpenAI Chat Completions API delta (streaming chunk) to a LangChain BaseMessageChunk.\n *\n * This converter is used during streaming responses to transform incremental updates from OpenAI's\n * Chat Completions API into LangChain message chunks. It handles various message types, tool calls,\n * function calls, audio content, and role-specific message chunk creation.\n *\n * @param params - Conversion parameters\n * @param params.delta - The delta object from an OpenAI streaming chunk containing incremental\n * message updates. May include content, role, tool_calls, function_call, audio, etc.\n * @param params.rawResponse - The complete raw ChatCompletionChunk response from OpenAI,\n * containing metadata like model info, usage stats, and the delta\n * @param params.includeRawResponse - Optional flag to include the raw OpenAI response in the\n * message chunk's additional_kwargs. Useful for debugging or accessing provider-specific data\n * @param params.defaultRole - Optional default role to use if the delta doesn't specify one.\n * Typically used to maintain role consistency across chunks in a streaming response\n *\n * @returns A BaseMessageChunk subclass appropriate for the message role:\n * - HumanMessageChunk for \"user\" role\n * - AIMessageChunk for \"assistant\" role (includes tool call chunks)\n * - SystemMessageChunk for \"system\" or \"developer\" roles\n * - FunctionMessageChunk for \"function\" role\n * - ToolMessageChunk for \"tool\" role\n * - ChatMessageChunk for any other role\n *\n * @example\n * Basic streaming text chunk:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: { role: \"assistant\", content: \"Hello\" },\n * rawResponse: { id: \"chatcmpl-123\", model: \"gpt-4\", ... }\n * });\n * // Returns: AIMessageChunk with content \"Hello\"\n * ```\n *\n * @example\n * Streaming chunk with tool call:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: {\n * role: \"assistant\",\n * tool_calls: [{\n * index: 0,\n * id: \"call_123\",\n * function: { name: \"get_weather\", arguments: '{\"location\":' }\n * }]\n * },\n * rawResponse: { id: \"chatcmpl-123\", ... }\n * });\n * // Returns: AIMessageChunk with tool_call_chunks containing partial tool call data\n * ```\n *\n * @remarks\n * - Tool calls are converted to ToolCallChunk objects with incremental data\n * - Audio content includes the chunk index from the raw response\n * - The \"developer\" role is mapped to SystemMessageChunk with a special marker\n * - Response metadata includes model provider info and usage statistics\n * - Function calls and tool calls are stored in additional_kwargs for compatibility\n */\nexport declare const convertCompletionsDeltaToBaseMessageChunk: Converter<{\n delta: Record<string, any>;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk;\n includeRawResponse?: boolean;\n defaultRole?: OpenAIClient.Chat.ChatCompletionRole;\n}, BaseMessageChunk>;\n/**\n * Converts a standard LangChain content block to an OpenAI Completions API content part.\n *\n * This converter transforms LangChain's standardized content blocks (image, audio, file)\n * into the format expected by OpenAI's Chat Completions API. It handles various content\n * types including images (URL or base64), audio (base64), and files (data or file ID).\n *\n * @param block - The standard content block to convert. Can be an image, audio, or file block.\n *\n * @returns An OpenAI Chat Completions content part object, or undefined if the block\n * cannot be converted (e.g., missing required data).\n *\n * @example\n * Image with URL:\n * ```typescript\n * const block = { type: \"image\", url: \"https://example.com/image.jpg\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * ```\n *\n * @example\n * Image with base64 data:\n * ```typescript\n * const block = { type: \"image\", data: \"iVBORw0KGgo...\", mimeType: \"image/png\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"data:image/png;base64,iVBORw0KGgo...\" } }\n * ```\n */\nexport declare const convertStandardContentBlockToCompletionsContentPart: Converter<ContentBlock.Standard, OpenAIClient.Chat.Completions.ChatCompletionContentPartImage | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File | undefined>;\n/**\n * Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.\n *\n * This converter transforms LangChain's standardized message format (using contentBlocks) into the format\n * expected by OpenAI's Chat Completions API. It handles role mapping, content filtering, and multi-modal\n * content conversion for various message types.\n *\n * @remarks\n * The converter performs the following transformations:\n * - Maps LangChain message roles to OpenAI API roles (user, assistant, system, developer, tool, function)\n * - For reasoning models, automatically converts \"system\" role to \"developer\" role\n * - Filters content blocks based on message role (most roles only include text blocks)\n * - For user messages, converts multi-modal content blocks (images, audio, files) to OpenAI format\n * - Preserves tool call IDs for tool messages and function names for function messages\n *\n * Role-specific behavior:\n * - **developer**: Returns only text content blocks (used for reasoning models)\n * - **system**: Returns only text content blocks\n * - **assistant**: Returns only text content blocks\n * - **tool**: Returns only text content blocks with tool_call_id preserved\n * - **function**: Returns text content blocks joined as a single string with function name\n * - **user** (default): Returns multi-modal content including text, images, audio, and files\n *\n * @param params - Conversion parameters\n * @param params.message - The LangChain BaseMessage to convert. Must have contentBlocks property\n * containing an array of standard content blocks (text, image, audio, file, etc.)\n * @param params.model - Optional model name. Used to determine if special role mapping is needed\n * (e.g., \"system\" -> \"developer\" for reasoning models like o1)\n *\n * @returns An OpenAI ChatCompletionMessageParam object formatted for the Chat Completions API.\n * The structure varies by role:\n * - Developer/System/Assistant: `{ role, content: TextBlock[] }`\n * - Tool: `{ role: \"tool\", tool_call_id, content: TextBlock[] }`\n * - Function: `{ role: \"function\", name, content: string }`\n * - User: `{ role: \"user\", content: Array<TextPart | ImagePart | AudioPart | FilePart> }`\n *\n * @example\n * Simple text message:\n * ```typescript\n * const message = new HumanMessage({\n * content: [{ type: \"text\", text: \"Hello!\" }]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: { role: \"user\", content: [{ type: \"text\", text: \"Hello!\" }] }\n * ```\n *\n * @example\n * Multi-modal user message with image:\n * ```typescript\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"What's in this image?\" },\n * { type: \"image\", url: \"https://example.com/image.jpg\" }\n * ]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: {\n * // role: \"user\",\n * // content: [\n * // { type: \"text\", text: \"What's in this image?\" },\n * // { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * // ]\n * // }\n * ```\n */\nexport declare const convertStandardContentMessageToCompletionsMessage: Converter<{\n message: BaseMessage;\n model?: string;\n}, OpenAIClient.Chat.Completions.ChatCompletionMessageParam>;\n/**\n * Converts an array of LangChain BaseMessages to OpenAI Chat Completions API message parameters.\n *\n * This converter transforms LangChain's internal message representation into the format required\n * by OpenAI's Chat Completions API. It handles various message types, roles, content formats,\n * tool calls, function calls, audio messages, and special model-specific requirements.\n *\n * @remarks\n * The converter performs several key transformations:\n * - Maps LangChain message types to OpenAI roles (user, assistant, system, tool, function, developer)\n * - Converts standard content blocks (v1 format) using a specialized converter\n * - Handles multimodal content including text, images, audio, and data blocks\n * - Preserves tool calls and function calls with proper formatting\n * - Applies model-specific role mappings (e.g., \"system\" → \"developer\" for reasoning models)\n * - Splits audio messages into separate message parameters when needed\n *\n * @param params - Conversion parameters\n * @param params.messages - Array of LangChain BaseMessages to convert. Can include any message\n * type: HumanMessage, AIMessage, SystemMessage, ToolMessage, FunctionMessage, etc.\n * @param params.model - Optional model name used to determine if special role mapping is needed.\n * For reasoning models (o1, o3, etc.), \"system\" role is converted to \"developer\" role.\n *\n * @returns Array of ChatCompletionMessageParam objects formatted for OpenAI's Chat Completions API.\n * Some messages may be split into multiple parameters (e.g., audio messages).\n *\n * @example\n * Basic message conversion:\n * ```typescript\n * const messages = [\n * new HumanMessage(\"What's the weather like?\"),\n * new AIMessage(\"Let me check that for you.\")\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({\n * messages,\n * model: \"gpt-4\"\n * });\n * // Returns:\n * // [\n * // { role: \"user\", content: \"What's the weather like?\" },\n * // { role: \"assistant\", content: \"Let me check that for you.\" }\n * // ]\n * ```\n *\n * @example\n * Message with tool calls:\n * ```typescript\n * const messages = [\n * new AIMessage({\n * content: \"\",\n * tool_calls: [{\n * id: \"call_123\",\n * name: \"get_weather\",\n * args: { location: \"San Francisco\" }\n * }]\n * })\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({ messages });\n * // Returns:\n * // [{\n * // role: \"assistant\",\n * // content: \"\",\n * // tool_calls: [{\n * // id: \"call_123\",\n * // type: \"function\",\n * // function: { name: \"get_weather\", arguments: '{\"location\":\"San Francisco\"}' }\n * // }]\n * // }]\n * ```\n */\nexport declare const convertMessagesToCompletionsMessageParams: Converter<{\n messages: BaseMessage[];\n model?: string;\n}, OpenAIClient.Chat.Completions.ChatCompletionMessageParam[]>;\n//# sourceMappingURL=completions.d.ts.map"],"mappings":";;;;;;;;;AAOA;AACUK,cADWM,mCACXN,EADgDH,6BAChDG,CAAAA;EACCC,IAAAA,EADDD,6BACCC;EACAC,KAAAA,EADAD,8BACAC;EACDC,KAAAA,EADCD,mCACyBK;EAJsBV,IAAAA,EAIhDM,yBAAAA,CAA0BI,IAJsBV;AAA6B,CAAA,CAAA;AA8DvF;;;;;AAAsE;AAgEtE;;;;;;AAAyE;AAkCzE;;;;;;AAAmF;AAkEnF;;;;AAAiF;AA2EjF;;;;AAAyE;;;;;;;;;;;;;;;;;;;;;;;;;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AA,EAAwC,SAAA;EAEjD,OAAA,EAAS,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,qBAAA;EACvC,WAAA,EAAa,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,cAAA;EAC3C,kBAAA;AAAA,GAEF,WAAA;;;;;AA0OF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmIA;;;;;;;;;;;;;;;;;;AAkIA;;;;;;;;;cA3Xa,yCAAA,EAA2C,SAAA;EAGpD,KAAA,EAAO,MAAA;EACP,WAAA,EAAa,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,mBAAA;EAC3C,kBAAA;EACA,WAAA,GAAc,QAAA,CAAa,IAAA,CAAK,kBAAA;AAAA,GAElC,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA8GW,mDAAA,EAAqD,SAAA,CAChE,YAAA,CAAa,QAAA,EACX,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,8BAAA,GAC9B,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,mCAAA,GAC9B,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,yBAAA,CAA0B,IAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA+H/C,iDAAA,EAAmD,SAAA;EAC5D,OAAA,EAAS,WAAA;EAAa,KAAA;AAAA,GACxB,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,0BAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgInB,yCAAA,EAA2C,SAAA;EACpD,QAAA,EAAU,WAAA;EAAe,KAAA;AAAA,GAC3B,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,0BAAA"}
|
|
@@ -4,7 +4,6 @@ import { Converter } from "@langchain/core/utils/format";
|
|
|
4
4
|
import { ChatCompletionContentPart, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPartText } from "openai/resources/chat/completions";
|
|
5
5
|
|
|
6
6
|
//#region src/converters/completions.d.ts
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.
|
|
10
9
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"completions.d.ts","names":["BaseMessage","BaseMessageChunk","StandardContentBlockConverter","ContentBlock","Converter","ChatCompletionContentPartText","ChatCompletionContentPartImage","ChatCompletionContentPartInputAudio","ChatCompletionContentPart","OpenAI","OpenAIClient","completionsApiContentBlockConverter","File","convertCompletionsMessageToBaseMessage","Chat","Completions","ChatCompletionMessage","ChatCompletion","convertCompletionsDeltaToBaseMessageChunk","Record","ChatCompletionChunk","ChatCompletionRole","convertStandardContentBlockToCompletionsContentPart","Standard","convertStandardContentMessageToCompletionsMessage","ChatCompletionMessageParam","convertMessagesToCompletionsMessageParams"],"sources":["../../src/converters/completions.d.ts"],"sourcesContent":["import { BaseMessage, BaseMessageChunk, StandardContentBlockConverter, ContentBlock } from \"@langchain/core/messages\";\nimport { Converter } from \"@langchain/core/utils/format\";\nimport type { ChatCompletionContentPartText, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPart } from \"openai/resources/chat/completions\";\nimport { OpenAI as OpenAIClient } from \"openai\";\n/**\n * @deprecated This converter is an internal detail of the OpenAI provider. Do not use it directly. This will be revisited in a future release.\n */\nexport declare const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>;\n/**\n * Converts an OpenAI Chat Completions API message to a LangChain BaseMessage.\n *\n * This converter transforms messages from OpenAI's Chat Completions API format into\n * LangChain's internal message representation, handling various message types and\n * preserving metadata, tool calls, and other relevant information.\n *\n * @remarks\n * The converter handles the following message roles:\n * - `assistant`: Converted to {@link AIMessage} with support for tool calls, function calls,\n * audio content, and multi-modal outputs\n * - Other roles: Converted to generic {@link ChatMessage}\n *\n * For assistant messages, the converter:\n * - Parses and validates tool calls, separating valid and invalid calls\n * - Preserves function call information in additional_kwargs\n * - Includes usage statistics and system fingerprint in response_metadata\n * - Handles multi-modal content (text, images, audio)\n * - Optionally includes the raw API response for debugging\n *\n * @param params - Conversion parameters\n * @param params.message - The OpenAI chat completion message to convert\n * @param params.rawResponse - The complete raw response from OpenAI's API, used to extract\n * metadata like model name, usage statistics, and system fingerprint\n * @param params.includeRawResponse - If true, includes the raw OpenAI response in the\n * message's additional_kwargs under the `__raw_response` key. Useful for debugging\n * or accessing provider-specific fields. Defaults to false.\n *\n * @returns A LangChain BaseMessage instance:\n * - {@link AIMessage} for assistant messages with tool calls, metadata, and content\n * - {@link ChatMessage} for all other message types\n *\n * @example\n * ```typescript\n * const baseMessage = convertCompletionsMessageToBaseMessage({\n * message: {\n * role: \"assistant\",\n * content: \"Hello! How can I help you?\",\n * tool_calls: [\n * {\n * id: \"call_123\",\n * type: \"function\",\n * function: { name: \"get_weather\", arguments: '{\"location\":\"NYC\"}' }\n * }\n * ]\n * },\n * rawResponse: completionResponse,\n * includeRawResponse: true\n * });\n * // Returns an AIMessage with parsed tool calls and metadata\n * ```\n *\n * @throws {Error} If tool call parsing fails, the invalid tool call is captured in\n * the `invalid_tool_calls` array rather than throwing an error\n *\n */\nexport declare const convertCompletionsMessageToBaseMessage: Converter<{\n message: OpenAIClient.Chat.Completions.ChatCompletionMessage;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;\n includeRawResponse?: boolean;\n}, BaseMessage>;\n/**\n * Converts an OpenAI Chat Completions API delta (streaming chunk) to a LangChain BaseMessageChunk.\n *\n * This converter is used during streaming responses to transform incremental updates from OpenAI's\n * Chat Completions API into LangChain message chunks. It handles various message types, tool calls,\n * function calls, audio content, and role-specific message chunk creation.\n *\n * @param params - Conversion parameters\n * @param params.delta - The delta object from an OpenAI streaming chunk containing incremental\n * message updates. May include content, role, tool_calls, function_call, audio, etc.\n * @param params.rawResponse - The complete raw ChatCompletionChunk response from OpenAI,\n * containing metadata like model info, usage stats, and the delta\n * @param params.includeRawResponse - Optional flag to include the raw OpenAI response in the\n * message chunk's additional_kwargs. Useful for debugging or accessing provider-specific data\n * @param params.defaultRole - Optional default role to use if the delta doesn't specify one.\n * Typically used to maintain role consistency across chunks in a streaming response\n *\n * @returns A BaseMessageChunk subclass appropriate for the message role:\n * - HumanMessageChunk for \"user\" role\n * - AIMessageChunk for \"assistant\" role (includes tool call chunks)\n * - SystemMessageChunk for \"system\" or \"developer\" roles\n * - FunctionMessageChunk for \"function\" role\n * - ToolMessageChunk for \"tool\" role\n * - ChatMessageChunk for any other role\n *\n * @example\n * Basic streaming text chunk:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: { role: \"assistant\", content: \"Hello\" },\n * rawResponse: { id: \"chatcmpl-123\", model: \"gpt-4\", ... }\n * });\n * // Returns: AIMessageChunk with content \"Hello\"\n * ```\n *\n * @example\n * Streaming chunk with tool call:\n * ```typescript\n * const chunk = convertCompletionsDeltaToBaseMessageChunk({\n * delta: {\n * role: \"assistant\",\n * tool_calls: [{\n * index: 0,\n * id: \"call_123\",\n * function: { name: \"get_weather\", arguments: '{\"location\":' }\n * }]\n * },\n * rawResponse: { id: \"chatcmpl-123\", ... }\n * });\n * // Returns: AIMessageChunk with tool_call_chunks containing partial tool call data\n * ```\n *\n * @remarks\n * - Tool calls are converted to ToolCallChunk objects with incremental data\n * - Audio content includes the chunk index from the raw response\n * - The \"developer\" role is mapped to SystemMessageChunk with a special marker\n * - Response metadata includes model provider info and usage statistics\n * - Function calls and tool calls are stored in additional_kwargs for compatibility\n */\nexport declare const convertCompletionsDeltaToBaseMessageChunk: Converter<{\n delta: Record<string, any>;\n rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk;\n includeRawResponse?: boolean;\n defaultRole?: OpenAIClient.Chat.ChatCompletionRole;\n}, BaseMessageChunk>;\n/**\n * Converts a standard LangChain content block to an OpenAI Completions API content part.\n *\n * This converter transforms LangChain's standardized content blocks (image, audio, file)\n * into the format expected by OpenAI's Chat Completions API. It handles various content\n * types including images (URL or base64), audio (base64), and files (data or file ID).\n *\n * @param block - The standard content block to convert. Can be an image, audio, or file block.\n *\n * @returns An OpenAI Chat Completions content part object, or undefined if the block\n * cannot be converted (e.g., missing required data).\n *\n * @example\n * Image with URL:\n * ```typescript\n * const block = { type: \"image\", url: \"https://example.com/image.jpg\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * ```\n *\n * @example\n * Image with base64 data:\n * ```typescript\n * const block = { type: \"image\", data: \"iVBORw0KGgo...\", mimeType: \"image/png\" };\n * const part = convertStandardContentBlockToCompletionsContentPart(block);\n * // Returns: { type: \"image_url\", image_url: { url: \"data:image/png;base64,iVBORw0KGgo...\" } }\n * ```\n */\nexport declare const convertStandardContentBlockToCompletionsContentPart: Converter<ContentBlock.Standard, OpenAIClient.Chat.Completions.ChatCompletionContentPartImage | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File | undefined>;\n/**\n * Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.\n *\n * This converter transforms LangChain's standardized message format (using contentBlocks) into the format\n * expected by OpenAI's Chat Completions API. It handles role mapping, content filtering, and multi-modal\n * content conversion for various message types.\n *\n * @remarks\n * The converter performs the following transformations:\n * - Maps LangChain message roles to OpenAI API roles (user, assistant, system, developer, tool, function)\n * - For reasoning models, automatically converts \"system\" role to \"developer\" role\n * - Filters content blocks based on message role (most roles only include text blocks)\n * - For user messages, converts multi-modal content blocks (images, audio, files) to OpenAI format\n * - Preserves tool call IDs for tool messages and function names for function messages\n *\n * Role-specific behavior:\n * - **developer**: Returns only text content blocks (used for reasoning models)\n * - **system**: Returns only text content blocks\n * - **assistant**: Returns only text content blocks\n * - **tool**: Returns only text content blocks with tool_call_id preserved\n * - **function**: Returns text content blocks joined as a single string with function name\n * - **user** (default): Returns multi-modal content including text, images, audio, and files\n *\n * @param params - Conversion parameters\n * @param params.message - The LangChain BaseMessage to convert. Must have contentBlocks property\n * containing an array of standard content blocks (text, image, audio, file, etc.)\n * @param params.model - Optional model name. Used to determine if special role mapping is needed\n * (e.g., \"system\" -> \"developer\" for reasoning models like o1)\n *\n * @returns An OpenAI ChatCompletionMessageParam object formatted for the Chat Completions API.\n * The structure varies by role:\n * - Developer/System/Assistant: `{ role, content: TextBlock[] }`\n * - Tool: `{ role: \"tool\", tool_call_id, content: TextBlock[] }`\n * - Function: `{ role: \"function\", name, content: string }`\n * - User: `{ role: \"user\", content: Array<TextPart | ImagePart | AudioPart | FilePart> }`\n *\n * @example\n * Simple text message:\n * ```typescript\n * const message = new HumanMessage({\n * content: [{ type: \"text\", text: \"Hello!\" }]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: { role: \"user\", content: [{ type: \"text\", text: \"Hello!\" }] }\n * ```\n *\n * @example\n * Multi-modal user message with image:\n * ```typescript\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"What's in this image?\" },\n * { type: \"image\", url: \"https://example.com/image.jpg\" }\n * ]\n * });\n * const param = convertStandardContentMessageToCompletionsMessage({ message });\n * // Returns: {\n * // role: \"user\",\n * // content: [\n * // { type: \"text\", text: \"What's in this image?\" },\n * // { type: \"image_url\", image_url: { url: \"https://example.com/image.jpg\" } }\n * // ]\n * // }\n * ```\n */\nexport declare const convertStandardContentMessageToCompletionsMessage: Converter<{\n message: BaseMessage;\n model?: string;\n}, OpenAIClient.Chat.Completions.ChatCompletionMessageParam>;\n/**\n * Converts an array of LangChain BaseMessages to OpenAI Chat Completions API message parameters.\n *\n * This converter transforms LangChain's internal message representation into the format required\n * by OpenAI's Chat Completions API. It handles various message types, roles, content formats,\n * tool calls, function calls, audio messages, and special model-specific requirements.\n *\n * @remarks\n * The converter performs several key transformations:\n * - Maps LangChain message types to OpenAI roles (user, assistant, system, tool, function, developer)\n * - Converts standard content blocks (v1 format) using a specialized converter\n * - Handles multimodal content including text, images, audio, and data blocks\n * - Preserves tool calls and function calls with proper formatting\n * - Applies model-specific role mappings (e.g., \"system\" → \"developer\" for reasoning models)\n * - Splits audio messages into separate message parameters when needed\n *\n * @param params - Conversion parameters\n * @param params.messages - Array of LangChain BaseMessages to convert. Can include any message\n * type: HumanMessage, AIMessage, SystemMessage, ToolMessage, FunctionMessage, etc.\n * @param params.model - Optional model name used to determine if special role mapping is needed.\n * For reasoning models (o1, o3, etc.), \"system\" role is converted to \"developer\" role.\n *\n * @returns Array of ChatCompletionMessageParam objects formatted for OpenAI's Chat Completions API.\n * Some messages may be split into multiple parameters (e.g., audio messages).\n *\n * @example\n * Basic message conversion:\n * ```typescript\n * const messages = [\n * new HumanMessage(\"What's the weather like?\"),\n * new AIMessage(\"Let me check that for you.\")\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({\n * messages,\n * model: \"gpt-4\"\n * });\n * // Returns:\n * // [\n * // { role: \"user\", content: \"What's the weather like?\" },\n * // { role: \"assistant\", content: \"Let me check that for you.\" }\n * // ]\n * ```\n *\n * @example\n * Message with tool calls:\n * ```typescript\n * const messages = [\n * new AIMessage({\n * content: \"\",\n * tool_calls: [{\n * id: \"call_123\",\n * name: \"get_weather\",\n * args: { location: \"San Francisco\" }\n * }]\n * })\n * ];\n *\n * const params = convertMessagesToCompletionsMessageParams({ messages });\n * // Returns:\n * // [{\n * // role: \"assistant\",\n * // content: \"\",\n * // tool_calls: [{\n * // id: \"call_123\",\n * // type: \"function\",\n * // function: { name: \"get_weather\", arguments: '{\"location\":\"San Francisco\"}' }\n * // }]\n * // }]\n * ```\n */\nexport declare const convertMessagesToCompletionsMessageParams: Converter<{\n messages: BaseMessage[];\n model?: string;\n}, OpenAIClient.Chat.Completions.ChatCompletionMess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AAAA,GACxB,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,0BAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgInB,yCAAA,EAA2C,SAAA;EACpD,QAAA,EAAU,WAAA;EAAe,KAAA;AAAA,GAC3B,QAAA,CAAa,IAAA,CAAK,WAAA,CAAY,0BAAA"}
|
|
@@ -23,16 +23,13 @@ const completionsApiContentBlockConverter = {
|
|
|
23
23
|
...block.metadata?.detail ? { detail: block.metadata.detail } : {}
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
if (block.source_type === "base64") {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
}
|
|
26
|
+
if (block.source_type === "base64") return {
|
|
27
|
+
type: "image_url",
|
|
28
|
+
image_url: {
|
|
29
|
+
url: `data:${block.mime_type ?? ""};base64,${block.data}`,
|
|
30
|
+
...block.metadata?.detail ? { detail: block.metadata.detail } : {}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
36
33
|
throw new Error(`Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
|
|
37
34
|
},
|
|
38
35
|
fromStandardAudioBlock(block) {
|
|
@@ -184,9 +181,8 @@ const convertCompletionsMessageToBaseMessage = ({ message, rawResponse, includeR
|
|
|
184
181
|
} : {}
|
|
185
182
|
};
|
|
186
183
|
if (message.audio) additional_kwargs.audio = message.audio;
|
|
187
|
-
const content = handleMultiModalOutput(message.content || "", rawResponse.choices?.[0]?.message);
|
|
188
184
|
return new AIMessage({
|
|
189
|
-
content,
|
|
185
|
+
content: handleMultiModalOutput(message.content || "", rawResponse.choices?.[0]?.message),
|
|
190
186
|
tool_calls: toolCalls,
|
|
191
187
|
invalid_tool_calls: invalidToolCalls,
|
|
192
188
|
additional_kwargs,
|
|
@@ -361,8 +357,8 @@ const convertStandardContentBlockToCompletionsContentPart = (block) => {
|
|
|
361
357
|
if (block.type === "audio") {
|
|
362
358
|
if (block.data) {
|
|
363
359
|
const format = iife(() => {
|
|
364
|
-
const [, format
|
|
365
|
-
if (format
|
|
360
|
+
const [, format] = block.mimeType.split("/");
|
|
361
|
+
if (format === "wav" || format === "mp3") return format;
|
|
366
362
|
return "wav";
|
|
367
363
|
});
|
|
368
364
|
return {
|
|
@@ -390,7 +386,6 @@ const convertStandardContentBlockToCompletionsContentPart = (block) => {
|
|
|
390
386
|
file: { file_id: block.fileId }
|
|
391
387
|
};
|
|
392
388
|
}
|
|
393
|
-
return void 0;
|
|
394
389
|
};
|
|
395
390
|
/**
|
|
396
391
|
* Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.
|
|
@@ -589,13 +584,10 @@ const convertMessagesToCompletionsMessageParams = ({ messages, model }) => {
|
|
|
589
584
|
if (message.additional_kwargs.tool_calls != null) completionParam.tool_calls = message.additional_kwargs.tool_calls;
|
|
590
585
|
if (ToolMessage.isInstance(message) && message.tool_call_id != null) completionParam.tool_call_id = message.tool_call_id;
|
|
591
586
|
}
|
|
592
|
-
if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) {
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
};
|
|
597
|
-
return [completionParam, audioMessage];
|
|
598
|
-
}
|
|
587
|
+
if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) return [completionParam, {
|
|
588
|
+
role: "assistant",
|
|
589
|
+
audio: { id: message.additional_kwargs.audio.id }
|
|
590
|
+
}];
|
|
599
591
|
return completionParam;
|
|
600
592
|
});
|
|
601
593
|
};
|