@langchain/openai 0.5.4 → 0.5.6
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/dist/azure/embeddings.cjs +6 -1
- package/dist/azure/embeddings.js +6 -1
- package/dist/chat_models.cjs +6 -4
- package/dist/chat_models.js +6 -4
- package/dist/types.d.ts +2 -12
- package/dist/utils/openai.cjs +5 -2
- package/dist/utils/openai.d.ts +2 -13
- package/dist/utils/openai.js +5 -2
- package/dist/utils/tools.cjs +2 -24
- package/dist/utils/tools.js +3 -25
- package/package.json +3 -3
|
@@ -116,7 +116,12 @@ class AzureOpenAIEmbeddings extends embeddings_js_1.OpenAIEmbeddings {
|
|
|
116
116
|
}
|
|
117
117
|
return this.caller.call(async () => {
|
|
118
118
|
try {
|
|
119
|
-
const res = await this.client.embeddings.create(request,
|
|
119
|
+
const res = await this.client.embeddings.create(request,
|
|
120
|
+
// This unknown cast is required because OpenAI types seem to be incorrect here
|
|
121
|
+
// without it, we can't pass arbitrary keys via the `query` field in the options,
|
|
122
|
+
// which means we can't specify the `api-version` as required by Azure OpenAI.
|
|
123
|
+
// See https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
|
|
124
|
+
requestOptions);
|
|
120
125
|
return res;
|
|
121
126
|
}
|
|
122
127
|
catch (e) {
|
package/dist/azure/embeddings.js
CHANGED
|
@@ -113,7 +113,12 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
|
|
|
113
113
|
}
|
|
114
114
|
return this.caller.call(async () => {
|
|
115
115
|
try {
|
|
116
|
-
const res = await this.client.embeddings.create(request,
|
|
116
|
+
const res = await this.client.embeddings.create(request,
|
|
117
|
+
// This unknown cast is required because OpenAI types seem to be incorrect here
|
|
118
|
+
// without it, we can't pass arbitrary keys via the `query` field in the options,
|
|
119
|
+
// which means we can't specify the `api-version` as required by Azure OpenAI.
|
|
120
|
+
// See https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
|
|
121
|
+
requestOptions);
|
|
117
122
|
return res;
|
|
118
123
|
}
|
|
119
124
|
catch (e) {
|
package/dist/chat_models.cjs
CHANGED
|
@@ -201,7 +201,6 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
|
|
|
201
201
|
name: toolCall.name,
|
|
202
202
|
arguments: JSON.stringify(toolCall.args),
|
|
203
203
|
call_id: toolCall.id,
|
|
204
|
-
// @ts-expect-error Might come from a non-Responses API message
|
|
205
204
|
id: functionCallIds?.[toolCall.id],
|
|
206
205
|
})));
|
|
207
206
|
}
|
|
@@ -210,7 +209,6 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
|
|
|
210
209
|
type: "function_call",
|
|
211
210
|
name: toolCall.function.name,
|
|
212
211
|
call_id: toolCall.id,
|
|
213
|
-
// @ts-expect-error Might come from a non-Responses API message
|
|
214
212
|
id: functionCallIds?.[toolCall.id],
|
|
215
213
|
arguments: toolCall.function.arguments,
|
|
216
214
|
})));
|
|
@@ -322,7 +320,9 @@ function _convertOpenAIResponsesMessageToBaseMessage(response) {
|
|
|
322
320
|
invalid_tool_calls.push((0, openai_tools_1.makeInvalidToolCall)(fnAdapter, errMessage));
|
|
323
321
|
}
|
|
324
322
|
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
|
|
325
|
-
|
|
323
|
+
if (item.id) {
|
|
324
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
|
|
325
|
+
}
|
|
326
326
|
}
|
|
327
327
|
else if (item.type === "reasoning") {
|
|
328
328
|
additional_kwargs.reasoning = item;
|
|
@@ -466,7 +466,9 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
|
466
466
|
return (0, tools_js_1._convertToOpenAITool)(tool, fields);
|
|
467
467
|
}
|
|
468
468
|
function isReasoningModel(model) {
|
|
469
|
-
return model?.startsWith("o1") ||
|
|
469
|
+
return (model?.startsWith("o1") ||
|
|
470
|
+
model?.startsWith("o3") ||
|
|
471
|
+
model?.startsWith("o4"));
|
|
470
472
|
}
|
|
471
473
|
/**
|
|
472
474
|
* OpenAI chat model integration.
|
package/dist/chat_models.js
CHANGED
|
@@ -196,7 +196,6 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
|
|
|
196
196
|
name: toolCall.name,
|
|
197
197
|
arguments: JSON.stringify(toolCall.args),
|
|
198
198
|
call_id: toolCall.id,
|
|
199
|
-
// @ts-expect-error Might come from a non-Responses API message
|
|
200
199
|
id: functionCallIds?.[toolCall.id],
|
|
201
200
|
})));
|
|
202
201
|
}
|
|
@@ -205,7 +204,6 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
|
|
|
205
204
|
type: "function_call",
|
|
206
205
|
name: toolCall.function.name,
|
|
207
206
|
call_id: toolCall.id,
|
|
208
|
-
// @ts-expect-error Might come from a non-Responses API message
|
|
209
207
|
id: functionCallIds?.[toolCall.id],
|
|
210
208
|
arguments: toolCall.function.arguments,
|
|
211
209
|
})));
|
|
@@ -317,7 +315,9 @@ function _convertOpenAIResponsesMessageToBaseMessage(response) {
|
|
|
317
315
|
invalid_tool_calls.push(makeInvalidToolCall(fnAdapter, errMessage));
|
|
318
316
|
}
|
|
319
317
|
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
|
|
320
|
-
|
|
318
|
+
if (item.id) {
|
|
319
|
+
additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
|
|
320
|
+
}
|
|
321
321
|
}
|
|
322
322
|
else if (item.type === "reasoning") {
|
|
323
323
|
additional_kwargs.reasoning = item;
|
|
@@ -461,7 +461,9 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
|
|
|
461
461
|
return _convertToOpenAITool(tool, fields);
|
|
462
462
|
}
|
|
463
463
|
function isReasoningModel(model) {
|
|
464
|
-
return model?.startsWith("o1") ||
|
|
464
|
+
return (model?.startsWith("o1") ||
|
|
465
|
+
model?.startsWith("o3") ||
|
|
466
|
+
model?.startsWith("o4"));
|
|
465
467
|
}
|
|
466
468
|
/**
|
|
467
469
|
* OpenAI chat model integration.
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import type { RequestOptions as _OpenAICoreRequestOptions } from "openai/core";
|
|
2
3
|
import type { ResponseFormatText, ResponseFormatJSONObject, ResponseFormatJSONSchema } from "openai/resources/shared";
|
|
3
4
|
import { TiktokenModel } from "js-tiktoken/lite";
|
|
4
5
|
import type { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
|
|
@@ -73,18 +74,7 @@ export declare interface OpenAIBaseInput {
|
|
|
73
74
|
*/
|
|
74
75
|
apiKey?: string;
|
|
75
76
|
}
|
|
76
|
-
export type OpenAICoreRequestOptions<Req
|
|
77
|
-
path?: string;
|
|
78
|
-
query?: Req | undefined;
|
|
79
|
-
body?: Req | undefined;
|
|
80
|
-
headers?: Record<string, string | null | undefined> | undefined;
|
|
81
|
-
maxRetries?: number;
|
|
82
|
-
stream?: boolean | undefined;
|
|
83
|
-
timeout?: number;
|
|
84
|
-
httpAgent?: any;
|
|
85
|
-
signal?: AbortSignal | undefined | null;
|
|
86
|
-
idempotencyKey?: string;
|
|
87
|
-
};
|
|
77
|
+
export type OpenAICoreRequestOptions<Req = Record<string, unknown>> = _OpenAICoreRequestOptions<Req>;
|
|
88
78
|
export interface OpenAICallOptions extends BaseLanguageModelCallOptions {
|
|
89
79
|
/**
|
|
90
80
|
* Additional options to pass to the underlying axios request.
|
package/dist/utils/openai.cjs
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatToOpenAIToolChoice = exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
|
|
4
4
|
const openai_1 = require("openai");
|
|
5
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
6
5
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
7
6
|
Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return function_calling_1.convertToOpenAIFunction; } });
|
|
8
7
|
Object.defineProperty(exports, "formatToOpenAITool", { enumerable: true, get: function () { return function_calling_1.convertToOpenAITool; } });
|
|
8
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
9
|
+
const types_1 = require("@langchain/core/utils/types");
|
|
9
10
|
const errors_js_1 = require("./errors.cjs");
|
|
10
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
12
|
function wrapOpenAIClientError(e) {
|
|
@@ -42,7 +43,9 @@ function formatToOpenAIAssistantTool(tool) {
|
|
|
42
43
|
function: {
|
|
43
44
|
name: tool.name,
|
|
44
45
|
description: tool.description,
|
|
45
|
-
parameters: (0,
|
|
46
|
+
parameters: (0, types_1.isZodSchema)(tool.schema)
|
|
47
|
+
? (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema)
|
|
48
|
+
: tool.schema,
|
|
46
49
|
},
|
|
47
50
|
};
|
|
48
51
|
}
|
package/dist/utils/openai.d.ts
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
import { OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
3
3
|
import { convertToOpenAIFunction, convertToOpenAITool } from "@langchain/core/utils/function_calling";
|
|
4
|
+
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
4
5
|
export declare function wrapOpenAIClientError(e: any): any;
|
|
5
6
|
export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
|
|
6
|
-
export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface):
|
|
7
|
-
type: string;
|
|
8
|
-
function: {
|
|
9
|
-
name: string;
|
|
10
|
-
description: string;
|
|
11
|
-
parameters: import("zod-to-json-schema").JsonSchema7Type & {
|
|
12
|
-
$schema?: string | undefined;
|
|
13
|
-
definitions?: {
|
|
14
|
-
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
15
|
-
} | undefined;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
};
|
|
7
|
+
export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface): ToolDefinition;
|
|
19
8
|
export type OpenAIToolChoice = OpenAIClient.ChatCompletionToolChoiceOption | "any" | string;
|
|
20
9
|
export declare function formatToOpenAIToolChoice(toolChoice?: OpenAIToolChoice): OpenAIClient.ChatCompletionToolChoiceOption | undefined;
|
package/dist/utils/openai.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
|
|
2
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
2
|
import { convertToOpenAIFunction, convertToOpenAITool, } from "@langchain/core/utils/function_calling";
|
|
3
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
|
+
import { isZodSchema } from "@langchain/core/utils/types";
|
|
4
5
|
import { addLangChainErrorFields } from "./errors.js";
|
|
5
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
7
|
export function wrapOpenAIClientError(e) {
|
|
@@ -37,7 +38,9 @@ export function formatToOpenAIAssistantTool(tool) {
|
|
|
37
38
|
function: {
|
|
38
39
|
name: tool.name,
|
|
39
40
|
description: tool.description,
|
|
40
|
-
parameters:
|
|
41
|
+
parameters: isZodSchema(tool.schema)
|
|
42
|
+
? zodToJsonSchema(tool.schema)
|
|
43
|
+
: tool.schema,
|
|
41
44
|
},
|
|
42
45
|
};
|
|
43
46
|
}
|
package/dist/utils/tools.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._convertToOpenAITool = void 0;
|
|
4
4
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
5
|
-
const
|
|
5
|
+
const openai_js_1 = require("./openai.cjs");
|
|
6
6
|
/**
|
|
7
7
|
* Formats a tool in either OpenAI format, or LangChain structured tool format
|
|
8
8
|
* into an OpenAI tool format. If the tool is already in OpenAI format, return without
|
|
@@ -19,29 +19,7 @@ function _convertToOpenAITool(
|
|
|
19
19
|
tool, fields) {
|
|
20
20
|
let toolDef;
|
|
21
21
|
if ((0, function_calling_1.isLangChainTool)(tool)) {
|
|
22
|
-
|
|
23
|
-
name: tool.name,
|
|
24
|
-
parameters: tool.schema,
|
|
25
|
-
description: tool.description,
|
|
26
|
-
});
|
|
27
|
-
if (!oaiToolDef.function.parameters) {
|
|
28
|
-
// Fallback to the `convertToOpenAIFunction` util if the parameters are not defined.
|
|
29
|
-
toolDef = {
|
|
30
|
-
type: "function",
|
|
31
|
-
function: (0, function_calling_1.convertToOpenAIFunction)(tool, fields),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
toolDef = {
|
|
36
|
-
type: oaiToolDef.type,
|
|
37
|
-
function: {
|
|
38
|
-
name: oaiToolDef.function.name,
|
|
39
|
-
description: oaiToolDef.function.description,
|
|
40
|
-
parameters: oaiToolDef.function.parameters,
|
|
41
|
-
...(fields?.strict !== undefined ? { strict: fields.strict } : {}),
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
}
|
|
22
|
+
toolDef = (0, openai_js_1.formatToOpenAITool)(tool);
|
|
45
23
|
}
|
|
46
24
|
else {
|
|
47
25
|
toolDef = tool;
|
package/dist/utils/tools.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
2
|
+
import { formatToOpenAITool } from "./openai.js";
|
|
3
3
|
/**
|
|
4
4
|
* Formats a tool in either OpenAI format, or LangChain structured tool format
|
|
5
5
|
* into an OpenAI tool format. If the tool is already in OpenAI format, return without
|
|
@@ -16,29 +16,7 @@ export function _convertToOpenAITool(
|
|
|
16
16
|
tool, fields) {
|
|
17
17
|
let toolDef;
|
|
18
18
|
if (isLangChainTool(tool)) {
|
|
19
|
-
|
|
20
|
-
name: tool.name,
|
|
21
|
-
parameters: tool.schema,
|
|
22
|
-
description: tool.description,
|
|
23
|
-
});
|
|
24
|
-
if (!oaiToolDef.function.parameters) {
|
|
25
|
-
// Fallback to the `convertToOpenAIFunction` util if the parameters are not defined.
|
|
26
|
-
toolDef = {
|
|
27
|
-
type: "function",
|
|
28
|
-
function: convertToOpenAIFunction(tool, fields),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
toolDef = {
|
|
33
|
-
type: oaiToolDef.type,
|
|
34
|
-
function: {
|
|
35
|
-
name: oaiToolDef.function.name,
|
|
36
|
-
description: oaiToolDef.function.description,
|
|
37
|
-
parameters: oaiToolDef.function.parameters,
|
|
38
|
-
...(fields?.strict !== undefined ? { strict: fields.strict } : {}),
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
}
|
|
19
|
+
toolDef = formatToOpenAITool(tool);
|
|
42
20
|
}
|
|
43
21
|
else {
|
|
44
22
|
toolDef = tool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"js-tiktoken": "^1.0.12",
|
|
39
|
-
"openai": "^4.
|
|
39
|
+
"openai": "^4.93.0",
|
|
40
40
|
"zod": "^3.22.4",
|
|
41
41
|
"zod-to-json-schema": "^3.22.3"
|
|
42
42
|
},
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"jest": "^29.5.0",
|
|
63
63
|
"jest-environment-node": "^29.6.4",
|
|
64
64
|
"prettier": "^2.8.3",
|
|
65
|
-
"release-it": "^
|
|
65
|
+
"release-it": "^18.1.2",
|
|
66
66
|
"rimraf": "^5.0.1",
|
|
67
67
|
"ts-jest": "^29.1.0",
|
|
68
68
|
"typescript": "~5.1.6"
|