Back to Integrations
Integration/LlamaIndex

LlamaIndex Integration

Build RAG applications with LlamaIndex and code execution capabilities.

Category
AI Frameworks
Difficulty
Intermediate
Tags
3 features
Setup Time
5-15 min
RAG
Data Indexing
LLM Apps

Quick Start Guide

Installation

Terminal
npm install llamaindex cognitora

Implementation

TypeScript
typescript
1import { FunctionTool, OpenAI, ReActAgent } from 'llamaindex';
2import { Cognitora } from 'cognitora';
3
4const cognitora = new Cognitora({ apiKey: process.env.COGNITORA_API_KEY });
5
6async function createLlamaIndexCodeInterpreter() {
7  const session = await cognitora.sessions.create({
8    image: 'python:3.11-slim',
9    timeout: 300,
10    persistent: true
11  });
12
13  const codeExecutionTool = FunctionTool.from(
14    async (code: string) => {
15      const execution = await cognitora.compute.execute({
16        sessionId: session.id,
17        command: ["python", "-c", code]
18      });
19      
20      if (execution.exitCode === 0) {
21        return `Execution successful:\n${execution.stdout}`;
22      } else {
23        return `Execution failed:\n${execution.stderr}`;
24      }
25    },
26    {
27      name: "execute_python",
28      description: "Execute Python code in a secure sandbox environment",
29      parameters: {
30        type: "object",
31        properties: {
32          code: {
33            type: "string",
34            description: "Python code to execute"
35          }
36        },
37        required: ["code"]
38      }
39    }
40  );
41
42  return { session, codeExecutionTool };
43}
44
45async function runLlamaIndexAgent(userQuery: string) {
46  const { session, codeExecutionTool } = await createLlamaIndexCodeInterpreter();
47  
48  const llm = new OpenAI({ model: "gpt-4", temperature: 0 });
49
50  const agent = new ReActAgent({
51    tools: [codeExecutionTool],
52    llm,
53    verbose: true,
54  });
55
56  const response = await agent.chat({
57    message: userQuery,
58  });
59
60  return {
61    response: response.response,
62    sessionId: session.id
63  };
64}

Ready to integrate LlamaIndex?

Get started with LlamaIndex and Cognitora in minutes. Secure, scalable, and ready for anything.