-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
112 lines (99 loc) · 3.12 KB
/
agent.js
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
const { MultiPromptChain, LLMChain } = require("langchain/chains");
const { OpenAI } = require("langchain/llms/openai");
const { personas } = require("./prompts");
const { PromptTemplate } = require("langchain/prompts");
const { ConversationSummaryBufferMemory } = require("langchain/memory");
require("dotenv").config();
class Agent {
constructor() {
this.model = new OpenAI({
temperature: 0.9,
modelName: "gpt-4-1106-preview",
});
this.chatPromptMemory = new ConversationSummaryBufferMemory({
memoryKey: "chat_history",
llm: this.model,
maxTokenLimit: 400,
});
const default_prompt = PromptTemplate.fromTemplate(personas[1]["promptTemplate"]);
const default_chain = new LLMChain({
llm: this.model,
prompt: default_prompt,
memory: this.chatPromptMemory,
verbose: true,
});
this.multiPromptChain = MultiPromptChain.fromLLMAndPrompts(this.model, {
promptNames: personas.map((p) => p.name),
promptDescriptions: personas.map((p) => p.description),
promptTemplates: personas.map((p) => p.promptTemplate),
defaultChain: default_chain,
llmChainOpts: {
memory: this.chatPromptMemory,
verbose: true,
},
});
const developPhasePrompt = PromptTemplate.fromTemplate(personas[2].developPhasePrompt);
this.developPhaseChain = new LLMChain({
llm: this.model,
prompt: developPhasePrompt,
memory: this.chatPromptMemory,
verbose: true,
});
}
async chatWithUserInDesignPhase(message) {
try {
const response = await this.multiPromptChain.call({
input: message,
});
return response.text;
} catch (e) {
console.log(e);
return JSON.stringify({
name: personas[1]["name"],
role: personas[1]["role"],
text: "Sorry, I didn't understand that. Can you rephrase your message?",
});
}
}
async getUserStory() {
try {
const response = await this.model.predict(personas[2].developPhaseInitialPrompt);
await this.chatPromptMemory.saveContext(
{ input: "What's the user story?" },
{ output: response }
);
return JSON.stringify({
name: personas[2]["name"],
role: personas[2]["role"],
text: response,
});
} catch (e) {
console.log(e);
return JSON.stringify({
name: personas[2]["name"],
role: personas[2]["role"],
text: "Sorry, I didn't understand that. Can you rephrase your message?",
});
}
}
async chatWithUserInDevelopPhase(message, code) {
try {
const response = await this.developPhaseChain.call({
input: `${message}\n\nHere's the code written by the novice developer:\n${code}`,
});
return JSON.stringify({
name: personas[2]["name"],
role: personas[2]["role"],
text: response.text,
});
} catch (e) {
console.log(e);
return JSON.stringify({
name: personas[2]["name"],
role: personas[2]["role"],
text: "Sorry, I didn't understand that. Can you rephrase your message?",
});
}
}
}
module.exports = Agent;