-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAiApi.js
76 lines (66 loc) · 2.45 KB
/
AiApi.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
const { GoogleGenerativeAI } = require("@google/generative-ai");
import Apikey from './Apikey';
const genAI = new GoogleGenerativeAI(Apikey);
async function GetGeminiProResponse(historyData, message) {
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const chat = model.startChat(
{
history: [
{
role: "user",
parts: "Hello, remember that you are a chat bot developed by Ankit Kumar Shah."
},
{
role: "model",
parts: "Ok I will remember that I am a chat bot developed by Ankit Kumar Shah.",
},
{
role: "user",
parts: "Your name is O2 AI."
},
{
role: "model",
parts: "Ok I will remember that my name is O2 AI.",
},
{
role: "user",
parts: "Your are trained by Ankit Kumar Shah."
},
{
role: "model",
parts: "Ok I will remember that I am trained by Ankit Kumar Shah.",
},
{
role: "user",
parts: "Try not to revel that you are a product of Google."
},
{
role: "model",
parts: "Ok I will try my best not to revel that I am a product of Google.",
},
...historyData,
],
}
)
const msg = message;
const result = await chat.sendMessage(msg);
const response = await result.response;
return response.text().length>0 ? response.text(): "Hmm.. I didn't get that.";
}
async function GetImageResponse(image,message) {
function fileToGenerativePart(img, mimeType) {
return {
inlineData: {
data: img,
mimeType
},
};
}
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const prompt = message;
const imageParts = image.map((img) => fileToGenerativePart(img.base64, img.type));
const result = await model.generateContent([prompt, ...imageParts]);
const response = await result.response;
return response.text().length>0 ? response.text(): "Hmm.. I didn't get that.";
}
export {GetGeminiProResponse, GetImageResponse}