Skip to content

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.9 KB
Raw Blame
/**
 * Google GenAI Example
 *
 * This example demonstrates how to use Composio SDK with Google's GenAI (Gemini).
 *
 * Prerequisites:
 * 1. Set up your COMPOSIO_API_KEY in the .env file
 * 2. Set up your GEMINI_API_KEY in the .env file
 * 3. Run the example: pnpm start
 */

import { Composio } from '@composio/core';
import { GoogleGenAI } from '@google/genai';
import { GoogleProvider } from '@composio/google';
import 'dotenv/config';

const ai = new GoogleGenAI({
  apiKey: process.env.GEMINI_API_KEY,
});

const composio = new Composio({
  apiKey: process.env.COMPOSIO_API_KEY,
  provider: new GoogleProvider(),
});

/**
 * Main function to run the example
 */
async function main() {
  try {
    console.log('πŸš€ Starting Google GenAI Example...');

    const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');

    console.log(`βœ… Found ${tools.length} tools`);

    const task = "Fetch the details of the user 'pg'";
    console.log(`πŸ“ Task: ${task}`);

    const response = await ai.models.generateContent({
      model: 'gemini-2.0-flash-001',
      contents: task,
      config: {
        tools: [{ functionDeclarations: tools }],
      },
    });

    console.log('βœ… Response received from Gemini');

    if (response.functionCalls) {
      console.log(`πŸ”„ Calling tool ${response.functionCalls[0].name}`);
      const functionCall = {
        name: response.functionCalls[0].name || '',
        args: response.functionCalls[0].args || {},
      };
      const result = await composio.provider.executeToolCall('default', functionCall);
      console.log('πŸ“Š Result:');
      console.log(JSON.parse(result).data);
    } else {
      console.log('πŸ“ No function calls in the response');
      console.log(response.text);
    }
  } catch (error) {
    console.error('❌ Error running example:', error);
    process.exitCode = 1;
  }
}

// Run the example
main().catch(error => {
  console.error(error);
  process.exitCode = 1;
});