@langchain/openai 0.0.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/chat_models.cjs +700 -0
- package/dist/chat_models.d.ts +141 -0
- package/dist/chat_models.js +695 -0
- package/dist/embeddings.cjs +243 -0
- package/dist/embeddings.d.ts +82 -0
- package/dist/embeddings.js +239 -0
- package/dist/index.cjs +26 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/legacy.cjs +448 -0
- package/dist/legacy.d.ts +124 -0
- package/dist/legacy.js +444 -0
- package/dist/llms.cjs +531 -0
- package/dist/llms.d.ts +126 -0
- package/dist/llms.js +527 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.js +1 -0
- package/dist/utils/azure.cjs +44 -0
- package/dist/utils/azure.d.ts +30 -0
- package/dist/utils/azure.js +40 -0
- package/dist/utils/chunk.cjs +11 -0
- package/dist/utils/chunk.d.ts +1 -0
- package/dist/utils/chunk.js +7 -0
- package/dist/utils/openai-format-fndef.cjs +81 -0
- package/dist/utils/openai-format-fndef.d.ts +44 -0
- package/dist/utils/openai-format-fndef.js +77 -0
- package/dist/utils/openai.cjs +59 -0
- package/dist/utils/openai.d.ts +12 -0
- package/dist/utils/openai.js +52 -0
- package/index.cjs +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +90 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEndpoint = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This function generates an endpoint URL for (Azure) OpenAI
|
|
6
|
+
* based on the configuration parameters provided.
|
|
7
|
+
*
|
|
8
|
+
* @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
|
|
11
|
+
* @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
|
|
12
|
+
* @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
|
|
13
|
+
* @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
|
|
14
|
+
* @property {string} config.baseURL - Some other custom base path URL.
|
|
15
|
+
*
|
|
16
|
+
* The function operates as follows:
|
|
17
|
+
* - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
|
|
18
|
+
* - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
|
|
19
|
+
* - If none of the above conditions are met, return any custom `baseURL`.
|
|
20
|
+
* - The function returns the generated URL as a string, or undefined if no custom paths are specified.
|
|
21
|
+
*
|
|
22
|
+
* @throws Will throw an error if the necessary parameters for generating the URL are missing.
|
|
23
|
+
*
|
|
24
|
+
* @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
|
|
25
|
+
*/
|
|
26
|
+
function getEndpoint(config) {
|
|
27
|
+
const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, } = config;
|
|
28
|
+
if (azureOpenAIApiKey &&
|
|
29
|
+
azureOpenAIBasePath &&
|
|
30
|
+
azureOpenAIApiDeploymentName) {
|
|
31
|
+
return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
|
|
32
|
+
}
|
|
33
|
+
if (azureOpenAIApiKey) {
|
|
34
|
+
if (!azureOpenAIApiInstanceName) {
|
|
35
|
+
throw new Error("azureOpenAIApiInstanceName is required when using azureOpenAIApiKey");
|
|
36
|
+
}
|
|
37
|
+
if (!azureOpenAIApiDeploymentName) {
|
|
38
|
+
throw new Error("azureOpenAIApiDeploymentName is a required parameter when using azureOpenAIApiKey");
|
|
39
|
+
}
|
|
40
|
+
return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`;
|
|
41
|
+
}
|
|
42
|
+
return baseURL;
|
|
43
|
+
}
|
|
44
|
+
exports.getEndpoint = getEndpoint;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface OpenAIEndpointConfig {
|
|
2
|
+
azureOpenAIApiDeploymentName?: string;
|
|
3
|
+
azureOpenAIApiInstanceName?: string;
|
|
4
|
+
azureOpenAIApiKey?: string;
|
|
5
|
+
azureOpenAIBasePath?: string;
|
|
6
|
+
baseURL?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* This function generates an endpoint URL for (Azure) OpenAI
|
|
10
|
+
* based on the configuration parameters provided.
|
|
11
|
+
*
|
|
12
|
+
* @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
|
|
13
|
+
*
|
|
14
|
+
* @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
|
|
15
|
+
* @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
|
|
16
|
+
* @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
|
|
17
|
+
* @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
|
|
18
|
+
* @property {string} config.baseURL - Some other custom base path URL.
|
|
19
|
+
*
|
|
20
|
+
* The function operates as follows:
|
|
21
|
+
* - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
|
|
22
|
+
* - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
|
|
23
|
+
* - If none of the above conditions are met, return any custom `baseURL`.
|
|
24
|
+
* - The function returns the generated URL as a string, or undefined if no custom paths are specified.
|
|
25
|
+
*
|
|
26
|
+
* @throws Will throw an error if the necessary parameters for generating the URL are missing.
|
|
27
|
+
*
|
|
28
|
+
* @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getEndpoint(config: OpenAIEndpointConfig): string | undefined;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function generates an endpoint URL for (Azure) OpenAI
|
|
3
|
+
* based on the configuration parameters provided.
|
|
4
|
+
*
|
|
5
|
+
* @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
|
|
6
|
+
*
|
|
7
|
+
* @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
|
|
8
|
+
* @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
|
|
9
|
+
* @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
|
|
10
|
+
* @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
|
|
11
|
+
* @property {string} config.baseURL - Some other custom base path URL.
|
|
12
|
+
*
|
|
13
|
+
* The function operates as follows:
|
|
14
|
+
* - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
|
|
15
|
+
* - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
|
|
16
|
+
* - If none of the above conditions are met, return any custom `baseURL`.
|
|
17
|
+
* - The function returns the generated URL as a string, or undefined if no custom paths are specified.
|
|
18
|
+
*
|
|
19
|
+
* @throws Will throw an error if the necessary parameters for generating the URL are missing.
|
|
20
|
+
*
|
|
21
|
+
* @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
|
|
22
|
+
*/
|
|
23
|
+
export function getEndpoint(config) {
|
|
24
|
+
const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, } = config;
|
|
25
|
+
if (azureOpenAIApiKey &&
|
|
26
|
+
azureOpenAIBasePath &&
|
|
27
|
+
azureOpenAIApiDeploymentName) {
|
|
28
|
+
return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
|
|
29
|
+
}
|
|
30
|
+
if (azureOpenAIApiKey) {
|
|
31
|
+
if (!azureOpenAIApiInstanceName) {
|
|
32
|
+
throw new Error("azureOpenAIApiInstanceName is required when using azureOpenAIApiKey");
|
|
33
|
+
}
|
|
34
|
+
if (!azureOpenAIApiDeploymentName) {
|
|
35
|
+
throw new Error("azureOpenAIApiDeploymentName is a required parameter when using azureOpenAIApiKey");
|
|
36
|
+
}
|
|
37
|
+
return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`;
|
|
38
|
+
}
|
|
39
|
+
return baseURL;
|
|
40
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.chunkArray = void 0;
|
|
4
|
+
const chunkArray = (arr, chunkSize) => arr.reduce((chunks, elem, index) => {
|
|
5
|
+
const chunkIndex = Math.floor(index / chunkSize);
|
|
6
|
+
const chunk = chunks[chunkIndex] || [];
|
|
7
|
+
// eslint-disable-next-line no-param-reassign
|
|
8
|
+
chunks[chunkIndex] = chunk.concat([elem]);
|
|
9
|
+
return chunks;
|
|
10
|
+
}, []);
|
|
11
|
+
exports.chunkArray = chunkArray;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const chunkArray: <T>(arr: T[], chunkSize: number) => T[][];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const chunkArray = (arr, chunkSize) => arr.reduce((chunks, elem, index) => {
|
|
2
|
+
const chunkIndex = Math.floor(index / chunkSize);
|
|
3
|
+
const chunk = chunks[chunkIndex] || [];
|
|
4
|
+
// eslint-disable-next-line no-param-reassign
|
|
5
|
+
chunks[chunkIndex] = chunk.concat([elem]);
|
|
6
|
+
return chunks;
|
|
7
|
+
}, []);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatFunctionDefinitions = void 0;
|
|
4
|
+
function isAnyOfProp(prop) {
|
|
5
|
+
return (prop.anyOf !== undefined &&
|
|
6
|
+
Array.isArray(prop.anyOf));
|
|
7
|
+
}
|
|
8
|
+
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
|
|
9
|
+
// This function converts the JSON schemas into TypeScript definitions.
|
|
10
|
+
function formatFunctionDefinitions(functions) {
|
|
11
|
+
const lines = ["namespace functions {", ""];
|
|
12
|
+
for (const f of functions) {
|
|
13
|
+
if (f.description) {
|
|
14
|
+
lines.push(`// ${f.description}`);
|
|
15
|
+
}
|
|
16
|
+
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
17
|
+
lines.push(`type ${f.name} = (_: {`);
|
|
18
|
+
lines.push(formatObjectProperties(f.parameters, 0));
|
|
19
|
+
lines.push("}) => any;");
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
lines.push(`type ${f.name} = () => any;`);
|
|
23
|
+
}
|
|
24
|
+
lines.push("");
|
|
25
|
+
}
|
|
26
|
+
lines.push("} // namespace functions");
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
|
29
|
+
exports.formatFunctionDefinitions = formatFunctionDefinitions;
|
|
30
|
+
// Format just the properties of an object (not including the surrounding braces)
|
|
31
|
+
function formatObjectProperties(obj, indent) {
|
|
32
|
+
const lines = [];
|
|
33
|
+
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
34
|
+
if (param.description && indent < 2) {
|
|
35
|
+
lines.push(`// ${param.description}`);
|
|
36
|
+
}
|
|
37
|
+
if (obj.required?.includes(name)) {
|
|
38
|
+
lines.push(`${name}: ${formatType(param, indent)},`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
45
|
+
}
|
|
46
|
+
// Format a single property type
|
|
47
|
+
function formatType(param, indent) {
|
|
48
|
+
if (isAnyOfProp(param)) {
|
|
49
|
+
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
50
|
+
}
|
|
51
|
+
switch (param.type) {
|
|
52
|
+
case "string":
|
|
53
|
+
if (param.enum) {
|
|
54
|
+
return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
55
|
+
}
|
|
56
|
+
return "string";
|
|
57
|
+
case "number":
|
|
58
|
+
if (param.enum) {
|
|
59
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
60
|
+
}
|
|
61
|
+
return "number";
|
|
62
|
+
case "integer":
|
|
63
|
+
if (param.enum) {
|
|
64
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
65
|
+
}
|
|
66
|
+
return "number";
|
|
67
|
+
case "boolean":
|
|
68
|
+
return "boolean";
|
|
69
|
+
case "null":
|
|
70
|
+
return "null";
|
|
71
|
+
case "object":
|
|
72
|
+
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
|
|
73
|
+
case "array":
|
|
74
|
+
if (param.items) {
|
|
75
|
+
return `${formatType(param.items, indent)}[]`;
|
|
76
|
+
}
|
|
77
|
+
return "any[]";
|
|
78
|
+
default:
|
|
79
|
+
return "";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
function wrapOpenAIClientError(e) {
|
|
8
|
+
let error;
|
|
9
|
+
if (e.constructor.name === openai_1.APIConnectionTimeoutError.name) {
|
|
10
|
+
error = new Error(e.message);
|
|
11
|
+
error.name = "TimeoutError";
|
|
12
|
+
}
|
|
13
|
+
else if (e.constructor.name === openai_1.APIUserAbortError.name) {
|
|
14
|
+
error = new Error(e.message);
|
|
15
|
+
error.name = "AbortError";
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
error = e;
|
|
19
|
+
}
|
|
20
|
+
return error;
|
|
21
|
+
}
|
|
22
|
+
exports.wrapOpenAIClientError = wrapOpenAIClientError;
|
|
23
|
+
/**
|
|
24
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
25
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
26
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
27
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
28
|
+
*/
|
|
29
|
+
function formatToOpenAIFunction(tool) {
|
|
30
|
+
return {
|
|
31
|
+
name: tool.name,
|
|
32
|
+
description: tool.description,
|
|
33
|
+
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.formatToOpenAIFunction = formatToOpenAIFunction;
|
|
37
|
+
function formatToOpenAITool(tool) {
|
|
38
|
+
const schema = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
|
|
39
|
+
return {
|
|
40
|
+
type: "function",
|
|
41
|
+
function: {
|
|
42
|
+
name: tool.name,
|
|
43
|
+
description: tool.description,
|
|
44
|
+
parameters: schema,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
exports.formatToOpenAITool = formatToOpenAITool;
|
|
49
|
+
function formatToOpenAIAssistantTool(tool) {
|
|
50
|
+
return {
|
|
51
|
+
type: "function",
|
|
52
|
+
function: {
|
|
53
|
+
name: tool.name,
|
|
54
|
+
description: tool.description,
|
|
55
|
+
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import { StructuredTool } from "@langchain/core/tools";
|
|
3
|
+
export declare function wrapOpenAIClientError(e: any): any;
|
|
4
|
+
/**
|
|
5
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
6
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
7
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
8
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function formatToOpenAIFunction(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionCreateParams.Function;
|
|
11
|
+
export declare function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionTool;
|
|
12
|
+
export declare function formatToOpenAIAssistantTool(tool: StructuredTool): OpenAIClient.Beta.AssistantCreateParams.AssistantToolsFunction;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
export function wrapOpenAIClientError(e) {
|
|
5
|
+
let error;
|
|
6
|
+
if (e.constructor.name === APIConnectionTimeoutError.name) {
|
|
7
|
+
error = new Error(e.message);
|
|
8
|
+
error.name = "TimeoutError";
|
|
9
|
+
}
|
|
10
|
+
else if (e.constructor.name === APIUserAbortError.name) {
|
|
11
|
+
error = new Error(e.message);
|
|
12
|
+
error.name = "AbortError";
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
error = e;
|
|
16
|
+
}
|
|
17
|
+
return error;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
21
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
22
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
23
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
24
|
+
*/
|
|
25
|
+
export function formatToOpenAIFunction(tool) {
|
|
26
|
+
return {
|
|
27
|
+
name: tool.name,
|
|
28
|
+
description: tool.description,
|
|
29
|
+
parameters: zodToJsonSchema(tool.schema),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function formatToOpenAITool(tool) {
|
|
33
|
+
const schema = zodToJsonSchema(tool.schema);
|
|
34
|
+
return {
|
|
35
|
+
type: "function",
|
|
36
|
+
function: {
|
|
37
|
+
name: tool.name,
|
|
38
|
+
description: tool.description,
|
|
39
|
+
parameters: schema,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function formatToOpenAIAssistantTool(tool) {
|
|
44
|
+
return {
|
|
45
|
+
type: "function",
|
|
46
|
+
function: {
|
|
47
|
+
name: tool.name,
|
|
48
|
+
description: tool.description,
|
|
49
|
+
parameters: zodToJsonSchema(tool.schema),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/index.cjs');
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js'
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js'
|
package/package.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@langchain/openai",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "OpenAI integrations for LangChain.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=18"
|
|
8
|
+
},
|
|
9
|
+
"main": "./index.js",
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git@github.com:langchain-ai/langchainjs.git"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts",
|
|
17
|
+
"build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/",
|
|
18
|
+
"build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs",
|
|
19
|
+
"build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
|
|
20
|
+
"build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
|
|
21
|
+
"lint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src && dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
22
|
+
"lint:fix": "yarn lint --fix",
|
|
23
|
+
"clean": "rimraf dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre",
|
|
24
|
+
"prepack": "yarn build",
|
|
25
|
+
"release": "release-it --only-version --config .release-it.json",
|
|
26
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
27
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
|
|
28
|
+
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
|
|
29
|
+
"format": "prettier --write \"src\"",
|
|
30
|
+
"format:check": "prettier --check \"src\""
|
|
31
|
+
},
|
|
32
|
+
"author": "LangChain",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@langchain/core": "^0.0.0",
|
|
36
|
+
"js-tiktoken": "^1.0.7",
|
|
37
|
+
"openai": "^4.19.0",
|
|
38
|
+
"zod-to-json-schema": "3.20.3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@jest/globals": "^29.5.0",
|
|
42
|
+
"@langchain/core": "workspace:*",
|
|
43
|
+
"@swc/core": "^1.3.90",
|
|
44
|
+
"@swc/jest": "^0.2.29",
|
|
45
|
+
"dpdm": "^3.12.0",
|
|
46
|
+
"eslint": "^8.33.0",
|
|
47
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
48
|
+
"eslint-config-prettier": "^8.6.0",
|
|
49
|
+
"eslint-plugin-import": "^2.27.5",
|
|
50
|
+
"eslint-plugin-no-instanceof": "^1.0.1",
|
|
51
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
52
|
+
"jest": "^29.5.0",
|
|
53
|
+
"jest-environment-node": "^29.6.4",
|
|
54
|
+
"prettier": "^2.8.3",
|
|
55
|
+
"release-it": "^15.10.1",
|
|
56
|
+
"rimraf": "^5.0.1",
|
|
57
|
+
"typescript": "^5.0.0"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"keywords": [
|
|
63
|
+
"llm",
|
|
64
|
+
"ai",
|
|
65
|
+
"gpt3",
|
|
66
|
+
"chain",
|
|
67
|
+
"prompt",
|
|
68
|
+
"prompt engineering",
|
|
69
|
+
"chatgpt",
|
|
70
|
+
"machine learning",
|
|
71
|
+
"ml",
|
|
72
|
+
"openai",
|
|
73
|
+
"embeddings",
|
|
74
|
+
"vectorstores"
|
|
75
|
+
],
|
|
76
|
+
"exports": {
|
|
77
|
+
".": {
|
|
78
|
+
"types": "./index.d.ts",
|
|
79
|
+
"import": "./index.js",
|
|
80
|
+
"require": "./index.cjs"
|
|
81
|
+
},
|
|
82
|
+
"./package.json": "./package.json"
|
|
83
|
+
},
|
|
84
|
+
"files": [
|
|
85
|
+
"dist/",
|
|
86
|
+
"index.cjs",
|
|
87
|
+
"index.js",
|
|
88
|
+
"index.d.ts"
|
|
89
|
+
]
|
|
90
|
+
}
|