Build intelligent applications with Google's Gemini Pro models and function calling.
npm install @google/generative-ai cognitora
1import { GoogleGenerativeAI } from '@google/generative-ai';
2import { Cognitora } from 'cognitora';
3
4const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);
5const cognitora = new Cognitora({ apiKey: process.env.COGNITORA_API_KEY });
6
7async function runGeminiCodeInterpreter(userQuery: string) {
8 const session = await cognitora.sessions.create({
9 image: 'python:3.11-slim',
10 timeout: 300,
11 persistent: true
12 });
13
14 const model = genAI.getGenerativeModel({
15 model: "gemini-1.5-pro",
16 tools: [{
17 functionDeclarations: [{
18 name: "execute_python",
19 description: "Execute Python code in a secure sandbox",
20 parameters: {
21 type: "object",
22 properties: {
23 code: {
24 type: "string",
25 description: "Python code to execute"
26 }
27 },
28 required: ["code"]
29 }
30 }]
31 }]
32 });
33
34 const chat = model.startChat({
35 history: [{
36 role: "user",
37 parts: [{ text: "You are a Python expert. Use the execute_python function to run code." }]
38 }]
39 });
40
41 const result = await chat.sendMessage(userQuery);
42 const response = await result.response;
43
44 const functionCalls = response.functionCalls();
45 if (functionCalls && functionCalls.length > 0) {
46 const call = functionCalls[0];
47 if (call.name === "execute_python") {
48 const code = call.args.code as string;
49
50 const execution = await cognitora.compute.execute({
51 sessionId: session.id,
52 command: ["python", "-c", code]
53 });
54
55 return {
56 code,
57 result: execution.stdout,
58 error: execution.stderr,
59 exitCode: execution.exitCode
60 };
61 }
62 }
63}
Get started with Google Gemini Pro and Cognitora in minutes. Secure, scalable, and ready for anything.