langchain 0.0.171 → 0.0.173
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/agents/format_scratchpad/log.cjs +1 -0
- package/agents/format_scratchpad/log.d.ts +1 -0
- package/agents/format_scratchpad/log.js +1 -0
- package/agents/format_scratchpad/log_to_message.cjs +1 -0
- package/agents/format_scratchpad/log_to_message.d.ts +1 -0
- package/agents/format_scratchpad/log_to_message.js +1 -0
- package/agents/format_scratchpad/xml.cjs +1 -0
- package/agents/format_scratchpad/xml.d.ts +1 -0
- package/agents/format_scratchpad/xml.js +1 -0
- package/agents/format_scratchpad.cjs +1 -1
- package/agents/format_scratchpad.d.ts +1 -1
- package/agents/format_scratchpad.js +1 -1
- package/agents/openai/output_parser.cjs +1 -0
- package/agents/openai/output_parser.d.ts +1 -0
- package/agents/openai/output_parser.js +1 -0
- package/agents/react/output_parser.cjs +1 -0
- package/agents/react/output_parser.d.ts +1 -0
- package/agents/react/output_parser.js +1 -0
- package/agents/xml/output_parser.cjs +1 -0
- package/agents/xml/output_parser.d.ts +1 -0
- package/agents/xml/output_parser.js +1 -0
- package/dist/agents/format_scratchpad/log.cjs +16 -0
- package/dist/agents/format_scratchpad/log.d.ts +9 -0
- package/dist/agents/format_scratchpad/log.js +12 -0
- package/dist/agents/format_scratchpad/log_to_message.cjs +22 -0
- package/dist/agents/format_scratchpad/log_to_message.d.ts +2 -0
- package/dist/agents/format_scratchpad/log_to_message.js +18 -0
- package/dist/agents/{format_scratchpad.cjs → format_scratchpad/openai_functions.cjs} +3 -3
- package/dist/agents/{format_scratchpad.d.ts → format_scratchpad/openai_functions.d.ts} +1 -1
- package/dist/agents/{format_scratchpad.js → format_scratchpad/openai_functions.js} +3 -3
- package/dist/agents/format_scratchpad/xml.cjs +12 -0
- package/dist/agents/format_scratchpad/xml.d.ts +2 -0
- package/dist/agents/format_scratchpad/xml.js +8 -0
- package/dist/agents/index.cjs +3 -1
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.js +1 -0
- package/dist/agents/openai/index.cjs +8 -31
- package/dist/agents/openai/index.d.ts +2 -0
- package/dist/agents/openai/index.js +8 -31
- package/dist/agents/openai/output_parser.cjs +65 -0
- package/dist/agents/openai/output_parser.d.ts +22 -0
- package/dist/agents/openai/output_parser.js +61 -0
- package/dist/agents/react/output_parser.cjs +96 -0
- package/dist/agents/react/output_parser.d.ts +47 -0
- package/dist/agents/react/output_parser.js +92 -0
- package/dist/agents/react/prompt.cjs +13 -0
- package/dist/agents/react/prompt.d.ts +1 -0
- package/dist/agents/react/prompt.js +10 -0
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs +2 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.js +2 -1
- package/dist/agents/xml/index.cjs +9 -25
- package/dist/agents/xml/index.d.ts +2 -7
- package/dist/agents/xml/index.js +8 -23
- package/dist/agents/xml/output_parser.cjs +44 -0
- package/dist/agents/xml/output_parser.d.ts +14 -0
- package/dist/agents/xml/output_parser.js +40 -0
- package/dist/document_loaders/fs/pdf.cjs +2 -1
- package/dist/document_loaders/fs/pdf.js +2 -1
- package/dist/document_loaders/web/pdf.cjs +2 -1
- package/dist/document_loaders/web/pdf.js +2 -1
- package/dist/embeddings/openai.cjs +11 -0
- package/dist/embeddings/openai.d.ts +2 -0
- package/dist/embeddings/openai.js +11 -0
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +11 -3
- package/dist/load/import_map.d.ts +9 -1
- package/dist/load/import_map.js +9 -1
- package/dist/memory/vector_store.cjs +2 -1
- package/dist/memory/vector_store.js +2 -1
- package/dist/storage/file_system.cjs +167 -0
- package/dist/storage/file_system.d.ts +60 -0
- package/dist/storage/file_system.js +140 -0
- package/dist/tools/index.cjs +3 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/render.cjs +36 -0
- package/dist/tools/render.d.ts +25 -0
- package/dist/tools/render.js +31 -0
- package/dist/tools/serpapi.d.ts +2 -2
- package/dist/tools/webbrowser.cjs +2 -1
- package/dist/tools/webbrowser.js +2 -1
- package/dist/util/document.cjs +12 -0
- package/dist/util/document.d.ts +9 -0
- package/dist/util/document.js +8 -0
- package/dist/vectorstores/analyticdb.cjs +7 -3
- package/dist/vectorstores/analyticdb.d.ts +1 -1
- package/dist/vectorstores/analyticdb.js +7 -3
- package/dist/vectorstores/cassandra.cjs +130 -35
- package/dist/vectorstores/cassandra.d.ts +21 -10
- package/dist/vectorstores/cassandra.js +130 -35
- package/dist/vectorstores/pgvector.cjs +13 -7
- package/dist/vectorstores/pgvector.d.ts +7 -0
- package/dist/vectorstores/pgvector.js +13 -7
- package/dist/vectorstores/qdrant.cjs +19 -11
- package/dist/vectorstores/qdrant.d.ts +1 -1
- package/dist/vectorstores/qdrant.js +19 -11
- package/dist/vectorstores/redis.cjs +4 -1
- package/dist/vectorstores/redis.d.ts +1 -1
- package/dist/vectorstores/redis.js +4 -1
- package/package.json +75 -3
- package/storage/file_system.cjs +1 -0
- package/storage/file_system.d.ts +1 -0
- package/storage/file_system.js +1 -0
- package/tools/render.cjs +1 -0
- package/tools/render.d.ts +1 -0
- package/tools/render.js +1 -0
- package/util/document.cjs +1 -0
- package/util/document.d.ts +1 -0
- package/util/document.js +1 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AgentActionOutputParser } from "../types.js";
|
|
2
|
+
import { AgentAction, AgentFinish } from "../../schema/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Parses ReAct-style LLM calls that have a single tool input.
|
|
5
|
+
*
|
|
6
|
+
* Expects output to be in one of two formats.
|
|
7
|
+
*
|
|
8
|
+
* If the output signals that an action should be taken,
|
|
9
|
+
* should be in the below format. This will result in an AgentAction
|
|
10
|
+
* being returned.
|
|
11
|
+
*
|
|
12
|
+
* ```
|
|
13
|
+
* Thought: agent thought here
|
|
14
|
+
* Action: search
|
|
15
|
+
* Action Input: what is the temperature in SF?
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* If the output signals that a final answer should be given,
|
|
19
|
+
* should be in the below format. This will result in an AgentFinish
|
|
20
|
+
* being returned.
|
|
21
|
+
*
|
|
22
|
+
* ```
|
|
23
|
+
* Thought: agent thought here
|
|
24
|
+
* Final Answer: The temperature is 100 degrees
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
28
|
+
lc_namespace: string[];
|
|
29
|
+
private toolNames;
|
|
30
|
+
constructor(fields: {
|
|
31
|
+
toolNames: string[];
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* Parses the given text into an AgentAction or AgentFinish object. If an
|
|
35
|
+
* output fixing parser is defined, uses it to parse the text.
|
|
36
|
+
* @param text Text to parse.
|
|
37
|
+
* @returns Promise that resolves to an AgentAction or AgentFinish object.
|
|
38
|
+
*/
|
|
39
|
+
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
40
|
+
/**
|
|
41
|
+
* Returns the format instructions as a string. If the 'raw' option is
|
|
42
|
+
* true, returns the raw FORMAT_INSTRUCTIONS.
|
|
43
|
+
* @param options Options for getting the format instructions.
|
|
44
|
+
* @returns Format instructions as a string.
|
|
45
|
+
*/
|
|
46
|
+
getFormatInstructions(): string;
|
|
47
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { renderTemplate } from "../../prompts/template.js";
|
|
2
|
+
import { AgentActionOutputParser } from "../types.js";
|
|
3
|
+
import { FORMAT_INSTRUCTIONS } from "./prompt.js";
|
|
4
|
+
const FINAL_ANSWER_ACTION = "Final Answer:";
|
|
5
|
+
const FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Parsing LLM output produced both a final answer and a parse-able action:";
|
|
6
|
+
/**
|
|
7
|
+
* Parses ReAct-style LLM calls that have a single tool input.
|
|
8
|
+
*
|
|
9
|
+
* Expects output to be in one of two formats.
|
|
10
|
+
*
|
|
11
|
+
* If the output signals that an action should be taken,
|
|
12
|
+
* should be in the below format. This will result in an AgentAction
|
|
13
|
+
* being returned.
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* Thought: agent thought here
|
|
17
|
+
* Action: search
|
|
18
|
+
* Action Input: what is the temperature in SF?
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* If the output signals that a final answer should be given,
|
|
22
|
+
* should be in the below format. This will result in an AgentFinish
|
|
23
|
+
* being returned.
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
* Thought: agent thought here
|
|
27
|
+
* Final Answer: The temperature is 100 degrees
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
31
|
+
constructor(fields) {
|
|
32
|
+
super(...arguments);
|
|
33
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: ["langchain", "agents", "react"]
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "toolNames", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
this.toolNames = fields.toolNames;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parses the given text into an AgentAction or AgentFinish object. If an
|
|
49
|
+
* output fixing parser is defined, uses it to parse the text.
|
|
50
|
+
* @param text Text to parse.
|
|
51
|
+
* @returns Promise that resolves to an AgentAction or AgentFinish object.
|
|
52
|
+
*/
|
|
53
|
+
async parse(text) {
|
|
54
|
+
const includesAnswer = text.includes(FINAL_ANSWER_ACTION);
|
|
55
|
+
const regex = /Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)/;
|
|
56
|
+
const actionMatch = text.match(regex);
|
|
57
|
+
if (actionMatch) {
|
|
58
|
+
if (includesAnswer) {
|
|
59
|
+
throw new Error(`${FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: ${text}`);
|
|
60
|
+
}
|
|
61
|
+
const action = actionMatch[1];
|
|
62
|
+
const actionInput = actionMatch[2];
|
|
63
|
+
const toolInput = actionInput.trim().replace(/"/g, "");
|
|
64
|
+
return {
|
|
65
|
+
tool: action,
|
|
66
|
+
toolInput,
|
|
67
|
+
log: text,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (includesAnswer) {
|
|
71
|
+
const finalAnswerText = text.split(FINAL_ANSWER_ACTION)[1].trim();
|
|
72
|
+
return {
|
|
73
|
+
returnValues: {
|
|
74
|
+
output: finalAnswerText,
|
|
75
|
+
},
|
|
76
|
+
log: text,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Could not parse LLM output: ${text}`);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns the format instructions as a string. If the 'raw' option is
|
|
83
|
+
* true, returns the raw FORMAT_INSTRUCTIONS.
|
|
84
|
+
* @param options Options for getting the format instructions.
|
|
85
|
+
* @returns Format instructions as a string.
|
|
86
|
+
*/
|
|
87
|
+
getFormatInstructions() {
|
|
88
|
+
return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
|
|
89
|
+
tool_names: this.toolNames.join(", "),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FORMAT_INSTRUCTIONS = void 0;
|
|
4
|
+
exports.FORMAT_INSTRUCTIONS /* #__PURE__ */ = `Use the following format:
|
|
5
|
+
|
|
6
|
+
Question: the input question you must answer
|
|
7
|
+
Thought: you should always think about what to do
|
|
8
|
+
Action: the action to take, should be one of [{tool_names}]
|
|
9
|
+
Action Input: the input to the action
|
|
10
|
+
Observation: the result of the action
|
|
11
|
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
|
12
|
+
Thought: I now know the final answer
|
|
13
|
+
Final Answer: the final answer to the original input question`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const FORMAT_INSTRUCTIONS = "Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const FORMAT_INSTRUCTIONS /* #__PURE__ */ = `Use the following format:
|
|
2
|
+
|
|
3
|
+
Question: the input question you must answer
|
|
4
|
+
Thought: you should always think about what to do
|
|
5
|
+
Action: the action to take, should be one of [{tool_names}]
|
|
6
|
+
Action Input: the input to the action
|
|
7
|
+
Observation: the result of the action
|
|
8
|
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
|
9
|
+
Thought: I now know the final answer
|
|
10
|
+
Final Answer: the final answer to the original input question`;
|
|
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createRetrieverTool = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const dynamic_js_1 = require("../../../tools/dynamic.cjs");
|
|
6
|
+
const document_js_1 = require("../../../util/document.cjs");
|
|
6
7
|
function createRetrieverTool(retriever, input) {
|
|
7
8
|
const func = async ({ input }, runManager) => {
|
|
8
9
|
const docs = await retriever.getRelevantDocuments(input, runManager?.getChild("retriever"));
|
|
9
|
-
return
|
|
10
|
+
return (0, document_js_1.formatDocumentsAsString)(docs, "\n");
|
|
10
11
|
};
|
|
11
12
|
const schema = zod_1.z.object({
|
|
12
13
|
input: zod_1.z
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { DynamicStructuredTool, } from "../../../tools/dynamic.js";
|
|
3
|
+
import { formatDocumentsAsString } from "../../../util/document.js";
|
|
3
4
|
export function createRetrieverTool(retriever, input) {
|
|
4
5
|
const func = async ({ input }, runManager) => {
|
|
5
6
|
const docs = await retriever.getRelevantDocuments(input, runManager?.getChild("retriever"));
|
|
6
|
-
return docs
|
|
7
|
+
return formatDocumentsAsString(docs, "\n");
|
|
7
8
|
};
|
|
8
9
|
const schema = z.object({
|
|
9
10
|
input: z
|
|
@@ -1,33 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.XMLAgent =
|
|
3
|
+
exports.XMLAgent = void 0;
|
|
4
4
|
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
5
|
const chat_js_1 = require("../../prompts/chat.cjs");
|
|
6
6
|
const agent_js_1 = require("../agent.cjs");
|
|
7
|
-
const output_parser_js_1 = require("../../schema/output_parser.cjs");
|
|
8
7
|
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;
|
|
8
|
+
const output_parser_js_1 = require("./output_parser.cjs");
|
|
31
9
|
/**
|
|
32
10
|
* Class that represents an agent that uses XML tags.
|
|
33
11
|
*/
|
|
@@ -58,6 +36,12 @@ class XMLAgent extends agent_js_1.BaseSingleActionAgent {
|
|
|
58
36
|
writable: true,
|
|
59
37
|
value: void 0
|
|
60
38
|
});
|
|
39
|
+
Object.defineProperty(this, "outputParser", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: new output_parser_js_1.XMLAgentOutputParser()
|
|
44
|
+
});
|
|
61
45
|
this.tools = fields.tools;
|
|
62
46
|
this.llmChain = fields.llmChain;
|
|
63
47
|
}
|
|
@@ -94,7 +78,7 @@ class XMLAgent extends agent_js_1.BaseSingleActionAgent {
|
|
|
94
78
|
stop: ["</tool_input>", "</final_answer>"],
|
|
95
79
|
};
|
|
96
80
|
const response = await this.llmChain.call(_inputs, callbackManager);
|
|
97
|
-
return
|
|
81
|
+
return this.outputParser.parse(response[this.llmChain.outputKey]);
|
|
98
82
|
}
|
|
99
83
|
/**
|
|
100
84
|
* Creates an XMLAgent from a BaseLanguageModel and a list of tools.
|
|
@@ -5,6 +5,7 @@ import { ChatPromptTemplate } from "../../prompts/chat.js";
|
|
|
5
5
|
import { AgentArgs, BaseSingleActionAgent } from "../agent.js";
|
|
6
6
|
import { CallbackManager } from "../../callbacks/manager.js";
|
|
7
7
|
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
8
|
+
import { XMLAgentOutputParser } from "./output_parser.js";
|
|
8
9
|
/**
|
|
9
10
|
* Interface for the input to the XMLAgent class.
|
|
10
11
|
*/
|
|
@@ -12,13 +13,6 @@ export interface XMLAgentInput {
|
|
|
12
13
|
tools: Tool[];
|
|
13
14
|
llmChain: LLMChain;
|
|
14
15
|
}
|
|
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
16
|
/**
|
|
23
17
|
* Class that represents an agent that uses XML tags.
|
|
24
18
|
*/
|
|
@@ -27,6 +21,7 @@ export declare class XMLAgent extends BaseSingleActionAgent implements XMLAgentI
|
|
|
27
21
|
lc_namespace: string[];
|
|
28
22
|
tools: Tool[];
|
|
29
23
|
llmChain: LLMChain;
|
|
24
|
+
outputParser: XMLAgentOutputParser;
|
|
30
25
|
_agentType(): "xml";
|
|
31
26
|
constructor(fields: XMLAgentInput);
|
|
32
27
|
get inputKeys(): string[];
|
package/dist/agents/xml/index.js
CHANGED
|
@@ -1,29 +1,8 @@
|
|
|
1
1
|
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
2
|
import { AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, } from "../../prompts/chat.js";
|
|
3
3
|
import { BaseSingleActionAgent } from "../agent.js";
|
|
4
|
-
import { OutputParserException } from "../../schema/output_parser.js";
|
|
5
4
|
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
|
-
}
|
|
5
|
+
import { XMLAgentOutputParser } from "./output_parser.js";
|
|
27
6
|
/**
|
|
28
7
|
* Class that represents an agent that uses XML tags.
|
|
29
8
|
*/
|
|
@@ -54,6 +33,12 @@ export class XMLAgent extends BaseSingleActionAgent {
|
|
|
54
33
|
writable: true,
|
|
55
34
|
value: void 0
|
|
56
35
|
});
|
|
36
|
+
Object.defineProperty(this, "outputParser", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: new XMLAgentOutputParser()
|
|
41
|
+
});
|
|
57
42
|
this.tools = fields.tools;
|
|
58
43
|
this.llmChain = fields.llmChain;
|
|
59
44
|
}
|
|
@@ -90,7 +75,7 @@ export class XMLAgent extends BaseSingleActionAgent {
|
|
|
90
75
|
stop: ["</tool_input>", "</final_answer>"],
|
|
91
76
|
};
|
|
92
77
|
const response = await this.llmChain.call(_inputs, callbackManager);
|
|
93
|
-
return
|
|
78
|
+
return this.outputParser.parse(response[this.llmChain.outputKey]);
|
|
94
79
|
}
|
|
95
80
|
/**
|
|
96
81
|
* Creates an XMLAgent from a BaseLanguageModel and a list of tools.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XMLAgentOutputParser = void 0;
|
|
4
|
+
const output_parser_js_1 = require("../../schema/output_parser.cjs");
|
|
5
|
+
const types_js_1 = require("../types.cjs");
|
|
6
|
+
class XMLAgentOutputParser extends types_js_1.AgentActionOutputParser {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: ["langchain", "agents", "xml"]
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
static lc_name() {
|
|
17
|
+
return "XMLAgentOutputParser";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parses the output text from the agent and returns an AgentAction or
|
|
21
|
+
* AgentFinish object.
|
|
22
|
+
* @param text The output text from the agent.
|
|
23
|
+
* @returns An AgentAction or AgentFinish object.
|
|
24
|
+
*/
|
|
25
|
+
async parse(text) {
|
|
26
|
+
if (text.includes("</tool>")) {
|
|
27
|
+
const [tool, toolInput] = text.split("</tool>");
|
|
28
|
+
const _tool = tool.split("<tool>")[1];
|
|
29
|
+
const _toolInput = toolInput.split("<tool_input>")[1];
|
|
30
|
+
return { tool: _tool, toolInput: _toolInput, log: text };
|
|
31
|
+
}
|
|
32
|
+
else if (text.includes("<final_answer>")) {
|
|
33
|
+
const [, answer] = text.split("<final_answer>");
|
|
34
|
+
return { returnValues: { output: answer }, log: text };
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
throw new output_parser_js_1.OutputParserException(`Could not parse LLM output: ${text}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
getFormatInstructions() {
|
|
41
|
+
throw new Error("getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.XMLAgentOutputParser = XMLAgentOutputParser;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AgentAction, AgentFinish } from "../../schema/index.js";
|
|
2
|
+
import { AgentActionOutputParser } from "../types.js";
|
|
3
|
+
export declare class XMLAgentOutputParser extends AgentActionOutputParser {
|
|
4
|
+
lc_namespace: string[];
|
|
5
|
+
static lc_name(): string;
|
|
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
|
+
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
13
|
+
getFormatInstructions(): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { OutputParserException } from "../../schema/output_parser.js";
|
|
2
|
+
import { AgentActionOutputParser } from "../types.js";
|
|
3
|
+
export class XMLAgentOutputParser extends AgentActionOutputParser {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(...arguments);
|
|
6
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: ["langchain", "agents", "xml"]
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
static lc_name() {
|
|
14
|
+
return "XMLAgentOutputParser";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parses the output text from the agent and returns an AgentAction or
|
|
18
|
+
* AgentFinish object.
|
|
19
|
+
* @param text The output text from the agent.
|
|
20
|
+
* @returns An AgentAction or AgentFinish object.
|
|
21
|
+
*/
|
|
22
|
+
async parse(text) {
|
|
23
|
+
if (text.includes("</tool>")) {
|
|
24
|
+
const [tool, toolInput] = text.split("</tool>");
|
|
25
|
+
const _tool = tool.split("<tool>")[1];
|
|
26
|
+
const _toolInput = toolInput.split("<tool_input>")[1];
|
|
27
|
+
return { tool: _tool, toolInput: _toolInput, log: text };
|
|
28
|
+
}
|
|
29
|
+
else if (text.includes("<final_answer>")) {
|
|
30
|
+
const [, answer] = text.split("<final_answer>");
|
|
31
|
+
return { returnValues: { output: answer }, log: text };
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
throw new OutputParserException(`Could not parse LLM output: ${text}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
getFormatInstructions() {
|
|
38
|
+
throw new Error("getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PDFLoader = void 0;
|
|
4
4
|
const document_js_1 = require("../../document.cjs");
|
|
5
5
|
const buffer_js_1 = require("./buffer.cjs");
|
|
6
|
+
const document_js_2 = require("../../util/document.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* A class that extends the `BufferLoader` class. It represents a document
|
|
8
9
|
* loader that loads documents from PDF files.
|
|
@@ -85,7 +86,7 @@ class PDFLoader extends buffer_js_1.BufferLoader {
|
|
|
85
86
|
}
|
|
86
87
|
return [
|
|
87
88
|
new document_js_1.Document({
|
|
88
|
-
pageContent:
|
|
89
|
+
pageContent: (0, document_js_2.formatDocumentsAsString)(documents),
|
|
89
90
|
metadata: {
|
|
90
91
|
...metadata,
|
|
91
92
|
pdf: {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Document } from "../../document.js";
|
|
2
2
|
import { BufferLoader } from "./buffer.js";
|
|
3
|
+
import { formatDocumentsAsString } from "../../util/document.js";
|
|
3
4
|
/**
|
|
4
5
|
* A class that extends the `BufferLoader` class. It represents a document
|
|
5
6
|
* loader that loads documents from PDF files.
|
|
@@ -82,7 +83,7 @@ export class PDFLoader extends BufferLoader {
|
|
|
82
83
|
}
|
|
83
84
|
return [
|
|
84
85
|
new Document({
|
|
85
|
-
pageContent: documents
|
|
86
|
+
pageContent: formatDocumentsAsString(documents),
|
|
86
87
|
metadata: {
|
|
87
88
|
...metadata,
|
|
88
89
|
pdf: {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WebPDFLoader = void 0;
|
|
4
4
|
const document_js_1 = require("../../document.cjs");
|
|
5
5
|
const base_js_1 = require("../base.cjs");
|
|
6
|
+
const document_js_2 = require("../../util/document.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* A document loader for loading data from PDFs.
|
|
8
9
|
*/
|
|
@@ -77,7 +78,7 @@ class WebPDFLoader extends base_js_1.BaseDocumentLoader {
|
|
|
77
78
|
}
|
|
78
79
|
return [
|
|
79
80
|
new document_js_1.Document({
|
|
80
|
-
pageContent:
|
|
81
|
+
pageContent: (0, document_js_2.formatDocumentsAsString)(documents),
|
|
81
82
|
metadata: {
|
|
82
83
|
pdf: {
|
|
83
84
|
version,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Document } from "../../document.js";
|
|
2
2
|
import { BaseDocumentLoader } from "../base.js";
|
|
3
|
+
import { formatDocumentsAsString } from "../../util/document.js";
|
|
3
4
|
/**
|
|
4
5
|
* A document loader for loading data from PDFs.
|
|
5
6
|
*/
|
|
@@ -74,7 +75,7 @@ export class WebPDFLoader extends BaseDocumentLoader {
|
|
|
74
75
|
}
|
|
75
76
|
return [
|
|
76
77
|
new Document({
|
|
77
|
-
pageContent: documents
|
|
78
|
+
pageContent: formatDocumentsAsString(documents),
|
|
78
79
|
metadata: {
|
|
79
80
|
pdf: {
|
|
80
81
|
version,
|
|
@@ -70,6 +70,12 @@ class OpenAIEmbeddings extends base_js_1.Embeddings {
|
|
|
70
70
|
writable: true,
|
|
71
71
|
value: void 0
|
|
72
72
|
});
|
|
73
|
+
Object.defineProperty(this, "organization", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
73
79
|
Object.defineProperty(this, "client", {
|
|
74
80
|
enumerable: true,
|
|
75
81
|
configurable: true,
|
|
@@ -100,6 +106,9 @@ class OpenAIEmbeddings extends base_js_1.Embeddings {
|
|
|
100
106
|
this.azureOpenAIBasePath =
|
|
101
107
|
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
102
108
|
(0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
109
|
+
this.organization =
|
|
110
|
+
fieldsWithDefaults?.configuration?.organization ??
|
|
111
|
+
(0, env_js_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
|
|
103
112
|
this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
|
|
104
113
|
this.batchSize =
|
|
105
114
|
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
@@ -124,11 +133,13 @@ class OpenAIEmbeddings extends base_js_1.Embeddings {
|
|
|
124
133
|
}
|
|
125
134
|
this.clientConfig = {
|
|
126
135
|
apiKey,
|
|
136
|
+
organization: this.organization,
|
|
127
137
|
baseURL: configuration?.basePath,
|
|
128
138
|
dangerouslyAllowBrowser: true,
|
|
129
139
|
defaultHeaders: configuration?.baseOptions?.headers,
|
|
130
140
|
defaultQuery: configuration?.baseOptions?.params,
|
|
131
141
|
...configuration,
|
|
142
|
+
...fields?.configuration,
|
|
132
143
|
};
|
|
133
144
|
}
|
|
134
145
|
/**
|
|
@@ -38,11 +38,13 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
|
|
|
38
38
|
azureOpenAIApiInstanceName?: string;
|
|
39
39
|
azureOpenAIApiDeploymentName?: string;
|
|
40
40
|
azureOpenAIBasePath?: string;
|
|
41
|
+
organization?: string;
|
|
41
42
|
private client;
|
|
42
43
|
private clientConfig;
|
|
43
44
|
constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
|
|
44
45
|
verbose?: boolean;
|
|
45
46
|
openAIApiKey?: string;
|
|
47
|
+
configuration?: ClientOptions;
|
|
46
48
|
}, configuration?: ClientOptions & LegacyOpenAIInput);
|
|
47
49
|
/**
|
|
48
50
|
* Method to generate embeddings for an array of documents. Splits the
|
|
@@ -67,6 +67,12 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
67
67
|
writable: true,
|
|
68
68
|
value: void 0
|
|
69
69
|
});
|
|
70
|
+
Object.defineProperty(this, "organization", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: void 0
|
|
75
|
+
});
|
|
70
76
|
Object.defineProperty(this, "client", {
|
|
71
77
|
enumerable: true,
|
|
72
78
|
configurable: true,
|
|
@@ -97,6 +103,9 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
97
103
|
this.azureOpenAIBasePath =
|
|
98
104
|
fieldsWithDefaults?.azureOpenAIBasePath ??
|
|
99
105
|
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
106
|
+
this.organization =
|
|
107
|
+
fieldsWithDefaults?.configuration?.organization ??
|
|
108
|
+
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
100
109
|
this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
|
|
101
110
|
this.batchSize =
|
|
102
111
|
fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
|
|
@@ -121,11 +130,13 @@ export class OpenAIEmbeddings extends Embeddings {
|
|
|
121
130
|
}
|
|
122
131
|
this.clientConfig = {
|
|
123
132
|
apiKey,
|
|
133
|
+
organization: this.organization,
|
|
124
134
|
baseURL: configuration?.basePath,
|
|
125
135
|
dangerouslyAllowBrowser: true,
|
|
126
136
|
defaultHeaders: configuration?.baseOptions?.headers,
|
|
127
137
|
defaultQuery: configuration?.baseOptions?.params,
|
|
128
138
|
...configuration,
|
|
139
|
+
...fields?.configuration,
|
|
129
140
|
};
|
|
130
141
|
}
|
|
131
142
|
/**
|
|
@@ -146,6 +146,7 @@ exports.optionalImportEntrypoints = [
|
|
|
146
146
|
"langchain/storage/ioredis",
|
|
147
147
|
"langchain/storage/vercel_kv",
|
|
148
148
|
"langchain/storage/upstash_redis",
|
|
149
|
+
"langchain/storage/file_system",
|
|
149
150
|
"langchain/graphs/neo4j_graph",
|
|
150
151
|
"langchain/hub",
|
|
151
152
|
"langchain/experimental/multimodal_embeddings/googlevertexai",
|
|
@@ -143,6 +143,7 @@ export const optionalImportEntrypoints = [
|
|
|
143
143
|
"langchain/storage/ioredis",
|
|
144
144
|
"langchain/storage/vercel_kv",
|
|
145
145
|
"langchain/storage/upstash_redis",
|
|
146
|
+
"langchain/storage/file_system",
|
|
146
147
|
"langchain/graphs/neo4j_graph",
|
|
147
148
|
"langchain/hub",
|
|
148
149
|
"langchain/experimental/multimodal_embeddings/googlevertexai",
|