-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathvertexaiFunctions.ts
62 lines (56 loc) · 2.01 KB
/
vertexaiFunctions.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
import * as functions from 'firebase-functions/v2';
const poem = [
'The wind whispers secrets through the trees,',
'Rustling leaves in a gentle breeze.',
'Sunlight dances on the grass,',
'A fleeting moment, sure to pass.',
'Birdsong fills the air so bright,',
'A symphony of pure delight.',
'Time stands still, a peaceful pause,',
"In nature's beauty, no flaws.",
];
const response = {
candidates: [
{
content: {
parts: [
{
text: 'Google\'s mission is to "organize the world\'s information and make it universally accessible and useful."',
},
],
role: 'model',
},
finishReason: 'STOP',
index: 0,
safetyRatings: [
{ category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
],
},
],
promptFeedback: {
safetyRatings: [
{ category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
{ category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
],
},
};
export const testFetchStream = functions.https.onRequest(async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
const updatedResponse = { ...response };
for (let i = 0; i < poem.length; i++) {
updatedResponse.candidates[0].content.parts[0].text = poem[i];
res.write(`data: ${JSON.stringify(updatedResponse)}\n\n`);
await new Promise(resolve => setTimeout(resolve, 100));
}
res.end();
});
export const testFetch = functions.https.onRequest(async (req, res) => {
res.json(response);
});