An AutoGen extension that allows code execution using YepCode's secure serverless runtime
Project description
AutoGen Extension for YepCode
An AutoGen extension that enables secure code execution using YepCode's serverless runtime environment. Execute Python and JavaScript code in production-grade, isolated sandboxes with built-in security and scalability.
Features
- Secure Execution: Code runs in isolated, production-grade sandboxes
- Multi-language Support: Python and JavaScript execution
- Automatic Package Installation: YepCode automatically detects and installs dependencies in the sandbox
- Logging and Monitoring: Access to YepCode's execution logs, results and errors
- AutoGen Integration: Seamless integration with AutoGen agents and tools
Installation
Install the package using pip:
pip install autogen_ext_yepcode
Setup
- Create a YepCode Account: Sign up at yepcode.io
- Get Your API Token: Navigate to
Settings>API credentialsin your YepCode workspace - Set Environment Variable:
export YEPCODE_API_TOKEN="your-api-token-here"
Alternatively, you can pass the API token directly to the executor constructor.
Quick Start
Basic Integration with AutoGen
The YepCode executor is designed to work with AutoGen agents through the PythonCodeExecutionTool. Here's a complete example:
import asyncio
import os
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.code_execution import PythonCodeExecutionTool
from autogen_ext_yepcode import YepCodeCodeExecutor
async def main():
# Create OpenAI model client
model_client = OpenAIChatCompletionClient(
model="gpt-4",
api_key=os.getenv("OPENAI_API_KEY"),
)
# Initialize YepCode executor
yepcode_executor = YepCodeCodeExecutor(
timeout=120,
remove_on_done=False,
sync_execution=True,
)
# Start the executor
await yepcode_executor.start()
# Create a PythonCodeExecutionTool with the YepCode executor
code_tool = PythonCodeExecutionTool(executor=yepcode_executor)
# Create an AssistantAgent with the code execution tool
assistant = AssistantAgent(
name="assistant",
model_client=model_client,
tools=[code_tool],
)
# Run a task that requires code execution
task = "Calculate the sum of squares for numbers 1 to 10. Show the calculation step by step using Python code."
result = await assistant.run(task=task)
print(f"Result: {result}")
# Clean up
await yepcode_executor.stop()
await model_client.close()
if __name__ == "__main__":
asyncio.run(main())
With Anthropic Claude
The extension also works with other model providers like Anthropic:
import asyncio
import os
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.anthropic import AnthropicChatCompletionClient
from autogen_ext.tools.code_execution import PythonCodeExecutionTool
from autogen_ext_yepcode import YepCodeCodeExecutor
async def main():
# Create Anthropic model client
model_client = AnthropicChatCompletionClient(
model="claude-3-haiku-20240307",
api_key=os.getenv("ANTHROPIC_API_KEY"),
)
# Initialize YepCode executor
yepcode_executor = YepCodeCodeExecutor(
timeout=120,
remove_on_done=False,
sync_execution=True,
)
# Start the executor
await yepcode_executor.start()
# Create a PythonCodeExecutionTool with the YepCode executor
code_tool = PythonCodeExecutionTool(executor=yepcode_executor)
# Create an AssistantAgent
assistant = AssistantAgent(
name="assistant",
model_client=model_client,
tools=[code_tool],
)
# Run a task
task = "Fetch cryptocurrency price data from a public API and analyze the top 5 cryptocurrencies by market cap. Use the requests library to get data and calculate some basic statistics."
result = await assistant.run(task=task)
print(f"Result: {result}")
# Clean up
await yepcode_executor.stop()
await model_client.close()
if __name__ == "__main__":
asyncio.run(main())
Custom Configuration
You can customize the YepCode executor behavior:
# Custom executor configuration
yepcode_executor = YepCodeCodeExecutor(
api_token="your-api-token", # Optional: pass token directly
timeout=300, # 5 minutes timeout
remove_on_done=False, # Keep execution records for debugging
sync_execution=True, # Wait for completion
)
Usage Patterns
Standard AutoGen Pattern
The recommended approach is to use the YepCodeCodeExecutor with AutoGen's PythonCodeExecutionTool:
# 1. Create the YepCode executor
yepcode_executor = YepCodeCodeExecutor()
await yepcode_executor.start()
# 2. Wrap it in a PythonCodeExecutionTool
code_tool = PythonCodeExecutionTool(executor=yepcode_executor)
# 3. Add to your AssistantAgent
assistant = AssistantAgent(
name="assistant",
model_client=your_model_client,
tools=[code_tool],
)
# 4. Use normally with AutoGen
result = await assistant.run("Your task here")
Context Manager Pattern
You can also use the executor as a context manager:
async def main():
async with YepCodeCodeExecutor() as executor:
code_tool = PythonCodeExecutionTool(executor=executor)
# Use the tool with your agents
assistant = AssistantAgent(
name="assistant",
model_client=model_client,
tools=[code_tool],
)
result = await assistant.run("Your task")
API Reference
YepCodeCodeExecutor
The main executor class for running code in YepCode's serverless environment.
Constructor Parameters
api_token(Optional[str]): YepCode API token. If not provided, will useYEPCODE_API_TOKENenvironment variable.timeout(int): Execution timeout in seconds. Default: 60.remove_on_done(bool): Whether to remove execution records after completion. Default: True.sync_execution(bool): Whether to wait for execution completion. Default: True.
Methods
async start(): Initialize the executorasync stop(): Clean up the executorasync execute_code_blocks(code_blocks, cancellation_token): Execute code blocksasync restart(): Restart the executor
YepCodeCodeResult
Result object returned from code execution.
Properties
exit_code(int): Execution exit code (0 for success)output(str): Execution output and logsexecution_id(Optional[str]): YepCode execution ID for tracking
Supported Languages
| Language | Language Code | Aliases |
|---|---|---|
| Python | python |
py |
| JavaScript | javascript |
js |
Examples
Check out the samples directory for comprehensive examples:
- AutoGen Integration Sample: Complete example showing integration with AutoGen agents
Development
Setup Development Environment
git clone https://github.com/yepcode/autogen_ext_yepcode.git
cd autogen_ext_yepcode
poetry install
Run Tests
pytest tests/ -v
📚 Documentation
- YepCode Documentation: Complete YepCode platform documentation
- AutoGen Documentation: AutoGen framework documentation
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autogen_ext_yepcode-0.1.0.tar.gz.
File metadata
- Download URL: autogen_ext_yepcode-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
502f3ad5103da5cc647f4894cf31428f4be3206418e4903f33d3ad0ae90459bd
|
|
| MD5 |
0b2dc551b12fa7e791ca4df382773206
|
|
| BLAKE2b-256 |
e31b3e81eedde8867c5ae818ed947774bff38f525b3207b9dde740b5f09c0275
|
File details
Details for the file autogen_ext_yepcode-0.1.0-py3-none-any.whl.
File metadata
- Download URL: autogen_ext_yepcode-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2b2d72050fbd521d9e151afca0c54a6f95f69c4d9f7d4ec2053ab7a5300b753
|
|
| MD5 |
b90e645a0d46792cffcac231d2f814c1
|
|
| BLAKE2b-256 |
fb09234e8058d00ffe7fd272fcb5fc258bff03015932f1639a20af6301635ce5
|