-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemp2.js
More file actions
167 lines (139 loc) · 4.78 KB
/
temp2.js
File metadata and controls
167 lines (139 loc) · 4.78 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { GoogleGenAI } from "@google/genai";
import { createClient } from "redis";
import dotenv from "dotenv";
dotenv.config();
export function generateRandomString(length) {
console.log("function called");
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export const generateRandomStringDeclaration = {
"name": "generateRandomString",
"description": "Generates a random string of a specified length, composed of alphanumeric characters (a-z, A-Z, 0-9).",
"parameters": {
"type": "OBJECT",
"properties": {
"length": {
"type": "number",
"description": "The desired length of the random string."
}
},
"required": ["length"]
}
}
const availableTools = {
generateRandomString: generateRandomString,
};
let redisClient;
function getRedis() {
if (!redisClient) {
redisClient = createClient({
url: process.env.REDIS_URL || 'redis://localhost:6379',
});
redisClient.connect().catch((err) => {
console.error("Redis connection error:", err);
});
}
return redisClient;
}
async function handleFunctionCalls(ai, modelName, input, calls) {
if (!calls || calls.length === 0) {
return "Error: No function calls provided to handler.";
}
const call = calls[0];
const funcName = call.name;
const funcArgs = call.args;
if (!availableTools[funcName]) {
console.error(`Unknown tool requested: ${funcName}`);
return `Error: Unknown tool requested by model: ${funcName}`;
}
console.log(`\n🤖 Tool Call Detected: Executing ${funcName}(${JSON.stringify(funcArgs)})`);
const toolResult = availableTools[funcName](...Object.values(funcArgs));
const followUpResult = await ai.models.generateContent({
model: modelName,
contents: [{
role: "user",
parts: [{ text: input }]
}, {
role: "model",
parts: [{ functionCall: call }]
}, {
role: "function",
parts: [{
functionResponse: {
name: funcName,
response: { result: toolResult }
}
}]
}],
});
return followUpResult.text;
}
export async function geminiWithTools(apiKey, modelName, input, tools = [generateRandomStringDeclaration]) {
const cacheKey = JSON.stringify({ modelName, input, toolsName: tools.map(t => t.name) });
const redisClient = getRedis();
if (redisClient.status === 'ready') {
try {
const cached = await redisClient.get(cacheKey);
if (cached) return JSON.parse(cached);
} catch (err) {
console.error("Redis GET error:", err);
}
}
let output;
try {
const ai = new GoogleGenAI({ apiKey });
const result = await ai.models.generateContent({
model: modelName,
contents: input,
config: {
tools: tools.length > 0 ? [{ functionDeclarations: tools }] : undefined,
}
});
const response = result;
const calls = response.functionCalls || [];
if (calls.length > 0) {
output = await handleFunctionCalls(ai, modelName, input, calls);
} else {
output = response.text;
}
} catch (error) {
console.error("Error in geminiWithTools:", error);
output = "An internal error occurred while processing the request.";
}
if (redisClient.status === 'ready' && output) {
try {
await redisClient.set(cacheKey, JSON.stringify(output), {
EX: 60 * 60 * 24,
});
} catch (err) {
console.error("Redis SET error:", err);
}
}
return output;
}
async function main() {
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
const MODEL_NAME = "gemini-2.5-flash";
if (!GEMINI_API_KEY) {
console.error("Please set the GEMINI_API_KEY environment variable.");
return;
}
const prompt1 = "I need a random alphanumeric string that is exactly 5 characters long. Generate it for me.";
console.log(`\n--- Running Prompt 1 (Tool Test) ---`);
console.log(`Prompt: ${prompt1}`);
let result1 = await geminiWithTools(GEMINI_API_KEY, MODEL_NAME, prompt1);
console.log(`\nFinal Response:\n${result1}`);
if (redisClient) {
await redisClient.quit();
}
}
main().catch(error => {
console.error("Main execution failed:", error);
if (redisClient) redisClient.quit();
});