@langchain/core 0.1.12 → 0.1.13

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.
@@ -93,6 +93,10 @@ export interface FunctionDefinition {
93
93
  */
94
94
  description?: string;
95
95
  }
96
+ export interface ToolDefinition {
97
+ type: "function";
98
+ function: FunctionDefinition;
99
+ }
96
100
  export type FunctionCallOption = {
97
101
  name: string;
98
102
  };
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  return result;
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.vectorstores = exports.utils__types = exports.utils__tiktoken = exports.utils__testing = exports.utils__stream = exports.utils__math = exports.utils__json_schema = exports.utils__json_patch = exports.utils__hash = exports.utils__env = exports.utils__chunk_array = exports.utils__async_caller = exports.tracers__tracer_langchain_v1 = exports.tracers__tracer_langchain = exports.tracers__run_collector = exports.tracers__log_stream = exports.tracers__initialize = exports.tracers__console = exports.tracers__base = exports.tools = exports.stores = exports.retrievers = exports.runnables = exports.prompt_values = exports.prompts = exports.outputs = exports.output_parsers = exports.messages = exports.memory = exports.load__serializable = exports.language_models__llms = exports.language_models__chat_models = exports.language_models__base = exports.example_selectors = exports.embeddings = exports.documents = exports.chat_history = exports.callbacks__promises = exports.callbacks__manager = exports.callbacks__base = exports.caches = exports.agents = void 0;
27
+ exports.vectorstores = exports.utils__types = exports.utils__tiktoken = exports.utils__testing = exports.utils__stream = exports.utils__math = exports.utils__json_schema = exports.utils__json_patch = exports.utils__hash = exports.utils__function_calling = exports.utils__env = exports.utils__chunk_array = exports.utils__async_caller = exports.tracers__tracer_langchain_v1 = exports.tracers__tracer_langchain = exports.tracers__run_collector = exports.tracers__log_stream = exports.tracers__initialize = exports.tracers__console = exports.tracers__base = exports.tools = exports.stores = exports.retrievers = exports.runnables = exports.prompt_values = exports.prompts = exports.outputs = exports.output_parsers = exports.messages = exports.memory = exports.load__serializable = exports.language_models__llms = exports.language_models__chat_models = exports.language_models__base = exports.example_selectors = exports.embeddings = exports.documents = exports.chat_history = exports.callbacks__promises = exports.callbacks__manager = exports.callbacks__base = exports.caches = exports.agents = void 0;
28
28
  exports.agents = __importStar(require("../agents.cjs"));
29
29
  exports.caches = __importStar(require("../caches.cjs"));
30
30
  exports.callbacks__base = __importStar(require("../callbacks/base.cjs"));
