Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Google GenAI Example |
| 3 | * |
| 4 | * This example demonstrates how to use Composio SDK with Google's GenAI (Gemini). |
| 5 | * |
| 6 | * Prerequisites: |
| 7 | * 1. Set up your COMPOSIO_API_KEY in the .env file |
| 8 | * 2. Set up your GEMINI_API_KEY in the .env file |
| 9 | * 3. Run the example: pnpm start |
| 10 | */ |
| 11 | |
| 12 | import { Composio } from '@composio/core'; |
| 13 | import { GoogleGenAI } from '@google/genai'; |
| 14 | import { GoogleProvider } from '@composio/google'; |
| 15 | import 'dotenv/config'; |
| 16 | |
| 17 | const ai = new GoogleGenAI({ |
| 18 | apiKey: process.env.GEMINI_API_KEY, |
| 19 | }); |
| 20 | |
| 21 | const composio = new Composio({ |
| 22 | apiKey: process.env.COMPOSIO_API_KEY, |
| 23 | provider: new GoogleProvider(), |
| 24 | }); |
| 25 | |
| 26 | /** |
| 27 | * Main function to run the example |
| 28 | */ |
| 29 | async function main() { |
| 30 | try { |
| 31 | console.log('🚀 Starting Google GenAI Example...'); |
| 32 | |
| 33 | const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER'); |
| 34 | |
| 35 | console.log(`✅ Found ${tools.length} tools`); |
| 36 | |
| 37 | const task = "Fetch the details of the user 'pg'"; |
| 38 | console.log(`📝 Task: ${task}`); |
| 39 | |
| 40 | const response = await ai.models.generateContent({ |
| 41 | model: 'gemini-2.0-flash-001', |
| 42 | contents: task, |
| 43 | config: { |
| 44 | tools: [{ functionDeclarations: tools }], |
| 45 | }, |
| 46 | }); |
| 47 | |
| 48 | console.log('✅ Response received from Gemini'); |
| 49 | |
| 50 | if (response.functionCalls) { |
| 51 | console.log(`🔄 Calling tool ${response.functionCalls[0].name}`); |
| 52 | const functionCall = { |
| 53 | name: response.functionCalls[0].name || '', |
| 54 | args: response.functionCalls[0].args || {}, |
| 55 | }; |
| 56 | const result = await composio.provider.executeToolCall('default', functionCall); |
| 57 | console.log('📊 Result:'); |
| 58 | console.log(JSON.parse(result).data); |
| 59 | } else { |
| 60 | console.log('📝 No function calls in the response'); |
| 61 | console.log(response.text); |
| 62 | } |
| 63 | } catch (error) { |
| 64 | console.error('❌ Error running example:', error); |
| 65 | process.exitCode = 1; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Run the example |
| 70 | main().catch(error => { |
| 71 | console.error(error); |
| 72 | process.exitCode = 1; |
| 73 | }); |