-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgemini.js
55 lines (47 loc) · 1.34 KB
/
gemini.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
export function buildRequestGemini(request_params, apiKey, model) {
const prompt = request_params.prompt;
const contents = {
role: "user",
parts: {
text: prompt
}
};
const tools = [
{
"function_declarations": request_params.functions
}
];
let allowed_function_names = [];
for (const func of request_params.functions) {
allowed_function_names.push(func.name);
}
const tool_config = {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": allowed_function_names
}
};
const request_body = JSON.stringify({
contents: contents,
tools: tools,
tool_config: tool_config
});
const request = {
endpoint: `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`,
options: {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: request_body
}
};
return request;
}
export function parseResponseGemini(data) {
const parsedResponse = {
function_used: data.candidates[0].content.parts[0].functionCall.name,
event: data.candidates[0].content.parts[0].functionCall.args
}
return parsedResponse;
}