langchain 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/agent.d.ts +42 -0
- package/dist/cjs/agents/agent.js +129 -0
- package/dist/cjs/agents/agent.js.map +1 -0
- package/dist/cjs/agents/executor.d.ts +27 -0
- package/dist/cjs/agents/executor.js +105 -0
- package/dist/cjs/agents/executor.js.map +1 -0
- package/dist/cjs/agents/helpers.d.ts +3 -0
- package/dist/cjs/agents/helpers.js +30 -0
- package/dist/cjs/agents/helpers.js.map +1 -0
- package/dist/cjs/agents/index.d.ts +6 -0
- package/dist/cjs/agents/index.js +13 -0
- package/dist/cjs/agents/index.js.map +1 -0
- package/dist/cjs/agents/load.d.ts +6 -0
- package/dist/cjs/agents/load.js +28 -0
- package/dist/cjs/agents/load.js.map +1 -0
- package/dist/cjs/agents/mrkl/index.d.ts +33 -0
- package/dist/cjs/agents/mrkl/index.js +109 -0
- package/dist/cjs/agents/mrkl/index.js.map +1 -0
- package/dist/cjs/agents/mrkl/prompt.d.ts +3 -0
- package/dist/cjs/agents/mrkl/prompt.js +21 -0
- package/dist/cjs/agents/mrkl/prompt.js.map +1 -0
- package/dist/cjs/agents/tests/agent.test.d.ts +1 -0
- package/dist/cjs/agents/tests/agent.test.js +31 -0
- package/dist/cjs/agents/tests/agent.test.js.map +1 -0
- package/dist/cjs/agents/tools/index.d.ts +7 -0
- package/dist/cjs/agents/tools/index.js +6 -0
- package/dist/cjs/agents/tools/index.js.map +1 -0
- package/dist/cjs/agents/tools/llm-math.d.ts +0 -0
- package/dist/cjs/agents/tools/llm-math.js +2 -0
- package/dist/cjs/agents/tools/llm-math.js.map +1 -0
- package/dist/cjs/agents/tools/serpapi.d.ts +3 -0
- package/dist/cjs/agents/tools/serpapi.js +50 -0
- package/dist/cjs/agents/tools/serpapi.js.map +1 -0
- package/dist/cjs/agents/types.d.ts +25 -0
- package/dist/cjs/agents/types.js +3 -0
- package/dist/cjs/agents/types.js.map +1 -0
- package/dist/cjs/cache.d.ts +11 -0
- package/dist/cjs/cache.js +27 -0
- package/dist/cjs/cache.js.map +1 -0
- package/dist/cjs/chains/base.d.ts +12 -0
- package/dist/cjs/chains/base.js +23 -0
- package/dist/cjs/chains/base.js.map +1 -0
- package/dist/cjs/chains/index.d.ts +3 -0
- package/dist/cjs/chains/index.js +10 -0
- package/dist/cjs/chains/index.js.map +1 -0
- package/dist/cjs/chains/llm_chain.d.ts +30 -0
- package/dist/cjs/chains/llm_chain.js +83 -0
- package/dist/cjs/chains/llm_chain.js.map +1 -0
- package/dist/cjs/chains/llm_math_chain.d.ts +0 -0
- package/dist/cjs/chains/llm_math_chain.js +129 -0
- package/dist/cjs/chains/llm_math_chain.js.map +1 -0
- package/dist/cjs/chains/load.d.ts +2 -0
- package/dist/cjs/chains/load.js +29 -0
- package/dist/cjs/chains/load.js.map +1 -0
- package/dist/cjs/chains/tests/llm_chain.test.d.ts +1 -0
- package/dist/cjs/chains/tests/llm_chain.test.js +32 -0
- package/dist/cjs/chains/tests/llm_chain.test.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/llms/base.d.ts +23 -0
- package/dist/cjs/llms/base.js +159 -0
- package/dist/cjs/llms/base.js.map +1 -0
- package/dist/cjs/llms/index.d.ts +18 -0
- package/dist/cjs/llms/index.js +11 -0
- package/dist/cjs/llms/index.js.map +1 -0
- package/dist/cjs/llms/load.d.ts +3 -0
- package/dist/cjs/llms/load.js +9 -0
- package/dist/cjs/llms/load.js.map +1 -0
- package/dist/cjs/llms/openai.d.ts +63 -0
- package/dist/cjs/llms/openai.js +202 -0
- package/dist/cjs/llms/openai.js.map +1 -0
- package/dist/cjs/llms/tests/openai.test.d.ts +1 -0
- package/dist/cjs/llms/tests/openai.test.js +19 -0
- package/dist/cjs/llms/tests/openai.test.js.map +1 -0
- package/dist/cjs/prompt/base.d.ts +19 -0
- package/dist/cjs/prompt/base.js +54 -0
- package/dist/cjs/prompt/base.js.map +1 -0
- package/dist/cjs/prompt/few_shot.d.ts +47 -0
- package/dist/cjs/prompt/few_shot.js +143 -0
- package/dist/cjs/prompt/few_shot.js.map +1 -0
- package/dist/cjs/prompt/index.d.ts +3 -0
- package/dist/cjs/prompt/index.js +10 -0
- package/dist/cjs/prompt/index.js.map +1 -0
- package/dist/cjs/prompt/load.d.ts +2 -0
- package/dist/cjs/prompt/load.js +25 -0
- package/dist/cjs/prompt/load.js.map +1 -0
- package/dist/cjs/prompt/parser.d.ts +40 -0
- package/dist/cjs/prompt/parser.js +93 -0
- package/dist/cjs/prompt/parser.js.map +1 -0
- package/dist/cjs/prompt/prompt.d.ts +28 -0
- package/dist/cjs/prompt/prompt.js +91 -0
- package/dist/cjs/prompt/prompt.js.map +1 -0
- package/dist/cjs/prompt/template.d.ts +16 -0
- package/dist/cjs/prompt/template.js +87 -0
- package/dist/cjs/prompt/template.js.map +1 -0
- package/dist/cjs/prompt/tests/load.test.d.ts +1 -0
- package/dist/cjs/prompt/tests/load.test.js +30 -0
- package/dist/cjs/prompt/tests/load.test.js.map +1 -0
- package/dist/cjs/prompt/tests/template.test.d.ts +1 -0
- package/dist/cjs/prompt/tests/template.test.js +27 -0
- package/dist/cjs/prompt/tests/template.test.js.map +1 -0
- package/dist/cjs/util/hub.d.ts +1 -0
- package/dist/cjs/util/hub.js +50 -0
- package/dist/cjs/util/hub.js.map +1 -0
- package/dist/cjs/util/index.d.ts +12 -0
- package/dist/cjs/util/index.js +107 -0
- package/dist/cjs/util/index.js.map +1 -0
- package/dist/esm/agents/agent.d.ts +42 -0
- package/dist/esm/agents/agent.js +124 -0
- package/dist/esm/agents/agent.js.map +1 -0
- package/dist/esm/agents/executor.d.ts +27 -0
- package/dist/esm/agents/executor.js +101 -0
- package/dist/esm/agents/executor.js.map +1 -0
- package/dist/esm/agents/helpers.d.ts +3 -0
- package/dist/esm/agents/helpers.js +26 -0
- package/dist/esm/agents/helpers.js.map +1 -0
- package/dist/esm/agents/index.d.ts +6 -0
- package/dist/esm/agents/index.js +5 -0
- package/dist/esm/agents/index.js.map +1 -0
- package/dist/esm/agents/load.d.ts +6 -0
- package/dist/esm/agents/load.js +24 -0
- package/dist/esm/agents/load.js.map +1 -0
- package/dist/esm/agents/mrkl/index.d.ts +33 -0
- package/dist/esm/agents/mrkl/index.js +106 -0
- package/dist/esm/agents/mrkl/index.js.map +1 -0
- package/dist/esm/agents/mrkl/prompt.d.ts +3 -0
- package/dist/esm/agents/mrkl/prompt.js +17 -0
- package/dist/esm/agents/mrkl/prompt.js.map +1 -0
- package/dist/esm/agents/tests/agent.test.d.ts +1 -0
- package/dist/esm/agents/tests/agent.test.js +29 -0
- package/dist/esm/agents/tests/agent.test.js.map +1 -0
- package/dist/esm/agents/tools/index.d.ts +7 -0
- package/dist/esm/agents/tools/index.js +2 -0
- package/dist/esm/agents/tools/index.js.map +1 -0
- package/dist/esm/agents/tools/llm-math.d.ts +0 -0
- package/dist/esm/agents/tools/llm-math.js +2 -0
- package/dist/esm/agents/tools/llm-math.js.map +1 -0
- package/dist/esm/agents/tools/serpapi.d.ts +3 -0
- package/dist/esm/agents/tools/serpapi.js +46 -0
- package/dist/esm/agents/tools/serpapi.js.map +1 -0
- package/dist/esm/agents/types.d.ts +25 -0
- package/dist/esm/agents/types.js +2 -0
- package/dist/esm/agents/types.js.map +1 -0
- package/dist/esm/cache.d.ts +11 -0
- package/dist/esm/cache.js +22 -0
- package/dist/esm/cache.js.map +1 -0
- package/dist/esm/chains/base.d.ts +12 -0
- package/dist/esm/chains/base.js +19 -0
- package/dist/esm/chains/base.js.map +1 -0
- package/dist/esm/chains/index.d.ts +3 -0
- package/dist/esm/chains/index.js +4 -0
- package/dist/esm/chains/index.js.map +1 -0
- package/dist/esm/chains/llm_chain.d.ts +30 -0
- package/dist/esm/chains/llm_chain.js +79 -0
- package/dist/esm/chains/llm_chain.js.map +1 -0
- package/dist/esm/chains/llm_math_chain.d.ts +0 -0
- package/dist/esm/chains/llm_math_chain.js +129 -0
- package/dist/esm/chains/llm_math_chain.js.map +1 -0
- package/dist/esm/chains/load.d.ts +2 -0
- package/dist/esm/chains/load.js +25 -0
- package/dist/esm/chains/load.js.map +1 -0
- package/dist/esm/chains/tests/llm_chain.test.d.ts +1 -0
- package/dist/esm/chains/tests/llm_chain.test.js +30 -0
- package/dist/esm/chains/tests/llm_chain.test.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/llms/base.d.ts +23 -0
- package/dist/esm/llms/base.js +154 -0
- package/dist/esm/llms/base.js.map +1 -0
- package/dist/esm/llms/index.d.ts +18 -0
- package/dist/esm/llms/index.js +4 -0
- package/dist/esm/llms/index.js.map +1 -0
- package/dist/esm/llms/load.d.ts +3 -0
- package/dist/esm/llms/load.js +5 -0
- package/dist/esm/llms/load.js.map +1 -0
- package/dist/esm/llms/openai.d.ts +63 -0
- package/dist/esm/llms/openai.js +198 -0
- package/dist/esm/llms/openai.js.map +1 -0
- package/dist/esm/llms/tests/openai.test.d.ts +1 -0
- package/dist/esm/llms/tests/openai.test.js +17 -0
- package/dist/esm/llms/tests/openai.test.js.map +1 -0
- package/dist/esm/prompt/base.d.ts +19 -0
- package/dist/esm/prompt/base.js +50 -0
- package/dist/esm/prompt/base.js.map +1 -0
- package/dist/esm/prompt/few_shot.d.ts +47 -0
- package/dist/esm/prompt/few_shot.js +139 -0
- package/dist/esm/prompt/few_shot.js.map +1 -0
- package/dist/esm/prompt/index.d.ts +3 -0
- package/dist/esm/prompt/index.js +4 -0
- package/dist/esm/prompt/index.js.map +1 -0
- package/dist/esm/prompt/load.d.ts +2 -0
- package/dist/esm/prompt/load.js +21 -0
- package/dist/esm/prompt/load.js.map +1 -0
- package/dist/esm/prompt/parser.d.ts +40 -0
- package/dist/esm/prompt/parser.js +86 -0
- package/dist/esm/prompt/parser.js.map +1 -0
- package/dist/esm/prompt/prompt.d.ts +28 -0
- package/dist/esm/prompt/prompt.js +87 -0
- package/dist/esm/prompt/prompt.js.map +1 -0
- package/dist/esm/prompt/template.d.ts +16 -0
- package/dist/esm/prompt/template.js +80 -0
- package/dist/esm/prompt/template.js.map +1 -0
- package/dist/esm/prompt/tests/load.test.d.ts +1 -0
- package/dist/esm/prompt/tests/load.test.js +25 -0
- package/dist/esm/prompt/tests/load.test.js.map +1 -0
- package/dist/esm/prompt/tests/template.test.d.ts +1 -0
- package/dist/esm/prompt/tests/template.test.js +25 -0
- package/dist/esm/prompt/tests/template.test.js.map +1 -0
- package/dist/esm/util/hub.d.ts +1 -0
- package/dist/esm/util/hub.js +43 -0
- package/dist/esm/util/hub.js.map +1 -0
- package/dist/esm/util/index.d.ts +12 -0
- package/dist/esm/util/index.js +74 -0
- package/dist/esm/util/index.js.map +1 -0
- package/package.json +97 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BaseLLM } from "../../llms";
|
|
2
|
+
import { Agent, Tool, AgentInput, SerializedAgentT } from "../index";
|
|
3
|
+
import { PromptTemplate } from "../../prompt";
|
|
4
|
+
type SerializedFromLLMAndTools = {
|
|
5
|
+
suffix?: string;
|
|
6
|
+
prefix?: string;
|
|
7
|
+
input_variables?: string[];
|
|
8
|
+
};
|
|
9
|
+
export type SerializedZeroShotAgent = SerializedAgentT<"zero-shot-react-description", SerializedFromLLMAndTools, AgentInput>;
|
|
10
|
+
type CreatePromptArgs = {
|
|
11
|
+
suffix?: string;
|
|
12
|
+
prefix?: string;
|
|
13
|
+
inputVariables?: string[];
|
|
14
|
+
};
|
|
15
|
+
type ZeroShotAgentInput = AgentInput;
|
|
16
|
+
export declare class ZeroShotAgent extends Agent {
|
|
17
|
+
constructor(input: ZeroShotAgentInput);
|
|
18
|
+
_agentType(): "zero-shot-react-description";
|
|
19
|
+
observationPrefix(): string;
|
|
20
|
+
llmPrefix(): string;
|
|
21
|
+
static validateTools(tools: Tool[]): void;
|
|
22
|
+
static createPrompt(tools: Tool[], args?: CreatePromptArgs): PromptTemplate;
|
|
23
|
+
static fromLLMAndTools(llm: BaseLLM, tools: Tool[], args?: CreatePromptArgs): ZeroShotAgent;
|
|
24
|
+
extractToolAndInput(text: string): {
|
|
25
|
+
tool: string;
|
|
26
|
+
input: string;
|
|
27
|
+
} | null;
|
|
28
|
+
static deserialize(data: SerializedZeroShotAgent & {
|
|
29
|
+
llm?: BaseLLM;
|
|
30
|
+
tools?: Tool[];
|
|
31
|
+
}): Promise<ZeroShotAgent>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
17
|
+
var t = {};
|
|
18
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
19
|
+
t[p] = s[p];
|
|
20
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
21
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
22
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
23
|
+
t[p[i]] = s[p[i]];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
27
|
+
var ZeroShotAgent_1;
|
|
28
|
+
import { LLMChain } from "../../chains";
|
|
29
|
+
import { Agent, staticImplements, } from "../index";
|
|
30
|
+
import { PromptTemplate } from "../../prompt";
|
|
31
|
+
import { PREFIX, SUFFIX, formatInstructions } from "./prompt";
|
|
32
|
+
import { deserializeHelper } from "../helpers";
|
|
33
|
+
const FINAL_ANSWER_ACTION = "Final Answer:";
|
|
34
|
+
let ZeroShotAgent = ZeroShotAgent_1 = class ZeroShotAgent extends Agent {
|
|
35
|
+
constructor(input) {
|
|
36
|
+
super(input);
|
|
37
|
+
}
|
|
38
|
+
_agentType() {
|
|
39
|
+
return "zero-shot-react-description";
|
|
40
|
+
}
|
|
41
|
+
observationPrefix() {
|
|
42
|
+
return "Observation: ";
|
|
43
|
+
}
|
|
44
|
+
llmPrefix() {
|
|
45
|
+
return "Thought:";
|
|
46
|
+
}
|
|
47
|
+
static validateTools(tools) {
|
|
48
|
+
const invalidTool = tools.find((tool) => !tool.description);
|
|
49
|
+
if (invalidTool) {
|
|
50
|
+
const msg = `Got a tool ${invalidTool.name} without a description.` +
|
|
51
|
+
` This agent requires descriptions for all tools.`;
|
|
52
|
+
throw new Error(msg);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
static createPrompt(tools, args) {
|
|
56
|
+
const { prefix = PREFIX, suffix = SUFFIX, inputVariables = ["input", "agent_scratchpad"], } = args !== null && args !== void 0 ? args : {};
|
|
57
|
+
const toolStrings = tools
|
|
58
|
+
.map((tool) => `${tool.name}: ${tool.description}`)
|
|
59
|
+
.join("\n");
|
|
60
|
+
const instructions = formatInstructions(toolStrings);
|
|
61
|
+
const template = [prefix, toolStrings, instructions, suffix].join("\n\n");
|
|
62
|
+
return new PromptTemplate({
|
|
63
|
+
template,
|
|
64
|
+
inputVariables,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
static fromLLMAndTools(llm, tools, args) {
|
|
68
|
+
ZeroShotAgent_1.validateTools(tools);
|
|
69
|
+
const prompt = ZeroShotAgent_1.createPrompt(tools, args);
|
|
70
|
+
const chain = new LLMChain({ prompt, llm });
|
|
71
|
+
return new ZeroShotAgent_1({
|
|
72
|
+
llmChain: chain,
|
|
73
|
+
allowedTools: tools.map((t) => t.name),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
extractToolAndInput(text) {
|
|
77
|
+
if (text.includes(FINAL_ANSWER_ACTION)) {
|
|
78
|
+
const parts = text.split(FINAL_ANSWER_ACTION);
|
|
79
|
+
const input = parts[parts.length - 1].trim();
|
|
80
|
+
return { tool: "Final Answer", input };
|
|
81
|
+
}
|
|
82
|
+
const match = /Action: (.*)\nAction Input: (.*)/s.exec(text);
|
|
83
|
+
if (!match) {
|
|
84
|
+
throw new Error(`Could not parse LLM output: ${text}`);
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
tool: match[1].trim(),
|
|
88
|
+
input: match[2].trim().replace(/^"+|"+$/g, ""),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
static deserialize(data) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const { llm, tools } = data, rest = __rest(data, ["llm", "tools"]);
|
|
94
|
+
return deserializeHelper(llm, tools, rest, (llm, tools, args) => ZeroShotAgent_1.fromLLMAndTools(llm, tools, {
|
|
95
|
+
prefix: args.prefix,
|
|
96
|
+
suffix: args.suffix,
|
|
97
|
+
inputVariables: args.input_variables,
|
|
98
|
+
}), (args) => new ZeroShotAgent_1(args));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
ZeroShotAgent = ZeroShotAgent_1 = __decorate([
|
|
103
|
+
(staticImplements)
|
|
104
|
+
], ZeroShotAgent);
|
|
105
|
+
export { ZeroShotAgent };
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../agents/mrkl/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EACL,KAAK,EAIL,gBAAgB,GAEjB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAuBrC,IAAM,aAAa,qBAAnB,MAAM,aAAc,SAAQ,KAAK;IACtC,YAAY,KAAyB;QACnC,KAAK,CAAC,KAAK,CAAC,CAAC;IACf,CAAC;IAED,UAAU;QACR,OAAO,6BAAsC,CAAC;IAChD,CAAC;IAED,iBAAiB;QACf,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,SAAS;QACP,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,KAAa;QAChC,MAAM,WAAW,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC5D,IAAI,WAAW,EAAE;YACf,MAAM,GAAG,GACP,cAAc,WAAW,CAAC,IAAI,yBAAyB;gBACvD,kDAAkD,CAAC;YACrD,MAA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3 @@
|
|
|
1
|
+
export declare const PREFIX = "Answer the following questions as best you can. You have access to the following tools:";
|
|
2
|
+
export declare const formatInstructions: (toolNames: string) => string;
|
|
3
|
+
export declare const SUFFIX = "Begin!\n\nQuestion: {input}\nThought:{agent_scratchpad}";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
export const PREFIX = `Answer the following questions as best you can. You have access to the following tools:`;
|
|
3
|
+
export const formatInstructions = (toolNames) => `Use the following format:
|
|
4
|
+
|
|
5
|
+
Question: the input question you must answer
|
|
6
|
+
Thought: you should always think about what to do
|
|
7
|
+
Action: the action to take, should be one of [${toolNames}]
|
|
8
|
+
Action Input: the input to the action
|
|
9
|
+
Observation: the result of the action
|
|
10
|
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
|
11
|
+
Thought: I now know the final answer
|
|
12
|
+
Final Answer: the final answer to the original input question`;
|
|
13
|
+
export const SUFFIX = `Begin!
|
|
14
|
+
|
|
15
|
+
Question: {input}
|
|
16
|
+
Thought:{agent_scratchpad}`;
|
|
17
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../../agents/mrkl/prompt.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,MAAM,CAAC,MAAM,MAAM,GAAG,yFAAyF,CAAC;AAChH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,SAAiB,EACjB,EAAE,CAAC;;;;gDAI2C,SAAS;;;;;8DAKK,CAAC;AAC/D,MAAM,CAAC,MAAM,MAAM,GAAG;;;2BAGK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { test } from "@jest/globals";
|
|
11
|
+
import { OpenAI } from "../../llms/openai";
|
|
12
|
+
import { loadAgent } from "../load";
|
|
13
|
+
import { AgentExecutor } from "../index";
|
|
14
|
+
import { SerpAPI } from "../tools/serpapi";
|
|
15
|
+
test("Run agent from hub", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const model = new OpenAI({});
|
|
17
|
+
const tools = [SerpAPI()];
|
|
18
|
+
const agent = yield loadAgent("lc://agents/zero-shot-react-description/agent.json", { llm: model, tools });
|
|
19
|
+
const executor = AgentExecutor.fromAgentAndTools({
|
|
20
|
+
agent,
|
|
21
|
+
tools,
|
|
22
|
+
returnIntermediateSteps: true,
|
|
23
|
+
});
|
|
24
|
+
const res = yield executor.call({
|
|
25
|
+
input: "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
|
|
26
|
+
});
|
|
27
|
+
console.log(res);
|
|
28
|
+
}), 30000);
|
|
29
|
+
//# sourceMappingURL=agent.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.test.js","sourceRoot":"","sources":["../../../../agents/tests/agent.test.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,aAAa,EAAQ,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,IAAI,CAAC,oBAAoB,EAAE,GAAS,EAAE;IACpC,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC,CAAC;IAC7B,MAAM,KAAK,GAAW,CAAC,OAAO,EAAE,CAAC,CAAC;IAClC,MAAM,KAAK,GAAG,MAAM,SAAS,CAC3B,oDAAoD,EACpD,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,CACtB,CAAC;IACF,MAAM,QAAQ,GAAG,aAAa,CAAC,iBAAiB,CAAC;QAC/C,KAAK;QACL,KAAK;QACL,uBAAuB,EAAE,IAAI;KAC9B,CAAC,CAAC;IACH,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC;QAC9B,KAAK,EACH,oFAAoF;KACvF,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACnB,CAAC,CAAA,EAAE,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../agents/tools/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-math.js","sourceRoot":"","sources":["../../../../agents/tools/llm-math.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { getJson } from "serpapi";
|
|
11
|
+
export const SerpAPI = (params, apiKey) => {
|
|
12
|
+
const key = apiKey !== null && apiKey !== void 0 ? apiKey : process.env.SERPAPI_API_KEY;
|
|
13
|
+
return {
|
|
14
|
+
name: "search",
|
|
15
|
+
call: (input) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
17
|
+
const res = yield getJson("google", Object.assign(Object.assign({}, params), { api_key: key, q: input }));
|
|
18
|
+
if (res.error) {
|
|
19
|
+
throw new Error(`Got error from serpAPI: ${res.error}`);
|
|
20
|
+
}
|
|
21
|
+
if ((_a = res.answer_box) === null || _a === void 0 ? void 0 : _a.answer) {
|
|
22
|
+
return res.answer_box.answer;
|
|
23
|
+
}
|
|
24
|
+
if ((_b = res.answer_box) === null || _b === void 0 ? void 0 : _b.snippet) {
|
|
25
|
+
return res.answer_box.snippet;
|
|
26
|
+
}
|
|
27
|
+
if ((_c = res.answer_box) === null || _c === void 0 ? void 0 : _c.snippet_highlighted_words) {
|
|
28
|
+
return res.answer_box.snippet_highlighted_words[0];
|
|
29
|
+
}
|
|
30
|
+
if ((_d = res.sports_results) === null || _d === void 0 ? void 0 : _d.game_spotlight) {
|
|
31
|
+
return res.sports_results.game_spotlight;
|
|
32
|
+
}
|
|
33
|
+
if ((_e = res.knowledge_graph) === null || _e === void 0 ? void 0 : _e.description) {
|
|
34
|
+
return res.knowledge_graph.description;
|
|
35
|
+
}
|
|
36
|
+
if ((_g = (_f = res.organic_results) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.snippet) {
|
|
37
|
+
return res.organic_results[0].snippet;
|
|
38
|
+
}
|
|
39
|
+
return "No good search result found";
|
|
40
|
+
}),
|
|
41
|
+
description:
|
|
42
|
+
// eslint-disable-next-line max-len
|
|
43
|
+
"a search engine. useful for when you need to answer questions about current events. input should be a search query.",
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=serpapi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serpapi.js","sourceRoot":"","sources":["../../../../agents/tools/serpapi.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAoB,MAAM,SAAS,CAAC;AAGpD,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,MAAkC,EAClC,MAAe,EACT,EAAE;IACR,MAAM,GAAG,GAAG,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;IAClD,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,CAAO,KAAa,EAAE,EAAE;;YAC5B,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,QAAQ,kCAC7B,MAAM,KACT,OAAO,EAAE,GAAG,EACZ,CAAC,EAAE,KAAK,IACR,CAAC;YAEH,IAAI,GAAG,CAAC,KAAK,EAAE;gBACb,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;aACzD;YAED,IAAI,MAAA,GAAG,CAAC,UAAU,0CAAE,MAAM,EAAE;gBAC1B,OAAO,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC;aAC9B;YAED,IAAI,MAAA,GAAG,CAAC,UAAU,0CAAE,OAAO,EAAE;gBAC3B,OAAO,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC;aAC/B;YAED,IAAI,MAAA,GAAG,CAAC,UAAU,0CAAE,yBAAyB,EAAE;gBAC7C,OAAO,GAAG,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC;aACpD;YAED,IAAI,MAAA,GAAG,CAAC,cAAc,0CAAE,cAAc,EAAE;gBACtC,OAAO,GAAG,CAAC,cAAc,CAAC,cAAc,CAAC;aAC1C;YAED,IAAI,MAAA,GAAG,CAAC,eAAe,0CAAE,WAAW,EAAE;gBACpC,OAAO,GAAG,CAAC,eAAe,CAAC,WAAW,CAAC;aACxC;YAED,IAAI,MAAA,MAAA,GAAG,CAAC,eAAe,0CAAG,CAAC,CAAC,0CAAE,OAAO,EAAE;gBACrC,OAAO,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;aACvC;YAED,OAAO,6BAA6B,CAAC;QACvC,CAAC,CAAA;QAED,WAAW;QACT,mCAAmC;QACnC,qHAAqH;KACxH,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SerializedLLMChain } from "../chains";
|
|
2
|
+
import { AgentInput } from "./index";
|
|
3
|
+
export type AgentAction = {
|
|
4
|
+
tool: string;
|
|
5
|
+
toolInput: string;
|
|
6
|
+
log: string;
|
|
7
|
+
};
|
|
8
|
+
export type AgentFinish = {
|
|
9
|
+
returnValues: Record<string, any>;
|
|
10
|
+
log: string;
|
|
11
|
+
};
|
|
12
|
+
export type AgentStep = {
|
|
13
|
+
action: AgentAction;
|
|
14
|
+
observation: string;
|
|
15
|
+
};
|
|
16
|
+
export type StoppingMethod = "force" | "generate";
|
|
17
|
+
export type SerializedAgentT<TType extends string, FromLLMInput, ConstructorInput extends AgentInput> = {
|
|
18
|
+
_type: TType;
|
|
19
|
+
llm_chain?: SerializedLLMChain;
|
|
20
|
+
llm_chain_path?: string;
|
|
21
|
+
} & (({
|
|
22
|
+
load_from_llm_and_tools: true;
|
|
23
|
+
} & FromLLMInput) | ({
|
|
24
|
+
load_from_llm_and_tools?: false;
|
|
25
|
+
} & ConstructorInput));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../agents/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Generation } from "./llms";
|
|
2
|
+
export declare abstract class BaseCache<T = Generation[]> {
|
|
3
|
+
abstract lookup(prompt: string, llmKey: string): T | undefined;
|
|
4
|
+
abstract update(prompt: string, llmKey: string, value: T): void;
|
|
5
|
+
}
|
|
6
|
+
export declare class InMemoryCache<T = Generation[]> extends BaseCache<T> {
|
|
7
|
+
private cache;
|
|
8
|
+
constructor();
|
|
9
|
+
lookup(prompt: string, llmKey: string): T;
|
|
10
|
+
update(prompt: string, llmKey: string, value: T): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const getKey = (prompt, llmKey) => `${prompt}_${llmKey}`;
|
|
2
|
+
export class BaseCache {
|
|
3
|
+
}
|
|
4
|
+
export class InMemoryCache extends BaseCache {
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
Object.defineProperty(this, "cache", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: void 0
|
|
12
|
+
});
|
|
13
|
+
this.cache = {};
|
|
14
|
+
}
|
|
15
|
+
lookup(prompt, llmKey) {
|
|
16
|
+
return this.cache[getKey(prompt, llmKey)];
|
|
17
|
+
}
|
|
18
|
+
update(prompt, llmKey, value) {
|
|
19
|
+
this.cache[getKey(prompt, llmKey)] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../cache.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,CAAC,MAAc,EAAE,MAAc,EAAU,EAAE,CACxD,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;AAExB,MAAM,OAAgB,SAAS;CAI9B;AAED,MAAM,OAAO,aAAgC,SAAQ,SAAY;IAG/D;QACE,KAAK,EAAE,CAAC;QAHV;;;;;WAAiC;QAI/B,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;IAClB,CAAC;IAED,MAAM,CAAC,MAAc,EAAE,MAAc;QACnC,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,CAAC,MAAc,EAAE,MAAc,EAAE,KAAQ;QAC7C,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC;IAC7C,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SerializedLLMChain } from "./index";
|
|
2
|
+
export type ChainValues = Record<string, any>;
|
|
3
|
+
type SerializedBaseChain = SerializedLLMChain;
|
|
4
|
+
export declare abstract class BaseChain {
|
|
5
|
+
abstract _call(values: ChainValues): Promise<ChainValues>;
|
|
6
|
+
abstract _chainType(): string;
|
|
7
|
+
abstract serialize(): SerializedBaseChain;
|
|
8
|
+
call(values: ChainValues): Promise<ChainValues>;
|
|
9
|
+
apply(inputs: ChainValues[]): ChainValues[];
|
|
10
|
+
static deserialize(data: SerializedBaseChain): Promise<BaseChain>;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LLMChain } from "./index";
|
|
2
|
+
export class BaseChain {
|
|
3
|
+
call(values) {
|
|
4
|
+
// TODO(sean) add callback support
|
|
5
|
+
return this._call(values);
|
|
6
|
+
}
|
|
7
|
+
apply(inputs) {
|
|
8
|
+
return inputs.map(this.call);
|
|
9
|
+
}
|
|
10
|
+
static deserialize(data) {
|
|
11
|
+
switch (data._type) {
|
|
12
|
+
case "llm_chain":
|
|
13
|
+
return LLMChain.deserialize(data);
|
|
14
|
+
default:
|
|
15
|
+
throw new Error(`Invalid prompt type in config: ${data._type}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../chains/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAsB,MAAM,SAAS,CAAC;AAMvD,MAAM,OAAgB,SAAS;IAO7B,IAAI,CAAC,MAAmB;QACtB,kCAAkC;QAClC,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,MAAqB;QACzB,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,IAAyB;QAC1C,QAAQ,IAAI,CAAC,KAAK,EAAE;YAClB,KAAK,WAAW;gBACd,OAAO,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;YACpC;gBACE,MAAM,IAAI,KAAK,CACb,kCACG,IAA4B,CAAC,KAChC,EAAE,CACH,CAAC;SACL;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../chains/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAsB,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BaseChain, ChainValues } from "./index";
|
|
2
|
+
import { BaseLLM, SerializedLLM } from "../llms";
|
|
3
|
+
import { BasePromptTemplate, SerializedBasePromptTemplate } from "../prompt";
|
|
4
|
+
export interface LLMChainInput {
|
|
5
|
+
prompt: BasePromptTemplate;
|
|
6
|
+
llm: BaseLLM;
|
|
7
|
+
outputKey: string;
|
|
8
|
+
}
|
|
9
|
+
export type SerializedLLMChain = {
|
|
10
|
+
_type: "llm_chain";
|
|
11
|
+
llm?: SerializedLLM;
|
|
12
|
+
llm_path?: string;
|
|
13
|
+
prompt?: SerializedBasePromptTemplate;
|
|
14
|
+
prompt_path?: string;
|
|
15
|
+
};
|
|
16
|
+
export declare class LLMChain extends BaseChain implements LLMChainInput {
|
|
17
|
+
prompt: BasePromptTemplate;
|
|
18
|
+
llm: BaseLLM;
|
|
19
|
+
outputKey: string;
|
|
20
|
+
constructor(fields: {
|
|
21
|
+
prompt: BasePromptTemplate;
|
|
22
|
+
llm: BaseLLM;
|
|
23
|
+
outputKey?: string;
|
|
24
|
+
});
|
|
25
|
+
_call(values: ChainValues): Promise<ChainValues>;
|
|
26
|
+
predict(values: ChainValues): Promise<string>;
|
|
27
|
+
_chainType(): "llm_chain";
|
|
28
|
+
static deserialize(data: SerializedLLMChain): Promise<LLMChain>;
|
|
29
|
+
serialize(): SerializedLLMChain;
|
|
30
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BaseChain } from "./index";
|
|
11
|
+
import { BaseLLM } from "../llms";
|
|
12
|
+
import { BasePromptTemplate } from "../prompt";
|
|
13
|
+
import { resolveConfigFromFile } from "../util";
|
|
14
|
+
export class LLMChain extends BaseChain {
|
|
15
|
+
constructor(fields) {
|
|
16
|
+
var _a;
|
|
17
|
+
super();
|
|
18
|
+
Object.defineProperty(this, "prompt", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "llm", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "outputKey", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: "text"
|
|
35
|
+
});
|
|
36
|
+
this.prompt = fields.prompt;
|
|
37
|
+
this.llm = fields.llm;
|
|
38
|
+
this.outputKey = (_a = fields.outputKey) !== null && _a !== void 0 ? _a : this.outputKey;
|
|
39
|
+
}
|
|
40
|
+
_call(values) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
let stop;
|
|
43
|
+
if ("stop" in values && Array.isArray(values.stop)) {
|
|
44
|
+
stop = values.stop;
|
|
45
|
+
}
|
|
46
|
+
const formattedString = this.prompt.format(values);
|
|
47
|
+
const llmResult = yield this.llm.call(formattedString, stop);
|
|
48
|
+
const result = { [this.outputKey]: llmResult };
|
|
49
|
+
return result;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
predict(values) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
const output = yield this.call(values);
|
|
55
|
+
return output[this.outputKey];
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
_chainType() {
|
|
59
|
+
return "llm_chain";
|
|
60
|
+
}
|
|
61
|
+
static deserialize(data) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const serializedLLM = resolveConfigFromFile("llm", data);
|
|
64
|
+
const serializedPrompt = resolveConfigFromFile("prompt", data);
|
|
65
|
+
return new LLMChain({
|
|
66
|
+
llm: yield BaseLLM.deserialize(serializedLLM),
|
|
67
|
+
prompt: yield BasePromptTemplate.deserialize(serializedPrompt),
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
serialize() {
|
|
72
|
+
return {
|
|
73
|
+
_type: this._chainType(),
|
|
74
|
+
llm: this.llm.serialize(),
|
|
75
|
+
prompt: this.prompt.serialize(),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=llm_chain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm_chain.js","sourceRoot":"","sources":["../../../chains/llm_chain.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,OAAO,EAAiB,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAgC,MAAM,WAAW,CAAC;AAE7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAgBhD,MAAM,OAAO,QAAS,SAAQ,SAAS;IAOrC,YAAY,MAIX;;QACC,KAAK,EAAE,CAAC;QAXV;;;;;WAA2B;QAE3B;;;;;WAAa;QAEb;;;;mBAAY,MAAM;WAAC;QAQjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,SAAS,GAAG,MAAA,MAAM,CAAC,SAAS,mCAAI,IAAI,CAAC,SAAS,CAAC;IACtD,CAAC;IAEK,KAAK,CAAC,MAAmB;;YAC7B,IAAI,IAAI,CAAC;YACT,IAAI,MAAM,IAAI,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;gBAClD,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;aACpB;YACD,MAAM,eAAe,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACnD,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAAC;Y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