Skip to content

Commit de372f5

Browse files
Initial commit
0 parents  commit de372f5

File tree

17 files changed

+2481
-0
lines changed

17 files changed

+2481
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @yepcode/maintainers

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- spike/*
7+
pull_request:
8+
branches:
9+
- main
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
12+
cancel-in-progress: true
13+
jobs:
14+
tests:
15+
uses: ./.github/workflows/tests.yml
16+
secrets: inherit

.github/workflows/release.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- v[0-9]+.[0-9]+.[0-9]+
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
8+
cancel-in-progress: true
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
container:
13+
image: python:3.12-slim
14+
env:
15+
YEPCODE_API_TOKEN: ${{ secrets.TEST_YEPCODE_API_TOKEN }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Configure poetry
19+
run: |-
20+
apt update && apt install -y curl gcc g++ && curl -sSL https://install.python-poetry.org | python3 -
21+
export PATH="${PATH}:${HOME}/.local/bin"
22+
echo "${HOME}/.local/bin" >> $GITHUB_PATH
23+
poetry install
24+
- name: Build
25+
run: "poetry build"
26+
- name: Publish
27+
run: "poetry config pypi-token.pypi ${{ secrets.PUBLIC_YEPCODE_PYPI_API_TOKEN }} && poetry publish"
28+
create_release:
29+
needs: deploy
30+
name: Create Release
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Create Release
35+
id: create_release
36+
uses: softprops/action-gh-release@v2
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
with:
40+
draft: false
41+
prerelease: false

.github/workflows/tests.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Tests
2+
on:
3+
workflow_call:
4+
workflow_dispatch:
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
container:
9+
image: python:3.12-slim
10+
env:
11+
YEPCODE_API_TOKEN: ${{ secrets.TEST_YEPCODE_API_TOKEN }}
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Configure poetry
15+
run: |-
16+
apt update && apt install -y curl gcc g++ && curl -sSL https://install.python-poetry.org | python3 -
17+
export PATH="${PATH}:${HOME}/.local/bin"
18+
echo "${HOME}/.local/bin" >> $GITHUB_PATH
19+
poetry install
20+
- name: Run pytest
21+
run: "poetry run pytest"
22+
- name: Build
23+
run: "poetry build"

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Virtual Environment
24+
venv/
25+
.env
26+
.venv
27+
env.bak/
28+
venv.bak/
29+
30+
# IDE specific files
31+
.idea/
32+
.vscode/
33+
*.swp
34+
*.swo
35+
.DS_Store
36+
*.sublime-workspace
37+
*.sublime-project
38+
39+
# Test coverage
40+
htmlcov/
41+
.tox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
.hypothesis/
49+
.pytest_cache/
50+
51+
# Documentation
52+
docs/_build/
53+
docs/api/
54+
55+
# Logs
56+
*.log
57+
logs/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 YepCode
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
![YepCode Run SDK Preview](/readme-assets/cover.png)
2+
3+
<div align="center">
4+
5+
[![PyPI Version](https://img.shields.io/pypi/v/ag2-ext-yepcode)](https://pypi.org/project/ag2-ext-yepcode/)
6+
[![PyPI Downloads](https://img.shields.io/pypi/dm/ag2-ext-yepcode)](https://pypi.org/project/ag2-ext-yepcode/)
7+
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/yepcode/ag2-ext-yepcode-py/ci.yml)](https://github.com/yepcode/ag2-ext-yepcode-py/actions)
8+
9+
</div>
10+
11+
# AG2 Extension for YepCode
12+
13+
An [AG2](https://ag2.ai/) (formerly AutoGen) extension that enables secure code execution using [YepCode's](https://yepcode.io/) serverless runtime environment. Execute Python and JavaScript code in production-grade, isolated sandboxes with built-in security and scalability.
14+
15+
> **Note**: AG2 was evolved from AutoGen. If you are looking for the extension for AutoGen, please check the [autogen-ext-yepcode](https://github.com/yepcode/autogen-ext-yepcode) repository.
16+
17+
## Features
18+
19+
- **Secure Execution**: Code runs in isolated, production-grade sandboxes
20+
- **Multi-language Support**: Python and JavaScript execution
21+
- **Automatic Package Installation**: YepCode automatically detects and installs dependencies in the sandbox
22+
- **Logging and Monitoring**: Access to YepCode's execution logs, results and errors
23+
- **AG2 Integration**: Seamless integration with AG2 agents and tools
24+
25+
## Installation
26+
27+
Install the package using pip:
28+
29+
```bash
30+
pip install ag2_ext_yepcode
31+
```
32+
33+
## Setup
34+
35+
1. **Create a YepCode Account**: Sign up at [yepcode.io](https://yepcode.io/)
36+
2. **Get Your API Token**: Navigate to `Settings` > `API credentials` in your YepCode workspace
37+
3. **Set Environment Variable**:
38+
```bash
39+
export YEPCODE_API_TOKEN="your-api-token-here"
40+
```
41+
42+
Alternatively, you can pass the API token directly to the executor constructor.
43+
44+
## Quick Start
45+
46+
### Basic Integration with AG2
47+
48+
The YepCode executor is designed to work with AG2 agents as a `CodeExecutor`. Here's a example of just the code executor agent:
49+
50+
```python
51+
from autogen import ConversableAgent, LLMConfig
52+
from ag2_ext_yepcode import YepCodeCodeExecutor
53+
from dotenv import load_dotenv
54+
import os
55+
56+
# Load environment variables from .env file if it exists
57+
load_dotenv()
58+
59+
# Initialize YepCode executor
60+
yepcode_executor = YepCodeCodeExecutor(
61+
timeout=120,
62+
remove_on_done=False,
63+
sync_execution=True,
64+
)
65+
66+
# Create an agent with code executor configuration.
67+
code_executor_agent = ConversableAgent(
68+
"code_executor_agent",
69+
llm_config=False, # Turn off LLM for this agent.
70+
code_execution_config={
71+
"executor": yepcode_executor
72+
}, # Use the local command line code executor.
73+
human_input_mode="ALWAYS", # Always take human input for this agent for safety.
74+
)
75+
76+
task = """Run this code:
77+
\```python
78+
message = "Hello, world!"
79+
print(message)
80+
return message
81+
\```
82+
83+
and return the output.
84+
"""
85+
86+
# Generate a reply for the given code.
87+
reply = code_executor_agent.generate_reply(messages=[{"role": "user", "content": task}])
88+
print(reply)
89+
```
90+
91+
### With LLMs and code execution tools
92+
93+
The extension also works with more advanced patterns, like using LLMs and code execution tools:
94+
95+
```python
96+
from autogen import ConversableAgent, LLMConfig
97+
from ag2_ext_yepcode import YepCodeCodeExecutor
98+
from dotenv import load_dotenv
99+
import os
100+
101+
# Load environment variables from .env file if it exists
102+
load_dotenv()
103+
104+
# Initialize YepCode executor
105+
yepcode_executor = YepCodeCodeExecutor(
106+
timeout=120,
107+
remove_on_done=False,
108+
sync_execution=True,
109+
)
110+
111+
# Create an agent with code executor configuration.
112+
code_executor_agent = ConversableAgent(
113+
"code_executor_agent",
114+
llm_config=False, # Turn off LLM for this agent.
115+
code_execution_config={
116+
"executor": yepcode_executor
117+
}, # Use the local command line code executor.
118+
human_input_mode="ALWAYS",
119+
)
120+
121+
# The code writer agent's system message is to instruct the LLM on how to use
122+
# the code executor in the code executor agent.
123+
code_writer_system_message = """You are a helpful AI assistant.
124+
Solve tasks using your coding and language skills.
125+
In the following cases, suggest python code (in a python coding block) for the user to execute.
126+
1. When you need to collect info, use the code to output the info you need. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
127+
2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
128+
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
129+
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
130+
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
131+
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
132+
Reply 'TERMINATE' in the end when everything is done.
133+
"""
134+
135+
code_writer_agent = ConversableAgent(
136+
"code_writer_agent",
137+
system_message=code_writer_system_message,
138+
llm_config=LLMConfig(
139+
api_type="anthropic",
140+
model="claude-3-haiku-20240307",
141+
api_key=os.environ["ANTHROPIC_API_KEY"],
142+
),
143+
code_execution_config=False, # Turn off code execution for this agent.
144+
)
145+
146+
chat_result = code_executor_agent.initiate_chat(
147+
code_writer_agent,
148+
message="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.",
149+
)
150+
print(chat_result)
151+
```
152+
153+
### Custom Configuration
154+
155+
You can customize the YepCode executor behavior:
156+
157+
```python
158+
# Custom executor configuration
159+
yepcode_executor = YepCodeCodeExecutor(
160+
api_token="your-api-token", # Optional: pass token directly
161+
timeout=300, # 5 minutes timeout
162+
remove_on_done=False, # Keep execution records for debugging
163+
sync_execution=True, # Wait for completion
164+
)
165+
```
166+
167+
## API Reference
168+
169+
### YepCodeCodeExecutor
170+
171+
The main executor class for running code in YepCode's serverless environment.
172+
173+
#### Constructor Parameters
174+
175+
- `api_token` (Optional[str]): YepCode API token. If not provided, will use `YEPCODE_API_TOKEN` environment variable.
176+
- `timeout` (int): Execution timeout in seconds. Default: 60.
177+
- `remove_on_done` (bool): Whether to remove execution records after completion. Default: True.
178+
- `sync_execution` (bool): Whether to wait for execution completion. Default: True.
179+
180+
#### Methods
181+
182+
- `async execute_code_blocks(code_blocks, cancellation_token)`: Execute code blocks
183+
184+
### YepCodeCodeResult
185+
186+
Result object returned from code execution.
187+
188+
#### Properties
189+
190+
- `exit_code` (int): Execution exit code (0 for success)
191+
- `output` (str): Execution output and logs
192+
- `execution_id` (Optional[str]): YepCode execution ID for tracking
193+
194+
## Supported Languages
195+
196+
| Language | Language Code | Aliases |
197+
| ---------- | ------------- | ------- |
198+
| Python | `python` | `py` |
199+
| JavaScript | `javascript` | `js` |
200+
201+
## Development
202+
203+
### Setup Development Environment
204+
205+
```bash
206+
git clone https://github.com/yepcode/ag2_ext_yepcode.git
207+
cd ag2_ext_yepcode
208+
poetry install
209+
```
210+
211+
### Run Tests
212+
213+
```bash
214+
pytest tests/ -v
215+
```
216+
217+
## 📚 Documentation
218+
219+
- **[YepCode Documentation](https://yepcode.io/docs)**: Complete YepCode platform documentation
220+
- **[AG2 Documentation](https://docs.ag2.ai/)**: AG2 framework documentation
221+
222+
## License
223+
224+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)