-
Notifications
You must be signed in to change notification settings - Fork 5
/
agent.ts
122 lines (108 loc) · 5.07 KB
/
agent.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Agent, ModelMessage, PluginInvocation } from "../src/index";
import { Chat } from "../src/models/api/openai";
import { PluginOutput } from "../src/plugins/index";
import { MessageRoles } from "../src/types";
import { _detectPluginUse, _handlePluginOutput, pluginsPrompt } from "./prompt";
export class ChimaeraAgent extends Agent {
model = "gpt-3.5-turbo";
max_tokens = 750;
temperature = 0.1;
verbose = true;
chimaeraPrompt =
() => `You are Assistant, a helpful artificial intelligence that answers questions in a chat. You and the human you are chatting with work for Science Corporation, a company that pursues advances in brain-computer interfaces, genetic engineering, automated science, and artificial intelligence.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. You are able to engage in fluent, detailed conversation and provide responses that are coherent and relevant to the topic at hand. The text you generate uses Markdown syntax for formatting, so you are sure to, for example, reformat <a> links to [links](https://example.com) and <b> bold text to **bold text**. You can display images using Markdown's exclamation point syntax, for example: ![alt text](https://example.com/image.png).
You are careful to think step by step and show your work. When asked a question, your first instinct is to break down the question into sub-parts and work systematically to answer them one at a time before synthesizing your thoughts into a coherent response. You are also careful to provide explanations and justifications for your answers, so that the human you are chatting with can understand your reasoning and follow your thought process.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Knowledge cutoff: 2021-09
Current date: ${new Date().toLocaleDateString("sv")}`;
override pluginDetectRegex = /<%\*\?\?\*%>.*?<%\*\?\?\*%>/gs;
basePrompt = () =>
this.chimaeraPrompt() + (this.plugins.length > 0 ? "\n\n" + pluginsPrompt(this.plugins) : "");
handlePluginOutput = (input: PluginInvocation, output: PluginOutput) =>
_handlePluginOutput(this, input, output);
detectPluginUse = (response: string): false | PluginInvocation => _detectPluginUse(response);
apiSpecModel = async (invoke: PluginInvocation) => {
console.log(`Checking plugin invocation with apiSpecModel: ${JSON.stringify(invoke)}`);
const plugin = this.plugins.find(
(p) => p.manifest.name_for_model.toUpperCase() === invoke.name.toUpperCase()
);
if (!plugin) {
return Promise.reject("Plugin not found");
}
const input = await Chat.sync(
{
messages: [
{
role: "system",
content: `${pluginsPrompt([plugin])}\n${plugin.apiSpecPrompt()}`,
},
{
role: "user",
content: `<%*??*%>${invoke.name}: ${invoke.action}: ${invoke.input}<%*??*%>
Is this a correct invocation for the ${plugin.manifest.name_for_model} plugin? If it is, return the same text. If not, return the corrected invocation with the right pluginAction and pluginInput EXACTLY formatted per the provided specification in the schema and syntax required.`,
},
],
model: this.model,
max_tokens: 250,
},
{
onError: (e) => console.error(e),
}
);
if (!input || !input.content) {
return Promise.reject("Could not get API spec from model");
}
const corrected = this.detectPluginUse(input.content);
if (!corrected) {
return Promise.reject("Could not detect plugin invocation in response");
}
return corrected;
};
metaprompt: () => Promise<ModelMessage[]> = async () => {
// const compressed = await compressor(this.basePrompt(), this.model);
const compressed = false;
return [
{
role: "system",
content: compressed ? compressed : this.basePrompt(),
},
];
};
compilePrompt = async (messages: ModelMessage[]) => [
this.messages[0],
{
role: "agent",
},
this.messages[-1],
];
run = async (prompt: string, role?: MessageRoles) => {
if (!role) {
role = "user";
}
// const embed = await Embedding.create({
// input: [prompt],
// model: "text-embedding-ada-002",
// });
// const compressed = await compressor(prompt, this.model);
this.messages.push({
role: role,
content: prompt,
// embedding: embed?.embedding,
});
await Chat.stream(
{
messages: this.messages,
model: this.model,
max_tokens: this.max_tokens,
temperature: this.temperature,
},
{
onStart: this.onStart,
onFinish: this.onFinish,
onError: this.onError,
onMessage: this.onMessage,
onToken: this.onToken,
}
);
};
}