-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgemini.js
More file actions
86 lines (86 loc) · 2.64 KB
/
gemini.js
File metadata and controls
86 lines (86 loc) · 2.64 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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GEMINI_MODELS = void 0;
exports.callGemini = callGemini;
const generative_ai_1 = require("@google/generative-ai");
exports.GEMINI_MODELS = {
// TODO: more pre-config models
gemini20flashExp: 'gemini-2.0-flash-exp'
};
const genAI = new generative_ai_1.GoogleGenerativeAI(process.env.GOOGLE_GEMINI_API_KEY || '');
const actionSchema = {
type: generative_ai_1.SchemaType.OBJECT,
properties: {
tokens: {
type: generative_ai_1.SchemaType.STRING,
description: 'Comma-separated list of symbols of the involved tokens, for example: USDC, ETH',
nullable: false
},
description: {
type: generative_ai_1.SchemaType.STRING,
description: 'Free text describing the action',
nullable: false
}
},
required: ['tokens', 'description']
};
const strategySchema = {
type: generative_ai_1.SchemaType.OBJECT,
properties: {
name: {
type: generative_ai_1.SchemaType.STRING,
description: 'Name of the strategy',
nullable: false
},
risk: {
type: generative_ai_1.SchemaType.STRING,
description: 'Risk level of the strategy',
nullable: false,
enum: ['low', 'medium', 'high']
},
actions: {
description: 'List of actions for the strategy',
type: generative_ai_1.SchemaType.ARRAY,
items: actionSchema
}
},
required: ['name', 'risk', 'actions']
};
const schema = {
description: 'List of strategies',
type: generative_ai_1.SchemaType.ARRAY,
items: strategySchema
};
async function callGemini(llmInput) {
let output = null;
const model = llmInput.model || exports.GEMINI_MODELS.gemini20flashExp;
try {
const aiModel = genAI.getGenerativeModel({
model,
generationConfig: {
responseMimeType: 'application/json',
responseSchema: schema
}
});
const result = await aiModel.generateContent(llmInput.prompt);
// console.log(JSON.stringify(result))
const content = result.response.text();
try {
output = JSON.parse(content || '[]');
}
catch (error) {
console.error('Invalid JSON in Gemini AI output: ', error);
}
}
catch (error) {
console.error(`Error querying Gemini AI: ${error}`);
}
return {
llm: {
provider: 'Google',
model
},
response: output
};
}
//# sourceMappingURL=gemini.js.map