langchain 0.0.138 → 0.0.139

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.
Files changed (52) hide show
  1. package/dist/agents/initialize.cjs +11 -0
  2. package/dist/agents/initialize.d.ts +4 -0
  3. package/dist/agents/initialize.js +11 -0
  4. package/dist/agents/xml/index.cjs +119 -0
  5. package/dist/agents/xml/index.d.ts +51 -0
  6. package/dist/agents/xml/index.js +114 -0
  7. package/dist/agents/xml/prompt.cjs +23 -0
  8. package/dist/agents/xml/prompt.d.ts +1 -0
  9. package/dist/agents/xml/prompt.js +20 -0
  10. package/dist/callbacks/base.d.ts +12 -4
  11. package/dist/callbacks/handlers/run_collector.cjs +50 -0
  12. package/dist/callbacks/handlers/run_collector.d.ts +26 -0
  13. package/dist/callbacks/handlers/run_collector.js +46 -0
  14. package/dist/callbacks/handlers/tracer.cjs +16 -3
  15. package/dist/callbacks/handlers/tracer.d.ts +6 -2
  16. package/dist/callbacks/handlers/tracer.js +16 -3
  17. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -0
  18. package/dist/callbacks/handlers/tracer_langchain.d.ts +2 -1
  19. package/dist/callbacks/handlers/tracer_langchain.js +1 -0
  20. package/dist/callbacks/index.cjs +3 -1
  21. package/dist/callbacks/index.d.ts +1 -0
  22. package/dist/callbacks/index.js +1 -0
  23. package/dist/callbacks/manager.cjs +4 -4
  24. package/dist/callbacks/manager.d.ts +6 -2
  25. package/dist/callbacks/manager.js +4 -4
  26. package/dist/chains/openai_functions/extraction.cjs +2 -2
  27. package/dist/chains/openai_functions/extraction.d.ts +5 -4
  28. package/dist/chains/openai_functions/extraction.js +2 -2
  29. package/dist/chains/openai_functions/openapi.d.ts +2 -1
  30. package/dist/chains/openai_functions/structured_output.d.ts +4 -3
  31. package/dist/chains/openai_functions/tagging.cjs +2 -2
  32. package/dist/chains/openai_functions/tagging.d.ts +5 -4
  33. package/dist/chains/openai_functions/tagging.js +2 -2
  34. package/dist/chat_models/anthropic.cjs +7 -5
  35. package/dist/chat_models/anthropic.d.ts +17 -12
  36. package/dist/chat_models/anthropic.js +4 -2
  37. package/dist/experimental/chat_models/anthropic_functions.cjs +129 -0
  38. package/dist/experimental/chat_models/anthropic_functions.d.ts +20 -0
  39. package/dist/experimental/chat_models/anthropic_functions.js +125 -0
  40. package/dist/load/import_constants.cjs +1 -0
  41. package/dist/load/import_constants.js +1 -0
  42. package/dist/schema/output_parser.cjs +1 -1
  43. package/dist/schema/output_parser.js +1 -1
  44. package/dist/schema/runnable.cjs +54 -15
  45. package/dist/schema/runnable.d.ts +9 -3
  46. package/dist/schema/runnable.js +55 -16
  47. package/dist/sql_db.cjs +3 -1
  48. package/dist/sql_db.js +3 -1
  49. package/experimental/chat_models/anthropic_functions.cjs +1 -0
  50. package/experimental/chat_models/anthropic_functions.d.ts +1 -0
  51. package/experimental/chat_models/anthropic_functions.js +1 -0
  52. package/package.json +16 -3
@@ -8,6 +8,7 @@ const index_js_3 = require("./structured_chat/index.cjs");
8
8
  const executor_js_1 = require("./executor.cjs");
9
9
  const index_js_4 = require("./mrkl/index.cjs");
10
10
  const index_js_5 = require("./openai/index.cjs");
11
+ const index_js_6 = require("./xml/index.cjs");
11
12
  /**
12
13
  * @deprecated use initializeAgentExecutorWithOptions instead
13
14
  */
@@ -88,6 +89,16 @@ async function initializeAgentExecutorWithOptions(tools, llm, options = {
88
89
  });
