Update README.md
Browse files
README.md
CHANGED
@@ -4,4 +4,168 @@ language:
|
|
4 |
- en
|
5 |
base_model:
|
6 |
- Qwen/Qwen3-Coder-30B-A3B-Instruct
|
7 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
- en
|
5 |
base_model:
|
6 |
- Qwen/Qwen3-Coder-30B-A3B-Instruct
|
7 |
+
---
|
8 |
+
|
9 |
+
# Coding agent
|
10 |
+
|
11 |
+
A simple coding agent built with Qwen_Distilled_Coder (via OpenRouter) that can view/edit files and execute bash commands—all in ~200 lines.
|
12 |
+
|
13 |
+
|
14 |
+
```mermaid
|
15 |
+
flowchart TD
|
16 |
+
Start([Start]) --> UserInput[Get User Input]
|
17 |
+
UserInput --> Qwen_Distilled_Coder[Send to Qwen_Distilled_Coder]
|
18 |
+
Qwen_Distilled_Coder --> NeedsTools{Needs Tools?}
|
19 |
+
|
20 |
+
NeedsTools -->|No| ShowResponse[Show Response]
|
21 |
+
NeedsTools -->|Yes| ExecuteTools[Execute Tools]
|
22 |
+
|
23 |
+
ExecuteTools --> SendResults[Send Results to Qwen_Distilled_Coder]
|
24 |
+
SendResults --> Qwen_Distilled_Coder
|
25 |
+
|
26 |
+
ShowResponse --> UserInput
|
27 |
+
|
28 |
+
ExecuteTools -.-> Tools
|
29 |
+
```
|
30 |
+
|
31 |
+
## Quick start
|
32 |
+
|
33 |
+
1. **Create virtual environment and install dependencies**:
|
34 |
+
|
35 |
+
```bash
|
36 |
+
# Option 1: uv installed
|
37 |
+
uv venv
|
38 |
+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
39 |
+
uv sync
|
40 |
+
|
41 |
+
# Option 2: Without uv
|
42 |
+
python3 -m venv .venv
|
43 |
+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
44 |
+
pip3 install uv
|
45 |
+
uv sync
|
46 |
+
```
|
47 |
+
|
48 |
+
2. **Setup environment & add API key**:
|
49 |
+
|
50 |
+
```bash
|
51 |
+
cp .env.example .env
|
52 |
+
```
|
53 |
+
|
54 |
+
Be sure to add your OpenRouter API key! You can get one from [OpenRouter](https://openrouter.ai/keys).
|
55 |
+
|
56 |
+
3. **Run the CLI agent**:
|
57 |
+
|
58 |
+
```bash
|
59 |
+
uv run simple_agent.py
|
60 |
+
```
|
61 |
+
|
62 |
+
|
63 |
+
Note: `uv` and an appropriate virtualenv are prerequisites—our agent will use uv to execute Python scripts
|
64 |
+
|
65 |
+
## Using OpenRouter with Qwen Distilled Coder
|
66 |
+
|
67 |
+
This agent uses the [OpenRouter](https://openrouter.ai/) API to access the Qwen_Distilled_Coder model. OpenRouter provides a unified API for accessing various AI models, including those from Anthropic, OpenAI, and others.
|
68 |
+
|
69 |
+
To use this agent:
|
70 |
+
|
71 |
+
1. Sign up for an account at [OpenRouter](https://openrouter.ai/)
|
72 |
+
2. Create an API key at [OpenRouter Keys](https://openrouter.ai/keys)
|
73 |
+
3. Add your API key to the `.env` file
|
74 |
+
4. Run the agent with `uv run simple_agent.py`
|
75 |
+
|
76 |
+
The agent uses the OpenAI client library but points it to the OpenRouter API endpoint, allowing it to access the Qwen_Distilled_Coder model.
|
77 |
+
|
78 |
+
## What it does
|
79 |
+
|
80 |
+
- **Fix broken files**: `"can you help me fix broken_file.py?"`
|
81 |
+
- **Research and implement**: `"research new Python 3.13 features and write a file that demonstrates a simple example"`
|
82 |
+
- **Create new code**: `"write a simple tip splitting calculator Python file"`
|
83 |
+
|
84 |
+
## Architecture
|
85 |
+
|
86 |
+
The agent follows a straightforward pattern with three core components:
|
87 |
+
|
88 |
+
### Prompt structure
|
89 |
+
|
90 |
+
```xml
|
91 |
+
<role>
|
92 |
+
You are an expert software engineering assistant...
|
93 |
+
</role>
|
94 |
+
|
95 |
+
<thinking_process>
|
96 |
+
Before taking any action, think through the problem step by step...
|
97 |
+
</thinking_process>
|
98 |
+
|
99 |
+
<instructions>
|
100 |
+
When working with code:
|
101 |
+
1. Understanding First: Always examine existing files...
|
102 |
+
2. Targeted Changes: Use precise `str_replace` operations...
|
103 |
+
</instructions>
|
104 |
+
```
|
105 |
+
|
106 |
+
**Best practices:**
|
107 |
+
|
108 |
+
- Split system prompt (role) from user instructions for better caching
|
109 |
+
- Use XML tags for structured prompts and interpretability
|
110 |
+
- Include chain-of-thought reasoning with `<thinking_process>` blocks
|
111 |
+
- Cache tools, system prompt, and first user message for cost optimization
|
112 |
+
|
113 |
+
### Tool execution router
|
114 |
+
|
115 |
+
```python
|
116 |
+
def execute_tool(tool_name: str, tool_input: dict) -> dict:
|
117 |
+
"""Execute a tool and return structured result with error handling."""
|
118 |
+
try:
|
119 |
+
if tool_name == "view":
|
120 |
+
# Handle file/directory viewing
|
121 |
+
elif tool_name == "str_replace":
|
122 |
+
# Handle targeted file edits
|
123 |
+
elif tool_name == "bash":
|
124 |
+
# Handle command execution with timeout
|
125 |
+
# ...
|
126 |
+
except Exception as e:
|
127 |
+
return {"content": f"Error: {str(e)}", "is_error": True}
|
128 |
+
```
|
129 |
+
|
130 |
+
**Best practices:**
|
131 |
+
|
132 |
+
- Return structured responses with `is_error` flag for Qwen_Distilled_Coder
|
133 |
+
- Use proper timeout protection (30s default for bash)
|
134 |
+
- Include detailed error logging and handling
|
135 |
+
- Support both file operations and bash commands
|
136 |
+
|
137 |
+
### Agent loop
|
138 |
+
|
139 |
+
```python
|
140 |
+
while True:
|
141 |
+
response = client.messages.create(
|
142 |
+
model=ANTHROPIC_MODEL,
|
143 |
+
system=[{"type": "text", "text": system_prompt}],
|
144 |
+
messages=messages,
|
145 |
+
tools=ANTHROPIC_TOOLS,
|
146 |
+
)
|
147 |
+
|
148 |
+
if response.stop_reason == "tool_use":
|
149 |
+
# Execute tools in parallel when possible
|
150 |
+
# Return results to Qwen_Distilled_Coder for continued processing
|
151 |
+
else:
|
152 |
+
# Handle final response
|
153 |
+
break
|
154 |
+
```
|
155 |
+
|
156 |
+
**Best practices:**
|
157 |
+
|
158 |
+
- Handle all stop reasons robustly (tool_use, end_turn, etc.)
|
159 |
+
- Execute multiple tools in parallel when possible
|
160 |
+
- Maintain conversation state through message history
|
161 |
+
- Use low temperature (0.2) for consistent, focused responses
|
162 |
+
|
163 |
+
## Files
|
164 |
+
|
165 |
+
- `simple_agent.py` - CLI version
|
166 |
+
- `prompt.md` - System prompt and instructions
|
167 |
+
|
168 |
+
## Requirements
|
169 |
+
|
170 |
+
- Python 3.13+
|
171 |
+
- OpenRouter API key
|