Skip to main content

LlamaIndex Integration

Integrate InstaVM with LlamaIndex to create powerful AI agents that can execute Python code in secure cloud environments.

Installation

Install the required packages:

pip install instavm llama-index

Quick Start

Here's a complete example of integrating InstaVM with LlamaIndex:

import asyncio
import os
from instavm import InstaVM
from llama_index.core.tools import FunctionTool
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI

# Global VM instance - shared across all tool calls to preserve state
vm = None

def execute_python_code(code: str, timeout=300) -> str:
"""Execute Any Python code on InstaVM and return the result. Install python libraries via Code. Full VM at your disposal."""
global vm
try:
print(f"\n[Executing on InstaVM]\n{code}\n")
result = vm.execute(code, timeout=timeout)
return str(result)
except Exception as e:
return f"Error executing code: {str(e)}"

# Initialize global VM instance
vm = InstaVM("your-api-key-here")

# Create LlamaIndex tool
instavm_tool = FunctionTool.from_defaults(
name="execute_python_code",
description="Execute Python code on a secure cloud environment with InstaVM. Install libraries if needed with !pip",
fn=execute_python_code
)

# Initialize LLM and create agent
# Make sure to set OPENAI_API_KEY environment variable
# e.g., export OPENAI_API_KEY='your-key-here'
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
agent = ReActAgent(
tools=[instavm_tool],
llm=llm,
verbose=True
)

async def main():
# Use the agent to execute code through InstaVM
handler = agent.run("Calculate the factorial of 10 using Python and show the steps")
response = await handler
print("\n=== First Query Response ===")
print(response)

# Example: Data analysis with pandas
# This will run in the SAME VM session, so any variables/packages from
# the first query will still be available
handler2 = agent.run("""
Create a simple pandas DataFrame with sample sales data and calculate:
1. Total sales
2. Average sales per month
3. Create a simple visualization
""")
analysis_response = await handler2
print("\n=== Second Query Response ===")
print(analysis_response)

if __name__ == "__main__":
asyncio.run(main())

Key Features

  • Secure Code Execution: Run Python code safely in isolated cloud environments
  • ReActAgent Integration: Use LlamaIndex's ReActAgent for complex reasoning tasks
  • Tool Integration: Seamlessly integrate InstaVM as a tool in your LlamaIndex workflows
  • Error Handling: Built-in error handling for robust applications

Use Cases

  • Data Analysis: Have AI agents perform complex data analysis tasks
  • Code Generation: Generate and execute code dynamically based on user requests
  • Educational Tools: Create interactive coding assistants
  • Automated Reporting: Generate reports with data processing and visualization

Authentication

Remember to set your InstaVM API key. You can obtain one from the InstaVM Dashboard.

# Option 1: Pass directly
with InstaVM("your-api-key-here") as vm:
# Your code here

# Option 2: Use environment variable
import os
with InstaVM(os.getenv("INSTAVM_API_KEY")) as vm:
# Your code here