@langchain/openai 0.6.16 → 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/azure/chat_models.cjs +661 -808
- package/dist/azure/chat_models.cjs.map +1 -0
- package/dist/azure/chat_models.d.cts +449 -0
- package/dist/azure/chat_models.d.cts.map +1 -0
- package/dist/azure/chat_models.d.ts +32 -28
- package/dist/azure/chat_models.d.ts.map +1 -0
- package/dist/azure/chat_models.js +660 -804
- package/dist/azure/chat_models.js.map +1 -0
- package/dist/azure/embeddings.cjs +81 -130
- package/dist/azure/embeddings.cjs.map +1 -0
- package/dist/azure/embeddings.d.cts +27 -0
- package/dist/azure/embeddings.d.cts.map +1 -0
- package/dist/azure/embeddings.d.ts +25 -20
- package/dist/azure/embeddings.d.ts.map +1 -0
- package/dist/azure/embeddings.js +80 -126
- package/dist/azure/embeddings.js.map +1 -0
- package/dist/azure/llms.cjs +109 -168
- package/dist/azure/llms.cjs.map +1 -0
- package/dist/azure/llms.d.cts +33 -0
- package/dist/azure/llms.d.cts.map +1 -0
- package/dist/azure/llms.d.ts +31 -25
- package/dist/azure/llms.d.ts.map +1 -0
- package/dist/azure/llms.js +108 -164
- package/dist/azure/llms.js.map +1 -0
- package/dist/chat_models.cjs +1959 -2789
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +931 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +367 -351
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +1954 -2779
- package/dist/chat_models.js.map +1 -0
- package/dist/embeddings.cjs +140 -206
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +120 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +93 -86
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +138 -201
- package/dist/embeddings.js.map +1 -0
- package/dist/index.cjs +47 -34
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -13
- package/dist/index.js +17 -13
- package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
- package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
- package/dist/llms.cjs +312 -468
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +123 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +93 -87
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +309 -462
- package/dist/llms.js.map +1 -0
- package/dist/tools/custom.cjs +25 -26
- package/dist/tools/custom.cjs.map +1 -0
- package/dist/tools/custom.d.cts +10 -0
- package/dist/tools/custom.d.cts.map +1 -0
- package/dist/tools/custom.d.ts +8 -3
- package/dist/tools/custom.d.ts.map +1 -0
- package/dist/tools/custom.js +24 -23
- package/dist/tools/custom.js.map +1 -0
- package/dist/tools/dalle.cjs +104 -194
- package/dist/tools/dalle.cjs.map +1 -0
- package/dist/tools/dalle.d.cts +123 -0
- package/dist/tools/dalle.d.cts.map +1 -0
- package/dist/tools/dalle.d.ts +111 -104
- package/dist/tools/dalle.d.ts.map +1 -0
- package/dist/tools/dalle.js +102 -189
- package/dist/tools/dalle.js.map +1 -0
- package/dist/tools/index.cjs +1 -17
- package/dist/tools/index.js +1 -1
- package/dist/types.d.cts +263 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +244 -246
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/azure.cjs +86 -47
- package/dist/utils/azure.cjs.map +1 -0
- package/dist/utils/azure.d.cts +76 -0
- package/dist/utils/azure.d.cts.map +1 -0
- package/dist/utils/azure.d.ts +51 -9
- package/dist/utils/azure.d.ts.map +1 -0
- package/dist/utils/azure.js +85 -45
- package/dist/utils/azure.js.map +1 -0
- package/dist/utils/client.cjs +25 -0
- package/dist/utils/client.cjs.map +1 -0
- package/dist/utils/client.d.cts +5 -0
- package/dist/utils/client.d.cts.map +1 -0
- package/dist/utils/client.d.ts +5 -0
- package/dist/utils/client.d.ts.map +1 -0
- package/dist/utils/client.js +24 -0
- package/dist/utils/client.js.map +1 -0
- package/dist/utils/errors.cjs +9 -8
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/errors.js +9 -6
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/message_inputs.cjs +139 -0
- package/dist/utils/message_inputs.cjs.map +1 -0
- package/dist/utils/message_inputs.d.cts +9 -0
- package/dist/utils/message_inputs.d.cts.map +1 -0
- package/dist/utils/message_inputs.d.ts +9 -0
- package/dist/utils/message_inputs.d.ts.map +1 -0
- package/dist/utils/message_inputs.js +137 -0
- package/dist/utils/message_inputs.js.map +1 -0
- package/dist/utils/misc.cjs +32 -0
- package/dist/utils/misc.cjs.map +1 -0
- package/dist/utils/misc.d.cts +9 -0
- package/dist/utils/misc.d.cts.map +1 -0
- package/dist/utils/misc.d.ts +9 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +29 -0
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/output.cjs +106 -45
- package/dist/utils/output.cjs.map +1 -0
- package/dist/utils/output.js +104 -43
- package/dist/utils/output.js.map +1 -0
- package/dist/utils/prompts.cjs +39 -38
- package/dist/utils/prompts.cjs.map +1 -0
- package/dist/utils/prompts.d.cts +42 -0
- package/dist/utils/prompts.d.cts.map +1 -0
- package/dist/utils/prompts.d.ts +10 -4
- package/dist/utils/prompts.d.ts.map +1 -0
- package/dist/utils/prompts.js +40 -36
- package/dist/utils/prompts.js.map +1 -0
- package/dist/utils/standard.cjs +315 -0
- package/dist/utils/standard.cjs.map +1 -0
- package/dist/utils/standard.js +313 -0
- package/dist/utils/standard.js.map +1 -0
- package/dist/utils/tools.cjs +150 -127
- package/dist/utils/tools.cjs.map +1 -0
- package/dist/utils/tools.d.cts +13 -0
- package/dist/utils/tools.d.cts.map +1 -0
- package/dist/utils/tools.d.ts +12 -42
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +146 -123
- package/dist/utils/tools.js.map +1 -0
- package/package.json +51 -60
- package/dist/tools/index.d.ts +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -1
- package/dist/utils/errors.d.ts +0 -2
- package/dist/utils/headers.cjs +0 -39
- package/dist/utils/headers.d.ts +0 -8
- package/dist/utils/headers.js +0 -35
- package/dist/utils/openai-format-fndef.cjs +0 -80
- package/dist/utils/openai-format-fndef.d.ts +0 -44
- package/dist/utils/openai-format-fndef.js +0 -77
- package/dist/utils/openai.cjs +0 -127
- package/dist/utils/openai.d.ts +0 -15
- package/dist/utils/openai.js +0 -119
- package/dist/utils/output.d.ts +0 -11
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Formatting function definitions for calculating openai function defination token usage.
|
|
3
|
-
*
|
|
4
|
-
* https://github.com/hmarr/openai-chat-tokens/blob/main/src/functions.ts
|
|
5
|
-
* (c) 2023 Harry Marr
|
|
6
|
-
* MIT license
|
|
7
|
-
*/
|
|
8
|
-
import OpenAI from "openai";
|
|
9
|
-
type OpenAIFunction = OpenAI.Chat.ChatCompletionCreateParams.Function;
|
|
10
|
-
export interface FunctionDef extends Omit<OpenAIFunction, "parameters"> {
|
|
11
|
-
name: string;
|
|
12
|
-
description?: string;
|
|
13
|
-
parameters: ObjectProp;
|
|
14
|
-
}
|
|
15
|
-
interface ObjectProp {
|
|
16
|
-
type: "object";
|
|
17
|
-
properties?: {
|
|
18
|
-
[key: string]: Prop;
|
|
19
|
-
};
|
|
20
|
-
required?: string[];
|
|
21
|
-
}
|
|
22
|
-
interface AnyOfProp {
|
|
23
|
-
anyOf: Prop[];
|
|
24
|
-
}
|
|
25
|
-
type Prop = {
|
|
26
|
-
description?: string;
|
|
27
|
-
} & (AnyOfProp | ObjectProp | {
|
|
28
|
-
type: "string";
|
|
29
|
-
enum?: string[];
|
|
30
|
-
} | {
|
|
31
|
-
type: "number" | "integer";
|
|
32
|
-
minimum?: number;
|
|
33
|
-
maximum?: number;
|
|
34
|
-
enum?: number[];
|
|
35
|
-
} | {
|
|
36
|
-
type: "boolean";
|
|
37
|
-
} | {
|
|
38
|
-
type: "null";
|
|
39
|
-
} | {
|
|
40
|
-
type: "array";
|
|
41
|
-
items?: Prop;
|
|
42
|
-
});
|
|
43
|
-
export declare function formatFunctionDefinitions(functions: FunctionDef[]): string;
|
|
44
|
-
export {};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
function isAnyOfProp(prop) {
|
|
2
|
-
return (prop.anyOf !== undefined &&
|
|
3
|
-
Array.isArray(prop.anyOf));
|
|
4
|
-
}
|
|
5
|
-
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
|
|
6
|
-
// This function converts the JSON schemas into TypeScript definitions.
|
|
7
|
-
export function formatFunctionDefinitions(functions) {
|
|
8
|
-
const lines = ["namespace functions {", ""];
|
|
9
|
-
for (const f of functions) {
|
|
10
|
-
if (f.description) {
|
|
11
|
-
lines.push(`// ${f.description}`);
|
|
12
|
-
}
|
|
13
|
-
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
14
|
-
lines.push(`type ${f.name} = (_: {`);
|
|
15
|
-
lines.push(formatObjectProperties(f.parameters, 0));
|
|
16
|
-
lines.push("}) => any;");
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
lines.push(`type ${f.name} = () => any;`);
|
|
20
|
-
}
|
|
21
|
-
lines.push("");
|
|
22
|
-
}
|
|
23
|
-
lines.push("} // namespace functions");
|
|
24
|
-
return lines.join("\n");
|
|
25
|
-
}
|
|
26
|
-
// Format just the properties of an object (not including the surrounding braces)
|
|
27
|
-
function formatObjectProperties(obj, indent) {
|
|
28
|
-
const lines = [];
|
|
29
|
-
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
30
|
-
if (param.description && indent < 2) {
|
|
31
|
-
lines.push(`// ${param.description}`);
|
|
32
|
-
}
|
|
33
|
-
if (obj.required?.includes(name)) {
|
|
34
|
-
lines.push(`${name}: ${formatType(param, indent)},`);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
41
|
-
}
|
|
42
|
-
// Format a single property type
|
|
43
|
-
function formatType(param, indent) {
|
|
44
|
-
if (isAnyOfProp(param)) {
|
|
45
|
-
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
46
|
-
}
|
|
47
|
-
switch (param.type) {
|
|
48
|
-
case "string":
|
|
49
|
-
if (param.enum) {
|
|
50
|
-
return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
51
|
-
}
|
|
52
|
-
return "string";
|
|
53
|
-
case "number":
|
|
54
|
-
if (param.enum) {
|
|
55
|
-
return param.enum.map((v) => `${v}`).join(" | ");
|
|
56
|
-
}
|
|
57
|
-
return "number";
|
|
58
|
-
case "integer":
|
|
59
|
-
if (param.enum) {
|
|
60
|
-
return param.enum.map((v) => `${v}`).join(" | ");
|
|
61
|
-
}
|
|
62
|
-
return "number";
|
|
63
|
-
case "boolean":
|
|
64
|
-
return "boolean";
|
|
65
|
-
case "null":
|
|
66
|
-
return "null";
|
|
67
|
-
case "object":
|
|
68
|
-
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
|
|
69
|
-
case "array":
|
|
70
|
-
if (param.items) {
|
|
71
|
-
return `${formatType(param.items, indent)}[]`;
|
|
72
|
-
}
|
|
73
|
-
return "any[]";
|
|
74
|
-
default:
|
|
75
|
-
return "";
|
|
76
|
-
}
|
|
77
|
-
}
|
package/dist/utils/openai.cjs
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatToOpenAITool = exports.formatToOpenAIFunction = void 0;
|
|
4
|
-
exports.wrapOpenAIClientError = wrapOpenAIClientError;
|
|
5
|
-
exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
|
|
6
|
-
exports.formatToOpenAIToolChoice = formatToOpenAIToolChoice;
|
|
7
|
-
exports.interopZodResponseFormat = interopZodResponseFormat;
|
|
8
|
-
const openai_1 = require("openai");
|
|
9
|
-
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
10
|
-
Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return function_calling_1.convertToOpenAIFunction; } });
|
|
11
|
-
Object.defineProperty(exports, "formatToOpenAITool", { enumerable: true, get: function () { return function_calling_1.convertToOpenAITool; } });
|
|
12
|
-
const types_1 = require("@langchain/core/utils/types");
|
|
13
|
-
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
14
|
-
const core_1 = require("zod/v4/core");
|
|
15
|
-
const zod_1 = require("openai/helpers/zod");
|
|
16
|
-
const errors_js_1 = require("./errors.cjs");
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
function wrapOpenAIClientError(e) {
|
|
19
|
-
let error;
|
|
20
|
-
if (e.constructor.name === openai_1.APIConnectionTimeoutError.name) {
|
|
21
|
-
error = new Error(e.message);
|
|
22
|
-
error.name = "TimeoutError";
|
|
23
|
-
}
|
|
24
|
-
else if (e.constructor.name === openai_1.APIUserAbortError.name) {
|
|
25
|
-
error = new Error(e.message);
|
|
26
|
-
error.name = "AbortError";
|
|
27
|
-
}
|
|
28
|
-
else if (e.status === 400 && e.message.includes("tool_calls")) {
|
|
29
|
-
error = (0, errors_js_1.addLangChainErrorFields)(e, "INVALID_TOOL_RESULTS");
|
|
30
|
-
}
|
|
31
|
-
else if (e.status === 401) {
|
|
32
|
-
error = (0, errors_js_1.addLangChainErrorFields)(e, "MODEL_AUTHENTICATION");
|
|
33
|
-
}
|
|
34
|
-
else if (e.status === 429) {
|
|
35
|
-
error = (0, errors_js_1.addLangChainErrorFields)(e, "MODEL_RATE_LIMIT");
|
|
36
|
-
}
|
|
37
|
-
else if (e.status === 404) {
|
|
38
|
-
error = (0, errors_js_1.addLangChainErrorFields)(e, "MODEL_NOT_FOUND");
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
error = e;
|
|
42
|
-
}
|
|
43
|
-
return error;
|
|
44
|
-
}
|
|
45
|
-
function formatToOpenAIAssistantTool(tool) {
|
|
46
|
-
return {
|
|
47
|
-
type: "function",
|
|
48
|
-
function: {
|
|
49
|
-
name: tool.name,
|
|
50
|
-
description: tool.description,
|
|
51
|
-
parameters: (0, types_1.isInteropZodSchema)(tool.schema)
|
|
52
|
-
? (0, json_schema_1.toJsonSchema)(tool.schema)
|
|
53
|
-
: tool.schema,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
function formatToOpenAIToolChoice(toolChoice) {
|
|
58
|
-
if (!toolChoice) {
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
else if (toolChoice === "any" || toolChoice === "required") {
|
|
62
|
-
return "required";
|
|
63
|
-
}
|
|
64
|
-
else if (toolChoice === "auto") {
|
|
65
|
-
return "auto";
|
|
66
|
-
}
|
|
67
|
-
else if (toolChoice === "none") {
|
|
68
|
-
return "none";
|
|
69
|
-
}
|
|
70
|
-
else if (typeof toolChoice === "string") {
|
|
71
|
-
return {
|
|
72
|
-
type: "function",
|
|
73
|
-
function: {
|
|
74
|
-
name: toolChoice,
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
return toolChoice;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
// inlined from openai/lib/parser.ts
|
|
83
|
-
function makeParseableResponseFormat(response_format, parser) {
|
|
84
|
-
const obj = { ...response_format };
|
|
85
|
-
Object.defineProperties(obj, {
|
|
86
|
-
$brand: {
|
|
87
|
-
value: "auto-parseable-response-format",
|
|
88
|
-
enumerable: false,
|
|
89
|
-
},
|
|
90
|
-
$parseRaw: {
|
|
91
|
-
value: parser,
|
|
92
|
-
enumerable: false,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
return obj;
|
|
96
|
-
}
|
|
97
|
-
function interopZodResponseFormat(zodSchema, name, props) {
|
|
98
|
-
if ((0, types_1.isZodSchemaV3)(zodSchema)) {
|
|
99
|
-
return (0, zod_1.zodResponseFormat)(zodSchema, name, props);
|
|
100
|
-
}
|
|
101
|
-
if ((0, types_1.isZodSchemaV4)(zodSchema)) {
|
|
102
|
-
return makeParseableResponseFormat({
|
|
103
|
-
type: "json_schema",
|
|
104
|
-
json_schema: {
|
|
105
|
-
...props,
|
|
106
|
-
name,
|
|
107
|
-
strict: true,
|
|
108
|
-
schema: (0, core_1.toJSONSchema)(zodSchema, {
|
|
109
|
-
cycles: "ref", // equivalent to nameStrategy: 'duplicate-ref'
|
|
110
|
-
reused: "ref", // equivalent to $refStrategy: 'extract-to-root'
|
|
111
|
-
override(ctx) {
|
|
112
|
-
ctx.jsonSchema.title = name; // equivalent to `name` property
|
|
113
|
-
// TODO: implement `nullableStrategy` patch-fix (zod doesn't support openApi3 json schema target)
|
|
114
|
-
// TODO: implement `openaiStrictMode` patch-fix (where optional properties without `nullable` are not supported)
|
|
115
|
-
},
|
|
116
|
-
/// property equivalents from native `zodResponseFormat` fn
|
|
117
|
-
// openaiStrictMode: true,
|
|
118
|
-
// name,
|
|
119
|
-
// nameStrategy: 'duplicate-ref',
|
|
120
|
-
// $refStrategy: 'extract-to-root',
|
|
121
|
-
// nullableStrategy: 'property',
|
|
122
|
-
}),
|
|
123
|
-
},
|
|
124
|
-
}, (content) => (0, core_1.parse)(zodSchema, JSON.parse(content)));
|
|
125
|
-
}
|
|
126
|
-
throw new Error("Unsupported schema response format");
|
|
127
|
-
}
|
package/dist/utils/openai.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
-
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
3
|
-
import { convertToOpenAIFunction, convertToOpenAITool } from "@langchain/core/utils/function_calling";
|
|
4
|
-
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
5
|
-
import { InteropZodType } from "@langchain/core/utils/types";
|
|
6
|
-
import { ResponseFormatJSONSchema } from "openai/resources";
|
|
7
|
-
export declare function wrapOpenAIClientError(e: any): any;
|
|
8
|
-
export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
|
|
9
|
-
export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface): ToolDefinition;
|
|
10
|
-
export type OpenAIToolChoice = OpenAIClient.ChatCompletionToolChoiceOption | "any" | string;
|
|
11
|
-
export declare function formatToOpenAIToolChoice(toolChoice?: OpenAIToolChoice): OpenAIClient.ChatCompletionToolChoiceOption | undefined;
|
|
12
|
-
export declare function interopZodResponseFormat(zodSchema: InteropZodType, name: string, props: Omit<ResponseFormatJSONSchema.JSONSchema, "schema" | "strict" | "name">): {
|
|
13
|
-
json_schema: ResponseFormatJSONSchema.JSONSchema;
|
|
14
|
-
type: "json_schema";
|
|
15
|
-
};
|
package/dist/utils/openai.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
|
|
2
|
-
import { convertToOpenAIFunction, convertToOpenAITool, } from "@langchain/core/utils/function_calling";
|
|
3
|
-
import { isInteropZodSchema, isZodSchemaV3, isZodSchemaV4, } from "@langchain/core/utils/types";
|
|
4
|
-
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
5
|
-
import { toJSONSchema as toJSONSchemaV4, parse as parseV4 } from "zod/v4/core";
|
|
6
|
-
import { zodResponseFormat } from "openai/helpers/zod";
|
|
7
|
-
import { addLangChainErrorFields } from "./errors.js";
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export function wrapOpenAIClientError(e) {
|
|
10
|
-
let error;
|
|
11
|
-
if (e.constructor.name === APIConnectionTimeoutError.name) {
|
|
12
|
-
error = new Error(e.message);
|
|
13
|
-
error.name = "TimeoutError";
|
|
14
|
-
}
|
|
15
|
-
else if (e.constructor.name === APIUserAbortError.name) {
|
|
16
|
-
error = new Error(e.message);
|
|
17
|
-
error.name = "AbortError";
|
|
18
|
-
}
|
|
19
|
-
else if (e.status === 400 && e.message.includes("tool_calls")) {
|
|
20
|
-
error = addLangChainErrorFields(e, "INVALID_TOOL_RESULTS");
|
|
21
|
-
}
|
|
22
|
-
else if (e.status === 401) {
|
|
23
|
-
error = addLangChainErrorFields(e, "MODEL_AUTHENTICATION");
|
|
24
|
-
}
|
|
25
|
-
else if (e.status === 429) {
|
|
26
|
-
error = addLangChainErrorFields(e, "MODEL_RATE_LIMIT");
|
|
27
|
-
}
|
|
28
|
-
else if (e.status === 404) {
|
|
29
|
-
error = addLangChainErrorFields(e, "MODEL_NOT_FOUND");
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
error = e;
|
|
33
|
-
}
|
|
34
|
-
return error;
|
|
35
|
-
}
|
|
36
|
-
export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
|
|
37
|
-
export function formatToOpenAIAssistantTool(tool) {
|
|
38
|
-
return {
|
|
39
|
-
type: "function",
|
|
40
|
-
function: {
|
|
41
|
-
name: tool.name,
|
|
42
|
-
description: tool.description,
|
|
43
|
-
parameters: isInteropZodSchema(tool.schema)
|
|
44
|
-
? toJsonSchema(tool.schema)
|
|
45
|
-
: tool.schema,
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
export function formatToOpenAIToolChoice(toolChoice) {
|
|
50
|
-
if (!toolChoice) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
else if (toolChoice === "any" || toolChoice === "required") {
|
|
54
|
-
return "required";
|
|
55
|
-
}
|
|
56
|
-
else if (toolChoice === "auto") {
|
|
57
|
-
return "auto";
|
|
58
|
-
}
|
|
59
|
-
else if (toolChoice === "none") {
|
|
60
|
-
return "none";
|
|
61
|
-
}
|
|
62
|
-
else if (typeof toolChoice === "string") {
|
|
63
|
-
return {
|
|
64
|
-
type: "function",
|
|
65
|
-
function: {
|
|
66
|
-
name: toolChoice,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
return toolChoice;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// inlined from openai/lib/parser.ts
|
|
75
|
-
function makeParseableResponseFormat(response_format, parser) {
|
|
76
|
-
const obj = { ...response_format };
|
|
77
|
-
Object.defineProperties(obj, {
|
|
78
|
-
$brand: {
|
|
79
|
-
value: "auto-parseable-response-format",
|
|
80
|
-
enumerable: false,
|
|
81
|
-
},
|
|
82
|
-
$parseRaw: {
|
|
83
|
-
value: parser,
|
|
84
|
-
enumerable: false,
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
return obj;
|
|
88
|
-
}
|
|
89
|
-
export function interopZodResponseFormat(zodSchema, name, props) {
|
|
90
|
-
if (isZodSchemaV3(zodSchema)) {
|
|
91
|
-
return zodResponseFormat(zodSchema, name, props);
|
|
92
|
-
}
|
|
93
|
-
if (isZodSchemaV4(zodSchema)) {
|
|
94
|
-
return makeParseableResponseFormat({
|
|
95
|
-
type: "json_schema",
|
|
96
|
-
json_schema: {
|
|
97
|
-
...props,
|
|
98
|
-
name,
|
|
99
|
-
strict: true,
|
|
100
|
-
schema: toJSONSchemaV4(zodSchema, {
|
|
101
|
-
cycles: "ref", // equivalent to nameStrategy: 'duplicate-ref'
|
|
102
|
-
reused: "ref", // equivalent to $refStrategy: 'extract-to-root'
|
|
103
|
-
override(ctx) {
|
|
104
|
-
ctx.jsonSchema.title = name; // equivalent to `name` property
|
|
105
|
-
// TODO: implement `nullableStrategy` patch-fix (zod doesn't support openApi3 json schema target)
|
|
106
|
-
// TODO: implement `openaiStrictMode` patch-fix (where optional properties without `nullable` are not supported)
|
|
107
|
-
},
|
|
108
|
-
/// property equivalents from native `zodResponseFormat` fn
|
|
109
|
-
// openaiStrictMode: true,
|
|
110
|
-
// name,
|
|
111
|
-
// nameStrategy: 'duplicate-ref',
|
|
112
|
-
// $refStrategy: 'extract-to-root',
|
|
113
|
-
// nullableStrategy: 'property',
|
|
114
|
-
}),
|
|
115
|
-
},
|
|
116
|
-
}, (content) => parseV4(zodSchema, JSON.parse(content)));
|
|
117
|
-
}
|
|
118
|
-
throw new Error("Unsupported schema response format");
|
|
119
|
-
}
|
package/dist/utils/output.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
-
import { StandardImageBlock, StandardTextBlock, UsageMetadata } from "@langchain/core/messages";
|
|
3
|
-
/**
|
|
4
|
-
* Handle multi modal response content.
|
|
5
|
-
*
|
|
6
|
-
* @param content The content of the message.
|
|
7
|
-
* @param messages The messages of the response.
|
|
8
|
-
* @returns The new content of the message.
|
|
9
|
-
*/
|
|
10
|
-
export declare function handleMultiModalOutput(content: string, messages: unknown): (StandardImageBlock | StandardTextBlock)[] | string;
|
|
11
|
-
export declare function _convertOpenAIResponsesUsageToLangChainUsage(usage?: OpenAIClient.Responses.ResponseUsage): UsageMetadata;
|
package/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./dist/index.cjs');
|
package/index.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist/index.js'
|
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist/index.js'
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist/index.js'
|