langchain 0.0.76 → 0.0.77
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/chains/query_constructor/ir.cjs +1 -0
- package/chains/query_constructor/ir.d.ts +1 -0
- package/chains/query_constructor/ir.js +1 -0
- package/chains/query_constructor.cjs +1 -0
- package/chains/query_constructor.d.ts +1 -0
- package/chains/query_constructor.js +1 -0
- package/dist/agents/chat_convo/index.cjs +27 -11
- package/dist/agents/chat_convo/index.d.ts +4 -1
- package/dist/agents/chat_convo/index.js +28 -12
- package/dist/agents/chat_convo/outputParser.cjs +79 -7
- package/dist/agents/chat_convo/outputParser.d.ts +25 -13
- package/dist/agents/chat_convo/outputParser.js +77 -6
- package/dist/agents/chat_convo/prompt.cjs +11 -8
- package/dist/agents/chat_convo/prompt.d.ts +2 -2
- package/dist/agents/chat_convo/prompt.js +11 -8
- package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
- package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
- package/dist/callbacks/handlers/tracer_langchain.js +12 -4
- package/dist/callbacks/manager.cjs +6 -2
- package/dist/callbacks/manager.js +6 -2
- package/dist/chains/query_constructor/index.cjs +105 -0
- package/dist/chains/query_constructor/index.d.ts +37 -0
- package/dist/chains/query_constructor/index.js +95 -0
- package/dist/chains/query_constructor/ir.cjs +116 -0
- package/dist/chains/query_constructor/ir.d.ts +60 -0
- package/dist/chains/query_constructor/ir.js +107 -0
- package/dist/chains/query_constructor/parser.cjs +103 -0
- package/dist/chains/query_constructor/parser.d.ts +12 -0
- package/dist/chains/query_constructor/parser.js +99 -0
- package/dist/chains/query_constructor/prompt.cjs +127 -0
- package/dist/chains/query_constructor/prompt.d.ts +15 -0
- package/dist/chains/query_constructor/prompt.js +124 -0
- package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
- package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
- package/dist/chains/sql_db/sql_db_chain.js +13 -0
- package/dist/client/langchainplus.cjs +21 -15
- package/dist/client/langchainplus.d.ts +4 -2
- package/dist/client/langchainplus.js +21 -15
- package/dist/llms/sagemaker_endpoint.cjs +123 -0
- package/dist/llms/sagemaker_endpoint.d.ts +82 -0
- package/dist/llms/sagemaker_endpoint.js +118 -0
- package/dist/memory/buffer_memory.cjs +1 -1
- package/dist/memory/buffer_memory.js +1 -1
- package/dist/memory/buffer_window_memory.cjs +1 -1
- package/dist/memory/buffer_window_memory.js +1 -1
- package/dist/output_parsers/expression.cjs +68 -0
- package/dist/output_parsers/expression.d.ts +25 -0
- package/dist/output_parsers/expression.js +49 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
- package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
- package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
- package/dist/output_parsers/expression_type_handlers/base.js +62 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
- package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
- package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
- package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
- package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
- package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
- package/dist/output_parsers/expression_type_handlers/types.js +1 -0
- package/dist/output_parsers/index.cjs +2 -1
- package/dist/output_parsers/index.d.ts +1 -1
- package/dist/output_parsers/index.js +1 -1
- package/dist/output_parsers/structured.cjs +81 -23
- package/dist/output_parsers/structured.d.ts +18 -0
- package/dist/output_parsers/structured.js +79 -22
- package/dist/retrievers/self_query/index.cjs +79 -0
- package/dist/retrievers/self_query/index.d.ts +33 -0
- package/dist/retrievers/self_query/index.js +74 -0
- package/dist/retrievers/self_query/translator.cjs +72 -0
- package/dist/retrievers/self_query/translator.d.ts +14 -0
- package/dist/retrievers/self_query/translator.js +67 -0
- package/dist/schema/query_constructor.cjs +26 -0
- package/dist/schema/query_constructor.d.ts +6 -0
- package/dist/schema/query_constructor.js +22 -0
- package/dist/tools/json.cjs +3 -1
- package/dist/tools/json.js +3 -1
- package/dist/util/event-source-parse.cjs +31 -5
- package/dist/util/event-source-parse.d.ts +3 -3
- package/dist/util/event-source-parse.js +31 -5
- package/llms/sagemaker_endpoint.cjs +1 -0
- package/llms/sagemaker_endpoint.d.ts +1 -0
- package/llms/sagemaker_endpoint.js +1 -0
- package/output_parsers/expression.cjs +1 -0
- package/output_parsers/expression.d.ts +1 -0
- package/output_parsers/expression.js +1 -0
- package/package.json +61 -3
- package/retrievers/self_query.cjs +1 -0
- package/retrievers/self_query.d.ts +1 -0
- package/retrievers/self_query.js +1 -0
- package/schema/query_constructor.cjs +1 -0
- package/schema/query_constructor.d.ts +1 -0
- package/schema/query_constructor.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/chains/query_constructor/ir.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/chains/query_constructor/ir.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/chains/query_constructor/ir.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/chains/query_constructor/index.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chains/query_constructor/index.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chains/query_constructor/index.js'
|
|
@@ -47,8 +47,15 @@ class ChatConversationalAgent extends agent_js_1.Agent {
|
|
|
47
47
|
}
|
|
48
48
|
return thoughts;
|
|
49
49
|
}
|
|
50
|
-
static getDefaultOutputParser(
|
|
51
|
-
|
|
50
|
+
static getDefaultOutputParser(fields) {
|
|
51
|
+
if (fields?.llm) {
|
|
52
|
+
return outputParser_js_1.ChatConversationalAgentOutputParserWithRetries.fromLLM(fields.llm, {
|
|
53
|
+
toolNames: fields.toolNames,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return new outputParser_js_1.ChatConversationalAgentOutputParserWithRetries({
|
|
57
|
+
toolNames: fields?.toolNames,
|
|
58
|
+
});
|
|
52
59
|
}
|
|
53
60
|
/**
|
|
54
61
|
* Create prompt in the style of the ChatConversationAgent.
|
|
@@ -57,39 +64,48 @@ class ChatConversationalAgent extends agent_js_1.Agent {
|
|
|
57
64
|
* @param args - Arguments to create the prompt with.
|
|
58
65
|
* @param args.systemMessage - String to put before the list of tools.
|
|
59
66
|
* @param args.humanMessage - String to put after the list of tools.
|
|
67
|
+
* @param args.outputParser - Output parser to use for formatting.
|
|
60
68
|
*/
|
|
61
69
|
static createPrompt(tools, args) {
|
|
62
70
|
const systemMessage = (args?.systemMessage ?? prompt_js_1.DEFAULT_PREFIX) + prompt_js_1.PREFIX_END;
|
|
63
71
|
const humanMessage = args?.humanMessage ?? prompt_js_1.DEFAULT_SUFFIX;
|
|
64
|
-
const outputParser = args?.outputParser ?? new outputParser_js_1.ChatConversationalAgentOutputParser();
|
|
65
72
|
const toolStrings = tools
|
|
66
73
|
.map((tool) => `${tool.name}: ${tool.description}`)
|
|
67
74
|
.join("\n");
|
|
68
|
-
const
|
|
69
|
-
|
|
75
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
76
|
+
const outputParser = args?.outputParser ??
|
|
77
|
+
ChatConversationalAgent.getDefaultOutputParser({ toolNames });
|
|
78
|
+
const formatInstructions = outputParser.getFormatInstructions({
|
|
79
|
+
toolNames,
|
|
70
80
|
});
|
|
71
|
-
const
|
|
72
|
-
|
|
81
|
+
const renderedHumanMessage = (0, template_js_1.renderTemplate)(humanMessage, "f-string", {
|
|
82
|
+
format_instructions: formatInstructions,
|
|
73
83
|
tools: toolStrings,
|
|
74
|
-
tool_names: toolNames,
|
|
75
84
|
});
|
|
76
85
|
const messages = [
|
|
77
86
|
chat_js_1.SystemMessagePromptTemplate.fromTemplate(systemMessage),
|
|
78
87
|
new chat_js_1.MessagesPlaceholder("chat_history"),
|
|
79
|
-
chat_js_1.HumanMessagePromptTemplate.fromTemplate(
|
|
88
|
+
chat_js_1.HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
|
|
80
89
|
new chat_js_1.MessagesPlaceholder("agent_scratchpad"),
|
|
81
90
|
];
|
|
82
91
|
return chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
|
|
83
92
|
}
|
|
84
93
|
static fromLLMAndTools(llm, tools, args) {
|
|
85
94
|
ChatConversationalAgent.validateTools(tools);
|
|
86
|
-
const
|
|
95
|
+
const outputParser = args?.outputParser ??
|
|
96
|
+
ChatConversationalAgent.getDefaultOutputParser({
|
|
97
|
+
llm,
|
|
98
|
+
toolNames: tools.map((tool) => tool.name),
|
|
99
|
+
});
|
|
100
|
+
const prompt = ChatConversationalAgent.createPrompt(tools, {
|
|
101
|
+
...args,
|
|
102
|
+
outputParser,
|
|
103
|
+
});
|
|
87
104
|
const chain = new llm_chain_js_1.LLMChain({
|
|
88
105
|
prompt,
|
|
89
106
|
llm,
|
|
90
107
|
callbacks: args?.callbacks ?? args?.callbackManager,
|
|
91
108
|
});
|
|
92
|
-
const outputParser = args?.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
|
|
93
109
|
return new ChatConversationalAgent({
|
|
94
110
|
llmChain: chain,
|
|
95
111
|
outputParser,
|
|
@@ -28,7 +28,9 @@ export declare class ChatConversationalAgent extends Agent {
|
|
|
28
28
|
_stop(): string[];
|
|
29
29
|
static validateTools(tools: Tool[]): void;
|
|
30
30
|
constructScratchPad(steps: AgentStep[]): Promise<BaseChatMessage[]>;
|
|
31
|
-
static getDefaultOutputParser(
|
|
31
|
+
static getDefaultOutputParser(fields?: OutputParserArgs & {
|
|
32
|
+
toolNames: string[];
|
|
33
|
+
}): AgentActionOutputParser;
|
|
32
34
|
/**
|
|
33
35
|
* Create prompt in the style of the ChatConversationAgent.
|
|
34
36
|
*
|
|
@@ -36,6 +38,7 @@ export declare class ChatConversationalAgent extends Agent {
|
|
|
36
38
|
* @param args - Arguments to create the prompt with.
|
|
37
39
|
* @param args.systemMessage - String to put before the list of tools.
|
|
38
40
|
* @param args.humanMessage - String to put after the list of tools.
|
|
41
|
+
* @param args.outputParser - Output parser to use for formatting.
|
|
39
42
|
*/
|
|
40
43
|
static createPrompt(tools: Tool[], args?: ChatConversationalCreatePromptArgs): ChatPromptTemplate;
|
|
41
44
|
static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: ChatConversationalCreatePromptArgs & AgentArgs): ChatConversationalAgent;
|
|
@@ -3,7 +3,7 @@ import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, Sy
|
|
|
3
3
|
import { renderTemplate } from "../../prompts/template.js";
|
|
4
4
|
import { AIChatMessage, HumanChatMessage, } from "../../schema/index.js";
|
|
5
5
|
import { Agent } from "../agent.js";
|
|
6
|
-
import {
|
|
6
|
+
import { ChatConversationalAgentOutputParserWithRetries } from "./outputParser.js";
|
|
7
7
|
import { PREFIX_END, DEFAULT_PREFIX, DEFAULT_SUFFIX, TEMPLATE_TOOL_RESPONSE, } from "./prompt.js";
|
|
8
8
|
/**
|
|
9
9
|
* Agent for the MRKL chain.
|
|
@@ -44,8 +44,15 @@ export class ChatConversationalAgent extends Agent {
|
|
|
44
44
|
}
|
|
45
45
|
return thoughts;
|
|
46
46
|
}
|
|
47
|
-
static getDefaultOutputParser(
|
|
48
|
-
|
|
47
|
+
static getDefaultOutputParser(fields) {
|
|
48
|
+
if (fields?.llm) {
|
|
49
|
+
return ChatConversationalAgentOutputParserWithRetries.fromLLM(fields.llm, {
|
|
50
|
+
toolNames: fields.toolNames,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return new ChatConversationalAgentOutputParserWithRetries({
|
|
54
|
+
toolNames: fields?.toolNames,
|
|
55
|
+
});
|
|
49
56
|
}
|
|
50
57
|
/**
|
|
51
58
|
* Create prompt in the style of the ChatConversationAgent.
|
|
@@ -54,39 +61,48 @@ export class ChatConversationalAgent extends Agent {
|
|
|
54
61
|
* @param args - Arguments to create the prompt with.
|
|
55
62
|
* @param args.systemMessage - String to put before the list of tools.
|
|
56
63
|
* @param args.humanMessage - String to put after the list of tools.
|
|
64
|
+
* @param args.outputParser - Output parser to use for formatting.
|
|
57
65
|
*/
|
|
58
66
|
static createPrompt(tools, args) {
|
|
59
67
|
const systemMessage = (args?.systemMessage ?? DEFAULT_PREFIX) + PREFIX_END;
|
|
60
68
|
const humanMessage = args?.humanMessage ?? DEFAULT_SUFFIX;
|
|
61
|
-
const outputParser = args?.outputParser ?? new ChatConversationalAgentOutputParser();
|
|
62
69
|
const toolStrings = tools
|
|
63
70
|
.map((tool) => `${tool.name}: ${tool.description}`)
|
|
64
71
|
.join("\n");
|
|
65
|
-
const
|
|
66
|
-
|
|
72
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
73
|
+
const outputParser = args?.outputParser ??
|
|
74
|
+
ChatConversationalAgent.getDefaultOutputParser({ toolNames });
|
|
75
|
+
const formatInstructions = outputParser.getFormatInstructions({
|
|
76
|
+
toolNames,
|
|
67
77
|
});
|
|
68
|
-
const
|
|
69
|
-
|
|
78
|
+
const renderedHumanMessage = renderTemplate(humanMessage, "f-string", {
|
|
79
|
+
format_instructions: formatInstructions,
|
|
70
80
|
tools: toolStrings,
|
|
71
|
-
tool_names: toolNames,
|
|
72
81
|
});
|
|
73
82
|
const messages = [
|
|
74
83
|
SystemMessagePromptTemplate.fromTemplate(systemMessage),
|
|
75
84
|
new MessagesPlaceholder("chat_history"),
|
|
76
|
-
HumanMessagePromptTemplate.fromTemplate(
|
|
85
|
+
HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
|
|
77
86
|
new MessagesPlaceholder("agent_scratchpad"),
|
|
78
87
|
];
|
|
79
88
|
return ChatPromptTemplate.fromPromptMessages(messages);
|
|
80
89
|
}
|
|
81
90
|
static fromLLMAndTools(llm, tools, args) {
|
|
82
91
|
ChatConversationalAgent.validateTools(tools);
|
|
83
|
-
const
|
|
92
|
+
const outputParser = args?.outputParser ??
|
|
93
|
+
ChatConversationalAgent.getDefaultOutputParser({
|
|
94
|
+
llm,
|
|
95
|
+
toolNames: tools.map((tool) => tool.name),
|
|
96
|
+
});
|
|
97
|
+
const prompt = ChatConversationalAgent.createPrompt(tools, {
|
|
98
|
+
...args,
|
|
99
|
+
outputParser,
|
|
100
|
+
});
|
|
84
101
|
const chain = new LLMChain({
|
|
85
102
|
prompt,
|
|
86
103
|
llm,
|
|
87
104
|
callbacks: args?.callbacks ?? args?.callbackManager,
|
|
88
105
|
});
|
|
89
|
-
const outputParser = args?.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
|
|
90
106
|
return new ChatConversationalAgent({
|
|
91
107
|
llmChain: chain,
|
|
92
108
|
outputParser,
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ChatConversationalAgentOutputParser = void 0;
|
|
3
|
+
exports.ChatConversationalAgentOutputParserWithRetries = exports.ChatConversationalAgentOutputParser = void 0;
|
|
4
|
+
const output_parser_js_1 = require("../../schema/output_parser.cjs");
|
|
5
|
+
const template_js_1 = require("../../prompts/template.cjs");
|
|
4
6
|
const types_js_1 = require("../types.cjs");
|
|
5
7
|
const prompt_js_1 = require("./prompt.cjs");
|
|
8
|
+
const fix_js_1 = require("../../output_parsers/fix.cjs");
|
|
6
9
|
class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputParser {
|
|
10
|
+
constructor(toolNames) {
|
|
11
|
+
super();
|
|
12
|
+
Object.defineProperty(this, "toolNames", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: toolNames
|
|
17
|
+
});
|
|
18
|
+
}
|
|
7
19
|
async parse(text) {
|
|
8
20
|
let jsonOutput = text.trim();
|
|
9
21
|
if (jsonOutput.includes("```json")) {
|
|
@@ -17,15 +29,75 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
|
|
|
17
29
|
if (lastIndex !== -1) {
|
|
18
30
|
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
19
31
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
try {
|
|
33
|
+
const response = JSON.parse(jsonOutput);
|
|
34
|
+
const { action, action_input } = response;
|
|
35
|
+
if (action === "Final Answer") {
|
|
36
|
+
return { returnValues: { output: action_input }, log: text };
|
|
37
|
+
}
|
|
38
|
+
return { tool: action, toolInput: action_input, log: text };
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
throw new output_parser_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
|
|
24
42
|
}
|
|
25
|
-
return { tool: action, toolInput: action_input, log: text };
|
|
26
43
|
}
|
|
27
44
|
getFormatInstructions() {
|
|
28
|
-
return prompt_js_1.FORMAT_INSTRUCTIONS
|
|
45
|
+
return (0, template_js_1.renderTemplate)(prompt_js_1.FORMAT_INSTRUCTIONS, "f-string", {
|
|
46
|
+
tool_names: this.toolNames.join(", "),
|
|
47
|
+
});
|
|
29
48
|
}
|
|
30
49
|
}
|
|
31
50
|
exports.ChatConversationalAgentOutputParser = ChatConversationalAgentOutputParser;
|
|
51
|
+
class ChatConversationalAgentOutputParserWithRetries extends types_js_1.AgentActionOutputParser {
|
|
52
|
+
constructor(fields) {
|
|
53
|
+
super();
|
|
54
|
+
Object.defineProperty(this, "baseParser", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(this, "outputFixingParser", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: void 0
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(this, "toolNames", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
writable: true,
|
|
70
|
+
value: []
|
|
71
|
+
});
|
|
72
|
+
this.toolNames = fields.toolNames ?? this.toolNames;
|
|
73
|
+
this.baseParser =
|
|
74
|
+
fields?.baseParser ??
|
|
75
|
+
new ChatConversationalAgentOutputParser(this.toolNames);
|
|
76
|
+
this.outputFixingParser = fields?.outputFixingParser;
|
|
77
|
+
}
|
|
78
|
+
getFormatInstructions(options) {
|
|
79
|
+
if (options.raw) {
|
|
80
|
+
return prompt_js_1.FORMAT_INSTRUCTIONS;
|
|
81
|
+
}
|
|
82
|
+
return (0, template_js_1.renderTemplate)(prompt_js_1.FORMAT_INSTRUCTIONS, "f-string", {
|
|
83
|
+
tool_names: options.toolNames.join(", "),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async parse(text) {
|
|
87
|
+
if (this.outputFixingParser !== undefined) {
|
|
88
|
+
return this.outputFixingParser.parse(text);
|
|
89
|
+
}
|
|
90
|
+
return this.baseParser.parse(text);
|
|
91
|
+
}
|
|
92
|
+
static fromLLM(llm, options) {
|
|
93
|
+
const baseParser = options.baseParser ??
|
|
94
|
+
new ChatConversationalAgentOutputParser(options.toolNames ?? []);
|
|
95
|
+
const outputFixingParser = fix_js_1.OutputFixingParser.fromLLM(llm, baseParser);
|
|
96
|
+
return new ChatConversationalAgentOutputParserWithRetries({
|
|
97
|
+
baseParser,
|
|
98
|
+
outputFixingParser,
|
|
99
|
+
toolNames: options.toolNames,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.ChatConversationalAgentOutputParserWithRetries = ChatConversationalAgentOutputParserWithRetries;
|
|
@@ -1,17 +1,29 @@
|
|
|
1
|
+
import { FormatInstructionsOptions } from "../../schema/output_parser.js";
|
|
1
2
|
import { AgentActionOutputParser } from "../types.js";
|
|
3
|
+
import { AgentAction, AgentFinish } from "../../schema/index.js";
|
|
4
|
+
import { OutputFixingParser } from "../../output_parsers/fix.js";
|
|
5
|
+
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
6
|
+
export type ChatConversationalAgentOutputParserFormatInstructionsOptions = FormatInstructionsOptions & {
|
|
7
|
+
toolNames: string[];
|
|
8
|
+
raw?: boolean;
|
|
9
|
+
};
|
|
2
10
|
export declare class ChatConversationalAgentOutputParser extends AgentActionOutputParser {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
log: string;
|
|
8
|
-
tool?: undefined;
|
|
9
|
-
toolInput?: undefined;
|
|
10
|
-
} | {
|
|
11
|
-
tool: any;
|
|
12
|
-
toolInput: any;
|
|
13
|
-
log: string;
|
|
14
|
-
returnValues?: undefined;
|
|
15
|
-
}>;
|
|
11
|
+
private toolNames;
|
|
12
|
+
constructor(toolNames: string[]);
|
|
13
|
+
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
16
14
|
getFormatInstructions(): string;
|
|
17
15
|
}
|
|
16
|
+
export type ChatConversationalAgentOutputParserArgs = {
|
|
17
|
+
baseParser?: ChatConversationalAgentOutputParser;
|
|
18
|
+
outputFixingParser?: OutputFixingParser<AgentAction | AgentFinish>;
|
|
19
|
+
toolNames?: string[];
|
|
20
|
+
};
|
|
21
|
+
export declare class ChatConversationalAgentOutputParserWithRetries extends AgentActionOutputParser {
|
|
22
|
+
private baseParser;
|
|
23
|
+
private outputFixingParser?;
|
|
24
|
+
private toolNames;
|
|
25
|
+
constructor(fields: ChatConversationalAgentOutputParserArgs);
|
|
26
|
+
getFormatInstructions(options: ChatConversationalAgentOutputParserFormatInstructionsOptions): string;
|
|
27
|
+
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
28
|
+
static fromLLM(llm: BaseLanguageModel, options: Omit<ChatConversationalAgentOutputParserArgs, "outputFixingParser">): ChatConversationalAgentOutputParserWithRetries;
|
|
29
|
+
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
import { OutputParserException, } from "../../schema/output_parser.js";
|
|
2
|
+
import { renderTemplate } from "../../prompts/template.js";
|
|
1
3
|
import { AgentActionOutputParser } from "../types.js";
|
|
2
4
|
import { FORMAT_INSTRUCTIONS } from "./prompt.js";
|
|
5
|
+
import { OutputFixingParser } from "../../output_parsers/fix.js";
|
|
3
6
|
export class ChatConversationalAgentOutputParser extends AgentActionOutputParser {
|
|
7
|
+
constructor(toolNames) {
|
|
8
|
+
super();
|
|
9
|
+
Object.defineProperty(this, "toolNames", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: toolNames
|
|
14
|
+
});
|
|
15
|
+
}
|
|
4
16
|
async parse(text) {
|
|
5
17
|
let jsonOutput = text.trim();
|
|
6
18
|
if (jsonOutput.includes("```json")) {
|
|
@@ -14,14 +26,73 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
|
|
|
14
26
|
if (lastIndex !== -1) {
|
|
15
27
|
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
try {
|
|
30
|
+
const response = JSON.parse(jsonOutput);
|
|
31
|
+
const { action, action_input } = response;
|
|
32
|
+
if (action === "Final Answer") {
|
|
33
|
+
return { returnValues: { output: action_input }, log: text };
|
|
34
|
+
}
|
|
35
|
+
return { tool: action, toolInput: action_input, log: text };
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
|
|
21
39
|
}
|
|
22
|
-
return { tool: action, toolInput: action_input, log: text };
|
|
23
40
|
}
|
|
24
41
|
getFormatInstructions() {
|
|
25
|
-
return FORMAT_INSTRUCTIONS
|
|
42
|
+
return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
|
|
43
|
+
tool_names: this.toolNames.join(", "),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class ChatConversationalAgentOutputParserWithRetries extends AgentActionOutputParser {
|
|
48
|
+
constructor(fields) {
|
|
49
|
+
super();
|
|
50
|
+
Object.defineProperty(this, "baseParser", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "outputFixingParser", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "toolNames", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: []
|
|
67
|
+
});
|
|
68
|
+
this.toolNames = fields.toolNames ?? this.toolNames;
|
|
69
|
+
this.baseParser =
|
|
70
|
+
fields?.baseParser ??
|
|
71
|
+
new ChatConversationalAgentOutputParser(this.toolNames);
|
|
72
|
+
this.outputFixingParser = fields?.outputFixingParser;
|
|
73
|
+
}
|
|
74
|
+
getFormatInstructions(options) {
|
|
75
|
+
if (options.raw) {
|
|
76
|
+
return FORMAT_INSTRUCTIONS;
|
|
77
|
+
}
|
|
78
|
+
return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
|
|
79
|
+
tool_names: options.toolNames.join(", "),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async parse(text) {
|
|
83
|
+
if (this.outputFixingParser !== undefined) {
|
|
84
|
+
return this.outputFixingParser.parse(text);
|
|
85
|
+
}
|
|
86
|
+
return this.baseParser.parse(text);
|
|
87
|
+
}
|
|
88
|
+
static fromLLM(llm, options) {
|
|
89
|
+
const baseParser = options.baseParser ??
|
|
90
|
+
new ChatConversationalAgentOutputParser(options.toolNames ?? []);
|
|
91
|
+
const outputFixingParser = OutputFixingParser.fromLLM(llm, baseParser);
|
|
92
|
+
return new ChatConversationalAgentOutputParserWithRetries({
|
|
93
|
+
baseParser,
|
|
94
|
+
outputFixingParser,
|
|
95
|
+
toolNames: options.toolNames,
|
|
96
|
+
});
|
|
26
97
|
}
|
|
27
98
|
}
|
|
@@ -12,7 +12,7 @@ exports.PREFIX_END = ` However, above all else, all responses must adhere to the
|
|
|
12
12
|
exports.FORMAT_INSTRUCTIONS = `RESPONSE FORMAT INSTRUCTIONS
|
|
13
13
|
----------------------------
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Output a JSON markdown code snippet containing a valid JSON object in one of two formats:
|
|
16
16
|
|
|
17
17
|
**Option 1:**
|
|
18
18
|
Use this if you want the human to use a tool.
|
|
@@ -20,25 +20,28 @@ Markdown code snippet formatted in the following schema:
|
|
|
20
20
|
|
|
21
21
|
\`\`\`json
|
|
22
22
|
{{{{
|
|
23
|
-
"action": string
|
|
24
|
-
"action_input": string
|
|
23
|
+
"action": string // The action to take. Must be one of [{tool_names}]
|
|
24
|
+
"action_input": string // The input to the action. May be a stringified object.
|
|
25
25
|
}}}}
|
|
26
26
|
\`\`\`
|
|
27
27
|
|
|
28
28
|
**Option #2:**
|
|
29
|
-
Use this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:
|
|
29
|
+
Use this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:
|
|
30
30
|
|
|
31
31
|
\`\`\`json
|
|
32
32
|
{{{{
|
|
33
33
|
"action": "Final Answer",
|
|
34
|
-
"action_input": string
|
|
34
|
+
"action_input": string // You should put what you want to return to use here and make sure to use valid json newline characters.
|
|
35
35
|
}}}}
|
|
36
|
-
|
|
36
|
+
\`\`\`
|
|
37
|
+
|
|
38
|
+
For both options, remember to always include the surrounding markdown code snippet delimiters (begin with "\`\`\`json" and end with "\`\`\`")!
|
|
39
|
+
`;
|
|
37
40
|
exports.DEFAULT_SUFFIX = `TOOLS
|
|
38
41
|
------
|
|
39
42
|
Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:
|
|
40
43
|
|
|
41
|
-
{
|
|
44
|
+
{tools}
|
|
42
45
|
|
|
43
46
|
{format_instructions}
|
|
44
47
|
|
|
@@ -46,7 +49,7 @@ USER'S INPUT
|
|
|
46
49
|
--------------------
|
|
47
50
|
Here is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):
|
|
48
51
|
|
|
49
|
-
{{
|
|
52
|
+
{{input}}`;
|
|
50
53
|
exports.TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
|
|
51
54
|
---------------------
|
|
52
55
|
{observation}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const DEFAULT_PREFIX = "Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.";
|
|
2
2
|
export declare const PREFIX_END = " However, above all else, all responses must adhere to the format of RESPONSE FORMAT INSTRUCTIONS.";
|
|
3
|
-
export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\
|
|
4
|
-
export declare const DEFAULT_SUFFIX = "TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{
|
|
3
|
+
export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nOutput a JSON markdown code snippet containing a valid JSON object in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string // The action to take. Must be one of [{tool_names}]\n \"action_input\": string // The input to the action. May be a stringified object.\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string // You should put what you want to return to use here and make sure to use valid json newline characters.\n}}}}\n```\n\nFor both options, remember to always include the surrounding markdown code snippet delimiters (begin with \"```json\" and end with \"```\")!\n";
|
|
4
|
+
export declare const DEFAULT_SUFFIX = "TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{tools}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{input}}";
|
|
5
5
|
export declare const TEMPLATE_TOOL_RESPONSE = "TOOL RESPONSE:\n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my original question? If using information from tools, you must say it explicitly - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.";
|
|
@@ -9,7 +9,7 @@ export const PREFIX_END = ` However, above all else, all responses must adhere t
|
|
|
9
9
|
export const FORMAT_INSTRUCTIONS = `RESPONSE FORMAT INSTRUCTIONS
|
|
10
10
|
----------------------------
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Output a JSON markdown code snippet containing a valid JSON object in one of two formats:
|
|
13
13
|
|
|
14
14
|
**Option 1:**
|
|
15
15
|
Use this if you want the human to use a tool.
|
|
@@ -17,25 +17,28 @@ Markdown code snippet formatted in the following schema:
|
|
|
17
17
|
|
|
18
18
|
\`\`\`json
|
|
19
19
|
{{{{
|
|
20
|
-
"action": string
|
|
21
|
-
"action_input": string
|
|
20
|
+
"action": string // The action to take. Must be one of [{tool_names}]
|
|
21
|
+
"action_input": string // The input to the action. May be a stringified object.
|
|
22
22
|
}}}}
|
|
23
23
|
\`\`\`
|
|
24
24
|
|
|
25
25
|
**Option #2:**
|
|
26
|
-
Use this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:
|
|
26
|
+
Use this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:
|
|
27
27
|
|
|
28
28
|
\`\`\`json
|
|
29
29
|
{{{{
|
|
30
30
|
"action": "Final Answer",
|
|
31
|
-
"action_input": string
|
|
31
|
+
"action_input": string // You should put what you want to return to use here and make sure to use valid json newline characters.
|
|
32
32
|
}}}}
|
|
33
|
-
|
|
33
|
+
\`\`\`
|
|
34
|
+
|
|
35
|
+
For both options, remember to always include the surrounding markdown code snippet delimiters (begin with "\`\`\`json" and end with "\`\`\`")!
|
|
36
|
+
`;
|
|
34
37
|
export const DEFAULT_SUFFIX = `TOOLS
|
|
35
38
|
------
|
|
36
39
|
Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:
|
|
37
40
|
|
|
38
|
-
{
|
|
41
|
+
{tools}
|
|
39
42
|
|
|
40
43
|
{format_instructions}
|
|
41
44
|
|
|
@@ -43,7 +46,7 @@ USER'S INPUT
|
|
|
43
46
|
--------------------
|
|
44
47
|
Here is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):
|
|
45
48
|
|
|
46
|
-
{{
|
|
49
|
+
{{input}}`;
|
|
47
50
|
export const TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
|
|
48
51
|
---------------------
|
|
49
52
|
{observation}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LangChainTracer = void 0;
|
|
4
|
+
const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
4
5
|
const env_js_1 = require("../../util/env.cjs");
|
|
5
6
|
const tracer_js_1 = require("./tracer.cjs");
|
|
6
7
|
class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
7
|
-
constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
|
|
8
|
+
constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, } = {}) {
|
|
8
9
|
super();
|
|
9
10
|
Object.defineProperty(this, "name", {
|
|
10
11
|
enumerable: true,
|
|
@@ -59,6 +60,12 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
59
60
|
writable: true,
|
|
60
61
|
value: void 0
|
|
61
62
|
});
|
|
63
|
+
Object.defineProperty(this, "caller", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: void 0
|
|
68
|
+
});
|
|
62
69
|
// eslint-disable-next-line no-process-env
|
|
63
70
|
if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
|
|
64
71
|
// eslint-disable-next-line no-process-env
|
|
@@ -79,6 +86,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
79
86
|
"default";
|
|
80
87
|
this.sessionExtra = sessionExtra;
|
|
81
88
|
this.exampleId = exampleId;
|
|
89
|
+
this.caller = new async_caller_js_1.AsyncCaller(callerParams ?? {});
|
|
82
90
|
}
|
|
83
91
|
async ensureSession() {
|
|
84
92
|
if (this.session) {
|
|
@@ -86,7 +94,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
86
94
|
}
|
|
87
95
|
const tenantId = await this.ensureTenantId();
|
|
88
96
|
const endpoint = `${this.endpoint}/sessions?upsert=true`;
|
|
89
|
-
const res = await fetch
|
|
97
|
+
const res = await this.caller.call(fetch, endpoint, {
|
|
90
98
|
method: "POST",
|
|
91
99
|
headers: this.headers,
|
|
92
100
|
body: JSON.stringify({
|
|
@@ -108,7 +116,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
108
116
|
return this.tenantId;
|
|
109
117
|
}
|
|
110
118
|
const endpoint = `${this.endpoint}/tenants`;
|
|
111
|
-
const response = await fetch
|
|
119
|
+
const response = await this.caller.call(fetch, endpoint, {
|
|
112
120
|
method: "GET",
|
|
113
121
|
headers: this.headers,
|
|
114
122
|
});
|
|
@@ -149,7 +157,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
149
157
|
async persistRun(run) {
|
|
150
158
|
const persistedRun = await this._convertToCreate(run, this.exampleId);
|
|
151
159
|
const endpoint = `${this.endpoint}/runs`;
|
|
152
|
-
const response = await fetch
|
|
160
|
+
const response = await this.caller.call(fetch, endpoint, {
|
|
153
161
|
method: "POST",
|
|
154
162
|
headers: this.headers,
|
|
155
163
|
body: JSON.stringify(persistedRun),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncCaller, AsyncCallerParams } from "../../util/async_caller.js";
|
|
1
2
|
import { BaseTracer, Run, BaseRun } from "./tracer.js";
|
|
2
3
|
export interface RunCreate extends BaseRun {
|
|
3
4
|
child_runs: this[];
|
|
@@ -21,6 +22,7 @@ export interface LangChainTracerFields {
|
|
|
21
22
|
tenantId?: string;
|
|
22
23
|
sessionName?: string;
|
|
23
24
|
sessionExtra?: Record<string, unknown>;
|
|
25
|
+
callerParams?: AsyncCallerParams;
|
|
24
26
|
}
|
|
25
27
|
export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
|
|
26
28
|
name: string;
|
|
@@ -31,7 +33,8 @@ export declare class LangChainTracer extends BaseTracer implements LangChainTrac
|
|
|
31
33
|
protected session: TracerSession;
|
|
32
34
|
exampleId?: string;
|
|
33
35
|
tenantId?: string;
|
|
34
|
-
|
|
36
|
+
caller: AsyncCaller;
|
|
37
|
+
constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, }?: LangChainTracerFields);
|
|
35
38
|
protected ensureSession(): Promise<TracerSession>;
|
|
36
39
|
protected ensureTenantId(): Promise<string>;
|
|
37
40
|
private _convertToCreate;
|