oj-problem-import 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oj_problem_import-0.1.0/PKG-INFO +374 -0
- oj_problem_import-0.1.0/README.md +341 -0
- oj_problem_import-0.1.0/oj_engine/__init__.py +40 -0
- oj_problem_import-0.1.0/oj_engine/agent/__init__.py +4 -0
- oj_problem_import-0.1.0/oj_engine/agent/problem_agent.py +364 -0
- oj_problem_import-0.1.0/oj_engine/cli.py +170 -0
- oj_problem_import-0.1.0/oj_engine/config/__init__.py +4 -0
- oj_problem_import-0.1.0/oj_engine/config/settings.py +171 -0
- oj_problem_import-0.1.0/oj_engine/config_manager.py +185 -0
- oj_problem_import-0.1.0/oj_engine/config_wizard.py +180 -0
- oj_problem_import-0.1.0/oj_engine/sandbox.py +361 -0
- oj_problem_import-0.1.0/oj_engine/services/__init__.py +4 -0
- oj_problem_import-0.1.0/oj_engine/state.py +68 -0
- oj_problem_import-0.1.0/oj_engine/tools/__init__.py +22 -0
- oj_problem_import-0.1.0/oj_engine/tools/sandbox_tools.py +585 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/PKG-INFO +374 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/SOURCES.txt +21 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/dependency_links.txt +1 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/entry_points.txt +2 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/requires.txt +13 -0
- oj_problem_import-0.1.0/oj_problem_import.egg-info/top_level.txt +1 -0
- oj_problem_import-0.1.0/pyproject.toml +52 -0
- oj_problem_import-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: oj-problem-import
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI OJ Content Engine - 智能 OJ 题目生成引擎
|
|
5
|
+
Author-email: str1ct <str1ct@qq.com>
|
|
6
|
+
Maintainer-email: str1ct <str1ct@qq.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/pengGgxp/Oj-problem-import
|
|
9
|
+
Project-URL: Documentation, https://github.com/pengGgxp/Oj-problem-import#readme
|
|
10
|
+
Project-URL: Repository, https://github.com/pengGgxp/Oj-problem-import
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/pengGgxp/Oj-problem-import/issues
|
|
12
|
+
Keywords: oj,problem,import,ai,langchain
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
Requires-Dist: langgraph>=0.2.0
|
|
21
|
+
Requires-Dist: langchain>=0.3.0
|
|
22
|
+
Requires-Dist: langchain-openai>=0.2.0
|
|
23
|
+
Requires-Dist: docker>=7.0.0
|
|
24
|
+
Requires-Dist: fastapi>=0.110.0
|
|
25
|
+
Requires-Dist: pydantic>=2.0.0
|
|
26
|
+
Requires-Dist: pydantic-settings>=2.0.0
|
|
27
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
28
|
+
Requires-Dist: langchain-community>=0.4.1
|
|
29
|
+
Requires-Dist: langchain-classic>=1.0.7
|
|
30
|
+
Requires-Dist: click>=8.0.0
|
|
31
|
+
Requires-Dist: platformdirs>=4.0.0
|
|
32
|
+
Requires-Dist: questionary>=2.0.0
|
|
33
|
+
|
|
34
|
+
# OJ Problem Import - AI 智能 OJ 题目生成引擎
|
|
35
|
+
|
|
36
|
+
[](https://www.python.org/)
|
|
37
|
+
[](LICENSE)
|
|
38
|
+
[](https://pypi.org/project/oj-problem-import/)
|
|
39
|
+
|
|
40
|
+
一个基于 LangGraph ReAct Agent 的智能 OJ(Online Judge)题目内容生成系统,能够自主决策并生成完整的测试数据包。
|
|
41
|
+
|
|
42
|
+
## ✨ 核心特性
|
|
43
|
+
|
|
44
|
+
- 🤖 **ReAct Agent**:基于 LangGraph 的推理+行动模式,AI 自主决策执行流程
|
|
45
|
+
- 🐳 **Docker 沙箱**:安全的代码执行环境,支持资源限制和隔离
|
|
46
|
+
- 🔄 **持久化会话**:Agent 生命周期内复用容器和工作目录,性能提升 75%
|
|
47
|
+
- 📊 **智能测试**:自动生成小/中/大规模测试数据,确保数据强度分布合理
|
|
48
|
+
- 🛠️ **分层工具**:提供基础、专用、高级三层工具供 Agent 调用
|
|
49
|
+
- 💾 **产物管理**:自动收集和保存生成的代码、测试数据等产物
|
|
50
|
+
|
|
51
|
+
## 🚀 快速开始
|
|
52
|
+
|
|
53
|
+
### 前置要求
|
|
54
|
+
|
|
55
|
+
- Python 3.12+
|
|
56
|
+
- Docker Desktop(正在运行)
|
|
57
|
+
- OpenAI API Key(或其他 LLM API)
|
|
58
|
+
|
|
59
|
+
### 安装
|
|
60
|
+
|
|
61
|
+
#### 方式一:使用 uvx(推荐,无需安装)
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
# 直接运行,uvx 会自动下载并执行
|
|
65
|
+
uvx oj-problem-import configure
|
|
66
|
+
uvx oj-problem-import generate -f problem.txt
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### 方式二:从 PyPI 安装
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# 使用 uv 安装
|
|
73
|
+
uv pip install oj-problem-import
|
|
74
|
+
|
|
75
|
+
# 或使用 pip
|
|
76
|
+
pip install oj-problem-import
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### 方式三:从源码安装
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# 克隆项目
|
|
83
|
+
git clone <repository-url>
|
|
84
|
+
cd Oj-problem-import
|
|
85
|
+
|
|
86
|
+
# 安装依赖
|
|
87
|
+
pip install -e .
|
|
88
|
+
|
|
89
|
+
# 配置环境变量
|
|
90
|
+
cp .env.example .env
|
|
91
|
+
# 编辑 .env 文件,填入你的 API Key
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 基本使用
|
|
95
|
+
|
|
96
|
+
#### 配置
|
|
97
|
+
|
|
98
|
+
首次使用时,需要配置 AI 模型提供商和 API Key。
|
|
99
|
+
|
|
100
|
+
**方式一:交互式配置向导(推荐)**
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# 启动配置向导
|
|
104
|
+
oj-engine configure
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
向导将引导你完成:
|
|
108
|
+
1. 选择 AI 模型提供商(OpenAI/Claude/自定义)
|
|
109
|
+
2. 输入 API Key
|
|
110
|
+
3. 选择模型
|
|
111
|
+
4. 确认配置
|
|
112
|
+
|
|
113
|
+
配置文件将保存在你的用户目录下,无需手动编辑。
|
|
114
|
+
|
|
115
|
+
**方式二:查看当前配置**
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
oj-engine show-config
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 生成题目
|
|
122
|
+
|
|
123
|
+
#### 方式一:命令行工具(推荐)
|
|
124
|
+
|
|
125
|
+
安装依赖后,可以直接使用 `oj-engine` 命令:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# 查看帮助
|
|
129
|
+
oj-engine --help
|
|
130
|
+
oj-engine generate --help
|
|
131
|
+
|
|
132
|
+
# 从文件读取题目描述
|
|
133
|
+
oj-engine generate -f problem.txt
|
|
134
|
+
|
|
135
|
+
# 直接传入题目描述
|
|
136
|
+
oj-engine generate -d "A+B Problem..."
|
|
137
|
+
|
|
138
|
+
# 自定义参数
|
|
139
|
+
oj-engine generate -f problem.txt -m 30 -o ./results
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### 方式二:Python API
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from oj_engine.agent import ProblemGenerationAgent
|
|
146
|
+
|
|
147
|
+
problem_description = """
|
|
148
|
+
A + B Problem
|
|
149
|
+
|
|
150
|
+
计算两个整数的和。
|
|
151
|
+
|
|
152
|
+
输入格式:
|
|
153
|
+
一行,包含两个整数 a 和 b,用空格分隔。
|
|
154
|
+
|
|
155
|
+
输出格式:
|
|
156
|
+
一行,包含 a + b 的结果。
|
|
157
|
+
|
|
158
|
+
数据范围:
|
|
159
|
+
-10^9 <= a, b <= 10^9
|
|
160
|
+
"""
|
|
161
|
+
|
|
162
|
+
# 使用上下文管理器(推荐)
|
|
163
|
+
with ProblemGenerationAgent(max_iterations=20) as agent:
|
|
164
|
+
result = agent.generate_problem(problem_description)
|
|
165
|
+
# 处理结果...
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 运行示例
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
# 方式一:使用 CLI 命令(推荐)
|
|
172
|
+
oj-engine generate -f problem.txt
|
|
173
|
+
|
|
174
|
+
# 方式二:运行 Python 示例脚本
|
|
175
|
+
python examples/agent_usage.py
|
|
176
|
+
|
|
177
|
+
# 查看生成的产物
|
|
178
|
+
python view_outputs.py
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## 📖 文档
|
|
182
|
+
|
|
183
|
+
- [快速开始指南](QUICKSTART.md)
|
|
184
|
+
- [持久化沙箱机制](docs/PERSISTENT_SANDBOX.md)
|
|
185
|
+
- [实现总结](docs/PERSISTENT_SANDBOX_IMPLEMENTATION.md)
|
|
186
|
+
- [配置管理指南](docs/配置管理指南.md)
|
|
187
|
+
- [架构方案](docs/架构方案.md)
|
|
188
|
+
|
|
189
|
+
## 🏗️ 架构设计
|
|
190
|
+
|
|
191
|
+
### 核心组件
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
┌─────────────────────────────────────────────┐
|
|
195
|
+
│ ProblemGenerationAgent (ReAct Agent) │
|
|
196
|
+
│ ├─ LLM Client (OpenAI/Claude/etc.) │
|
|
197
|
+
│ ├─ Tools Layer │
|
|
198
|
+
│ │ ├─ write_code_file │
|
|
199
|
+
│ │ ├─ read_file_content │
|
|
200
|
+
│ │ ├─ edit_file_content │
|
|
201
|
+
│ │ ├─ search_in_file │
|
|
202
|
+
│ │ ├─ delete_file │
|
|
203
|
+
│ │ ├─ execute_code │
|
|
204
|
+
│ │ └─ save_outputs_to_host │
|
|
205
|
+
│ └─ SandboxSession (Persistent) │
|
|
206
|
+
│ ├─ Docker Container │
|
|
207
|
+
│ └─ Workspace Directory │
|
|
208
|
+
└─────────────────────────────────────────────┘
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### 工作流程
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
1. Agent 接收题目描述
|
|
215
|
+
↓
|
|
216
|
+
2. 分析阶段:理解算法类型、数据范围、输入输出格式
|
|
217
|
+
↓
|
|
218
|
+
3. 生成标答:编写正确的 solution 代码
|
|
219
|
+
↓
|
|
220
|
+
4. 生成数据生成器:编写 generator 代码
|
|
221
|
+
↓
|
|
222
|
+
5. 批量生成测试:生成10组测试数据(3小/5中/2大)
|
|
223
|
+
↓
|
|
224
|
+
6. 验证测试:确保标答正确处理每组数据
|
|
225
|
+
↓
|
|
226
|
+
7. 分析强度:检查数据分布是否符合要求
|
|
227
|
+
↓
|
|
228
|
+
8. 保存产物:整理并保存所有生成的内容
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## 🔧 技术栈
|
|
232
|
+
|
|
233
|
+
- **LangGraph**: ReAct Agent 框架
|
|
234
|
+
- **LangChain**: LLM 集成和工具定义
|
|
235
|
+
- **Docker**: 沙箱执行环境
|
|
236
|
+
- **Pydantic**: 数据验证和状态管理
|
|
237
|
+
- **FastAPI**: (可选)Web API 接口
|
|
238
|
+
|
|
239
|
+
## 📁 项目结构
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
Oj-problem-import/
|
|
243
|
+
├── oj_engine/
|
|
244
|
+
│ ├── agent/ # ReAct Agent 实现
|
|
245
|
+
│ │ └── problem_agent.py
|
|
246
|
+
│ ├── sandbox.py # Docker 沙箱执行器
|
|
247
|
+
│ ├── tools/ # 工具层
|
|
248
|
+
│ │ └── sandbox_tools.py
|
|
249
|
+
│ ├── services/ # 服务层
|
|
250
|
+
│ │ └── output_manager.py
|
|
251
|
+
│ ├── config/ # 配置管理
|
|
252
|
+
│ │ └── settings.py
|
|
253
|
+
│ └── state.py # 状态定义
|
|
254
|
+
├── examples/ # 示例代码
|
|
255
|
+
│ └── agent_usage.py
|
|
256
|
+
├── docs/ # 文档
|
|
257
|
+
├── outputs/ # 生成的产物
|
|
258
|
+
├── main.py # 主入口
|
|
259
|
+
└── README.md
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## 🎯 使用场景
|
|
263
|
+
|
|
264
|
+
### 1. 自动生成 OJ 题目
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
problem = "最长上升子序列(LIS)问题..."
|
|
268
|
+
|
|
269
|
+
with ProblemGenerationAgent() as agent:
|
|
270
|
+
result = agent.generate_problem(problem)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### 2. 批量生成题目
|
|
274
|
+
|
|
275
|
+
```python
|
|
276
|
+
problems = [...]
|
|
277
|
+
|
|
278
|
+
for desc in problems:
|
|
279
|
+
with ProblemGenerationAgent() as agent:
|
|
280
|
+
result = agent.generate_problem(desc)
|
|
281
|
+
save_result(result)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 3. 带重试机制
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
with ProblemGenerationAgent() as agent:
|
|
288
|
+
result = agent.generate_problem_with_retry(
|
|
289
|
+
problem_description,
|
|
290
|
+
max_retries=3
|
|
291
|
+
)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## ⚡ 性能优化
|
|
295
|
+
|
|
296
|
+
### 持久化沙箱会话
|
|
297
|
+
|
|
298
|
+
传统方式每次执行都创建新容器:
|
|
299
|
+
```
|
|
300
|
+
execute() → 创建容器 → 执行 → 销毁 (重复 N 次)
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
新方式使用持久化会话:
|
|
304
|
+
```
|
|
305
|
+
初始化 → 创建容器
|
|
306
|
+
execute() → 复用容器 (重复 N 次)
|
|
307
|
+
清理 → 销毁容器
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**性能提升**:
|
|
311
|
+
- 容器创建次数减少 95%
|
|
312
|
+
- 平均执行时间减少 75%
|
|
313
|
+
- 总体耗时减少 75%
|
|
314
|
+
|
|
315
|
+
详见:[持久化沙箱文档](docs/PERSISTENT_SANDBOX.md)
|
|
316
|
+
|
|
317
|
+
## 🧪 测试
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
# 运行沙箱会话测试
|
|
321
|
+
python test_simple_sandbox.py
|
|
322
|
+
|
|
323
|
+
# 运行完整示例
|
|
324
|
+
python examples/agent_usage.py
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## 📝 配置说明
|
|
328
|
+
|
|
329
|
+
### 环境变量 (.env)
|
|
330
|
+
|
|
331
|
+
```bash
|
|
332
|
+
# OpenAI API 配置
|
|
333
|
+
OPENAI_API_KEY=sk-your-api-key
|
|
334
|
+
OPENAI_MODEL=gpt-4
|
|
335
|
+
|
|
336
|
+
# 或使用其他 LLM
|
|
337
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
338
|
+
ANTHROPIC_MODEL=claude-3-opus
|
|
339
|
+
|
|
340
|
+
# 沙箱配置
|
|
341
|
+
SANDBOX_IMAGE=python:3.10-slim
|
|
342
|
+
SANDBOX_MEM_LIMIT=512m
|
|
343
|
+
SANDBOX_CPU_QUOTA=50000
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
详见:[配置管理指南](docs/配置管理指南.md)
|
|
347
|
+
|
|
348
|
+
## 🤝 贡献
|
|
349
|
+
|
|
350
|
+
欢迎提交 Issue 和 Pull Request!
|
|
351
|
+
|
|
352
|
+
1. Fork 本项目
|
|
353
|
+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|
354
|
+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|
355
|
+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|
356
|
+
5. 开启 Pull Request
|
|
357
|
+
|
|
358
|
+
## 📄 许可证
|
|
359
|
+
|
|
360
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
|
|
361
|
+
|
|
362
|
+
## 🙏 致谢
|
|
363
|
+
|
|
364
|
+
- [LangGraph](https://github.com/langchain-ai/langgraph) - ReAct Agent 框架
|
|
365
|
+
- [LangChain](https://github.com/langchain-ai/langchain) - LLM 集成
|
|
366
|
+
- [Docker](https://www.docker.com/) - 容器化技术
|
|
367
|
+
|
|
368
|
+
## 📧 联系方式
|
|
369
|
+
|
|
370
|
+
如有问题或建议,请提交 Issue 或联系维护者。
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
**注意**:本项目仍处于早期开发阶段,API 可能会有变化。
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
# OJ Problem Import - AI 智能 OJ 题目生成引擎
|
|
2
|
+
|
|
3
|
+
[](https://www.python.org/)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://pypi.org/project/oj-problem-import/)
|
|
6
|
+
|
|
7
|
+
一个基于 LangGraph ReAct Agent 的智能 OJ(Online Judge)题目内容生成系统,能够自主决策并生成完整的测试数据包。
|
|
8
|
+
|
|
9
|
+
## ✨ 核心特性
|
|
10
|
+
|
|
11
|
+
- 🤖 **ReAct Agent**:基于 LangGraph 的推理+行动模式,AI 自主决策执行流程
|
|
12
|
+
- 🐳 **Docker 沙箱**:安全的代码执行环境,支持资源限制和隔离
|
|
13
|
+
- 🔄 **持久化会话**:Agent 生命周期内复用容器和工作目录,性能提升 75%
|
|
14
|
+
- 📊 **智能测试**:自动生成小/中/大规模测试数据,确保数据强度分布合理
|
|
15
|
+
- 🛠️ **分层工具**:提供基础、专用、高级三层工具供 Agent 调用
|
|
16
|
+
- 💾 **产物管理**:自动收集和保存生成的代码、测试数据等产物
|
|
17
|
+
|
|
18
|
+
## 🚀 快速开始
|
|
19
|
+
|
|
20
|
+
### 前置要求
|
|
21
|
+
|
|
22
|
+
- Python 3.12+
|
|
23
|
+
- Docker Desktop(正在运行)
|
|
24
|
+
- OpenAI API Key(或其他 LLM API)
|
|
25
|
+
|
|
26
|
+
### 安装
|
|
27
|
+
|
|
28
|
+
#### 方式一:使用 uvx(推荐,无需安装)
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# 直接运行,uvx 会自动下载并执行
|
|
32
|
+
uvx oj-problem-import configure
|
|
33
|
+
uvx oj-problem-import generate -f problem.txt
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
#### 方式二:从 PyPI 安装
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# 使用 uv 安装
|
|
40
|
+
uv pip install oj-problem-import
|
|
41
|
+
|
|
42
|
+
# 或使用 pip
|
|
43
|
+
pip install oj-problem-import
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### 方式三:从源码安装
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# 克隆项目
|
|
50
|
+
git clone <repository-url>
|
|
51
|
+
cd Oj-problem-import
|
|
52
|
+
|
|
53
|
+
# 安装依赖
|
|
54
|
+
pip install -e .
|
|
55
|
+
|
|
56
|
+
# 配置环境变量
|
|
57
|
+
cp .env.example .env
|
|
58
|
+
# 编辑 .env 文件,填入你的 API Key
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 基本使用
|
|
62
|
+
|
|
63
|
+
#### 配置
|
|
64
|
+
|
|
65
|
+
首次使用时,需要配置 AI 模型提供商和 API Key。
|
|
66
|
+
|
|
67
|
+
**方式一:交互式配置向导(推荐)**
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# 启动配置向导
|
|
71
|
+
oj-engine configure
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
向导将引导你完成:
|
|
75
|
+
1. 选择 AI 模型提供商(OpenAI/Claude/自定义)
|
|
76
|
+
2. 输入 API Key
|
|
77
|
+
3. 选择模型
|
|
78
|
+
4. 确认配置
|
|
79
|
+
|
|
80
|
+
配置文件将保存在你的用户目录下,无需手动编辑。
|
|
81
|
+
|
|
82
|
+
**方式二:查看当前配置**
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
oj-engine show-config
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### 生成题目
|
|
89
|
+
|
|
90
|
+
#### 方式一:命令行工具(推荐)
|
|
91
|
+
|
|
92
|
+
安装依赖后,可以直接使用 `oj-engine` 命令:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# 查看帮助
|
|
96
|
+
oj-engine --help
|
|
97
|
+
oj-engine generate --help
|
|
98
|
+
|
|
99
|
+
# 从文件读取题目描述
|
|
100
|
+
oj-engine generate -f problem.txt
|
|
101
|
+
|
|
102
|
+
# 直接传入题目描述
|
|
103
|
+
oj-engine generate -d "A+B Problem..."
|
|
104
|
+
|
|
105
|
+
# 自定义参数
|
|
106
|
+
oj-engine generate -f problem.txt -m 30 -o ./results
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### 方式二:Python API
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from oj_engine.agent import ProblemGenerationAgent
|
|
113
|
+
|
|
114
|
+
problem_description = """
|
|
115
|
+
A + B Problem
|
|
116
|
+
|
|
117
|
+
计算两个整数的和。
|
|
118
|
+
|
|
119
|
+
输入格式:
|
|
120
|
+
一行,包含两个整数 a 和 b,用空格分隔。
|
|
121
|
+
|
|
122
|
+
输出格式:
|
|
123
|
+
一行,包含 a + b 的结果。
|
|
124
|
+
|
|
125
|
+
数据范围:
|
|
126
|
+
-10^9 <= a, b <= 10^9
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
# 使用上下文管理器(推荐)
|
|
130
|
+
with ProblemGenerationAgent(max_iterations=20) as agent:
|
|
131
|
+
result = agent.generate_problem(problem_description)
|
|
132
|
+
# 处理结果...
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 运行示例
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# 方式一:使用 CLI 命令(推荐)
|
|
139
|
+
oj-engine generate -f problem.txt
|
|
140
|
+
|
|
141
|
+
# 方式二:运行 Python 示例脚本
|
|
142
|
+
python examples/agent_usage.py
|
|
143
|
+
|
|
144
|
+
# 查看生成的产物
|
|
145
|
+
python view_outputs.py
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## 📖 文档
|
|
149
|
+
|
|
150
|
+
- [快速开始指南](QUICKSTART.md)
|
|
151
|
+
- [持久化沙箱机制](docs/PERSISTENT_SANDBOX.md)
|
|
152
|
+
- [实现总结](docs/PERSISTENT_SANDBOX_IMPLEMENTATION.md)
|
|
153
|
+
- [配置管理指南](docs/配置管理指南.md)
|
|
154
|
+
- [架构方案](docs/架构方案.md)
|
|
155
|
+
|
|
156
|
+
## 🏗️ 架构设计
|
|
157
|
+
|
|
158
|
+
### 核心组件
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
┌─────────────────────────────────────────────┐
|
|
162
|
+
│ ProblemGenerationAgent (ReAct Agent) │
|
|
163
|
+
│ ├─ LLM Client (OpenAI/Claude/etc.) │
|
|
164
|
+
│ ├─ Tools Layer │
|
|
165
|
+
│ │ ├─ write_code_file │
|
|
166
|
+
│ │ ├─ read_file_content │
|
|
167
|
+
│ │ ├─ edit_file_content │
|
|
168
|
+
│ │ ├─ search_in_file │
|
|
169
|
+
│ │ ├─ delete_file │
|
|
170
|
+
│ │ ├─ execute_code │
|
|
171
|
+
│ │ └─ save_outputs_to_host │
|
|
172
|
+
│ └─ SandboxSession (Persistent) │
|
|
173
|
+
│ ├─ Docker Container │
|
|
174
|
+
│ └─ Workspace Directory │
|
|
175
|
+
└─────────────────────────────────────────────┘
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 工作流程
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
1. Agent 接收题目描述
|
|
182
|
+
↓
|
|
183
|
+
2. 分析阶段:理解算法类型、数据范围、输入输出格式
|
|
184
|
+
↓
|
|
185
|
+
3. 生成标答:编写正确的 solution 代码
|
|
186
|
+
↓
|
|
187
|
+
4. 生成数据生成器:编写 generator 代码
|
|
188
|
+
↓
|
|
189
|
+
5. 批量生成测试:生成10组测试数据(3小/5中/2大)
|
|
190
|
+
↓
|
|
191
|
+
6. 验证测试:确保标答正确处理每组数据
|
|
192
|
+
↓
|
|
193
|
+
7. 分析强度:检查数据分布是否符合要求
|
|
194
|
+
↓
|
|
195
|
+
8. 保存产物:整理并保存所有生成的内容
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## 🔧 技术栈
|
|
199
|
+
|
|
200
|
+
- **LangGraph**: ReAct Agent 框架
|
|
201
|
+
- **LangChain**: LLM 集成和工具定义
|
|
202
|
+
- **Docker**: 沙箱执行环境
|
|
203
|
+
- **Pydantic**: 数据验证和状态管理
|
|
204
|
+
- **FastAPI**: (可选)Web API 接口
|
|
205
|
+
|
|
206
|
+
## 📁 项目结构
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
Oj-problem-import/
|
|
210
|
+
├── oj_engine/
|
|
211
|
+
│ ├── agent/ # ReAct Agent 实现
|
|
212
|
+
│ │ └── problem_agent.py
|
|
213
|
+
│ ├── sandbox.py # Docker 沙箱执行器
|
|
214
|
+
│ ├── tools/ # 工具层
|
|
215
|
+
│ │ └── sandbox_tools.py
|
|
216
|
+
│ ├── services/ # 服务层
|
|
217
|
+
│ │ └── output_manager.py
|
|
218
|
+
│ ├── config/ # 配置管理
|
|
219
|
+
│ │ └── settings.py
|
|
220
|
+
│ └── state.py # 状态定义
|
|
221
|
+
├── examples/ # 示例代码
|
|
222
|
+
│ └── agent_usage.py
|
|
223
|
+
├── docs/ # 文档
|
|
224
|
+
├── outputs/ # 生成的产物
|
|
225
|
+
├── main.py # 主入口
|
|
226
|
+
└── README.md
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## 🎯 使用场景
|
|
230
|
+
|
|
231
|
+
### 1. 自动生成 OJ 题目
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
problem = "最长上升子序列(LIS)问题..."
|
|
235
|
+
|
|
236
|
+
with ProblemGenerationAgent() as agent:
|
|
237
|
+
result = agent.generate_problem(problem)
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 2. 批量生成题目
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
problems = [...]
|
|
244
|
+
|
|
245
|
+
for desc in problems:
|
|
246
|
+
with ProblemGenerationAgent() as agent:
|
|
247
|
+
result = agent.generate_problem(desc)
|
|
248
|
+
save_result(result)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### 3. 带重试机制
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
with ProblemGenerationAgent() as agent:
|
|
255
|
+
result = agent.generate_problem_with_retry(
|
|
256
|
+
problem_description,
|
|
257
|
+
max_retries=3
|
|
258
|
+
)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## ⚡ 性能优化
|
|
262
|
+
|
|
263
|
+
### 持久化沙箱会话
|
|
264
|
+
|
|
265
|
+
传统方式每次执行都创建新容器:
|
|
266
|
+
```
|
|
267
|
+
execute() → 创建容器 → 执行 → 销毁 (重复 N 次)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
新方式使用持久化会话:
|
|
271
|
+
```
|
|
272
|
+
初始化 → 创建容器
|
|
273
|
+
execute() → 复用容器 (重复 N 次)
|
|
274
|
+
清理 → 销毁容器
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
**性能提升**:
|
|
278
|
+
- 容器创建次数减少 95%
|
|
279
|
+
- 平均执行时间减少 75%
|
|
280
|
+
- 总体耗时减少 75%
|
|
281
|
+
|
|
282
|
+
详见:[持久化沙箱文档](docs/PERSISTENT_SANDBOX.md)
|
|
283
|
+
|
|
284
|
+
## 🧪 测试
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
# 运行沙箱会话测试
|
|
288
|
+
python test_simple_sandbox.py
|
|
289
|
+
|
|
290
|
+
# 运行完整示例
|
|
291
|
+
python examples/agent_usage.py
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## 📝 配置说明
|
|
295
|
+
|
|
296
|
+
### 环境变量 (.env)
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
# OpenAI API 配置
|
|
300
|
+
OPENAI_API_KEY=sk-your-api-key
|
|
301
|
+
OPENAI_MODEL=gpt-4
|
|
302
|
+
|
|
303
|
+
# 或使用其他 LLM
|
|
304
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
305
|
+
ANTHROPIC_MODEL=claude-3-opus
|
|
306
|
+
|
|
307
|
+
# 沙箱配置
|
|
308
|
+
SANDBOX_IMAGE=python:3.10-slim
|
|
309
|
+
SANDBOX_MEM_LIMIT=512m
|
|
310
|
+
SANDBOX_CPU_QUOTA=50000
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
详见:[配置管理指南](docs/配置管理指南.md)
|
|
314
|
+
|
|
315
|
+
## 🤝 贡献
|
|
316
|
+
|
|
317
|
+
欢迎提交 Issue 和 Pull Request!
|
|
318
|
+
|
|
319
|
+
1. Fork 本项目
|
|
320
|
+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|
321
|
+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|
322
|
+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|
323
|
+
5. 开启 Pull Request
|
|
324
|
+
|
|
325
|
+
## 📄 许可证
|
|
326
|
+
|
|
327
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
|
|
328
|
+
|
|
329
|
+
## 🙏 致谢
|
|
330
|
+
|
|
331
|
+
- [LangGraph](https://github.com/langchain-ai/langgraph) - ReAct Agent 框架
|
|
332
|
+
- [LangChain](https://github.com/langchain-ai/langchain) - LLM 集成
|
|
333
|
+
- [Docker](https://www.docker.com/) - 容器化技术
|
|
334
|
+
|
|
335
|
+
## 📧 联系方式
|
|
336
|
+
|
|
337
|
+
如有问题或建议,请提交 Issue 或联系维护者。
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
**注意**:本项目仍处于早期开发阶段,API 可能会有变化。
|