forked from wrtnlabs/agentica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGptAgent.ts
More file actions
52 lines (46 loc) · 1.9 KB
/
ChatGptAgent.ts
File metadata and controls
52 lines (46 loc) · 1.9 KB
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
import { IWrtnAgentContext } from "../structures/IWrtnAgentContext";
import { IWrtnAgentPrompt } from "../structures/IWrtnAgentPrompt";
import { ChatGptCancelFunctionAgent } from "./ChatGptCancelFunctionAgent";
import { ChatGptDescribeFunctionAgent } from "./ChatGptDescribeFunctionAgent";
import { ChatGptExecuteFunctionAgent } from "./ChatGptExecuteFunctionAgent";
import { ChatGptInitializeFunctionAgent } from "./ChatGptInitializeFunctionAgent";
import { ChatGptSelectFunctionAgent } from "./ChatGptSelectFunctionAgent";
export namespace ChatGptAgent {
export const execute = async (
ctx: IWrtnAgentContext,
): Promise<IWrtnAgentPrompt[]> => {
const histories: IWrtnAgentPrompt[] = [];
// FUNCTIONS ARE NOT LISTED YET
if (ctx.ready() === false) {
histories.push(...(await ChatGptInitializeFunctionAgent.execute(ctx)));
if (ctx.ready() === false) return histories;
}
// CANCEL CANDIDATE FUNCTIONS
if (ctx.stack.length !== 0)
histories.push(...(await ChatGptCancelFunctionAgent.execute(ctx)));
// SELECT CANDIDATE FUNCTIONS
histories.push(...(await ChatGptSelectFunctionAgent.execute(ctx)));
if (ctx.stack.length === 0) return histories;
// FUNCTION CALLING LOOP
while (true) {
// EXECUTE FUNCTIONS
const prompts: IWrtnAgentPrompt[] =
await ChatGptExecuteFunctionAgent.execute(ctx);
histories.push(...prompts);
// EXPLAIN RETURN VALUES
const executes: IWrtnAgentPrompt.IExecute[] = prompts.filter(
(prompt) => prompt.type === "execute",
);
for (const e of executes)
await ChatGptCancelFunctionAgent.cancelFunction(ctx, {
reason: "completed",
name: e.function.name,
});
histories.push(
...(await ChatGptDescribeFunctionAgent.execute(ctx, executes)),
);
if (executes.length === 0 || ctx.stack.length === 0) break;
}
return histories;
};
}