-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertexai.ts
377 lines (364 loc) · 14.9 KB
/
vertexai.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import {CalculateChatData} from "../data/CalculateChatData";
import {calculateDispatcher, divideDispatcher} from "../common/calculator";
import {FunctionDeclarationSchemaType, VertexAI} from "@google-cloud/vertexai";
import {firestore} from "firebase-admin";
import {CHATS, VERTEXAI_THREADS} from "../data/Collections";
import {
factory,
VertexAiAssistantConfig,
VertexAiChatState,
VertexAiSystemInstructions
} from "@motorro/firebase-ai-chat-vertexai";
import {getFunctions} from "firebase-admin/functions";
import {DIVIDER_NAME, NAME, region} from "../env";
import {CalculateChatRequest} from "../data/CalculateChatRequest";
import {CalculateChatResponse} from "../data/CalculateChatResponse";
import {PostCalculateRequest} from "../data/PostCalculateRequest";
import {CloseCalculateRequest} from "../data/CloseCalculateRequest";
import {ChatError, ChatWorker, Continuation, ContinuationCommand, logger} from "@motorro/firebase-ai-chat-core";
import {projectID} from "firebase-functions/params";
import {ChatDispatchData} from "@motorro/firebase-ai-chat-openai";
import {CalculatorMeta} from "../data/MessageMeta";
import CollectionReference = firestore.CollectionReference;
import DocumentReference = firestore.DocumentReference;
import {ToolDispatcherReturnValue} from "@motorro/firebase-ai-chat-core/lib/aichat/ToolsDispatcher";
const db = firestore();
const chats = db.collection(CHATS) as CollectionReference<VertexAiChatState<CalculateChatData>>;
const chatFactory = factory(db, getFunctions(), region);
const assistantChat = chatFactory.chat<CalculateChatData>("calculator");
const continuationScheduler = chatFactory.continuationScheduler(NAME);
interface HandOverMeta extends CalculatorMeta {
continuation: ContinuationCommand<unknown>
}
export const switchToDivider = async (
chatData: ChatDispatchData,
args: Record<string, unknown>,
continuation: ContinuationCommand<unknown>
): Promise<ToolDispatcherReturnValue<CalculateChatData>> => {
const config: VertexAiAssistantConfig = {
engine: "vertexai",
instructionsId: DIVIDER_NAME
};
const meta: HandOverMeta = {
aiMessageMeta: {
name: DIVIDER_NAME,
engine: "VertexAi"
},
continuation: continuation
};
logger.d("Switching to divider:", chatData, args);
const summary = args.summary as string;
if (undefined === summary || 0 === summary.length) {
return {
error: "You should provide some summary about what user wants"
};
}
await assistantChat.handOver(
db.doc(chatData.chatDocumentPath) as DocumentReference<VertexAiChatState<CalculateChatData>>,
chatData.ownerId,
config,
[(args.summary as string)],
undefined,
meta
);
return Continuation.suspend();
};
const switchToCalculator = async (data: CalculateChatData, chatData: ChatDispatchData, args: Record<string, unknown>): Promise<void> => {
logger.d("Switching to calculator", chatData, args);
const meta: HandOverMeta | null = <HandOverMeta>chatData.meta;
if (null === meta) {
return Promise.reject(new ChatError("invalid-argument", true, "No continuation in chat meta"));
}
await assistantChat.handBack(
db.doc(chatData.chatDocumentPath) as DocumentReference<VertexAiChatState<CalculateChatData>>,
chatData.ownerId
);
await continuationScheduler.continue(meta.continuation, {data: data, comment: (args.summary as string)});
};
const vertexMainInstructions: VertexAiSystemInstructions<CalculateChatData> = {
instructions: `
You are a calculator which can add and subtract integers to an accumulated value
- The current accumulated value is stored in application state.
- Call 'getSum' function to get current value
- If user asks you to add some value, call 'add' function and supply the argument provided by user.
- If user asks you to subtract some value, call 'subtract' function and supply the argument provided by user.
- If user asks you to multiply by some value, call 'multiply' function and supply the argument provided by user.
- If user asks you to divide by some value, call 'divide' function and provide a short summary of what should be done.
- Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.
`,
examples: [
{
type: "functionCall",
input: "What is the current sum?",
name: "getSum",
arguments: {}
},
{
type: "functionCall",
input: "Add 25",
name: "add",
arguments: {value: 25}
},
{
type: "functionCall",
input: "Subtract 25",
name: "subtract",
arguments: {value: 25}
},
{
type: "functionCall",
input: "Multiply by 25",
name: "multiply",
arguments: {value: 25}
},
{
type: "functionCall",
input: "I want to divide the value by some number",
name: "divide",
arguments: {summary: "User wants to divide current value by some number"}
}
],
tools: {
dispatcher: calculateDispatcher(switchToDivider),
definition: [
{
functionDeclarations: [
{
name: "getSum",
description: "Returns current accumulated value or error description if there is an error"
},
{
name: "add",
description: "Adds supplied argument to the accumulated value and returns new value or error if there is an error",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
value: {
type: FunctionDeclarationSchemaType.NUMBER,
description: "Value to add to accumulated number"
}
},
required: ["value"]
}
},
{
name: "subtract",
description: "Subtracts supplied argument from the accumulated value and returns new value or error if there is an error",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
value: {
type: FunctionDeclarationSchemaType.NUMBER,
description: "Value to subtract from accumulated number"
}
},
required: ["value"]
}
},
{
name: "multiply",
description: "Multiplies current accumulated value by supplied argument and returns new value or error if there is an error",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
value: {
type: FunctionDeclarationSchemaType.NUMBER,
description: "Value to multiply accumulated number by"
}
},
required: ["value"]
}
},
{
name: "divide",
description: `
Use to call a special division assistant to help divide current accumulated number.
The function will return the updated accumulated state and optionally a comment what has been
done or what user wants to do next.
`,
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
summary: {
type: FunctionDeclarationSchemaType.STRING,
description: "A short summary about the division task"
}
},
required: ["summary"]
}
}
]
}
]
}
};
const vertexDividerInstructions: VertexAiSystemInstructions<CalculateChatData> = {
instructions: `
You are a calculator assistant who divides passed accumulated value by some numbers. You have a boss - main
calculator who will call you from time to time to help with division.
- The current accumulated value is stored in application state.
- Call 'getSum' function to get current value
- If user asks you to divide by some value, call 'divide' function and provide a divider value.
- If user asks you for some other operation you need to return the request to your boss. Ask user
for a confirmation to switch back and call 'returnToBoss' providing a short summary of what user wants to be done.
- User may ask you to return to your boss. Call 'done' providing a short summary of what you have done so far.
- Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.
`,
examples: [
{
type: "functionCall",
input: "What is the current sum?",
name: "getSum",
arguments: {}
},
{
type: "functionCall",
input: "Divide by 25",
name: "divide",
arguments: {value: 25}
},
{
type: "functionCall",
input: "Subtract 25",
name: "returnToBoss",
arguments: {comment: "Subtract 25"}
},
{
type: "functionCall",
input: "Done",
name: "returnToBoss",
arguments: {comment: "Work done"}
}
],
tools: {
dispatcher: divideDispatcher(switchToCalculator),
definition: [
{
functionDeclarations: [
{
name: "getSum",
description: "Returns current accumulated value or error description if there is an error"
},
{
name: "divide",
description: "Divides accumulated value by supplied argument and returns new value or error if there is an error",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
value: {
type: FunctionDeclarationSchemaType.NUMBER,
description: "Value to divide accumulated number by"
}
},
required: ["value"]
}
},
{
name: "returnToBoss",
description: "Hands control back to your boss when user needs anything else but to divide",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
summary: {
type: FunctionDeclarationSchemaType.STRING,
description: "Summary of what user wants to be done"
}
},
required: ["summary"]
}
},
{
name: "done",
description: "Hands control back to your boss when user is done with you",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
summary: {
type: FunctionDeclarationSchemaType.NUMBER,
description: "Short description of what have been done so far"
}
},
required: ["summary"]
}
}
]
}
]
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const instructions: Record<string, VertexAiSystemInstructions<any>> = {
[NAME]: vertexMainInstructions,
[DIVIDER_NAME]: vertexDividerInstructions
};
export const calculate = async (uid: string, data: CalculateChatRequest): Promise<CalculateChatResponse> => {
const chat = chats.doc();
const config: VertexAiAssistantConfig = {
engine: "vertexai",
instructionsId: NAME
};
const result = await assistantChat.create(
chat,
uid,
{sum: 0},
config,
[data.message],
undefined,
<CalculatorMeta>{
aiMessageMeta: {
name: NAME,
engine: "VertexAi"
}
}
);
return {
chatDocument: chat.path,
status: result.status,
data: result.data
};
};
export const postToCalculate = async (uid: string, data: PostCalculateRequest): Promise<CalculateChatResponse> => {
const result = await assistantChat.postMessage(
db.doc(data.chatDocument) as DocumentReference<VertexAiChatState<CalculateChatData>>,
uid,
[data.message]
);
return {
chatDocument: data.chatDocument,
status: result.status,
data: result.data
};
};
export const closeCalculate = async (uid: string, data: CloseCalculateRequest): Promise<CalculateChatResponse> => {
const result = await assistantChat.closeChat(
db.doc(data.chatDocument) as DocumentReference<VertexAiChatState<CalculateChatData>>,
uid,
);
return {
chatDocument: data.chatDocument,
status: result.status,
data: result.data
};
};
export const getWorker = (): ChatWorker => {
const vertexAi = new VertexAI({
project: projectID.value(),
location: region
});
const model = vertexAi.getGenerativeModel(
{
model: "gemini-1.0-pro",
generationConfig: {
candidateCount: 1
}
},
{
timeout: 30 * 1000
}
);
return chatFactory.worker(
model,
VERTEXAI_THREADS,
instructions
);
};