@langchain/openai 0.2.6 → 0.2.8

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.
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._convertToOpenAITool = void 0;
4
+ const function_calling_1 = require("@langchain/core/utils/function_calling");
5
+ const zod_1 = require("openai/helpers/zod");
6
+ /**
7
+ * Formats a tool in either OpenAI format, or LangChain structured tool format
8
+ * into an OpenAI tool format. If the tool is already in OpenAI format, return without
9
+ * any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format
10
+ * using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters
11
+ * returned from the `zodFunction` util are not defined.
12
+ *
13
+ * @param {BindToolsInput} tool The tool to convert to an OpenAI tool.
14
+ * @param {Object} [fields] Additional fields to add to the OpenAI tool.
15
+ * @returns {ToolDefinition} The inputted tool in OpenAI tool format.
16
+ */
17
+ function _convertToOpenAITool(
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ tool, fields) {
20
+ let toolDef;
21
+ if ((0, function_calling_1.isLangChainTool)(tool)) {
22
+ const oaiToolDef = (0, zod_1.zodFunction)({
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
+ }
45
+ }
46
+ else {
47
+ toolDef = tool;
48
+ }
49
+ if (fields?.strict !== undefined) {
50
+ toolDef.function.strict = fields.strict;
51
+ }
52
+ return toolDef;
53
+ }
54
+ exports._convertToOpenAITool = _convertToOpenAITool;
@@ -0,0 +1,20 @@
1
+ import { ToolDefinition } from "@langchain/core/language_models/base";
2
+ import { BindToolsInput } from "@langchain/core/language_models/chat_models";
3
+ /**
4
+ * Formats a tool in either OpenAI format, or LangChain structured tool format
5
+ * into an OpenAI tool format. If the tool is already in OpenAI format, return without
6
+ * any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format
7
+ * using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters
8
+ * returned from the `zodFunction` util are not defined.
9
+ *
10
+ * @param {BindToolsInput} tool The tool to convert to an OpenAI tool.
11
+ * @param {Object} [fields] Additional fields to add to the OpenAI tool.
12
+ * @returns {ToolDefinition} The inputted tool in OpenAI tool format.
13
+ */
14
+ export declare function _convertToOpenAITool(tool: BindToolsInput, fields?: {
15
+ /**
16
+ * If `true`, model output is guaranteed to exactly match the JSON Schema
17
+ * provided in the function definition.
18
+ */
19
+ strict?: boolean;
20
+ }): ToolDefinition;
@@ -0,0 +1,50 @@
1
+ import { convertToOpenAIFunction, isLangChainTool, } from "@langchain/core/utils/function_calling";
2
+ import { zodFunction } from "openai/helpers/zod";
3
+ /**
4
+ * Formats a tool in either OpenAI format, or LangChain structured tool format
5
+ * into an OpenAI tool format. If the tool is already in OpenAI format, return without
6
+ * any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format
7
+ * using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters
8
+ * returned from the `zodFunction` util are not defined.
9
+ *
10
+ * @param {BindToolsInput} tool The tool to convert to an OpenAI tool.
11
+ * @param {Object} [fields] Additional fields to add to the OpenAI tool.
12
+ * @returns {ToolDefinition} The inputted tool in OpenAI tool format.
13
+ */
14
+ export function _convertToOpenAITool(
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ tool, fields) {
17
+ let toolDef;
18
+ if (isLangChainTool(tool)) {
19
+ const oaiToolDef = zodFunction({
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
+ }
42
+ }
43
+ else {
44
+ toolDef = tool;
45
+ }
46
+ if (fields?.strict !== undefined) {
47
+ toolDef.function.strict = fields.strict;
48
+ }
49
+ return toolDef;
50
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -15,7 +15,7 @@
15
15
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-openai/",
16
16
  "scripts": {
17
17
  "build": "yarn turbo:command build:internal --filter=@langchain/openai",
18
- "build:internal": "yarn lc_build_v2 --create-entrypoints --pre --tree-shaking",
18
+ "build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
19
19
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
20
20
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
21
21
  "lint": "yarn lint:eslint && yarn lint:dpdm",
@@ -35,7 +35,7 @@
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@langchain/core": ">=0.2.21 <0.3.0",
38
+ "@langchain/core": ">=0.2.26 <0.3.0",
39
39
  "js-tiktoken": "^1.0.12",
40
40
  "openai": "^4.55.0",
41
41
  "zod": "^3.22.4",
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@azure/identity": "^4.2.1",
46
46
  "@jest/globals": "^29.5.0",
47
- "@langchain/scripts": "~0.0.20",
47
+ "@langchain/scripts": ">=0.1.0 <0.2.0",
48
48
  "@langchain/standard-tests": "0.0.0",
49
49
  "@swc/core": "^1.3.90",
50
50
  "@swc/jest": "^0.2.29",