@@ -58,6 +58,7 @@ exports.tracers__tracer_langchain_v1 = __importStar(require("../tracers/tracer_l
58
58
  exports.utils__async_caller = __importStar(require("../utils/async_caller.cjs"));
59
59
  exports.utils__chunk_array = __importStar(require("../utils/chunk_array.cjs"));
60
60
  exports.utils__env = __importStar(require("../utils/env.cjs"));
61
+ exports.utils__function_calling = __importStar(require("../utils/function_calling.cjs"));
61
62
  exports.utils__hash = __importStar(require("../utils/hash.cjs"));
62
63
  exports.utils__json_patch = __importStar(require("../utils/json_patch.cjs"));
63
64
  exports.utils__json_schema = __importStar(require("../utils/json_schema.cjs"));
@@ -31,6 +31,7 @@ export * as tracers__tracer_langchain_v1 from "../tracers/tracer_langchain_v1.js
31
31
  export * as utils__async_caller from "../utils/async_caller.js";
32
32
  export * as utils__chunk_array from "../utils/chunk_array.js";
33
33
  export * as utils__env from "../utils/env.js";
34
+ export * as utils__function_calling from "../utils/function_calling.js";
34
35
  export * as utils__hash from "../utils/hash.js";
35
36
  export * as utils__json_patch from "../utils/json_patch.js";
36
37
  export * as utils__json_schema from "../utils/json_schema.js";
@@ -32,6 +32,7 @@ export * as tracers__tracer_langchain_v1 from "../tracers/tracer_langchain_v1.js
32
32
  export * as utils__async_caller from "../utils/async_caller.js";
33
33
  export * as utils__chunk_array from "../utils/chunk_array.js";
34
34
  export * as utils__env from "../utils/env.js";
35
+ export * as utils__function_calling from "../utils/function_calling.js";
35
36
  export * as utils__hash from "../utils/hash.js";
36
37
  export * as utils__json_patch from "../utils/json_patch.js";
37
38
  export * as utils__json_schema from "../utils/json_schema.js";
@@ -218,7 +218,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
218
218
  throw new Error("No chain run to end.");
219
219
  }
220
220
  run.end_time = Date.now();
221
- run.error = error.message;
221
+ run.error = error.message + (error.stack ? `\n\n${error.stack}` : "");
222
222
  run.events.push({
223
223
  name: "error",
224
224
  time: new Date(run.end_time).toISOString(),
@@ -215,7 +215,7 @@ export class BaseTracer extends BaseCallbackHandler {
215
215
  throw new Error("No chain run to end.");
216
216
  }
217
217
  run.end_time = Date.now();
218
- run.error = error.message;
218
+ run.error = error.message + (error.stack ? `\n\n${error.stack}` : "");
219
219
  run.events.push({
220
220
  name: "error",
221
221
  time: new Date(run.end_time).toISOString(),
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToOpenAITool = exports.convertToOpenAIFunction = void 0;
4
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
5
+ /**
6
+ * Formats a `StructuredTool` instance into a format that is compatible
7
+ * with OpenAI function calling. It uses the `zodToJsonSchema`
8
+ * function to convert the schema of the `StructuredTool` into a JSON
9
+ * schema, which is then used as the parameters for the OpenAI function.
10
+ */
11
+ function convertToOpenAIFunction(tool) {
12
+ return {
13
+ name: tool.name,
14
+ description: tool.description,
15
+ parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
16
+ };
17
+ }
18
+ exports.convertToOpenAIFunction = convertToOpenAIFunction;
19
+ /**
20
+ * Formats a `StructuredTool` instance into a format that is compatible
21
+ * with OpenAI tool calling. 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 tool.
24
+ */
25
+ function convertToOpenAITool(tool) {
26
+ return {
27
+ type: "function",
28
+ function: convertToOpenAIFunction(tool),
29
+ };
30
+ }
31
+ exports.convertToOpenAITool = convertToOpenAITool;
@@ -0,0 +1,16 @@
1
+ import { StructuredToolInterface } from "../tools.js";
2
+ import { FunctionDefinition, ToolDefinition } from "../language_models/base.js";
3
+ /**
4
+ * Formats a `StructuredTool` instance into a format that is compatible
5
+ * with OpenAI function calling. It uses the `zodToJsonSchema`
6
+ * function to convert the schema of the `StructuredTool` into a JSON
7
+ * schema, which is then used as the parameters for the OpenAI function.
8
+ */
9
+ export declare function convertToOpenAIFunction(tool: StructuredToolInterface): FunctionDefinition;
10
+ /**
11
+ * Formats a `StructuredTool` instance into a format that is compatible
12
+ * with OpenAI tool calling. It uses the `zodToJsonSchema`
13
+ * function to convert the schema of the `StructuredTool` into a JSON
14
+ * schema, which is then used as the parameters for the OpenAI tool.
15
+ */
16
+ export declare function convertToOpenAITool(tool: StructuredToolInterface): ToolDefinition;
@@ -0,0 +1,26 @@
1
+ import { zodToJsonSchema } from "zod-to-json-schema";
2
+ /**
3
+ * Formats a `StructuredTool` instance into a format that is compatible
4
+ * with OpenAI function calling. It uses the `zodToJsonSchema`
5
+ * function to convert the schema of the `StructuredTool` into a JSON
6
+ * schema, which is then used as the parameters for the OpenAI function.
7
+ */
8
+ export function convertToOpenAIFunction(tool) {
9
+ return {
10
+ name: tool.name,
11
+ description: tool.description,
12
+ parameters: zodToJsonSchema(tool.schema),
13
+ };
14
+ }
15
+ /**
16
+ * Formats a `StructuredTool` instance into a format that is compatible
17
+ * with OpenAI tool calling. It uses the `zodToJsonSchema`
18
+ * function to convert the schema of the `StructuredTool` into a JSON
19
+ * schema, which is then used as the parameters for the OpenAI tool.
20
+ */
21
+ export function convertToOpenAITool(tool) {
22
+ return {
23
+ type: "function",
24
+ function: convertToOpenAIFunction(tool),
25
+ };
26
+ }
@@ -1,4 +1,4 @@
1
- export interface IterableReadableStreamInterface<T> extends ReadableStream<T>, AsyncGenerator<T> {
1
+ export interface IterableReadableStreamInterface<T> extends ReadableStream<T>, AsyncIterable<T> {
2
2
  }
3
3
  export declare class IterableReadableStream<T> extends ReadableStream<T> implements IterableReadableStreamInterface<T> {
4
4
  reader: ReadableStreamDefaultReader<T>;
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
- /* eslint-disable no-promise-executor-return */
3
- /* eslint-disable @typescript-eslint/no-explicit-any */
4
2
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
3
+ exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
6
4
  const chat_history_js_1 = require("../../chat_history.cjs");
7
5
  const document_js_1 = require("../../documents/document.cjs");
8
6
  const chat_models_js_1 = require("../../language_models/chat_models.cjs");
@@ -12,6 +10,7 @@ const base_js_1 = require("../../output_parsers/base.cjs");
12
10
  const outputs_js_1 = require("../../outputs.cjs");
13
11
  const retrievers_js_1 = require("../../retrievers.cjs");
14
12
  const base_js_2 = require("../../runnables/base.cjs");
13
+ const tools_js_1 = require("../../tools.cjs");
15
14
  const base_js_3 = require("../../tracers/base.cjs");
16
15
  /**
17
16
  * Parser for comma-separated values. It splits the input text by commas
@@ -366,3 +365,33 @@ class FakeTracer extends base_js_3.BaseTracer {
366
365
  }
367
366
  }
368
367
  exports.FakeTracer = FakeTracer;
368
+ class FakeTool extends tools_js_1.StructuredTool {
369
+ constructor(fields) {
370
+ super(fields);
371
+ Object.defineProperty(this, "name", {
372
+ enumerable: true,
373
+ configurable: true,
374
+ writable: true,
375
+ value: void 0
376
+ });
377
+ Object.defineProperty(this, "description", {
378
+ enumerable: true,
379
+ configurable: true,
380
+ writable: true,
381
+ value: void 0
382
+ });
383
+ Object.defineProperty(this, "schema", {
384
+ enumerable: true,
385
+ configurable: true,
386
+ writable: true,
387
+ value: void 0
388
+ });
389
+ this.name = fields.name;
390
+ this.description = fields.description;
391
+ this.schema = fields.schema;
392
+ }
393
+ async _call(arg, _runManager) {
394
+ return JSON.stringify(arg);
395
+ }
396
+ }
397
+ exports.FakeTool = FakeTool;
@@ -1,4 +1,5 @@
1
- import { BaseCallbackConfig, CallbackManagerForLLMRun } from "../../callbacks/manager.js";
1
+ import { z } from "zod";
2
+ import { BaseCallbackConfig, CallbackManagerForLLMRun, CallbackManagerForToolRun } from "../../callbacks/manager.js";
2
3
  import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../../chat_history.js";
3
4
  import { Document } from "../../documents/document.js";
4
5
  import { BaseChatModel, BaseChatModelParams } from "../../language_models/chat_models.js";
@@ -8,6 +9,7 @@ import { BaseOutputParser } from "../../output_parsers/base.js";
8
9
  import { GenerationChunk, type ChatResult, ChatGenerationChunk } from "../../outputs.js";
9
10
  import { BaseRetriever } from "../../retrievers.js";
10
11
  import { Runnable } from "../../runnables/base.js";
12
+ import { StructuredTool, ToolParams } from "../../tools.js";
11
13
  import { BaseTracer, Run } from "../../tracers/base.js";
12
14
  /**
13
15
  * Parser for comma-separated values. It splits the input text by commas
@@ -130,3 +132,15 @@ export declare class FakeTracer extends BaseTracer {
130
132
  constructor();
131
133
  protected persistRun(run: Run): Promise<void>;
132
134
  }
135
+ export interface FakeToolParams<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends ToolParams {
136
+ name: string;
137
+ description: string;
138
+ schema: T;
139
+ }
140
+ export declare class FakeTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends StructuredTool<T> {
141
+ name: string;
142
+ description: string;
143
+ schema: T;
144
+ constructor(fields: FakeToolParams<T>);
145
+ protected _call(arg: z.output<T>, _runManager?: CallbackManagerForToolRun): Promise<string>;
146
+ }
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-promise-executor-return */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
1
  import { BaseChatMessageHistory, BaseListChatMessageHistory, } from "../../chat_history.js";
4
2
  import { Document } from "../../documents/document.js";
5
3
  import { BaseChatModel, } from "../../language_models/chat_models.js";
@@ -9,6 +7,7 @@ import { BaseOutputParser } from "../../output_parsers/base.js";
9
7
  import { ChatGenerationChunk, } from "../../outputs.js";
10
8
  import { BaseRetriever } from "../../retrievers.js";
11
9
  import { Runnable } from "../../runnables/base.js";
10
+ import { StructuredTool } from "../../tools.js";
12
11
  import { BaseTracer } from "../../tracers/base.js";
13
12
  /**
14
13
  * Parser for comma-separated values. It splits the input text by commas
@@ -353,3 +352,32 @@ export class FakeTracer extends BaseTracer {
353
352
  return Promise.resolve();
354
353
  }
355
354
  }
355
+ export class FakeTool extends StructuredTool {
356
+ constructor(fields) {
357
+ super(fields);
358
+ Object.defineProperty(this, "name", {
359
+ enumerable: true,
360
+ configurable: true,
361
+ writable: true,
362
+ value: void 0
363
+ });
364
+ Object.defineProperty(this, "description", {
365
+ enumerable: true,
366
+ configurable: true,
367
+ writable: true,
368
+ value: void 0
369
+ });
370
+ Object.defineProperty(this, "schema", {
371
+ enumerable: true,
372
+ configurable: true,
373
+ writable: true,
374
+ value: void 0
375
+ });
376
+ this.name = fields.name;
377
+ this.description = fields.description;
378
+ this.schema = fields.schema;
379
+ }
380
+ async _call(arg, _runManager) {
381
+ return JSON.stringify(arg);
382
+ }
383
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,7 +43,8 @@
43
43
  "p-queue": "^6.6.2",
44
44
  "p-retry": "4",
45
45
  "uuid": "^9.0.0",
46
- "zod": "^3.22.3"
46
+ "zod": "^3.22.3",
47
+ "zod-to-json-schema": "3.20.3"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@jest/globals": "^29.5.0",
@@ -254,6 +255,11 @@
254
255
  "import": "./utils/env.js",
255
256
  "require": "./utils/env.cjs"
256
257
  },
258
+ "./utils/function_calling": {
259
+ "types": "./utils/function_calling.d.ts",
260
+ "import": "./utils/function_calling.js",
261
+ "require": "./utils/function_calling.cjs"
262
+ },
257
263
  "./utils/hash": {
258
264
  "types": "./utils/hash.d.ts",
259
265
  "import": "./utils/hash.js",
@@ -405,6 +411,9 @@
405
411
  "utils/env.cjs",
406
412
  "utils/env.js",
407
413
  "utils/env.d.ts",
414
+ "utils/function_calling.cjs",
415
+ "utils/function_calling.js",
416
+ "utils/function_calling.d.ts",
408
417
  "utils/hash.cjs",
409
418
  "utils/hash.js",
410
419
  "utils/hash.d.ts",
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/utils/function_calling.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/utils/function_calling.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/utils/function_calling.js'