-
Notifications
You must be signed in to change notification settings - Fork 1
/
aiBot.js
127 lines (113 loc) · 4.54 KB
/
aiBot.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class GenAIBot {
constructor(AIModel, client, template, clientSessionFlag) {
this.AIModel = AIModel
this.client = client
this.template = template
this.fullReply = []
//should bot be responsible for keeping client session history or not
this.clientSessionFlag = clientSessionFlag
this.lastReplyMessage = "NA"
this.lastQuestion = "NA"
this.sessionId = template + this.generateUUID()
}
generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
reset() {
this.fullReply = []
this.sessionId = this.template + this.generateUUID()
}
getSessionId() {
return this.sessionId
}
getLastQuestion() {
return this.lastQuestion;
}
getTemplate() {
return this.template;
}
getLastReplyMessage() {
return this.lastReplyMessage;
}
getFullReply() {
return this.getFullReply;
}
async getSystemMessage() {
const template = await client.templates.get(this.template);
var aINode = template.nodes.find(node=> node.properties.sensor && node.properties.sensor.requiredProperties.find(a=>a["system"] !== undefined))
return aINode?.properties?.sensor?.requiredProperties?.find(a=>a["system"])?.system
}
async updateSystemMessage(text) {
const template = await client.templates.get(this.template);
var aINode = template.nodes.find(node=> node.properties.sensor && node.properties.sensor.requiredProperties.find(a=>a["system"] !== undefined))
if(aINode){
var properties = aINode.properties.sensor.requiredProperties.filter(v => !v.system)
properties.push({system: text})
aINode.properties.sensor.requiredProperties = properties
}
template.nodes = template.nodes.filter(n=> n.name != aINode.name)
template.nodes.push(aINode)
return await client.templates.update(template.name, template)
}
async getAgents() {
const template = await client.templates.get(this.template);
const agents = template.nodes.filter(a => a.properties.sensor?.requiredProperties?.find(b=>b.system) === undefined) //filter LLM node
return agents.map(n=> n.name).filter(a=> (a.indexOf("AND") === -1 && a.indexOf("TaskOut") === -1 && a.indexOf("AIContext") === -1 ) ) || []
}
_extractMessageText = (message) => {
if (message.content) {
if (typeof message.content === 'string') {
// openai style
return message.content
}
else if (Array.isArray(message.content) && message.content.length > 0) {
// AWS bedrock style
return message.content[message.content.length-1].text
}
}
return "no answer, please try another question"
}
async runBot(question) {
console.log('runBot', question);
this.lastQuestion = question;
let ret, variables
try{
if(this.clientSessionFlag) {
variables = { question, messages: this.fullReply, model: this.AIModel}
} else {
variables = { question, sessionId: this.sessionId, model: this.AIModel}
}
ret = await client.templates.run(this.template, { variables })
} catch(error){
throw new Error("error running the bot, template failed to run", error);
}
if(ret === undefined) {
throw new Error("error running the bot, template failed to respond", error);
} else if(ret.taskOutput === undefined) {
this.reset()
const errorMessage = ret?.log.filter(a=> a.level === "WARN").length > 0? ret.log.filter(a=> a.level === "WARN")[0].message : "error running the bot, I will reset the cache, please try again"
throw new Error(errorMessage, error);
}
const response = ret.taskOutput;
const messages = response.messages || response.context?.messages
const signleResponse = response.response
if(messages?.length > 1) {
this.fullReply = messages;
this.lastReplyMessage = this._extractMessageText(messages[messages.length - 1])
} else if(signleResponse) {
this.lastReplyMessage = signleResponse.content
this.fullReply.push(signleResponse)
} else {
throw new Error("error running the bot, there are no messages in the response", error);
}
return {
fullReply: this.fullReply,
lastReplyMessage: this.lastReplyMessage,
question
}
}
}