-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgemini-api.js
More file actions
49 lines (46 loc) · 1.49 KB
/
gemini-api.js
File metadata and controls
49 lines (46 loc) · 1.49 KB
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
class GoogleGenerativeAI {
constructor(apiKey) {
this.apiKey = apiKey;
}
getGenerativeModel({ model, systemInstruction }) {
return {
startChat: ({ generationConfig, history }) => {
return {
sendMessage: async (message) => {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1/models/${model}:generateContent?key=${this.apiKey}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [{
parts: [{
text: systemInstruction + "\n\nAnalyze this code:\n" + message
}]
}],
generationConfig: {
temperature: generationConfig.temperature,
topP: generationConfig.topP,
topK: generationConfig.topK,
maxOutputTokens: generationConfig.maxOutputTokens,
}
})
}
);
if (!response.ok) {
throw new Error(`API request failed: ${response.status} ${response.statusText}`);
}
const result = await response.json();
return {
response: {
text: () => result.candidates[0].content.parts[0].text
}
};
}
};
}
};
}
}