89
90
  return executor;
90
91
  }
92
+ case "xml": {
93
+ const { agentArgs, tags, ...rest } = options;
94
+ const executor = executor_js_1.AgentExecutor.fromAgentAndTools({
95
+ tags: [...(tags ?? []), "xml"],
96
+ agent: index_js_6.XMLAgent.fromLLMAndTools(llm, tools, agentArgs),
97
+ tools,
98
+ ...rest,
99
+ });
100
+ return executor;
101
+ }
91
102
  case "structured-chat-zero-shot-react-description": {
92
103
  const { agentArgs, memory, tags, ...rest } = options;
93
104
  const executor = executor_js_1.AgentExecutor.fromAgentAndTools({
@@ -7,6 +7,7 @@ import { StructuredChatAgent } from "./structured_chat/index.js";
7
7
  import { AgentExecutor, AgentExecutorInput } from "./executor.js";
8
8
  import { ZeroShotAgent } from "./mrkl/index.js";
9
9
  import { OpenAIAgent } from "./openai/index.js";
10
+ import { XMLAgent } from "./xml/index.js";
10
11
  /**
11
12
  * Represents the type of an agent in LangChain. It can be
12
13
  * "zero-shot-react-description", "chat-zero-shot-react-description", or
@@ -31,6 +32,9 @@ export type InitializeAgentExecutorOptions = ({
31
32
  } & Omit<AgentExecutorInput, "agent" | "tools">) | ({
32
33
  agentType: "chat-conversational-react-description";
33
34
  agentArgs?: Parameters<typeof ChatConversationalAgent.fromLLMAndTools>[2];
35
+ } & Omit<AgentExecutorInput, "agent" | "tools">) | ({
36
+ agentType: "xml";
37
+ agentArgs?: Parameters<typeof XMLAgent.fromLLMAndTools>[2];
34
38
  } & Omit<AgentExecutorInput, "agent" | "tools">);
35
39
  /**
36
40
  * @interface
@@ -5,6 +5,7 @@ import { StructuredChatAgent } from "./structured_chat/index.js";
5
5
  import { AgentExecutor } from "./executor.js";
6
6
  import { ZeroShotAgent } from "./mrkl/index.js";
7
7
  import { OpenAIAgent } from "./openai/index.js";
8
+ import { XMLAgent } from "./xml/index.js";
8
9
  /**
9
10
  * @deprecated use initializeAgentExecutorWithOptions instead
10
11
  */
@@ -84,6 +85,16 @@ export async function initializeAgentExecutorWithOptions(tools, llm, options = {
84
85
  });
85
86
  return executor;
86
87
  }
88
+ case "xml": {
89
+ const { agentArgs, tags, ...rest } = options;
90
+ const executor = AgentExecutor.fromAgentAndTools({
91
+ tags: [...(tags ?? []), "xml"],
92
+ agent: XMLAgent.fromLLMAndTools(llm, tools, agentArgs),
93
+ tools,
94
+ ...rest,
95
+ });
96
+ return executor;
97
+ }
87
98
  case "structured-chat-zero-shot-react-description": {
88
99
  const { agentArgs, memory, tags, ...rest } = options;
89
100
  const executor = AgentExecutor.fromAgentAndTools({
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XMLAgent = exports.parseOutput = void 0;
4
+ const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
5
+ const chat_js_1 = require("../../prompts/chat.cjs");
6
+ const agent_js_1 = require("../agent.cjs");
7
+ const output_parser_js_1 = require("../../schema/output_parser.cjs");
8
+ const prompt_js_1 = require("./prompt.cjs");
9
+ /**
10
+ * Parses the output text from the agent and returns an AgentAction or
11
+ * AgentFinish object.
12
+ * @param text The output text from the agent.
13
+ * @returns An AgentAction or AgentFinish object.
14
+ */
15
+ async function parseOutput(text) {
16
+ if (text.includes("</tool>")) {
17
+ const [tool, toolInput] = text.split("</tool>");
18
+ const _tool = tool.split("<tool>")[1];
19
+ const _toolInput = toolInput.split("<tool_input>")[1];
20
+ return { tool: _tool, toolInput: _toolInput, log: text };
21
+ }
22
+ else if (text.includes("<final_answer>")) {
23
+ const [, answer] = text.split("<final_answer>");
24
+ return { returnValues: { output: answer }, log: text };
25
+ }
26
+ else {
27
+ throw new output_parser_js_1.OutputParserException(`Could not parse LLM output: ${text}`);
28
+ }
29
+ }
30
+ exports.parseOutput = parseOutput;
31
+ /**
32
+ * Class that represents an agent that uses XML tags.
33
+ */
34
+ class XMLAgent extends agent_js_1.BaseSingleActionAgent {
35
+ static lc_name() {
36
+ return "XMLAgent";
37
+ }
38
+ _agentType() {
39
+ return "xml";
40
+ }
41
+ constructor(fields) {
42
+ super(fields);
43
+ Object.defineProperty(this, "lc_namespace", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: ["langchain", "agents", "xml"]
48
+ });
49
+ Object.defineProperty(this, "tools", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ Object.defineProperty(this, "llmChain", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ this.tools = fields.tools;
62
+ this.llmChain = fields.llmChain;
63
+ }
64
+ get inputKeys() {
65
+ return ["input"];
66
+ }
67
+ static createPrompt() {
68
+ return chat_js_1.ChatPromptTemplate.fromPromptMessages([
69
+ chat_js_1.HumanMessagePromptTemplate.fromTemplate(prompt_js_1.AGENT_INSTRUCTIONS),
70
+ chat_js_1.AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
71
+ ]);
72
+ }
73
+ /**
74
+ * Plans the next action or finish state of the agent based on the
75
+ * provided steps, inputs, and optional callback manager.
76
+ * @param steps The steps to consider in planning.
77
+ * @param inputs The inputs to consider in planning.
78
+ * @param callbackManager Optional CallbackManager to use in planning.
79
+ * @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
80
+ */
81
+ async plan(steps, inputs, callbackManager) {
82
+ let log = "";
83
+ for (const { action, observation } of steps) {
84
+ log += `<tool>${action.tool}</tool><tool_input>${action.toolInput}</tool_input><observation>${observation}</observation>`;
85
+ }
86
+ let tools = "";
87
+ for (const tool of this.tools) {
88
+ tools += `${tool.name}: ${tool.description}\n`;
89
+ }
90
+ const _inputs = {
91
+ intermediate_steps: log,
92
+ tools,
93
+ question: inputs.input,
94
+ stop: ["</tool_input>", "</final_answer>"],
95
+ };
96
+ const response = await this.llmChain.call(_inputs, callbackManager);
97
+ return parseOutput(response[this.llmChain.outputKey]);
98
+ }
99
+ /**
100
+ * Creates an XMLAgent from a BaseLanguageModel and a list of tools.
101
+ * @param llm The BaseLanguageModel to use.
102
+ * @param tools The tools to be used by the agent.
103
+ * @param args Optional arguments for creating the agent.
104
+ * @returns An instance of XMLAgent.
105
+ */
106
+ static fromLLMAndTools(llm, tools, args) {
107
+ const prompt = XMLAgent.createPrompt();
108
+ const chain = new llm_chain_js_1.LLMChain({
109
+ prompt,
110
+ llm,
111
+ callbacks: args?.callbacks,
112
+ });
113
+ return new XMLAgent({
114
+ llmChain: chain,
115
+ tools,
116
+ });
117
+ }
118
+ }
119
+ exports.XMLAgent = XMLAgent;
@@ -0,0 +1,51 @@
1
+ import { Tool } from "../../tools/base.js";
2
+ import { LLMChain } from "../../chains/llm_chain.js";
3
+ import { AgentStep, AgentAction, AgentFinish, ChainValues } from "../../schema/index.js";
4
+ import { ChatPromptTemplate } from "../../prompts/chat.js";
5
+ import { AgentArgs, BaseSingleActionAgent } from "../agent.js";
6
+ import { CallbackManager } from "../../callbacks/manager.js";
7
+ import { BaseLanguageModel } from "../../base_language/index.js";
8
+ /**
9
+ * Interface for the input to the XMLAgent class.
10
+ */
11
+ export interface XMLAgentInput {
12
+ tools: Tool[];
13
+ llmChain: LLMChain;
14
+ }
15
+ /**
16
+ * Parses the output text from the agent and returns an AgentAction or
17
+ * AgentFinish object.
18
+ * @param text The output text from the agent.
19
+ * @returns An AgentAction or AgentFinish object.
20
+ */
21
+ export declare function parseOutput(text: string): Promise<AgentAction | AgentFinish>;
22
+ /**
23
+ * Class that represents an agent that uses XML tags.
24
+ */
25
+ export declare class XMLAgent extends BaseSingleActionAgent implements XMLAgentInput {
26
+ static lc_name(): string;
27
+ lc_namespace: string[];
28
+ tools: Tool[];
29
+ llmChain: LLMChain;
30
+ _agentType(): "xml";
31
+ constructor(fields: XMLAgentInput);
32
+ get inputKeys(): string[];
33
+ static createPrompt(): ChatPromptTemplate<any, any>;
34
+ /**
35
+ * Plans the next action or finish state of the agent based on the
36
+ * provided steps, inputs, and optional callback manager.
37
+ * @param steps The steps to consider in planning.
38
+ * @param inputs The inputs to consider in planning.
39
+ * @param callbackManager Optional CallbackManager to use in planning.
40
+ * @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
41
+ */
42
+ plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager): Promise<AgentAction | AgentFinish>;
43
+ /**
44
+ * Creates an XMLAgent from a BaseLanguageModel and a list of tools.
45
+ * @param llm The BaseLanguageModel to use.
46
+ * @param tools The tools to be used by the agent.
47
+ * @param args Optional arguments for creating the agent.
48
+ * @returns An instance of XMLAgent.
49
+ */
50
+ static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: XMLAgentInput & Pick<AgentArgs, "callbacks">): XMLAgent;
51
+ }
@@ -0,0 +1,114 @@
1
+ import { LLMChain } from "../../chains/llm_chain.js";
2
+ import { AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, } from "../../prompts/chat.js";
3
+ import { BaseSingleActionAgent } from "../agent.js";
4
+ import { OutputParserException } from "../../schema/output_parser.js";
5
+ import { AGENT_INSTRUCTIONS } from "./prompt.js";
6
+ /**
7
+ * Parses the output text from the agent and returns an AgentAction or
8
+ * AgentFinish object.
9
+ * @param text The output text from the agent.
10
+ * @returns An AgentAction or AgentFinish object.
11
+ */
12
+ export async function parseOutput(text) {
13
+ if (text.includes("</tool>")) {
14
+ const [tool, toolInput] = text.split("</tool>");
15
+ const _tool = tool.split("<tool>")[1];
16
+ const _toolInput = toolInput.split("<tool_input>")[1];
17
+ return { tool: _tool, toolInput: _toolInput, log: text };
18
+ }
19
+ else if (text.includes("<final_answer>")) {
20
+ const [, answer] = text.split("<final_answer>");
21
+ return { returnValues: { output: answer }, log: text };
22
+ }
23
+ else {
24
+ throw new OutputParserException(`Could not parse LLM output: ${text}`);
25
+ }
26
+ }
27
+ /**
28
+ * Class that represents an agent that uses XML tags.
29
+ */
30
+ export class XMLAgent extends BaseSingleActionAgent {
31
+ static lc_name() {
32
+ return "XMLAgent";
33
+ }
34
+ _agentType() {
35
+ return "xml";
36
+ }
37
+ constructor(fields) {
38
+ super(fields);
39
+ Object.defineProperty(this, "lc_namespace", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: ["langchain", "agents", "xml"]
44
+ });
45
+ Object.defineProperty(this, "tools", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "llmChain", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ this.tools = fields.tools;
58
+ this.llmChain = fields.llmChain;
59
+ }
60
+ get inputKeys() {
61
+ return ["input"];
62
+ }
63
+ static createPrompt() {
64
+ return ChatPromptTemplate.fromPromptMessages([
65
+ HumanMessagePromptTemplate.fromTemplate(AGENT_INSTRUCTIONS),
66
+ AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
67
+ ]);
68
+ }
69
+ /**
70
+ * Plans the next action or finish state of the agent based on the
71
+ * provided steps, inputs, and optional callback manager.
72
+ * @param steps The steps to consider in planning.
73
+ * @param inputs The inputs to consider in planning.
74
+ * @param callbackManager Optional CallbackManager to use in planning.
75
+ * @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
76
+ */
77
+ async plan(steps, inputs, callbackManager) {
78
+ let log = "";
79
+ for (const { action, observation } of steps) {
80
+ log += `<tool>${action.tool}</tool><tool_input>${action.toolInput}</tool_input><observation>${observation}</observation>`;
81
+ }
82
+ let tools = "";
83
+ for (const tool of this.tools) {
84
+ tools += `${tool.name}: ${tool.description}\n`;
85
+ }
86
+ const _inputs = {
87
+ intermediate_steps: log,
88
+ tools,
89
+ question: inputs.input,
90
+ stop: ["</tool_input>", "</final_answer>"],
91
+ };
92
+ const response = await this.llmChain.call(_inputs, callbackManager);
93
+ return parseOutput(response[this.llmChain.outputKey]);
94
+ }
95
+ /**
96
+ * Creates an XMLAgent from a BaseLanguageModel and a list of tools.
97
+ * @param llm The BaseLanguageModel to use.
98
+ * @param tools The tools to be used by the agent.
99
+ * @param args Optional arguments for creating the agent.
100
+ * @returns An instance of XMLAgent.
101
+ */
102
+ static fromLLMAndTools(llm, tools, args) {
103
+ const prompt = XMLAgent.createPrompt();
104
+ const chain = new LLMChain({
105
+ prompt,
106
+ llm,
107
+ callbacks: args?.callbacks,
108
+ });
109
+ return new XMLAgent({
110
+ llmChain: chain,
111
+ tools,
112
+ });
113
+ }
114
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AGENT_INSTRUCTIONS = void 0;
4
+ exports.AGENT_INSTRUCTIONS = `You are a helpful assistant. Help the user answer any questions.
5
+
6
+ You have access to the following tools:
7
+
8
+ {tools}
9
+
10
+ In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags.
11
+ You will then get back a response in the form <observation></observation>
12
+ For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:
13
+
14
+ <tool>search</tool><tool_input>weather in SF</tool_input>
15
+ <observation>64 degrees</observation>
16
+
17
+ When you are done, respond with a final answer between <final_answer></final_answer>. For example:
18
+
19
+ <final_answer>The weather in SF is 64 degrees</final_answer>
20
+
21
+ Begin!
22
+
23
+ Question: {question}`;
@@ -0,0 +1 @@
1
+ export declare const AGENT_INSTRUCTIONS = "You are a helpful assistant. Help the user answer any questions.\n\nYou have access to the following tools:\n\n{tools}\n\nIn order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags.\nYou will then get back a response in the form <observation></observation>\nFor example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:\n\n<tool>search</tool><tool_input>weather in SF</tool_input>\n<observation>64 degrees</observation>\n\nWhen you are done, respond with a final answer between <final_answer></final_answer>. For example:\n\n<final_answer>The weather in SF is 64 degrees</final_answer>\n\nBegin!\n\nQuestion: {question}";
@@ -0,0 +1,20 @@
1
+ export const AGENT_INSTRUCTIONS = `You are a helpful assistant. Help the user answer any questions.
2
+
3
+ You have access to the following tools:
4
+
5
+ {tools}
6
+
7
+ In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags.
8
+ You will then get back a response in the form <observation></observation>
9
+ For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:
10
+
11
+ <tool>search</tool><tool_input>weather in SF</tool_input>
12
+ <observation>64 degrees</observation>
13
+
14
+ When you are done, respond with a final answer between <final_answer></final_answer>. For example:
15
+
16
+ <final_answer>The weather in SF is 64 degrees</final_answer>
17
+
18
+ Begin!
19
+
20
+ Question: {question}`;
@@ -68,11 +68,15 @@ declare abstract class BaseCallbackHandlerMethodsClass {
68
68
  /**
69
69
  * Called if a Chain run encounters an error
70
70
  */
71
- handleChainError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
71
+ handleChainError?(err: Error, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
72
+ inputs?: Record<string, unknown>;
73
+ }): Promise<void> | void;
72
74
  /**
73
75
  * Called at the end of a Chain run, with the outputs and the run ID.
74
76
  */
75
- handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
77
+ handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
78
+ inputs?: Record<string, unknown>;
79
+ }): Promise<void> | void;
76
80
  /**
77
81
  * Called at the start of a Tool run, with the tool name and input
78
82
  * and the run ID.
@@ -207,11 +211,15 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
207
211
  /**
208
212
  * Called if a Chain run encounters an error
209
213
  */
210
- handleChainError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
214
+ handleChainError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
215
+ inputs?: Record<string, unknown> | undefined;
216
+ } | undefined): void | Promise<void>;
211
217
  /**
212
218
  * Called at the end of a Chain run, with the outputs and the run ID.
213
219
  */
214
- handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
220
+ handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
221
+ inputs?: Record<string, unknown> | undefined;
222
+ } | undefined): void | Promise<void>;
215
223
  /**
216
224
  * Called at the start of a Tool run, with the tool name and input
217
225
  * and the run ID.
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RunCollectorCallbackHandler = void 0;
4
+ const tracer_js_1 = require("./tracer.cjs");
5
+ /**
6
+ * A callback handler that collects traced runs and makes it easy to fetch the traced run object from calls through any langchain object.
7
+ * For instance, it makes it easy to fetch the run ID and then do things with that, such as log feedback.
8
+ */
9
+ class RunCollectorCallbackHandler extends tracer_js_1.BaseTracer {
10
+ /**
11
+ * Creates a new instance of the RunCollectorCallbackHandler class.
12
+ * @param exampleId The ID of the example.
13
+ */
14
+ constructor({ exampleId } = {}) {
15
+ super();
16
+ /** The name of the callback handler. */
17
+ Object.defineProperty(this, "name", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: "run_collector"
22
+ });
23
+ /** The ID of the example. */
24
+ Object.defineProperty(this, "exampleId", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ /** An array of traced runs. */
31
+ Object.defineProperty(this, "tracedRuns", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ this.exampleId = exampleId;
38
+ this.tracedRuns = [];
39
+ }
40
+ /**
41
+ * Persists the given run object.
42
+ * @param run The run object to persist.
43
+ */
44
+ async persistRun(run) {
45
+ const run_ = { ...run };
46
+ run_.reference_example_id = this.exampleId;
47
+ this.tracedRuns.push(run_);
48
+ }
49
+ }
50
+ exports.RunCollectorCallbackHandler = RunCollectorCallbackHandler;
@@ -0,0 +1,26 @@
1
+ import { BaseRun, Run } from "langsmith/schemas";
2
+ import { BaseTracer } from "./tracer.js";
3
+ /**
4
+ * A callback handler that collects traced runs and makes it easy to fetch the traced run object from calls through any langchain object.
5
+ * For instance, it makes it easy to fetch the run ID and then do things with that, such as log feedback.
6
+ */
7
+ export declare class RunCollectorCallbackHandler extends BaseTracer {
8
+ /** The name of the callback handler. */
9
+ name: string;
10
+ /** The ID of the example. */
11
+ exampleId?: string;
12
+ /** An array of traced runs. */
13
+ tracedRuns: Run[];
14
+ /**
15
+ * Creates a new instance of the RunCollectorCallbackHandler class.
16
+ * @param exampleId The ID of the example.
17
+ */
18
+ constructor({ exampleId }?: {
19
+ exampleId?: string;
20
+ });
21
+ /**
22
+ * Persists the given run object.
23
+ * @param run The run object to persist.
24
+ */
25
+ protected persistRun(run: BaseRun): Promise<void>;
26
+ }
@@ -0,0 +1,46 @@
1
+ import { BaseTracer } from "./tracer.js";
2
+ /**
3
+ * A callback handler that collects traced runs and makes it easy to fetch the traced run object from calls through any langchain object.
4
+ * For instance, it makes it easy to fetch the run ID and then do things with that, such as log feedback.
5
+ */
6
+ export class RunCollectorCallbackHandler extends BaseTracer {
7
+ /**
8
+ * Creates a new instance of the RunCollectorCallbackHandler class.
9
+ * @param exampleId The ID of the example.
10
+ */
11
+ constructor({ exampleId } = {}) {
12
+ super();
13
+ /** The name of the callback handler. */
14
+ Object.defineProperty(this, "name", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: "run_collector"
19
+ });
20
+ /** The ID of the example. */
21
+ Object.defineProperty(this, "exampleId", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ /** An array of traced runs. */
28
+ Object.defineProperty(this, "tracedRuns", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ this.exampleId = exampleId;
35
+ this.tracedRuns = [];
36
+ }
37
+ /**
38
+ * Persists the given run object.
39
+ * @param run The run object to persist.
40
+ */
41
+ async persistRun(run) {
42
+ const run_ = { ...run };
43
+ run_.reference_example_id = this.exampleId;
44
+ this.tracedRuns.push(run_);
45
+ }
46
+ }
@@ -2,6 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseTracer = void 0;
4
4
  const base_js_1 = require("../base.cjs");
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ function _coerceToDict(value, defaultKey) {
7
+ return value && !Array.isArray(value) && typeof value === "object"
8
+ ? value
9
+ : { [defaultKey]: value };
10
+ }
5
11
  class BaseTracer extends base_js_1.BaseCallbackHandler {
6
12
  constructor(_fields) {
7
13
  super(...arguments);
@@ -23,6 +29,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
23
29
  const parentRun = this.runMap.get(run.parent_run_id);
24
30
  if (parentRun) {
25
31
  this._addChildRun(parentRun, run);
32
+ parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
26
33
  }
27
34
  }
28
35
  this.runMap.set(run.id, run);
@@ -157,21 +164,24 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
157
164
  this._startTrace(run);
158
165
  await this.onChainStart?.(run);
159
166
  }
160
- async handleChainEnd(outputs, runId) {
167
+ async handleChainEnd(outputs, runId, _parentRunId, _tags, kwargs) {
161
168
  const run = this.runMap.get(runId);
162
169
  if (!run) {
163
170
  throw new Error("No chain run to end.");
164
171
  }
165
172
  run.end_time = Date.now();
166
- run.outputs = outputs;
173
+ run.outputs = _coerceToDict(outputs, "output");
167
174
  run.events.push({
168
175
  name: "end",
169
176
  time: new Date(run.end_time).toISOString(),
170
177
  });
178
+ if (kwargs?.inputs !== undefined) {
179
+ run.inputs = _coerceToDict(kwargs.inputs, "input");
180
+ }
171
181
  await this.onChainEnd?.(run);
172
182
  await this._endTrace(run);
173
183
  }
174
- async handleChainError(error, runId) {
184
+ async handleChainError(error, runId, _parentRunId, _tags, kwargs) {
175
185
  const run = this.runMap.get(runId);
176
186
  if (!run) {
177
187
  throw new Error("No chain run to end.");
@@ -182,6 +192,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
182
192
  name: "error",
183
193
  time: new Date(run.end_time).toISOString(),
184
194
  });
195
+ if (kwargs?.inputs !== undefined) {
196
+ run.inputs = _coerceToDict(kwargs.inputs, "input");
197
+ }
185
198
  await this.onChainError?.(run);
186
199
  await this._endTrace(run);
187
200
  }
@@ -33,8 +33,12 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
33
33
  handleLLMEnd(output: LLMResult, runId: string): Promise<void>;
34
34
  handleLLMError(error: Error, runId: string): Promise<void>;
35
35
  handleChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string): Promise<void>;
36
- handleChainEnd(outputs: ChainValues, runId: string): Promise<void>;
37
- handleChainError(error: Error, runId: string): Promise<void>;
36
+ handleChainEnd(outputs: ChainValues, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
37
+ inputs?: Record<string, unknown>;
38
+ }): Promise<void>;
39
+ handleChainError(error: Error, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
40
+ inputs?: Record<string, unknown>;
41
+ }): Promise<void>;
38
42
  handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap): Promise<void>;
39
43
  handleToolEnd(output: string, runId: string): Promise<void>;
40
44
  handleToolError(error: Error, runId: string): Promise<void>;