@langchain/core 0.2.16 → 0.2.18-rc.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/dist/callbacks/manager.cjs +71 -23
- package/dist/callbacks/manager.js +71 -23
- package/dist/callbacks/tests/callbacks.test.js +3 -0
- package/dist/language_models/chat_models.d.ts +18 -1
- package/dist/messages/ai.cjs +17 -0
- package/dist/messages/ai.d.ts +2 -0
- package/dist/messages/ai.js +17 -0
- package/dist/messages/base.cjs +50 -0
- package/dist/messages/base.d.ts +4 -1
- package/dist/messages/base.js +50 -0
- package/dist/messages/chat.cjs +12 -0
- package/dist/messages/chat.d.ts +2 -0
- package/dist/messages/chat.js +12 -0
- package/dist/messages/index.cjs +1 -0
- package/dist/messages/index.d.ts +1 -0
- package/dist/messages/index.js +1 -0
- package/dist/messages/modifier.cjs +35 -0
- package/dist/messages/modifier.d.ts +19 -0
- package/dist/messages/modifier.js +31 -0
- package/dist/messages/tool.cjs +14 -0
- package/dist/messages/tool.d.ts +2 -0
- package/dist/messages/tool.js +14 -0
- package/dist/messages/transformers.cjs +5 -0
- package/dist/messages/transformers.d.ts +3 -2
- package/dist/messages/transformers.js +5 -0
- package/dist/runnables/base.cjs +35 -4
- package/dist/runnables/base.d.ts +52 -5
- package/dist/runnables/base.js +35 -4
- package/dist/runnables/config.cjs +4 -7
- package/dist/runnables/config.d.ts +3 -17
- package/dist/runnables/config.js +5 -8
- package/dist/runnables/graph.d.ts +1 -1
- package/dist/runnables/iter.cjs +2 -4
- package/dist/runnables/iter.js +2 -4
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +52 -48
- package/dist/runnables/tests/runnable_tools.test.js +38 -0
- package/dist/runnables/types.d.ts +14 -1
- package/dist/singletons/index.cjs +35 -5
- package/dist/singletons/index.d.ts +2 -0
- package/dist/singletons/index.js +34 -4
- package/dist/singletons/tests/async_local_storage.test.js +2 -3
- package/dist/tools/index.cjs +17 -45
- package/dist/tools/index.d.ts +19 -28
- package/dist/tools/index.js +15 -42
- package/dist/tools/tests/tools.test.js +11 -0
- package/dist/tools/utils.cjs +28 -0
- package/dist/tools/utils.d.ts +11 -0
- package/dist/tools/utils.js +23 -0
- package/dist/tracers/base.cjs +67 -13
- package/dist/tracers/base.d.ts +176 -1
- package/dist/tracers/base.js +65 -12
- package/dist/tracers/event_stream.cjs +15 -2
- package/dist/tracers/event_stream.js +15 -2
- package/dist/tracers/tests/langsmith_interop.test.d.ts +1 -0
- package/dist/tracers/tests/langsmith_interop.test.js +551 -0
- package/dist/tracers/tracer_langchain.cjs +73 -31
- package/dist/tracers/tracer_langchain.d.ts +3 -1
- package/dist/tracers/tracer_langchain.js +73 -31
- package/dist/types/zod.d.ts +1 -1
- package/dist/utils/stream.cjs +8 -9
- package/dist/utils/stream.js +8 -9
- package/package.json +2 -2
package/dist/runnables/iter.js
CHANGED
|
@@ -17,9 +17,8 @@ export function isAsyncIterable(thing) {
|
|
|
17
17
|
"function");
|
|
18
18
|
}
|
|
19
19
|
export function* consumeIteratorInContext(context, iter) {
|
|
20
|
-
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
21
20
|
while (true) {
|
|
22
|
-
const { value, done } =
|
|
21
|
+
const { value, done } = AsyncLocalStorageProviderSingleton.runWithConfig(context, iter.next.bind(iter), true);
|
|
23
22
|
if (done) {
|
|
24
23
|
break;
|
|
25
24
|
}
|
|
@@ -29,10 +28,9 @@ export function* consumeIteratorInContext(context, iter) {
|
|
|
29
28
|
}
|
|
30
29
|
}
|
|
31
30
|
export async function* consumeAsyncIterableInContext(context, iter) {
|
|
32
|
-
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
33
31
|
const iterator = iter[Symbol.asyncIterator]();
|
|
34
32
|
while (true) {
|
|
35
|
-
const { value, done } = await
|
|
33
|
+
const { value, done } = await AsyncLocalStorageProviderSingleton.runWithConfig(context, iterator.next.bind(iter), true);
|
|
36
34
|
if (done) {
|
|
37
35
|
break;
|
|
38
36
|
}
|
|
@@ -57,6 +57,58 @@ test("Runnable streamEvents method", async () => {
|
|
|
57
57
|
},
|
|
58
58
|
]);
|
|
59
59
|
});
|
|
60
|
+
test("Runnable streamEvents method on a chat model", async () => {
|
|
61
|
+
const model = new FakeListChatModel({
|
|
62
|
+
responses: ["abc"],
|
|
63
|
+
});
|
|
64
|
+
const events = [];
|
|
65
|
+
const eventStream = await model.streamEvents("hello", { version: "v2" });
|
|
66
|
+
for await (const event of eventStream) {
|
|
67
|
+
events.push(event);
|
|
68
|
+
}
|
|
69
|
+
expect(events).toMatchObject([
|
|
70
|
+
{
|
|
71
|
+
data: { input: "hello" },
|
|
72
|
+
event: "on_chat_model_start",
|
|
73
|
+
name: "FakeListChatModel",
|
|
74
|
+
metadata: expect.any(Object),
|
|
75
|
+
run_id: expect.any(String),
|
|
76
|
+
tags: [],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
data: { chunk: new AIMessageChunk({ content: "a" }) },
|
|
80
|
+
event: "on_chat_model_stream",
|
|
81
|
+
name: "FakeListChatModel",
|
|
82
|
+
metadata: expect.any(Object),
|
|
83
|
+
run_id: expect.any(String),
|
|
84
|
+
tags: [],
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
data: { chunk: new AIMessageChunk({ content: "b" }) },
|
|
88
|
+
event: "on_chat_model_stream",
|
|
89
|
+
name: "FakeListChatModel",
|
|
90
|
+
metadata: expect.any(Object),
|
|
91
|
+
run_id: expect.any(String),
|
|
92
|
+
tags: [],
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
data: { chunk: new AIMessageChunk({ content: "c" }) },
|
|
96
|
+
event: "on_chat_model_stream",
|
|
97
|
+
name: "FakeListChatModel",
|
|
98
|
+
metadata: expect.any(Object),
|
|
99
|
+
run_id: expect.any(String),
|
|
100
|
+
tags: [],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
data: { output: new AIMessageChunk({ content: "abc" }) },
|
|
104
|
+
event: "on_chat_model_end",
|
|
105
|
+
name: "FakeListChatModel",
|
|
106
|
+
metadata: expect.any(Object),
|
|
107
|
+
run_id: expect.any(String),
|
|
108
|
+
tags: [],
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
60
112
|
test("Runnable streamEvents method with three runnables", async () => {
|
|
61
113
|
const r = RunnableLambda.from(reverse);
|
|
62
114
|
const chain = r
|
|
@@ -556,18 +608,6 @@ test("Runnable streamEvents method with llm", async () => {
|
|
|
556
608
|
a: "b",
|
|
557
609
|
},
|
|
558
610
|
},
|
|
559
|
-
{
|
|
560
|
-
event: "on_llm_stream",
|
|
561
|
-
run_id: expect.any(String),
|
|
562
|
-
name: "my_model",
|
|
563
|
-
tags: ["my_model"],
|
|
564
|
-
metadata: {
|
|
565
|
-
a: "b",
|
|
566
|
-
},
|
|
567
|
-
data: {
|
|
568
|
-
chunk: "h",
|
|
569
|
-
},
|
|
570
|
-
},
|
|
571
611
|
{
|
|
572
612
|
event: "on_llm_stream",
|
|
573
613
|
data: {
|
|
@@ -582,18 +622,6 @@ test("Runnable streamEvents method with llm", async () => {
|
|
|
582
622
|
a: "b",
|
|
583
623
|
},
|
|
584
624
|
},
|
|
585
|
-
{
|
|
586
|
-
event: "on_llm_stream",
|
|
587
|
-
run_id: expect.any(String),
|
|
588
|
-
name: "my_model",
|
|
589
|
-
tags: ["my_model"],
|
|
590
|
-
metadata: {
|
|
591
|
-
a: "b",
|
|
592
|
-
},
|
|
593
|
-
data: {
|
|
594
|
-
chunk: "e",
|
|
595
|
-
},
|
|
596
|
-
},
|
|
597
625
|
{
|
|
598
626
|
event: "on_llm_stream",
|
|
599
627
|
data: {
|
|
@@ -608,18 +636,6 @@ test("Runnable streamEvents method with llm", async () => {
|
|
|
608
636
|
a: "b",
|
|
609
637
|
},
|
|
610
638
|
},
|
|
611
|
-
{
|
|
612
|
-
event: "on_llm_stream",
|
|
613
|
-
run_id: expect.any(String),
|
|
614
|
-
name: "my_model",
|
|
615
|
-
tags: ["my_model"],
|
|
616
|
-
metadata: {
|
|
617
|
-
a: "b",
|
|
618
|
-
},
|
|
619
|
-
data: {
|
|
620
|
-
chunk: "y",
|
|
621
|
-
},
|
|
622
|
-
},
|
|
623
639
|
{
|
|
624
640
|
event: "on_llm_stream",
|
|
625
641
|
data: {
|
|
@@ -634,18 +650,6 @@ test("Runnable streamEvents method with llm", async () => {
|
|
|
634
650
|
a: "b",
|
|
635
651
|
},
|
|
636
652
|
},
|
|
637
|
-
{
|
|
638
|
-
event: "on_llm_stream",
|
|
639
|
-
run_id: expect.any(String),
|
|
640
|
-
name: "my_model",
|
|
641
|
-
tags: ["my_model"],
|
|
642
|
-
metadata: {
|
|
643
|
-
a: "b",
|
|
644
|
-
},
|
|
645
|
-
data: {
|
|
646
|
-
chunk: "!",
|
|
647
|
-
},
|
|
648
|
-
},
|
|
649
653
|
{
|
|
650
654
|
event: "on_llm_end",
|
|
651
655
|
data: {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { RunnableLambda, RunnableToolLike } from "../base.js";
|
|
3
|
+
import { FakeRetriever } from "../../utils/testing/index.js";
|
|
4
|
+
import { Document } from "../../documents/document.js";
|
|
3
5
|
test("Runnable asTool works", async () => {
|
|
4
6
|
const schema = z.object({
|
|
5
7
|
foo: z.string(),
|
|
@@ -109,3 +111,39 @@ test("Runnable asTool uses Zod schema description if not provided", async () =>
|
|
|
109
111
|
});
|
|
110
112
|
expect(tool.description).toBe(description);
|
|
111
113
|
});
|
|
114
|
+
test("Runnable asTool can accept a string zod schema", async () => {
|
|
115
|
+
const lambda = RunnableLambda.from((input) => {
|
|
116
|
+
return `${input}a`;
|
|
117
|
+
}).asTool({
|
|
118
|
+
name: "string_tool",
|
|
119
|
+
description: "A tool that appends 'a' to the input string",
|
|
120
|
+
schema: z.string(),
|
|
121
|
+
});
|
|
122
|
+
const result = await lambda.invoke("b");
|
|
123
|
+
expect(result).toBe("ba");
|
|
124
|
+
});
|
|
125
|
+
test("Runnables which dont accept ToolCalls as inputs can accept ToolCalls", async () => {
|
|
126
|
+
const pageContent = "Dogs are pretty cool, man!";
|
|
127
|
+
const retriever = new FakeRetriever({
|
|
128
|
+
output: [
|
|
129
|
+
new Document({
|
|
130
|
+
pageContent,
|
|
131
|
+
}),
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
const tool = retriever.asTool({
|
|
135
|
+
name: "pet_info_retriever",
|
|
136
|
+
description: "Get information about pets.",
|
|
137
|
+
schema: z.string(),
|
|
138
|
+
});
|
|
139
|
+
const result = await tool.invoke({
|
|
140
|
+
type: "tool_call",
|
|
141
|
+
name: "pet_info_retriever",
|
|
142
|
+
args: {
|
|
143
|
+
input: "dogs",
|
|
144
|
+
},
|
|
145
|
+
id: "string",
|
|
146
|
+
});
|
|
147
|
+
expect(result).toHaveLength(1);
|
|
148
|
+
expect(result[0].pageContent).toBe(pageContent);
|
|
149
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
|
-
import type { RunnableConfig } from "./config.js";
|
|
3
2
|
import type { IterableReadableStreamInterface } from "../utils/stream.js";
|
|
4
3
|
import type { SerializableInterface } from "../load/serializable.js";
|
|
4
|
+
import type { BaseCallbackConfig } from "../callbacks/manager.js";
|
|
5
5
|
export type RunnableBatchOptions = {
|
|
6
6
|
/** @deprecated Pass in via the standard runnable config object instead */
|
|
7
7
|
maxConcurrency?: number;
|
|
@@ -41,3 +41,16 @@ export interface Node {
|
|
|
41
41
|
id: string;
|
|
42
42
|
data: RunnableIOSchema | RunnableInterface;
|
|
43
43
|
}
|
|
44
|
+
export interface RunnableConfig extends BaseCallbackConfig {
|
|
45
|
+
/**
|
|
46
|
+
* Runtime values for attributes previously made configurable on this Runnable,
|
|
47
|
+
* or sub-Runnables.
|
|
48
|
+
*/
|
|
49
|
+
configurable?: Record<string, any>;
|
|
50
|
+
/**
|
|
51
|
+
* Maximum number of times a call can recurse. If not provided, defaults to 25.
|
|
52
|
+
*/
|
|
53
|
+
recursionLimit?: number;
|
|
54
|
+
/** Maximum number of parallel calls to make. */
|
|
55
|
+
maxConcurrency?: number;
|
|
56
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.AsyncLocalStorageProviderSingleton = exports.MockAsyncLocalStorage = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
const langsmith_1 = require("langsmith");
|
|
6
|
+
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
5
7
|
class MockAsyncLocalStorage {
|
|
6
8
|
getStore() {
|
|
7
9
|
return undefined;
|
|
@@ -12,14 +14,42 @@ class MockAsyncLocalStorage {
|
|
|
12
14
|
}
|
|
13
15
|
exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
|
|
14
16
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
17
|
+
const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
|
|
18
|
+
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
15
19
|
class AsyncLocalStorageProvider {
|
|
16
20
|
getInstance() {
|
|
17
|
-
return
|
|
18
|
-
|
|
21
|
+
return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
|
|
22
|
+
}
|
|
23
|
+
getRunnableConfig() {
|
|
24
|
+
const storage = this.getInstance();
|
|
25
|
+
// this has the runnable config
|
|
26
|
+
// which means that we should also have an instance of a LangChainTracer
|
|
27
|
+
// with the run map prepopulated
|
|
28
|
+
return storage.getStore()?.extra?.[LC_CHILD_KEY];
|
|
29
|
+
}
|
|
30
|
+
runWithConfig(config, callback, avoidCreatingRootRunTree) {
|
|
31
|
+
const callbackManager = manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
|
|
32
|
+
const storage = this.getInstance();
|
|
33
|
+
const parentRunId = callbackManager?.getParentRunId();
|
|
34
|
+
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
|
|
35
|
+
let runTree;
|
|
36
|
+
if (langChainTracer && parentRunId) {
|
|
37
|
+
runTree = langChainTracer.convertToRunTree(parentRunId);
|
|
38
|
+
}
|
|
39
|
+
else if (!avoidCreatingRootRunTree) {
|
|
40
|
+
runTree = new langsmith_1.RunTree({
|
|
41
|
+
name: "<runnable_lambda>",
|
|
42
|
+
tracingEnabled: false,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (runTree) {
|
|
46
|
+
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
|
|
47
|
+
}
|
|
48
|
+
return storage.run(runTree, callback);
|
|
19
49
|
}
|
|
20
50
|
initializeGlobalInstance(instance) {
|
|
21
|
-
if (globalThis
|
|
22
|
-
globalThis
|
|
51
|
+
if (globalThis[TRACING_ALS_KEY] === undefined) {
|
|
52
|
+
globalThis[TRACING_ALS_KEY] = instance;
|
|
23
53
|
}
|
|
24
54
|
}
|
|
25
55
|
}
|
|
@@ -8,6 +8,8 @@ export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface
|
|
|
8
8
|
}
|
|
9
9
|
declare class AsyncLocalStorageProvider {
|
|
10
10
|
getInstance(): AsyncLocalStorageInterface;
|
|
11
|
+
getRunnableConfig(): any;
|
|
12
|
+
runWithConfig<T>(config: any, callback: () => T, avoidCreatingRootRunTree?: boolean): T;
|
|
11
13
|
initializeGlobalInstance(instance: AsyncLocalStorageInterface): void;
|
|
12
14
|
}
|
|
13
15
|
declare const AsyncLocalStorageProviderSingleton: AsyncLocalStorageProvider;
|
package/dist/singletons/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { RunTree } from "langsmith";
|
|
3
|
+
import { CallbackManager } from "../callbacks/manager.js";
|
|
2
4
|
export class MockAsyncLocalStorage {
|
|
3
5
|
getStore() {
|
|
4
6
|
return undefined;
|
|
@@ -8,14 +10,42 @@ export class MockAsyncLocalStorage {
|
|
|
8
10
|
}
|
|
9
11
|
}
|
|
10
12
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
13
|
+
const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
|
|
14
|
+
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
11
15
|
class AsyncLocalStorageProvider {
|
|
12
16
|
getInstance() {
|
|
13
|
-
return
|
|
14
|
-
|
|
17
|
+
return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
|
|
18
|
+
}
|
|
19
|
+
getRunnableConfig() {
|
|
20
|
+
const storage = this.getInstance();
|
|
21
|
+
// this has the runnable config
|
|
22
|
+
// which means that we should also have an instance of a LangChainTracer
|
|
23
|
+
// with the run map prepopulated
|
|
24
|
+
return storage.getStore()?.extra?.[LC_CHILD_KEY];
|
|
25
|
+
}
|
|
26
|
+
runWithConfig(config, callback, avoidCreatingRootRunTree) {
|
|
27
|
+
const callbackManager = CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
|
|
28
|
+
const storage = this.getInstance();
|
|
29
|
+
const parentRunId = callbackManager?.getParentRunId();
|
|
30
|
+
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
|
|
31
|
+
let runTree;
|
|
32
|
+
if (langChainTracer && parentRunId) {
|
|
33
|
+
runTree = langChainTracer.convertToRunTree(parentRunId);
|
|
34
|
+
}
|
|
35
|
+
else if (!avoidCreatingRootRunTree) {
|
|
36
|
+
runTree = new RunTree({
|
|
37
|
+
name: "<runnable_lambda>",
|
|
38
|
+
tracingEnabled: false,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (runTree) {
|
|
42
|
+
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
|
|
43
|
+
}
|
|
44
|
+
return storage.run(runTree, callback);
|
|
15
45
|
}
|
|
16
46
|
initializeGlobalInstance(instance) {
|
|
17
|
-
if (globalThis
|
|
18
|
-
globalThis
|
|
47
|
+
if (globalThis[TRACING_ALS_KEY] === undefined) {
|
|
48
|
+
globalThis[TRACING_ALS_KEY] = instance;
|
|
19
49
|
}
|
|
20
50
|
}
|
|
21
51
|
}
|
|
@@ -108,7 +108,6 @@ test("Config should be automatically populated after setting global async local
|
|
|
108
108
|
});
|
|
109
109
|
test("Runnable streamEvents method with streaming nested in a RunnableLambda", async () => {
|
|
110
110
|
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
111
|
-
const asyncLocalStorage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
112
111
|
const chat = new FakeListChatModel({
|
|
113
112
|
responses: ["Hello"],
|
|
114
113
|
});
|
|
@@ -117,7 +116,7 @@ test("Runnable streamEvents method with streaming nested in a RunnableLambda", a
|
|
|
117
116
|
const innerRunId2 = v4();
|
|
118
117
|
const dummyHandler = new FakeCallbackHandler();
|
|
119
118
|
const myFunc = async (input) => {
|
|
120
|
-
const outerCallbackManager = await getCallbackManagerForConfig(
|
|
119
|
+
const outerCallbackManager = await getCallbackManagerForConfig(AsyncLocalStorageProviderSingleton.getRunnableConfig());
|
|
121
120
|
expect(outerCallbackManager?.getParentRunId()).toEqual(outerRunId);
|
|
122
121
|
const nestedLambdaWithOverriddenCallbacks = RunnableLambda.from(async (_, config) => {
|
|
123
122
|
expect(config?.callbacks?.handlers).toEqual([]);
|
|
@@ -127,7 +126,7 @@ test("Runnable streamEvents method with streaming nested in a RunnableLambda", a
|
|
|
127
126
|
callbacks: [],
|
|
128
127
|
});
|
|
129
128
|
const nestedLambdaWithoutOverriddenCallbacks = RunnableLambda.from(async (_, config) => {
|
|
130
|
-
const innerCallbackManager = await getCallbackManagerForConfig(
|
|
129
|
+
const innerCallbackManager = await getCallbackManagerForConfig(AsyncLocalStorageProviderSingleton.getRunnableConfig());
|
|
131
130
|
expect(innerCallbackManager?.getParentRunId()).toEqual(innerRunId2);
|
|
132
131
|
expect(config?.callbacks?.handlers).toContain(dummyHandler);
|
|
133
132
|
});
|
package/dist/tools/index.cjs
CHANGED
|
@@ -7,24 +7,8 @@ const base_js_1 = require("../language_models/base.cjs");
|
|
|
7
7
|
const config_js_1 = require("../runnables/config.cjs");
|
|
8
8
|
const tool_js_1 = require("../messages/tool.cjs");
|
|
9
9
|
const index_js_1 = require("../singletons/index.cjs");
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
13
|
-
* property that can hold the output that caused the exception.
|
|
14
|
-
*/
|
|
15
|
-
class ToolInputParsingException extends Error {
|
|
16
|
-
constructor(message, output) {
|
|
17
|
-
super(message);
|
|
18
|
-
Object.defineProperty(this, "output", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: void 0
|
|
23
|
-
});
|
|
24
|
-
this.output = output;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.ToolInputParsingException = ToolInputParsingException;
|
|
10
|
+
const utils_js_1 = require("./utils.cjs");
|
|
11
|
+
Object.defineProperty(exports, "ToolInputParsingException", { enumerable: true, get: function () { return utils_js_1.ToolInputParsingException; } });
|
|
28
12
|
/**
|
|
29
13
|
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
30
14
|
*/
|
|
@@ -66,7 +50,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
|
|
|
66
50
|
async invoke(input, config) {
|
|
67
51
|
let tool_call_id;
|
|
68
52
|
let toolInput;
|
|
69
|
-
if (_isToolCall(input)) {
|
|
53
|
+
if ((0, utils_js_1._isToolCall)(input)) {
|
|
70
54
|
tool_call_id = input.id;
|
|
71
55
|
toolInput = input.args;
|
|
72
56
|
}
|
|
@@ -101,7 +85,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
|
|
|
101
85
|
parsed = await this.schema.parseAsync(arg);
|
|
102
86
|
}
|
|
103
87
|
catch (e) {
|
|
104
|
-
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(arg));
|
|
88
|
+
throw new utils_js_1.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(arg));
|
|
105
89
|
}
|
|
106
90
|
const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
|
|
107
91
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
|
|
@@ -291,29 +275,23 @@ class BaseToolkit {
|
|
|
291
275
|
}
|
|
292
276
|
}
|
|
293
277
|
exports.BaseToolkit = BaseToolkit;
|
|
294
|
-
/**
|
|
295
|
-
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
|
|
296
|
-
* @function
|
|
297
|
-
* @template {RunInput extends ZodAny = ZodAny} RunInput The input schema for the tool. This corresponds to the input type when the tool is invoked.
|
|
298
|
-
* @template {RunOutput = any} RunOutput The output type for the tool. This corresponds to the output type when the tool is invoked.
|
|
299
|
-
* @template {FuncInput extends z.infer<RunInput> | ToolCall = z.infer<RunInput>} FuncInput The input type for the function.
|
|
300
|
-
*
|
|
301
|
-
* @param {RunnableFunc<z.infer<RunInput> | ToolCall, RunOutput>} func - The function to invoke when the tool is called.
|
|
302
|
-
* @param fields - An object containing the following properties:
|
|
303
|
-
* @param {string} fields.name The name of the tool.
|
|
304
|
-
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
|
|
305
|
-
* @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
|
|
306
|
-
*
|
|
307
|
-
* @returns {DynamicStructuredTool<RunInput, RunOutput>} A new StructuredTool instance.
|
|
308
|
-
*/
|
|
309
278
|
function tool(func, fields) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
279
|
+
// If the schema is not provided, or it's a string schema, create a DynamicTool
|
|
280
|
+
if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
|
|
281
|
+
return new DynamicTool({
|
|
282
|
+
name: fields.name,
|
|
283
|
+
description: fields.description ??
|
|
284
|
+
fields.schema?.description ??
|
|
285
|
+
`${fields.name} tool`,
|
|
286
|
+
responseFormat: fields.responseFormat,
|
|
287
|
+
func,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const description = fields.description ?? fields.schema.description ?? `${fields.name} tool`;
|
|
313
291
|
return new DynamicStructuredTool({
|
|
314
292
|
name: fields.name,
|
|
315
293
|
description,
|
|
316
|
-
schema: schema,
|
|
294
|
+
schema: fields.schema,
|
|
317
295
|
// TODO: Consider moving into DynamicStructuredTool constructor
|
|
318
296
|
func: async (input, runManager, config) => {
|
|
319
297
|
return new Promise((resolve, reject) => {
|
|
@@ -334,12 +312,6 @@ function tool(func, fields) {
|
|
|
334
312
|
});
|
|
335
313
|
}
|
|
336
314
|
exports.tool = tool;
|
|
337
|
-
function _isToolCall(toolCall) {
|
|
338
|
-
return !!(toolCall &&
|
|
339
|
-
typeof toolCall === "object" &&
|
|
340
|
-
"type" in toolCall &&
|
|
341
|
-
toolCall.type === "tool_call");
|
|
342
|
-
}
|
|
343
315
|
function _formatToolOutput(params) {
|
|
344
316
|
const { content, artifact, toolCallId } = params;
|
|
345
317
|
if (toolCallId) {
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -4,8 +4,10 @@ import { BaseLangChain, type BaseLangChainParams } from "../language_models/base
|
|
|
4
4
|
import { type RunnableConfig } from "../runnables/config.js";
|
|
5
5
|
import type { RunnableFunc, RunnableInterface } from "../runnables/base.js";
|
|
6
6
|
import { ToolCall } from "../messages/tool.js";
|
|
7
|
-
import {
|
|
7
|
+
import { ZodObjectAny } from "../types/zod.js";
|
|
8
8
|
import { MessageContent } from "../messages/base.js";
|
|
9
|
+
import { ToolInputParsingException } from "./utils.js";
|
|
10
|
+
export { ToolInputParsingException };
|
|
9
11
|
export type ResponseFormat = "content" | "content_and_artifact" | string;
|
|
10
12
|
type ToolReturnType = any;
|
|
11
13
|
export type ContentAndArtifact = [MessageContent, any];
|
|
@@ -24,16 +26,7 @@ export interface ToolParams extends BaseLangChainParams {
|
|
|
24
26
|
*/
|
|
25
27
|
responseFormat?: ResponseFormat;
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
* Custom error class used to handle exceptions related to tool input parsing.
|
|
29
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
30
|
-
* property that can hold the output that caused the exception.
|
|
31
|
-
*/
|
|
32
|
-
export declare class ToolInputParsingException extends Error {
|
|
33
|
-
output?: string;
|
|
34
|
-
constructor(message: string, output?: string);
|
|
35
|
-
}
|
|
36
|
-
export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
29
|
+
export interface StructuredToolInterface<T extends ZodObjectAny = ZodObjectAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
37
30
|
lc_namespace: string[];
|
|
38
31
|
schema: T | z.ZodEffects<T>;
|
|
39
32
|
/**
|
|
@@ -57,7 +50,7 @@ export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends Runn
|
|
|
57
50
|
/**
|
|
58
51
|
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
59
52
|
*/
|
|
60
|
-
export declare abstract class StructuredTool<T extends
|
|
53
|
+
export declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
61
54
|
abstract name: string;
|
|
62
55
|
abstract description: string;
|
|
63
56
|
abstract schema: T | z.ZodEffects<T>;
|
|
@@ -97,7 +90,7 @@ export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends
|
|
|
97
90
|
/** @deprecated */
|
|
98
91
|
tags?: string[]): Promise<ToolReturnType>;
|
|
99
92
|
}
|
|
100
|
-
export interface ToolInterface<T extends
|
|
93
|
+
export interface ToolInterface<T extends ZodObjectAny = ZodObjectAny> extends StructuredToolInterface<T> {
|
|
101
94
|
/**
|
|
102
95
|
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
103
96
|
*
|
|
@@ -112,7 +105,7 @@ export interface ToolInterface<T extends ZodAny = ZodAny> extends StructuredTool
|
|
|
112
105
|
/**
|
|
113
106
|
* Base class for Tools that accept input as a string.
|
|
114
107
|
*/
|
|
115
|
-
export declare abstract class Tool extends StructuredTool<
|
|
108
|
+
export declare abstract class Tool extends StructuredTool<ZodObjectAny> {
|
|
116
109
|
schema: z.ZodEffects<z.ZodObject<{
|
|
117
110
|
input: z.ZodOptional<z.ZodString>;
|
|
118
111
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -148,7 +141,7 @@ export interface DynamicToolInput extends BaseDynamicToolInput {
|
|
|
148
141
|
/**
|
|
149
142
|
* Interface for the input parameters of the DynamicStructuredTool class.
|
|
150
143
|
*/
|
|
151
|
-
export interface DynamicStructuredToolInput<T extends
|
|
144
|
+
export interface DynamicStructuredToolInput<T extends ZodObjectAny = ZodObjectAny> extends BaseDynamicToolInput {
|
|
152
145
|
func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
|
|
153
146
|
schema: T;
|
|
154
147
|
}
|
|
@@ -174,7 +167,7 @@ export declare class DynamicTool extends Tool {
|
|
|
174
167
|
* StructuredTool class and overrides the _call method to execute the
|
|
175
168
|
* provided function when the tool is called.
|
|
176
169
|
*/
|
|
177
|
-
export declare class DynamicStructuredTool<T extends
|
|
170
|
+
export declare class DynamicStructuredTool<T extends ZodObjectAny = ZodObjectAny> extends StructuredTool<T> {
|
|
178
171
|
static lc_name(): string;
|
|
179
172
|
name: string;
|
|
180
173
|
description: string;
|
|
@@ -200,10 +193,9 @@ export declare abstract class BaseToolkit {
|
|
|
200
193
|
}
|
|
201
194
|
/**
|
|
202
195
|
* Parameters for the tool function.
|
|
203
|
-
* @template {
|
|
204
|
-
* @template {any} RunOutput The output type for the tool.
|
|
196
|
+
* @template {ZodObjectAny | z.ZodString = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, or a Zod string.
|
|
205
197
|
*/
|
|
206
|
-
interface ToolWrapperParams<RunInput extends
|
|
198
|
+
interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString = ZodObjectAny> extends ToolParams {
|
|
207
199
|
/**
|
|
208
200
|
* The name of the tool. If using with an LLM, this
|
|
209
201
|
* will be passed as the tool name.
|
|
@@ -233,18 +225,17 @@ interface ToolWrapperParams<RunInput extends ZodAny = ZodAny> extends ToolParams
|
|
|
233
225
|
}
|
|
234
226
|
/**
|
|
235
227
|
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
|
|
228
|
+
*
|
|
236
229
|
* @function
|
|
237
|
-
* @template {
|
|
238
|
-
* @template {RunOutput = any} RunOutput The output type for the tool. This corresponds to the output type when the tool is invoked.
|
|
239
|
-
* @template {FuncInput extends z.infer<RunInput> | ToolCall = z.infer<RunInput>} FuncInput The input type for the function.
|
|
230
|
+
* @template {ZodObjectAny | z.ZodString = ZodObjectAny} T The input schema for the tool. Either any Zod object, or a Zod string.
|
|
240
231
|
*
|
|
241
|
-
* @param {RunnableFunc<z.
|
|
242
|
-
* @param fields - An object containing the following properties:
|
|
232
|
+
* @param {RunnableFunc<z.output<T>, ToolReturnType>} func - The function to invoke when the tool is called.
|
|
233
|
+
* @param {ToolWrapperParams<T>} fields - An object containing the following properties:
|
|
243
234
|
* @param {string} fields.name The name of the tool.
|
|
244
235
|
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
|
|
245
|
-
* @param {z.
|
|
236
|
+
* @param {ZodObjectAny | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.
|
|
246
237
|
*
|
|
247
|
-
* @returns {DynamicStructuredTool<
|
|
238
|
+
* @returns {DynamicStructuredTool<T>} A new StructuredTool instance.
|
|
248
239
|
*/
|
|
249
|
-
export declare function tool<T extends
|
|
250
|
-
export
|
|
240
|
+
export declare function tool<T extends z.ZodString = z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicTool;
|
|
241
|
+
export declare function tool<T extends ZodObjectAny = ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
|