langchain 0.0.63 → 0.0.65
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/agents/agent.cjs +3 -4
- package/dist/agents/agent.js +3 -4
- package/dist/agents/chat_convo/prompt.cjs +1 -1
- package/dist/agents/chat_convo/prompt.d.ts +1 -1
- package/dist/agents/chat_convo/prompt.js +1 -1
- package/dist/agents/initialize.cjs +1 -1
- package/dist/agents/initialize.d.ts +1 -1
- package/dist/agents/initialize.js +1 -1
- package/dist/agents/types.cjs +2 -2
- package/dist/agents/types.d.ts +2 -1
- package/dist/agents/types.js +1 -1
- package/dist/base_language/index.d.ts +4 -1
- package/dist/callbacks/manager.cjs +5 -1
- package/dist/callbacks/manager.js +5 -1
- package/dist/chains/analyze_documents_chain.cjs +3 -3
- package/dist/chains/analyze_documents_chain.d.ts +2 -1
- package/dist/chains/analyze_documents_chain.js +3 -3
- package/dist/chains/base.cjs +30 -11
- package/dist/chains/base.d.ts +7 -3
- package/dist/chains/base.js +30 -11
- package/dist/chains/chat_vector_db_chain.cjs +4 -4
- package/dist/chains/chat_vector_db_chain.d.ts +9 -15
- package/dist/chains/chat_vector_db_chain.js +4 -4
- package/dist/chains/combine_docs_chain.cjs +11 -11
- package/dist/chains/combine_docs_chain.d.ts +6 -5
- package/dist/chains/combine_docs_chain.js +11 -11
- package/dist/chains/conversational_retrieval_chain.cjs +4 -4
- package/dist/chains/conversational_retrieval_chain.d.ts +2 -1
- package/dist/chains/conversational_retrieval_chain.js +4 -4
- package/dist/chains/index.cjs +4 -3
- package/dist/chains/index.d.ts +2 -2
- package/dist/chains/index.js +1 -1
- package/dist/chains/llm_chain.cjs +4 -4
- package/dist/chains/llm_chain.d.ts +3 -2
- package/dist/chains/llm_chain.js +4 -4
- package/dist/chains/retrieval_qa.cjs +3 -3
- package/dist/chains/retrieval_qa.d.ts +2 -1
- package/dist/chains/retrieval_qa.js +3 -3
- package/dist/chains/sequential_chain.cjs +262 -0
- package/dist/chains/{simple_sequential_chain.d.ts → sequential_chain.d.ts} +31 -2
- package/dist/chains/sequential_chain.js +257 -0
- package/dist/chains/serde.d.ts +7 -1
- package/dist/chains/sql_db/sql_db_chain.cjs +7 -5
- package/dist/chains/sql_db/sql_db_chain.d.ts +2 -1
- package/dist/chains/sql_db/sql_db_chain.js +7 -5
- package/dist/chains/vector_db_qa.cjs +3 -3
- package/dist/chains/vector_db_qa.d.ts +2 -1
- package/dist/chains/vector_db_qa.js +3 -3
- package/dist/chat_models/base.d.ts +10 -8
- package/dist/chat_models/openai.cjs +9 -5
- package/dist/chat_models/openai.d.ts +14 -5
- package/dist/chat_models/openai.js +10 -6
- package/dist/document_loaders/fs/unstructured.cjs +48 -1
- package/dist/document_loaders/fs/unstructured.d.ts +9 -1
- package/dist/document_loaders/fs/unstructured.js +46 -0
- package/dist/embeddings/tensorflow.cjs +67 -0
- package/dist/embeddings/tensorflow.d.ts +12 -0
- package/dist/embeddings/tensorflow.js +40 -0
- package/dist/experimental/autogpt/output_parser.cjs +2 -2
- package/dist/experimental/autogpt/output_parser.d.ts +1 -1
- package/dist/experimental/autogpt/output_parser.js +1 -1
- package/dist/experimental/babyagi/agent.cjs +194 -0
- package/dist/experimental/babyagi/agent.d.ts +47 -0
- package/dist/experimental/babyagi/agent.js +190 -0
- package/dist/experimental/babyagi/index.cjs +11 -0
- package/dist/experimental/babyagi/index.d.ts +4 -0
- package/dist/experimental/babyagi/index.js +4 -0
- package/dist/experimental/babyagi/task_creation.cjs +29 -0
- package/dist/experimental/babyagi/task_creation.d.ts +5 -0
- package/dist/experimental/babyagi/task_creation.js +25 -0
- package/dist/experimental/babyagi/task_execution.cjs +20 -0
- package/dist/experimental/babyagi/task_execution.d.ts +5 -0
- package/dist/experimental/babyagi/task_execution.js +16 -0
- package/dist/experimental/babyagi/task_prioritization.cjs +23 -0
- package/dist/experimental/babyagi/task_prioritization.d.ts +5 -0
- package/dist/experimental/babyagi/task_prioritization.js +19 -0
- package/dist/llms/base.cjs +0 -9
- package/dist/llms/base.d.ts +11 -12
- package/dist/llms/base.js +0 -9
- package/dist/llms/openai-chat.cjs +9 -5
- package/dist/llms/openai-chat.d.ts +16 -7
- package/dist/llms/openai-chat.js +9 -5
- package/dist/llms/openai.cjs +9 -5
- package/dist/llms/openai.d.ts +15 -12
- package/dist/llms/openai.js +10 -6
- package/dist/memory/base.d.ts +1 -0
- package/dist/memory/buffer_memory.cjs +3 -0
- package/dist/memory/buffer_memory.d.ts +1 -0
- package/dist/memory/buffer_memory.js +3 -0
- package/dist/memory/buffer_window_memory.cjs +5 -0
- package/dist/memory/buffer_window_memory.d.ts +1 -0
- package/dist/memory/buffer_window_memory.js +5 -0
- package/dist/memory/motorhead_memory.cjs +3 -0
- package/dist/memory/motorhead_memory.d.ts +1 -0
- package/dist/memory/motorhead_memory.js +3 -0
- package/dist/memory/summary.cjs +3 -0
- package/dist/memory/summary.d.ts +1 -0
- package/dist/memory/summary.js +3 -0
- package/dist/output_parsers/combining.cjs +4 -4
- package/dist/output_parsers/combining.d.ts +3 -2
- package/dist/output_parsers/combining.js +3 -3
- package/dist/output_parsers/fix.cjs +6 -6
- package/dist/output_parsers/fix.d.ts +3 -2
- package/dist/output_parsers/fix.js +4 -4
- package/dist/output_parsers/list.cjs +3 -3
- package/dist/output_parsers/list.d.ts +1 -1
- package/dist/output_parsers/list.js +1 -1
- package/dist/output_parsers/regex.cjs +3 -3
- package/dist/output_parsers/regex.d.ts +1 -1
- package/dist/output_parsers/regex.js +1 -1
- package/dist/output_parsers/structured.cjs +10 -4
- package/dist/output_parsers/structured.d.ts +1 -1
- package/dist/output_parsers/structured.js +9 -3
- package/dist/prompts/base.d.ts +2 -1
- package/dist/prompts/chat.cjs +16 -2
- package/dist/prompts/chat.d.ts +2 -2
- package/dist/prompts/chat.js +16 -2
- package/dist/retrievers/supabase.cjs +2 -2
- package/dist/retrievers/supabase.js +2 -2
- package/dist/schema/index.cjs +1 -21
- package/dist/schema/index.d.ts +0 -30
- package/dist/schema/index.js +0 -18
- package/dist/schema/output_parser.cjs +23 -0
- package/dist/schema/output_parser.d.ts +32 -0
- package/dist/schema/output_parser.js +18 -0
- package/dist/tools/base.cjs +4 -4
- package/dist/tools/base.d.ts +4 -7
- package/dist/tools/base.js +4 -4
- package/dist/tools/fs.cjs +4 -4
- package/dist/tools/fs.d.ts +2 -2
- package/dist/tools/fs.js +4 -4
- package/dist/tools/serpapi.cjs +17 -10
- package/dist/tools/serpapi.d.ts +4 -1
- package/dist/tools/serpapi.js +17 -10
- package/dist/tools/webbrowser.cjs +5 -5
- package/dist/tools/webbrowser.d.ts +6 -6
- package/dist/tools/webbrowser.js +5 -5
- package/dist/util/async_caller.cjs +9 -0
- package/dist/util/async_caller.js +9 -0
- package/dist/util/axios-fetch-adapter.cjs +6 -0
- package/dist/util/axios-fetch-adapter.js +6 -0
- package/dist/util/axios-types.d.ts +3 -1
- package/dist/util/flatten.cjs +21 -0
- package/dist/util/flatten.d.ts +1 -0
- package/dist/util/flatten.js +17 -0
- package/dist/util/set.cjs +41 -0
- package/dist/util/set.d.ts +15 -0
- package/dist/util/set.js +35 -0
- package/dist/vectorstores/pinecone.cjs +4 -0
- package/dist/vectorstores/pinecone.js +4 -0
- package/dist/vectorstores/supabase.cjs +1 -1
- package/dist/vectorstores/supabase.js +1 -1
- package/dist/vectorstores/weaviate.cjs +15 -9
- package/dist/vectorstores/weaviate.js +15 -9
- package/embeddings/tensorflow.cjs +1 -0
- package/embeddings/tensorflow.d.ts +1 -0
- package/embeddings/tensorflow.js +1 -0
- package/experimental/babyagi.cjs +1 -0
- package/experimental/babyagi.d.ts +1 -0
- package/experimental/babyagi.js +1 -0
- package/package.json +36 -5
- package/dist/chains/simple_sequential_chain.cjs +0 -124
- package/dist/chains/simple_sequential_chain.js +0 -120
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BabyAGI = void 0;
|
|
4
|
+
const base_js_1 = require("../../chains/base.cjs");
|
|
5
|
+
const document_js_1 = require("../../document.cjs");
|
|
6
|
+
const task_creation_js_1 = require("./task_creation.cjs");
|
|
7
|
+
const task_execution_js_1 = require("./task_execution.cjs");
|
|
8
|
+
const task_prioritization_js_1 = require("./task_prioritization.cjs");
|
|
9
|
+
class BabyAGI extends base_js_1.BaseChain {
|
|
10
|
+
constructor({ creationChain, prioritizationChain, executionChain, vectorstore, maxIterations = 100, verbose, callbacks, }) {
|
|
11
|
+
super(undefined, verbose, callbacks);
|
|
12
|
+
Object.defineProperty(this, "taskList", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "creationChain", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "prioritizationChain", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "executionChain", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "taskIDCounter", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: void 0
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "vectorstore", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(this, "maxIterations", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: void 0
|
|
53
|
+
});
|
|
54
|
+
this.taskList = [];
|
|
55
|
+
this.creationChain = creationChain;
|
|
56
|
+
this.prioritizationChain = prioritizationChain;
|
|
57
|
+
this.executionChain = executionChain;
|
|
58
|
+
this.taskIDCounter = 1;
|
|
59
|
+
this.vectorstore = vectorstore;
|
|
60
|
+
this.maxIterations = maxIterations;
|
|
61
|
+
}
|
|
62
|
+
_chainType() {
|
|
63
|
+
return "BabyAGI";
|
|
64
|
+
}
|
|
65
|
+
get inputKeys() {
|
|
66
|
+
return ["objective", "firstTask"];
|
|
67
|
+
}
|
|
68
|
+
get outputKeys() {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
async addTask(task) {
|
|
72
|
+
this.taskList.push(task);
|
|
73
|
+
}
|
|
74
|
+
printTaskList() {
|
|
75
|
+
console.log("\x1b[95m\x1b[1m\n*****TASK LIST*****\n\x1b[0m\x1b[0m");
|
|
76
|
+
for (const t of this.taskList) {
|
|
77
|
+
console.log(`${t.taskID}: ${t.taskName}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
printNextTask(task) {
|
|
81
|
+
console.log("\x1b[92m\x1b[1m\n*****NEXT TASK*****\n\x1b[0m\x1b[0m");
|
|
82
|
+
console.log(`${task.taskID}: ${task.taskName}`);
|
|
83
|
+
}
|
|
84
|
+
printTaskResult(result) {
|
|
85
|
+
console.log("\x1b[93m\x1b[1m\n*****TASK RESULT*****\n\x1b[0m\x1b[0m");
|
|
86
|
+
console.log(result.trim());
|
|
87
|
+
}
|
|
88
|
+
async getNextTasks(result, task_description, objective, runManager) {
|
|
89
|
+
const taskNames = this.taskList.map((t) => t.taskName);
|
|
90
|
+
const incomplete_tasks = taskNames.join(", ");
|
|
91
|
+
const { [this.creationChain.outputKeys[0]]: text } = await this.creationChain.call({
|
|
92
|
+
result,
|
|
93
|
+
task_description,
|
|
94
|
+
incomplete_tasks,
|
|
95
|
+
objective,
|
|
96
|
+
}, runManager?.getChild());
|
|
97
|
+
const newTasks = text.split("\n");
|
|
98
|
+
return newTasks
|
|
99
|
+
.filter((taskName) => taskName.trim())
|
|
100
|
+
.map((taskName) => ({ taskName }));
|
|
101
|
+
}
|
|
102
|
+
async prioritizeTasks(thisTaskID, objective, runManager) {
|
|
103
|
+
const taskNames = this.taskList.map((t) => t.taskName);
|
|
104
|
+
const nextTaskID = thisTaskID + 1;
|
|
105
|
+
const { [this.prioritizationChain.outputKeys[0]]: text } = await this.prioritizationChain.call({
|
|
106
|
+
task_names: taskNames.join(", "),
|
|
107
|
+
next_task_id: String(nextTaskID),
|
|
108
|
+
objective,
|
|
109
|
+
}, runManager?.getChild());
|
|
110
|
+
const newTasks = text.trim().split("\n");
|
|
111
|
+
const prioritizedTaskList = [];
|
|
112
|
+
for (const taskString of newTasks) {
|
|
113
|
+
const taskParts = taskString.trim().split(".", 2);
|
|
114
|
+
if (taskParts.length === 2) {
|
|
115
|
+
const taskID = taskParts[0].trim();
|
|
116
|
+
const taskName = taskParts[1].trim();
|
|
117
|
+
prioritizedTaskList.push({ taskID, taskName });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return prioritizedTaskList;
|
|
121
|
+
}
|
|
122
|
+
async getTopTasks(query, k = 5) {
|
|
123
|
+
const results = await this.vectorstore.similaritySearch(query, k);
|
|
124
|
+
if (!results) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
return results.map((item) => String(item.metadata.task));
|
|
128
|
+
}
|
|
129
|
+
async executeTask(objective, task, runManager) {
|
|
130
|
+
const context = await this.getTopTasks(objective);
|
|
131
|
+
const { [this.executionChain.outputKeys[0]]: text } = await this.executionChain.call({
|
|
132
|
+
objective,
|
|
133
|
+
context: context.join("\n"),
|
|
134
|
+
task,
|
|
135
|
+
}, runManager?.getChild());
|
|
136
|
+
return text;
|
|
137
|
+
}
|
|
138
|
+
async _call({ objective, firstTask = "Make a todo list" }, runManager) {
|
|
139
|
+
this.taskList = [];
|
|
140
|
+
this.taskIDCounter = 1;
|
|
141
|
+
await this.addTask({ taskID: "1", taskName: firstTask });
|
|
142
|
+
let numIters = 0;
|
|
143
|
+
while (numIters < this.maxIterations && this.taskList.length > 0) {
|
|
144
|
+
this.printTaskList();
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
146
|
+
const task = this.taskList.shift();
|
|
147
|
+
this.printNextTask(task);
|
|
148
|
+
const result = await this.executeTask(objective, task.taskName, runManager);
|
|
149
|
+
const thisTaskID = parseInt(task.taskID, 10);
|
|
150
|
+
this.printTaskResult(result);
|
|
151
|
+
await this.vectorstore.addDocuments([
|
|
152
|
+
new document_js_1.Document({
|
|
153
|
+
pageContent: result,
|
|
154
|
+
metadata: { task: task.taskName },
|
|
155
|
+
}),
|
|
156
|
+
]);
|
|
157
|
+
const newTasks = await this.getNextTasks(result, task.taskName, objective, runManager);
|
|
158
|
+
for (const newTask of newTasks) {
|
|
159
|
+
this.taskIDCounter += 1;
|
|
160
|
+
newTask.taskID = this.taskIDCounter.toFixed();
|
|
161
|
+
await this.addTask(newTask);
|
|
162
|
+
}
|
|
163
|
+
this.taskList = await this.prioritizeTasks(thisTaskID, objective, runManager);
|
|
164
|
+
numIters += 1;
|
|
165
|
+
}
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
serialize() {
|
|
169
|
+
throw new Error("Method not implemented.");
|
|
170
|
+
}
|
|
171
|
+
static fromLLM({ llm, vectorstore, executionChain, verbose, callbacks, ...rest }) {
|
|
172
|
+
const creationChain = task_creation_js_1.TaskCreationChain.fromLLM({
|
|
173
|
+
llm,
|
|
174
|
+
verbose,
|
|
175
|
+
callbacks,
|
|
176
|
+
});
|
|
177
|
+
const prioritizationChain = task_prioritization_js_1.TaskPrioritizationChain.fromLLM({
|
|
178
|
+
llm,
|
|
179
|
+
verbose,
|
|
180
|
+
callbacks,
|
|
181
|
+
});
|
|
182
|
+
return new BabyAGI({
|
|
183
|
+
creationChain,
|
|
184
|
+
prioritizationChain,
|
|
185
|
+
executionChain: executionChain ||
|
|
186
|
+
task_execution_js_1.TaskExecutionChain.fromLLM({ llm, verbose, callbacks }),
|
|
187
|
+
vectorstore,
|
|
188
|
+
verbose,
|
|
189
|
+
callbacks,
|
|
190
|
+
...rest,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.BabyAGI = BabyAGI;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
2
|
+
import { CallbackManagerForChainRun } from "../../callbacks/manager.js";
|
|
3
|
+
import { BaseChain, ChainInputs } from "../../chains/base.js";
|
|
4
|
+
import { SerializedBaseChain } from "../../chains/serde.js";
|
|
5
|
+
import { ChainValues } from "../../schema/index.js";
|
|
6
|
+
import { Optional } from "../../types/type-utils.js";
|
|
7
|
+
import { VectorStore } from "../../vectorstores/base.js";
|
|
8
|
+
export interface Task {
|
|
9
|
+
taskID: string;
|
|
10
|
+
taskName: string;
|
|
11
|
+
}
|
|
12
|
+
export interface BabyAGIInputs extends Omit<ChainInputs, "memory" | "callbackManager"> {
|
|
13
|
+
creationChain: BaseChain;
|
|
14
|
+
prioritizationChain: BaseChain;
|
|
15
|
+
executionChain: BaseChain;
|
|
16
|
+
vectorstore: VectorStore;
|
|
17
|
+
maxIterations?: number;
|
|
18
|
+
}
|
|
19
|
+
export declare class BabyAGI extends BaseChain implements BabyAGIInputs {
|
|
20
|
+
taskList: Task[];
|
|
21
|
+
creationChain: BaseChain;
|
|
22
|
+
prioritizationChain: BaseChain;
|
|
23
|
+
executionChain: BaseChain;
|
|
24
|
+
taskIDCounter: number;
|
|
25
|
+
vectorstore: VectorStore;
|
|
26
|
+
maxIterations: number;
|
|
27
|
+
constructor({ creationChain, prioritizationChain, executionChain, vectorstore, maxIterations, verbose, callbacks, }: BabyAGIInputs);
|
|
28
|
+
_chainType(): "BabyAGI";
|
|
29
|
+
get inputKeys(): string[];
|
|
30
|
+
get outputKeys(): never[];
|
|
31
|
+
addTask(task: Task): Promise<void>;
|
|
32
|
+
printTaskList(): void;
|
|
33
|
+
printNextTask(task: Task): void;
|
|
34
|
+
printTaskResult(result: string): void;
|
|
35
|
+
getNextTasks(result: string, task_description: string, objective: string, runManager?: CallbackManagerForChainRun): Promise<Optional<Task, "taskID">[]>;
|
|
36
|
+
prioritizeTasks(thisTaskID: number, objective: string, runManager?: CallbackManagerForChainRun): Promise<{
|
|
37
|
+
taskID: string;
|
|
38
|
+
taskName: string;
|
|
39
|
+
}[]>;
|
|
40
|
+
getTopTasks(query: string, k?: number): Promise<string[]>;
|
|
41
|
+
executeTask(objective: string, task: string, runManager?: CallbackManagerForChainRun): Promise<string>;
|
|
42
|
+
_call({ objective, firstTask }: ChainValues, runManager?: CallbackManagerForChainRun): Promise<{}>;
|
|
43
|
+
serialize(): SerializedBaseChain;
|
|
44
|
+
static fromLLM({ llm, vectorstore, executionChain, verbose, callbacks, ...rest }: Optional<BabyAGIInputs, "executionChain" | "creationChain" | "prioritizationChain"> & {
|
|
45
|
+
llm: BaseLanguageModel;
|
|
46
|
+
}): BabyAGI;
|
|
47
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { BaseChain } from "../../chains/base.js";
|
|
2
|
+
import { Document } from "../../document.js";
|
|
3
|
+
import { TaskCreationChain } from "./task_creation.js";
|
|
4
|
+
import { TaskExecutionChain } from "./task_execution.js";
|
|
5
|
+
import { TaskPrioritizationChain } from "./task_prioritization.js";
|
|
6
|
+
export class BabyAGI extends BaseChain {
|
|
7
|
+
constructor({ creationChain, prioritizationChain, executionChain, vectorstore, maxIterations = 100, verbose, callbacks, }) {
|
|
8
|
+
super(undefined, verbose, callbacks);
|
|
9
|
+
Object.defineProperty(this, "taskList", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: void 0
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "creationChain", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "prioritizationChain", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "executionChain", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "taskIDCounter", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "vectorstore", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(this, "maxIterations", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: void 0
|
|
50
|
+
});
|
|
51
|
+
this.taskList = [];
|
|
52
|
+
this.creationChain = creationChain;
|
|
53
|
+
this.prioritizationChain = prioritizationChain;
|
|
54
|
+
this.executionChain = executionChain;
|
|
55
|
+
this.taskIDCounter = 1;
|
|
56
|
+
this.vectorstore = vectorstore;
|
|
57
|
+
this.maxIterations = maxIterations;
|
|
58
|
+
}
|
|
59
|
+
_chainType() {
|
|
60
|
+
return "BabyAGI";
|
|
61
|
+
}
|
|
62
|
+
get inputKeys() {
|
|
63
|
+
return ["objective", "firstTask"];
|
|
64
|
+
}
|
|
65
|
+
get outputKeys() {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
async addTask(task) {
|
|
69
|
+
this.taskList.push(task);
|
|
70
|
+
}
|
|
71
|
+
printTaskList() {
|
|
72
|
+
console.log("\x1b[95m\x1b[1m\n*****TASK LIST*****\n\x1b[0m\x1b[0m");
|
|
73
|
+
for (const t of this.taskList) {
|
|
74
|
+
console.log(`${t.taskID}: ${t.taskName}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
printNextTask(task) {
|
|
78
|
+
console.log("\x1b[92m\x1b[1m\n*****NEXT TASK*****\n\x1b[0m\x1b[0m");
|
|
79
|
+
console.log(`${task.taskID}: ${task.taskName}`);
|
|
80
|
+
}
|
|
81
|
+
printTaskResult(result) {
|
|
82
|
+
console.log("\x1b[93m\x1b[1m\n*****TASK RESULT*****\n\x1b[0m\x1b[0m");
|
|
83
|
+
console.log(result.trim());
|
|
84
|
+
}
|
|
85
|
+
async getNextTasks(result, task_description, objective, runManager) {
|
|
86
|
+
const taskNames = this.taskList.map((t) => t.taskName);
|
|
87
|
+
const incomplete_tasks = taskNames.join(", ");
|
|
88
|
+
const { [this.creationChain.outputKeys[0]]: text } = await this.creationChain.call({
|
|
89
|
+
result,
|
|
90
|
+
task_description,
|
|
91
|
+
incomplete_tasks,
|
|
92
|
+
objective,
|
|
93
|
+
}, runManager?.getChild());
|
|
94
|
+
const newTasks = text.split("\n");
|
|
95
|
+
return newTasks
|
|
96
|
+
.filter((taskName) => taskName.trim())
|
|
97
|
+
.map((taskName) => ({ taskName }));
|
|
98
|
+
}
|
|
99
|
+
async prioritizeTasks(thisTaskID, objective, runManager) {
|
|
100
|
+
const taskNames = this.taskList.map((t) => t.taskName);
|
|
101
|
+
const nextTaskID = thisTaskID + 1;
|
|
102
|
+
const { [this.prioritizationChain.outputKeys[0]]: text } = await this.prioritizationChain.call({
|
|
103
|
+
task_names: taskNames.join(", "),
|
|
104
|
+
next_task_id: String(nextTaskID),
|
|
105
|
+
objective,
|
|
106
|
+
}, runManager?.getChild());
|
|
107
|
+
const newTasks = text.trim().split("\n");
|
|
108
|
+
const prioritizedTaskList = [];
|
|
109
|
+
for (const taskString of newTasks) {
|
|
110
|
+
const taskParts = taskString.trim().split(".", 2);
|
|
111
|
+
if (taskParts.length === 2) {
|
|
112
|
+
const taskID = taskParts[0].trim();
|
|
113
|
+
const taskName = taskParts[1].trim();
|
|
114
|
+
prioritizedTaskList.push({ taskID, taskName });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return prioritizedTaskList;
|
|
118
|
+
}
|
|
119
|
+
async getTopTasks(query, k = 5) {
|
|
120
|
+
const results = await this.vectorstore.similaritySearch(query, k);
|
|
121
|
+
if (!results) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
return results.map((item) => String(item.metadata.task));
|
|
125
|
+
}
|
|
126
|
+
async executeTask(objective, task, runManager) {
|
|
127
|
+
const context = await this.getTopTasks(objective);
|
|
128
|
+
const { [this.executionChain.outputKeys[0]]: text } = await this.executionChain.call({
|
|
129
|
+
objective,
|
|
130
|
+
context: context.join("\n"),
|
|
131
|
+
task,
|
|
132
|
+
}, runManager?.getChild());
|
|
133
|
+
return text;
|
|
134
|
+
}
|
|
135
|
+
async _call({ objective, firstTask = "Make a todo list" }, runManager) {
|
|
136
|
+
this.taskList = [];
|
|
137
|
+
this.taskIDCounter = 1;
|
|
138
|
+
await this.addTask({ taskID: "1", taskName: firstTask });
|
|
139
|
+
let numIters = 0;
|
|
140
|
+
while (numIters < this.maxIterations && this.taskList.length > 0) {
|
|
141
|
+
this.printTaskList();
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
143
|
+
const task = this.taskList.shift();
|
|
144
|
+
this.printNextTask(task);
|
|
145
|
+
const result = await this.executeTask(objective, task.taskName, runManager);
|
|
146
|
+
const thisTaskID = parseInt(task.taskID, 10);
|
|
147
|
+
this.printTaskResult(result);
|
|
148
|
+
await this.vectorstore.addDocuments([
|
|
149
|
+
new Document({
|
|
150
|
+
pageContent: result,
|
|
151
|
+
metadata: { task: task.taskName },
|
|
152
|
+
}),
|
|
153
|
+
]);
|
|
154
|
+
const newTasks = await this.getNextTasks(result, task.taskName, objective, runManager);
|
|
155
|
+
for (const newTask of newTasks) {
|
|
156
|
+
this.taskIDCounter += 1;
|
|
157
|
+
newTask.taskID = this.taskIDCounter.toFixed();
|
|
158
|
+
await this.addTask(newTask);
|
|
159
|
+
}
|
|
160
|
+
this.taskList = await this.prioritizeTasks(thisTaskID, objective, runManager);
|
|
161
|
+
numIters += 1;
|
|
162
|
+
}
|
|
163
|
+
return {};
|
|
164
|
+
}
|
|
165
|
+
serialize() {
|
|
166
|
+
throw new Error("Method not implemented.");
|
|
167
|
+
}
|
|
168
|
+
static fromLLM({ llm, vectorstore, executionChain, verbose, callbacks, ...rest }) {
|
|
169
|
+
const creationChain = TaskCreationChain.fromLLM({
|
|
170
|
+
llm,
|
|
171
|
+
verbose,
|
|
172
|
+
callbacks,
|
|
173
|
+
});
|
|
174
|
+
const prioritizationChain = TaskPrioritizationChain.fromLLM({
|
|
175
|
+
llm,
|
|
176
|
+
verbose,
|
|
177
|
+
callbacks,
|
|
178
|
+
});
|
|
179
|
+
return new BabyAGI({
|
|
180
|
+
creationChain,
|
|
181
|
+
prioritizationChain,
|
|
182
|
+
executionChain: executionChain ||
|
|
183
|
+
TaskExecutionChain.fromLLM({ llm, verbose, callbacks }),
|
|
184
|
+
vectorstore,
|
|
185
|
+
verbose,
|
|
186
|
+
callbacks,
|
|
187
|
+
...rest,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BabyAGI = exports.TaskPrioritizationChain = exports.TaskExecutionChain = exports.TaskCreationChain = void 0;
|
|
4
|
+
var task_creation_js_1 = require("./task_creation.cjs");
|
|
5
|
+
Object.defineProperty(exports, "TaskCreationChain", { enumerable: true, get: function () { return task_creation_js_1.TaskCreationChain; } });
|
|
6
|
+
var task_execution_js_1 = require("./task_execution.cjs");
|
|
7
|
+
Object.defineProperty(exports, "TaskExecutionChain", { enumerable: true, get: function () { return task_execution_js_1.TaskExecutionChain; } });
|
|
8
|
+
var task_prioritization_js_1 = require("./task_prioritization.cjs");
|
|
9
|
+
Object.defineProperty(exports, "TaskPrioritizationChain", { enumerable: true, get: function () { return task_prioritization_js_1.TaskPrioritizationChain; } });
|
|
10
|
+
var agent_js_1 = require("./agent.cjs");
|
|
11
|
+
Object.defineProperty(exports, "BabyAGI", { enumerable: true, get: function () { return agent_js_1.BabyAGI; } });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskCreationChain = void 0;
|
|
4
|
+
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
|
+
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
6
|
+
/** Chain to generate tasks. */
|
|
7
|
+
class TaskCreationChain extends llm_chain_js_1.LLMChain {
|
|
8
|
+
static fromLLM(fields) {
|
|
9
|
+
const taskCreationTemplate = `You are an task creation AI that uses the result of an execution agent` +
|
|
10
|
+
` to create new tasks with the following objective: {objective},` +
|
|
11
|
+
` The last completed task has the result: {result}.` +
|
|
12
|
+
` This result was based on this task description: {task_description}.` +
|
|
13
|
+
` These are incomplete tasks: {incomplete_tasks}.` +
|
|
14
|
+
` Based on the result, create new tasks to be completed` +
|
|
15
|
+
` by the AI system that do not overlap with incomplete tasks.` +
|
|
16
|
+
` Return the tasks as an array.`;
|
|
17
|
+
const prompt = new prompt_js_1.PromptTemplate({
|
|
18
|
+
template: taskCreationTemplate,
|
|
19
|
+
inputVariables: [
|
|
20
|
+
"result",
|
|
21
|
+
"task_description",
|
|
22
|
+
"incomplete_tasks",
|
|
23
|
+
"objective",
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
return new TaskCreationChain({ prompt, ...fields });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.TaskCreationChain = TaskCreationChain;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
3
|
+
/** Chain to generate tasks. */
|
|
4
|
+
export class TaskCreationChain extends LLMChain {
|
|
5
|
+
static fromLLM(fields) {
|
|
6
|
+
const taskCreationTemplate = `You are an task creation AI that uses the result of an execution agent` +
|
|
7
|
+
` to create new tasks with the following objective: {objective},` +
|
|
8
|
+
` The last completed task has the result: {result}.` +
|
|
9
|
+
` This result was based on this task description: {task_description}.` +
|
|
10
|
+
` These are incomplete tasks: {incomplete_tasks}.` +
|
|
11
|
+
` Based on the result, create new tasks to be completed` +
|
|
12
|
+
` by the AI system that do not overlap with incomplete tasks.` +
|
|
13
|
+
` Return the tasks as an array.`;
|
|
14
|
+
const prompt = new PromptTemplate({
|
|
15
|
+
template: taskCreationTemplate,
|
|
16
|
+
inputVariables: [
|
|
17
|
+
"result",
|
|
18
|
+
"task_description",
|
|
19
|
+
"incomplete_tasks",
|
|
20
|
+
"objective",
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
return new TaskCreationChain({ prompt, ...fields });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskExecutionChain = void 0;
|
|
4
|
+
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
|
+
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
6
|
+
/** Chain to execute tasks. */
|
|
7
|
+
class TaskExecutionChain extends llm_chain_js_1.LLMChain {
|
|
8
|
+
static fromLLM(fields) {
|
|
9
|
+
const executionTemplate = `You are an AI who performs one task based on the following objective: ` +
|
|
10
|
+
`{objective}.` +
|
|
11
|
+
`Take into account these previously completed tasks: {context}.` +
|
|
12
|
+
` Your task: {task}. Response:`;
|
|
13
|
+
const prompt = new prompt_js_1.PromptTemplate({
|
|
14
|
+
template: executionTemplate,
|
|
15
|
+
inputVariables: ["objective", "context", "task"],
|
|
16
|
+
});
|
|
17
|
+
return new TaskExecutionChain({ prompt, ...fields });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.TaskExecutionChain = TaskExecutionChain;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
3
|
+
/** Chain to execute tasks. */
|
|
4
|
+
export class TaskExecutionChain extends LLMChain {
|
|
5
|
+
static fromLLM(fields) {
|
|
6
|
+
const executionTemplate = `You are an AI who performs one task based on the following objective: ` +
|
|
7
|
+
`{objective}.` +
|
|
8
|
+
`Take into account these previously completed tasks: {context}.` +
|
|
9
|
+
` Your task: {task}. Response:`;
|
|
10
|
+
const prompt = new PromptTemplate({
|
|
11
|
+
template: executionTemplate,
|
|
12
|
+
inputVariables: ["objective", "context", "task"],
|
|
13
|
+
});
|
|
14
|
+
return new TaskExecutionChain({ prompt, ...fields });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskPrioritizationChain = void 0;
|
|
4
|
+
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
|
+
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
6
|
+
/** Chain to prioritize tasks. */
|
|
7
|
+
class TaskPrioritizationChain extends llm_chain_js_1.LLMChain {
|
|
8
|
+
static fromLLM(fields) {
|
|
9
|
+
const taskPrioritizationTemplate = `You are a task prioritization AI tasked with cleaning the formatting of ` +
|
|
10
|
+
`and reprioritizing the following tasks: {task_names}.` +
|
|
11
|
+
` Consider the ultimate objective of your team: {objective}.` +
|
|
12
|
+
` Do not remove any tasks. Return the result as a numbered list, like:` +
|
|
13
|
+
` #. First task` +
|
|
14
|
+
` #. Second task` +
|
|
15
|
+
` Start the task list with number {next_task_id}.`;
|
|
16
|
+
const prompt = new prompt_js_1.PromptTemplate({
|
|
17
|
+
template: taskPrioritizationTemplate,
|
|
18
|
+
inputVariables: ["task_names", "next_task_id", "objective"],
|
|
19
|
+
});
|
|
20
|
+
return new TaskPrioritizationChain({ prompt, ...fields });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.TaskPrioritizationChain = TaskPrioritizationChain;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
3
|
+
/** Chain to prioritize tasks. */
|
|
4
|
+
export class TaskPrioritizationChain extends LLMChain {
|
|
5
|
+
static fromLLM(fields) {
|
|
6
|
+
const taskPrioritizationTemplate = `You are a task prioritization AI tasked with cleaning the formatting of ` +
|
|
7
|
+
`and reprioritizing the following tasks: {task_names}.` +
|
|
8
|
+
` Consider the ultimate objective of your team: {objective}.` +
|
|
9
|
+
` Do not remove any tasks. Return the result as a numbered list, like:` +
|
|
10
|
+
` #. First task` +
|
|
11
|
+
` #. Second task` +
|
|
12
|
+
` Start the task list with number {next_task_id}.`;
|
|
13
|
+
const prompt = new PromptTemplate({
|
|
14
|
+
template: taskPrioritizationTemplate,
|
|
15
|
+
inputVariables: ["task_names", "next_task_id", "objective"],
|
|
16
|
+
});
|
|
17
|
+
return new TaskPrioritizationChain({ prompt, ...fields });
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/llms/base.cjs
CHANGED
|
@@ -11,15 +11,6 @@ const manager_js_1 = require("../callbacks/manager.cjs");
|
|
|
11
11
|
class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
12
12
|
constructor({ cache, concurrency, ...rest }) {
|
|
13
13
|
super(concurrency ? { maxConcurrency: concurrency, ...rest } : rest);
|
|
14
|
-
/**
|
|
15
|
-
* The name of the LLM class
|
|
16
|
-
*/
|
|
17
|
-
Object.defineProperty(this, "name", {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: void 0
|
|
22
|
-
});
|
|
23
14
|
Object.defineProperty(this, "cache", {
|
|
24
15
|
enumerable: true,
|
|
25
16
|
configurable: true,
|