forked from reworkd/AgentGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.ts
100 lines (91 loc) · 3.54 KB
/
chain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
import type { ModelSettings } from "./types";
import { GPT_35_TURBO } from "./constants";
export const createModel = (settings: ModelSettings) =>
new OpenAI({
openAIApiKey:
settings.customApiKey === ""
? process.env.OPENAI_API_KEY
: settings.customApiKey,
temperature: 0.9,
modelName:
settings.customModelName === "" ? GPT_35_TURBO : settings.customModelName,
maxTokens: 750,
});
const startGoalPrompt = new PromptTemplate({
template:
"You are an autonomous task creation AI called AgentGPT. You have the following objective `{goal}`. Create a list of zero to three tasks to be completed by your AI system such that your goal is more closely reached or completely reached. Return the response as an array of strings that can be used in JSON.parse()",
inputVariables: ["goal"],
});
export const startGoalAgent = async (model: OpenAI, goal: string) => {
return await new LLMChain({
llm: model,
prompt: startGoalPrompt,
}).call({
goal,
});
};
const executeTaskPrompt = new PromptTemplate({
template:
"You are an autonomous task execution AI called AgentGPT. You have the following objective `{goal}`. You have the following tasks `{task}`. Execute the task and return the response as a string.",
inputVariables: ["goal", "task"],
});
export const executeTaskAgent = async (
model: OpenAI,
goal: string,
task: string
) => {
return await new LLMChain({ llm: model, prompt: executeTaskPrompt }).call({
goal,
task,
});
};
const createTaskPrompt = new PromptTemplate({
template:
"You are an AI task creation agent. You have the following objective `{goal}`. You have the following incomplete tasks `{tasks}` and have just executed the following task `{lastTask}` and received the following result `{result}`. Based on this, create a new task to be completed by your AI system ONLY IF NEEDED such that your goal is more closely reached or completely reached. Return the response as an array of strings that can be used in JSON.parse() and NOTHING ELSE",
inputVariables: ["goal", "tasks", "lastTask", "result"],
});
export const executeCreateTaskAgent = async (
model: OpenAI,
goal: string,
tasks: string[],
lastTask: string,
result: string
) => {
return await new LLMChain({ llm: model, prompt: createTaskPrompt }).call({
goal,
tasks,
lastTask,
result,
});
};
export const extractArray = (inputStr: string): string[] => {
// Match an outer array of strings (including nested arrays)
const regex = /(\[(?:\s*"(?:[^"\\]|\\.)*"\s*,?)+\s*\])/;
const match = inputStr.match(regex);
if (match && match[0]) {
try {
// Parse the matched string to get the array
return JSON.parse(match[0]) as string[];
} catch (error) {
console.error("Error parsing the matched array:", error);
}
}
console.warn("Error, could not extract array from inputString:", inputStr);
return [];
};
// Model will return tasks such as "No tasks added". We should filter these
export const realTasksFilter = (input: string): boolean => {
const noTaskRegex =
/^No( (new|further|additional|extra|other))? tasks? (is )?(required|needed|added|created|inputted).*$/i;
const taskCompleteRegex =
/^Task (complete|completed|finished|done|over|success).*/i;
const doNothingRegex = /^(\s*|Do nothing(\s.*)?)$/i;
return (
!noTaskRegex.test(input) &&
!taskCompleteRegex.test(input) &&
!doNothingRegex.test(input)
);
};