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:
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
def execute_python_code(code: str) -> str:
"""Execute Python code on InstaVM and return the result."""
try:
with InstaVM("your-api-key-here") as vm:
result = vm.execute(code)
return str(result)
except Exception as e:
return f"Error executing code: {str(e)}"
# Create LlamaIndex tool
instavm_tool = FunctionTool.from_defaults(
name="execute_python_code",
description="Execute Python code on a secure cloud environment with InstaVM",
fn=execute_python_code
)
# Initialize LLM and create agent
llm = OpenAI(model="gpt-3.5-turbo")
agent = ReActAgent.from_tools([instavm_tool], llm=llm, verbose=True)
# Use the agent to execute code through InstaVM
response = agent.chat("Calculate the factorial of 10 using Python and show the steps")
print(response)
# Example: Data analysis with pandas
analysis_response = agent.chat("""
Create a simple pandas DataFrame with sample sales data and calculate:
1. Total sales
2. Average sales per month
3. Create a simple visualization
""")
print(analysis_response)
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