Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 82 lines 2.21 KB
Raw
1 /**
2 * Llamaindex Example
3 *
4 * This example demonstrates how to use Composio SDK for llamaindex.
5 *
6 * Prerequisites:
7 * 1. Set up your COMPOSIO_API_KEY in the .env file
8 * 3. Run the example: pnpm start
9 */
10
11 import { Composio } from '@composio/core';
12 import { LlamaindexProvider } from '@composio/llamaindex';
13 import { openai } from '@llamaindex/openai';
14 import { agent, agentStreamEvent } from '@llamaindex/workflow';
15 import 'dotenv/config';
16
17 /**
18 * Initialize Composio
19 */
20 const composio = new Composio({
21 apiKey: process.env.COMPOSIO_API_KEY,
22 provider: new LlamaindexProvider(),
23 });
24
25 /**
26 * Main function to run the example
27 */
28 async function main() {
29 try {
30 console.log('🚀 Starting Llamaindex Example...');
31
32 // Get available tools
33 const tools = await composio.tools.get(
34 'default',
35 {
36 // Specify the apps you want to use
37 toolkits: ['hackernews'],
38 limit: 10,
39 },
40 {
41 beforeExecute: ({ toolSlug, toolkitSlug, params }) => {
42 console.log(`🔄 Executing tool ${toolSlug}/${toolkitSlug} with params:`, { params });
43 return params;
44 },
45 afterExecute: ({ toolSlug, toolkitSlug, result }) => {
46 console.log(`✅ Executed tool ${toolSlug}/${toolkitSlug} with result:`, { result });
47 return result;
48 },
49 }
50 );
51
52 console.log(`✅ Found ${tools.length} tools`);
53
54 // TODO: Add your example implementation here
55 console.log('📝 Fetching posts from hackernews!');
56 const hackernewsAgent = agent({
57 name: 'Hackernews Agent',
58 description: 'A helpful hackernews assistant',
59 llm: openai({ model: 'gpt-4o-mini' }),
60 systemPrompt:
61 'You are a helpful hackernews assistant that helps users with their queries related to hackernews',
62 tools,
63 });
64
65 const stream = await hackernewsAgent.runStream('Summarize the front page of hackernews');
66
67 for await (const event of stream) {
68 if (agentStreamEvent.include(event)) {
69 process.stdout.write(event.data.delta);
70 }
71 }
72 } catch (error) {
73 console.error('❌ Error running example:', error);
74 process.exitCode = 1;
75 }
76 }
77
78 // Run the example
79 main().catch(error => {
80 console.error(error);
81 process.exitCode = 1;
82 });