|
| 1 | +# 生产环境使用指南 |
| 2 | + |
| 3 | +## 🚀 快速开始 |
| 4 | + |
| 5 | +### 1. 安装和配置 |
| 6 | + |
| 7 | +```bash |
| 8 | +# Clone 项目 |
| 9 | +git clone https://github.com/yourusername/openspace-openhands-evolution.git |
| 10 | +cd openspace-openhands-evolution |
| 11 | + |
| 12 | +# 安装依赖 |
| 13 | +pip install -e . |
| 14 | + |
| 15 | +# 运行设置脚本 |
| 16 | +python setup_production.py |
| 17 | +``` |
| 18 | + |
| 19 | +### 2. 配置 API Key |
| 20 | + |
| 21 | +**选项 A: 环境变量(推荐)** |
| 22 | + |
| 23 | +```bash |
| 24 | +# OpenAI |
| 25 | +export OPENAI_API_KEY=sk-your-openai-key |
| 26 | + |
| 27 | +# Anthropic |
| 28 | +export ANTHROPIC_API_KEY=sk-ant-your-key |
| 29 | +``` |
| 30 | + |
| 31 | +**选项 B: 配置文件** |
| 32 | + |
| 33 | +编辑 `config.yaml`: |
| 34 | +```yaml |
| 35 | +llm: |
| 36 | + provider: openai |
| 37 | + model: gpt-4 |
| 38 | + api_key: "sk-your-key" |
| 39 | +``` |
| 40 | +
|
| 41 | +### 3. 运行第一个任务 |
| 42 | +
|
| 43 | +```bash |
| 44 | +# 交互式模式 |
| 45 | +openspace-evolution |
| 46 | + |
| 47 | +# 或直接运行任务 |
| 48 | +openspace-evolution run "Create a Flask REST API with user authentication" |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 💡 实际用例 |
| 54 | + |
| 55 | +### 用例 1: 创建 Web API |
| 56 | + |
| 57 | +```bash |
| 58 | +openspace-evolution run " |
| 59 | +Create a Flask REST API for a todo list application with: |
| 60 | +- CRUD operations (Create, Read, Update, Delete) |
| 61 | +- SQLite database |
| 62 | +- Input validation |
| 63 | +- Error handling |
| 64 | +- API documentation |
| 65 | +" |
| 66 | +``` |
| 67 | + |
| 68 | +**输出**: |
| 69 | +- `workspace/app.py` - Flask 应用 |
| 70 | +- `workspace/models.py` - 数据模型 |
| 71 | +- `workspace/requirements.txt` - 依赖列表 |
| 72 | + |
| 73 | +### 用例 2: 数据分析脚本 |
| 74 | + |
| 75 | +```bash |
| 76 | +openspace-evolution run " |
| 77 | +Write a Python script to: |
| 78 | +1. Read a CSV file from 'data/sales.csv' |
| 79 | +2. Calculate monthly revenue |
| 80 | +3. Generate a summary report |
| 81 | +4. Save results to 'output/report.txt' |
| 82 | +" |
| 83 | +``` |
| 84 | + |
| 85 | +### 用例 3: React 组件 |
| 86 | + |
| 87 | +```bash |
| 88 | +openspace-evolution run " |
| 89 | +Create a React component for a user profile card that shows: |
| 90 | +- User avatar |
| 91 | +- Name and bio |
| 92 | +- Social media links |
| 93 | +- Follow/unfollow button |
| 94 | +Use TypeScript and styled-components. |
| 95 | +" --language javascript --framework react |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## 🔧 高级功能 |
| 101 | + |
| 102 | +### 跨项目技能迁移 |
| 103 | + |
| 104 | +```bash |
| 105 | +# 从 project-a 迁移技能到 project-b |
| 106 | +openspace-evolution transfer \ |
| 107 | + --from project-a \ |
| 108 | + --to project-b \ |
| 109 | + --min-similarity 0.8 |
| 110 | +``` |
| 111 | + |
| 112 | +### 查看系统状态 |
| 113 | + |
| 114 | +```bash |
| 115 | +openspace-evolution status |
| 116 | +``` |
| 117 | + |
| 118 | +输出示例: |
| 119 | +``` |
| 120 | +OpenSpace Engine: ✅ Running (15 skills) |
| 121 | +OpenHands Engine: ✅ Running (GPT-4) |
| 122 | +Monitor System: ✅ Running (threshold: 0.8) |
| 123 | +Governance: ✅ Active (V-02, V-06 enabled) |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## 🛡️ 安全特性 |
| 129 | + |
| 130 | +### 沙箱执行 |
| 131 | + |
| 132 | +所有代码在隔离环境中执行: |
| 133 | +- ✅ 无法访问系统文件 |
| 134 | +- ✅ 超时保护(默认 30 秒) |
| 135 | +- ✅ 输出限制(1MB) |
| 136 | +- ✅ 危险命令阻止 |
| 137 | + |
| 138 | +### 权限控制 |
| 139 | + |
| 140 | +在 `config.yaml` 中配置: |
| 141 | + |
| 142 | +```yaml |
| 143 | +openhands: |
| 144 | + enable_file_operations: true |
| 145 | + allowed_directories: |
| 146 | + - ./workspace |
| 147 | + - ./output |
| 148 | + sandbox_timeout: 30 |
| 149 | +``` |
| 150 | +
|
| 151 | +--- |
| 152 | +
|
| 153 | +## 📊 监控和日志 |
| 154 | +
|
| 155 | +### 查看执行日志 |
| 156 | +
|
| 157 | +```bash |
| 158 | +# 实时日志 |
| 159 | +tail -f logs/evolution.log |
| 160 | + |
| 161 | +# 查看错误 |
| 162 | +grep ERROR logs/evolution.log |
| 163 | +``` |
| 164 | + |
| 165 | +### 质量指标 |
| 166 | + |
| 167 | +每次执行都会记录: |
| 168 | +- 执行时间 |
| 169 | +- 成功率 |
| 170 | +- Token 使用量 |
| 171 | +- 质量评分 |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## 🎯 最佳实践 |
| 176 | + |
| 177 | +### 1. 任务描述要清晰 |
| 178 | + |
| 179 | +❌ **不好**: "Create an API" |
| 180 | + |
| 181 | +✅ **好**: "Create a Flask REST API for user management with JWT authentication, including registration, login, and profile update endpoints" |
| 182 | + |
| 183 | +### 2. 指定技术栈 |
| 184 | + |
| 185 | +```bash |
| 186 | +openspace-evolution run "Build a web scraper" \ |
| 187 | + --language python \ |
| 188 | + --framework beautifulsoup |
| 189 | +``` |
| 190 | + |
| 191 | +### 3. 分步执行复杂任务 |
| 192 | + |
| 193 | +将大任务分解: |
| 194 | +```bash |
| 195 | +# Step 1: 设计数据库 |
| 196 | +openspace-evolution run "Design SQLite schema for blog" |
| 197 | + |
| 198 | +# Step 2: 实现后端 |
| 199 | +openspace-evolution run "Implement Flask backend for blog" |
| 200 | + |
| 201 | +# Step 3: 创建前端 |
| 202 | +openspace-evolution run "Create React frontend for blog" |
| 203 | +``` |
| 204 | + |
| 205 | +### 4. 使用工作目录 |
| 206 | + |
| 207 | +```bash |
| 208 | +# 生成的文件保存在 workspace/ |
| 209 | +ls workspace/ |
| 210 | + |
| 211 | +# 查看生成的代码 |
| 212 | +cat workspace/app.py |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## 🔍 故障排除 |
| 218 | + |
| 219 | +### 问题 1: API Key 错误 |
| 220 | + |
| 221 | +``` |
| 222 | +Error: OpenAI API key not configured |
| 223 | +``` |
| 224 | + |
| 225 | +**解决**: |
| 226 | +```bash |
| 227 | +export OPENAI_API_KEY=sk-your-key |
| 228 | +# 或编辑 config.yaml |
| 229 | +``` |
| 230 | + |
| 231 | +### 问题 2: 执行超时 |
| 232 | + |
| 233 | +``` |
| 234 | +Error: Execution timed out after 30 seconds |
| 235 | +``` |
| 236 | + |
| 237 | +**解决**: 增加超时时间 |
| 238 | +```yaml |
| 239 | +openhands: |
| 240 | + sandbox_timeout: 60 # 增加到 60 秒 |
| 241 | +``` |
| 242 | +
|
| 243 | +### 问题 3: 代码执行失败 |
| 244 | +
|
| 245 | +检查 `workspace/` 中的错误日志: |
| 246 | +```bash |
| 247 | +cat logs/evolution.log | grep ERROR |
| 248 | +``` |
| 249 | + |
| 250 | +--- |
| 251 | + |
| 252 | +## 🌐 支持的 LLM 提供商 |
| 253 | + |
| 254 | +### OpenAI GPT-4 |
| 255 | + |
| 256 | +**优点**: 最强大的代码生成能力 |
| 257 | +**成本**: $0.03/1K tokens |
| 258 | + |
| 259 | +```yaml |
| 260 | +llm: |
| 261 | + provider: openai |
| 262 | + model: gpt-4 |
| 263 | +``` |
| 264 | + |
| 265 | +### Anthropic Claude-3 |
| 266 | + |
| 267 | +**优点**: 更好的长上下文理解 |
| 268 | +**成本**: $0.015/1K tokens |
| 269 | + |
| 270 | +```yaml |
| 271 | +llm: |
| 272 | + provider: anthropic |
| 273 | + model: claude-3-opus-20240229 |
| 274 | +``` |
| 275 | + |
| 276 | +### Ollama (本地) |
| 277 | + |
| 278 | +**优点**: 免费、隐私保护 |
| 279 | +**缺点**: 需要本地 GPU |
| 280 | + |
| 281 | +```bash |
| 282 | +# 安装 Ollama |
| 283 | +curl -fsSL https://ollama.ai/install.sh | sh |
| 284 | +
|
| 285 | +# 拉取模型 |
| 286 | +ollama pull llama2 |
| 287 | +
|
| 288 | +# 配置 |
| 289 | +``` |
| 290 | + |
| 291 | +```yaml |
| 292 | +llm: |
| 293 | + provider: ollama |
| 294 | + model: llama2 |
| 295 | + base_url: http://localhost:11434 |
| 296 | +``` |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +## 📈 性能优化 |
| 301 | + |
| 302 | +### 1. 缓存常用技能 |
| 303 | + |
| 304 | +技能会自动缓存,重复任务更快。 |
| 305 | + |
| 306 | +### 2. 并行执行 |
| 307 | + |
| 308 | +```python |
| 309 | +import asyncio |
| 310 | +from openspace_openhands_evolution import EvolutionOrchestrator, TaskRequest |
| 311 | +
|
| 312 | +async def run_parallel_tasks(): |
| 313 | + orchestrator = EvolutionOrchestrator(config) |
| 314 | + |
| 315 | + tasks = [ |
| 316 | + TaskRequest(id="task-1", description="Create API endpoint 1", ...), |
| 317 | + TaskRequest(id="task-2", description="Create API endpoint 2", ...), |
| 318 | + ] |
| 319 | + |
| 320 | + results = await asyncio.gather( |
| 321 | + *[orchestrator.execute_task(t) for t in tasks] |
| 322 | + ) |
| 323 | + |
| 324 | + return results |
| 325 | +``` |
| 326 | + |
| 327 | +### 3. 调整质量阈值 |
| 328 | + |
| 329 | +```yaml |
| 330 | +monitor: |
| 331 | + quality_threshold: 0.7 # 降低以加快速度(默认 0.8) |
| 332 | +``` |
| 333 | + |
| 334 | +--- |
| 335 | + |
| 336 | +## 🎓 学习资源 |
| 337 | + |
| 338 | +- [架构设计文档](ENGINEERING_CHECKLIST.md) |
| 339 | +- [API 参考](examples.py) |
| 340 | +- [配置选项](config.production.yaml) |
| 341 | +- [贡献指南](CONTRIBUTING.md) |
| 342 | + |
| 343 | +--- |
| 344 | + |
| 345 | +## 💬 获取帮助 |
| 346 | + |
| 347 | +- **Issues**: [GitHub Issues](https://github.com/yourusername/openspace-openhands-evolution/issues) |
| 348 | +- **Discussions**: [GitHub Discussions](https://github.com/yourusername/openspace-openhands-evolution/discussions) |
| 349 | + |
| 350 | +--- |
| 351 | + |
| 352 | +**Happy Coding! 🚀** |
0 commit comments