@langchain/openai 0.2.3 → 0.2.4
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/chat_models.cjs +1 -1
- package/dist/chat_models.d.ts +2 -1
- package/dist/chat_models.js +2 -2
- package/dist/utils/openai.cjs +27 -1
- package/dist/utils/openai.d.ts +3 -0
- package/dist/utils/openai.js +26 -1
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -568,7 +568,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
568
568
|
tools: isStructuredToolArray(options?.tools)
|
|
569
569
|
? options?.tools.map(function_calling_1.convertToOpenAITool)
|
|
570
570
|
: options?.tools,
|
|
571
|
-
tool_choice: options?.tool_choice,
|
|
571
|
+
tool_choice: (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice),
|
|
572
572
|
response_format: options?.response_format,
|
|
573
573
|
seed: options?.seed,
|
|
574
574
|
...streamOptionsConfig,
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputM
|
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import { Runnable, RunnableToolLike } from "@langchain/core/runnables";
|
|
10
10
|
import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
|
|
11
|
+
import { OpenAIToolChoice } from "./utils/openai.js";
|
|
11
12
|
export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
|
|
12
13
|
interface TokenUsage {
|
|
13
14
|
completionTokens?: number;
|
|
@@ -21,7 +22,7 @@ type OpenAIRoleEnum = "system" | "assistant" | "user" | "function" | "tool";
|
|
|
21
22
|
export declare function messageToOpenAIRole(message: BaseMessage): OpenAIRoleEnum;
|
|
22
23
|
export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
|
|
23
24
|
tools?: StructuredToolInterface[] | OpenAIClient.ChatCompletionTool[];
|
|
24
|
-
tool_choice?:
|
|
25
|
+
tool_choice?: OpenAIToolChoice;
|
|
25
26
|
promptIndex?: number;
|
|
26
27
|
response_format?: {
|
|
27
28
|
type: "json_object";
|
package/dist/chat_models.js
CHANGED
|
@@ -9,7 +9,7 @@ import { JsonOutputParser, StructuredOutputParser, } from "@langchain/core/outpu
|
|
|
9
9
|
import { JsonOutputKeyToolsParser, convertLangChainToolCallToOpenAI, makeInvalidToolCall, parseToolCall, } from "@langchain/core/output_parsers/openai_tools";
|
|
10
10
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
11
11
|
import { getEndpoint } from "./utils/azure.js";
|
|
12
|
-
import { wrapOpenAIClientError } from "./utils/openai.js";
|
|
12
|
+
import { formatToOpenAIToolChoice, wrapOpenAIClientError, } from "./utils/openai.js";
|
|
13
13
|
import { formatFunctionDefinitions, } from "./utils/openai-format-fndef.js";
|
|
14
14
|
function extractGenericMessageCustomRole(message) {
|
|
15
15
|
if (message.role !== "system" &&
|
|
@@ -564,7 +564,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
564
564
|
tools: isStructuredToolArray(options?.tools)
|
|
565
565
|
? options?.tools.map(convertToOpenAITool)
|
|
566
566
|
: options?.tools,
|
|
567
|
-
tool_choice: options?.tool_choice,
|
|
567
|
+
tool_choice: formatToOpenAIToolChoice(options?.tool_choice),
|
|
568
568
|
response_format: options?.response_format,
|
|
569
569
|
seed: options?.seed,
|
|
570
570
|
...streamOptionsConfig,
|
package/dist/utils/openai.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
|
|
3
|
+
exports.formatToOpenAIToolChoice = exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
|
|
4
4
|
const openai_1 = require("openai");
|
|
5
5
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
6
6
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
@@ -34,3 +34,29 @@ function formatToOpenAIAssistantTool(tool) {
|
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
|
|
37
|
+
function formatToOpenAIToolChoice(toolChoice) {
|
|
38
|
+
if (!toolChoice) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
else if (toolChoice === "any" || toolChoice === "required") {
|
|
42
|
+
return "required";
|
|
43
|
+
}
|
|
44
|
+
else if (toolChoice === "auto") {
|
|
45
|
+
return "auto";
|
|
46
|
+
}
|
|
47
|
+
else if (toolChoice === "none") {
|
|
48
|
+
return "none";
|
|
49
|
+
}
|
|
50
|
+
else if (typeof toolChoice === "string") {
|
|
51
|
+
return {
|
|
52
|
+
type: "function",
|
|
53
|
+
function: {
|
|
54
|
+
name: toolChoice,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return toolChoice;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.formatToOpenAIToolChoice = formatToOpenAIToolChoice;
|
package/dist/utils/openai.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
1
2
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
3
|
import { convertToOpenAIFunction, convertToOpenAITool } from "@langchain/core/utils/function_calling";
|
|
3
4
|
export declare function wrapOpenAIClientError(e: any): any;
|
|
@@ -15,3 +16,5 @@ export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterfac
|
|
|
15
16
|
};
|
|
16
17
|
};
|
|
17
18
|
};
|
|
19
|
+
export type OpenAIToolChoice = OpenAIClient.ChatCompletionToolChoiceOption | "any" | string;
|
|
20
|
+
export declare function formatToOpenAIToolChoice(toolChoice?: OpenAIToolChoice): OpenAIClient.ChatCompletionToolChoiceOption | undefined;
|
package/dist/utils/openai.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIConnectionTimeoutError, APIUserAbortError } from "openai";
|
|
1
|
+
import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
|
|
2
2
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
3
|
import { convertToOpenAIFunction, convertToOpenAITool, } from "@langchain/core/utils/function_calling";
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -28,3 +28,28 @@ export function formatToOpenAIAssistantTool(tool) {
|
|
|
28
28
|
},
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
+
export function formatToOpenAIToolChoice(toolChoice) {
|
|
32
|
+
if (!toolChoice) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
else if (toolChoice === "any" || toolChoice === "required") {
|
|
36
|
+
return "required";
|
|
37
|
+
}
|
|
38
|
+
else if (toolChoice === "auto") {
|
|
39
|
+
return "auto";
|
|
40
|
+
}
|
|
41
|
+
else if (toolChoice === "none") {
|
|
42
|
+
return "none";
|
|
43
|
+
}
|
|
44
|
+
else if (typeof toolChoice === "string") {
|
|
45
|
+
return {
|
|
46
|
+
type: "function",
|
|
47
|
+
function: {
|
|
48
|
+
name: toolChoice,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return toolChoice;
|
|
54
|
+
}
|
|
55
|
+
}
|