|
| 1 | +# DeepResearch Integration for rLLM |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This module integrates Tongyi's DeepResearch ReAct agent into the rLLM framework, enabling evaluation on academic benchmarks like HLE (Humanity's Last Exam). The integration demonstrates how to port external agent architectures into rLLM's workflow system while maintaining compatibility with the training and evaluation infrastructure. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +``` |
| 10 | +DeepResearch Agent (ReAct with XML-based tool calling) |
| 11 | + ↓ |
| 12 | +DeepResearchWorkflow (rLLM Workflow wrapper) |
| 13 | + ↓ |
| 14 | +AgentWorkflowEngine (Parallel execution) |
| 15 | + ↓ |
| 16 | +Episode/Trajectory (rLLM data format) |
| 17 | +``` |
| 18 | + |
| 19 | +### Key Components |
| 20 | + |
| 21 | +- **`deepresearch_agent.py`**: MultiTurnReactAgent implementing Tongyi's ReAct loop with tool calling |
| 22 | +- **`deepresearch_workflow.py`**: Wrapper that converts agent outputs to rLLM Episodes for trajectory tracking |
| 23 | +- **`deepresearch_tools.py`**: Tool implementations (Search, Scholar, Visit, FileParser, PythonInterpreter) |
| 24 | +- **`evaluate_hle.py`**: Evaluation script for HLE (Humanity's Last Exam) benchmark |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +### Prerequisites |
| 29 | + |
| 30 | +```bash |
| 31 | +# Activate rLLM environment |
| 32 | +conda activate rllm |
| 33 | + |
| 34 | +# Install required dependencies |
| 35 | +pip install datasets # For HLE dataset access |
| 36 | +pip install tiktoken # Optional: for better token counting with OpenAI models |
| 37 | +``` |
| 38 | + |
| 39 | +### Environment Setup |
| 40 | + |
| 41 | +Create a `.env` file with your API keys: |
| 42 | + |
| 43 | +```bash |
| 44 | +# For model inference (choose one) |
| 45 | +OPENAI_API_KEY=your_openai_key |
| 46 | +TOGETHER_AI_API_KEY=your_together_key |
| 47 | + |
| 48 | +# Optional: For web search tool |
| 49 | +SERPER_API_KEY=your_serper_key # Get free key from serper.dev |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### Running HLE Evaluation |
| 55 | + |
| 56 | +```bash |
| 57 | +# Evaluate on HLE dataset with default settings |
| 58 | +python evaluate_hle.py --hf-dataset cais/hle --max-samples 10 --parallel-tasks 4 |
| 59 | + |
| 60 | +# Use specific model |
| 61 | +python evaluate_hle.py --model gpt-4o --max-samples 5 |
| 62 | + |
| 63 | +# Use Together AI for evaluation |
| 64 | +python evaluate_hle.py --model Qwen/Qwen2.5-7B-Instruct-Turbo \ |
| 65 | + --base-url https://api.together.xyz/v1 \ |
| 66 | + --max-samples 20 |
| 67 | + |
| 68 | +# Custom output directory |
| 69 | +python evaluate_hle.py --output-dir ./my_results --max-samples 20 |
| 70 | +``` |
| 71 | + |
| 72 | +### Using DeepResearch Agent Directly |
| 73 | + |
| 74 | +```python |
| 75 | +from rllm.engine.rollout import OpenAIEngine |
| 76 | +from deepresearch_agent import MultiTurnReactAgent |
| 77 | +from deepresearch_tools import get_all_tools |
| 78 | + |
| 79 | +# Setup rollout engine |
| 80 | +engine = OpenAIEngine( |
| 81 | + model="gpt-4o", |
| 82 | + api_key="your_key", |
| 83 | + base_url="https://api.openai.com/v1" |
| 84 | +) |
| 85 | + |
| 86 | +# Create agent with tools |
| 87 | +agent = MultiTurnReactAgent( |
| 88 | + rollout_engine=engine, |
| 89 | + tools=get_all_tools() |
| 90 | +) |
| 91 | + |
| 92 | +# Run a research task |
| 93 | +result = await agent.run( |
| 94 | + question="What is the reduced 12th dimensional Spin bordism of BG2?", |
| 95 | + answer="Z/2" # Optional ground truth for evaluation |
| 96 | +) |
| 97 | + |
| 98 | +print(f"Prediction: {result['prediction']}") |
| 99 | +print(f"Rounds: {result['rounds']}") |
| 100 | +print(f"Time taken: {result['time_taken']}s") |
| 101 | +``` |
| 102 | + |
| 103 | +### Integrating with rLLM Workflows |
| 104 | + |
| 105 | +```python |
| 106 | +from rllm.engine.agent_workflow_engine import AgentWorkflowEngine |
| 107 | +from deepresearch_workflow import DeepResearchWorkflow |
| 108 | + |
| 109 | +# Create workflow engine for parallel execution |
| 110 | +workflow_engine = AgentWorkflowEngine( |
| 111 | + workflow_cls=DeepResearchWorkflow, |
| 112 | + workflow_args={ |
| 113 | + "tools": get_all_tools(), |
| 114 | + "max_prompt_length": 4096, |
| 115 | + "max_response_length": 2048 |
| 116 | + }, |
| 117 | + rollout_engine=engine, |
| 118 | + n_parallel_tasks=4 # Run 4 tasks in parallel |
| 119 | +) |
| 120 | + |
| 121 | +# Run evaluation on multiple tasks |
| 122 | +tasks = [ |
| 123 | + {"question": "Question 1", "answer": "Answer 1"}, |
| 124 | + {"question": "Question 2", "answer": "Answer 2"} |
| 125 | +] |
| 126 | + |
| 127 | +episodes = await workflow_engine.execute_tasks(tasks) |
| 128 | + |
| 129 | +# Episodes contain full trajectories for training |
| 130 | +for episode in episodes: |
| 131 | + print(f"Task: {episode.task}") |
| 132 | + print(f"Prediction: {episode.metrics.get('prediction')}") |
| 133 | + print(f"Is correct: {episode.is_correct}") |
| 134 | +``` |
| 135 | + |
| 136 | +## Tools |
| 137 | + |
| 138 | +The agent has access to the following research tools: |
| 139 | + |
| 140 | +| Tool | Description | Implementation Status | |
| 141 | +| --------------------- | --------------------------- | ------------------------------------ | |
| 142 | +| **Search** | Web search via Serper API | ✅ Fully implemented (needs API key) | |
| 143 | +| **PythonInterpreter** | Execute Python code safely | ✅ Fully implemented with security | |
| 144 | +| **Scholar** | Academic paper search | ❌ Placeholder only | |
| 145 | +| **Visit** | Visit and analyze web pages | ❌ Placeholder only | |
| 146 | +| **FileParser** | Parse various file formats | ⚠️ Basic text only (no PDF/DOCX) | |
| 147 | + |
| 148 | +### Tool Implementation Notes |
| 149 | + |
| 150 | +- **Search**: Real web search with Serper API integration. Configure API key in `.env` file |
| 151 | +- **PythonInterpreter**: Enhanced security, 50s timeout, supports numpy/pandas when available |
| 152 | +- **Scholar**: Returns placeholder results. Needs integration with arXiv/Google Scholar APIs |
| 153 | +- **Visit**: Returns placeholder content. Needs requests/BeautifulSoup implementation |
| 154 | +- **FileParser**: Only reads text files up to 5000 chars. Original supports PDF/DOCX/media files |
| 155 | + |
| 156 | +## Key Improvements from Original |
| 157 | + |
| 158 | +### 1. Token Counting Fix |
| 159 | + |
| 160 | +- **Problem**: Original used mismatched tokenizers (GPT-2 for GPT-4o) causing incorrect context limits |
| 161 | +- **Solution**: Now uses OpenAI API's actual token statistics from response.prompt_tokens and response.completion_tokens |
| 162 | +- **Impact**: No more false "context exceeded" errors at 13k tokens when limit is 128k |
| 163 | + |
| 164 | +### 2. Context Management |
| 165 | + |
| 166 | +- **Problem**: System would incorrectly truncate messages based on wrong token counts |
| 167 | +- **Solution**: Track actual cumulative API token consumption for accurate context management |
| 168 | +- **Impact**: Model can use full context window effectively |
| 169 | + |
| 170 | +### 3. System Prompt Optimization |
| 171 | + |
| 172 | +- **Problem**: Over-constrained prompt requiring specific tags caused unnatural responses |
| 173 | +- **Solution**: Simplified prompt matching original Tongyi design, letting model reason naturally |
| 174 | +- **Impact**: Better convergence, fewer infinite loops |
| 175 | + |
| 176 | +### 4. Parallel Execution |
| 177 | + |
| 178 | +- \*\*Leverages AgentWorkflowEngine for concurrent task processing |
| 179 | +- \*\*Configurable parallelism (n_parallel_tasks parameter) |
| 180 | +- \*\*Automatic retry on failures |
| 181 | + |
| 182 | +## Evaluation Results |
| 183 | + |
| 184 | +Evaluation results will be added after running benchmarks. The system is designed to evaluate on HLE and other academic benchmarks. |
| 185 | + |
| 186 | +## Known Issues and Limitations |
| 187 | + |
| 188 | +1. **Tool Placeholders**: Scholar and Visit tools need real implementations for research tasks |
| 189 | +2. **Model-Specific Behavior**: |
| 190 | + - Some models may not consistently use `<answer>` tags |
| 191 | + - Tool calling format adherence varies by model |
| 192 | +3. **Long Context Tasks**: Very complex research may still hit token limits |
| 193 | +4. **Judge Accuracy**: LLM judge may not perfectly evaluate complex answers |
| 194 | + |
| 195 | +## Future Improvements |
| 196 | + |
| 197 | +- [ ] Implement real Scholar tool using arXiv/Semantic Scholar APIs |
| 198 | +- [ ] Implement real Visit tool using requests/BeautifulSoup |
| 199 | +- [ ] Add PDF/DOCX parsing to FileParser |
| 200 | +- [ ] Create unified evaluation framework for multiple benchmarks |
| 201 | +- [ ] Add more Tongyi agents (QwenCoder, etc.) |
| 202 | +- [ ] Improve judge accuracy with better prompts |
| 203 | + |
| 204 | +## Project Structure |
| 205 | + |
| 206 | +``` |
| 207 | +examples/deepresearch/ |
| 208 | +├── deepresearch_agent.py # Core ReAct agent implementation |
| 209 | +├── deepresearch_workflow.py # rLLM workflow wrapper |
| 210 | +├── deepresearch_tools.py # Tool implementations |
| 211 | +├── evaluate_hle.py # HLE evaluation script |
| 212 | +├── react_agent_original.py # Original Tongyi reference |
| 213 | +├── tool_*_original.py # Original tool references |
| 214 | +├── hle_outputs/ # Evaluation results (git ignored) |
| 215 | +└── README.md # This file |
| 216 | +``` |
| 217 | + |
| 218 | +## Contributing |
| 219 | + |
| 220 | +To add new tools or improve existing ones: |
| 221 | + |
| 222 | +1. Implement tool in `deepresearch_tools.py` following the pattern: |
| 223 | + |
| 224 | + ```python |
| 225 | + class YourTool(DeepResearchTool): |
| 226 | + async def call(self, **kwargs) -> str: |
| 227 | + # Your implementation |
| 228 | + return result_string |
| 229 | + ``` |
| 230 | + |
| 231 | +2. Add to `DEEPRESEARCH_TOOLS` registry |
| 232 | + |
| 233 | +3. Test with evaluation script |
| 234 | + |
| 235 | +4. Submit PR with test results |
| 236 | + |
| 237 | +## Related Work |
| 238 | + |
| 239 | +This integration is part of the rLLM evaluation framework initiative. See also: |
| 240 | + |
| 241 | +- `examples/strands/` - Strands agent integration |
| 242 | +- `rllm/agents/` - Native rLLM agents |
| 243 | +- `rllm/workflows/` - Workflow base classes |
| 244 | + |
| 245 | +## Citation |
| 246 | + |
| 247 | +If you use this integration, please cite: |
| 248 | + |
| 249 | +```bibtex |
| 250 | +@misc{deepresearch2024, |
| 251 | + title={DeepResearch: Multi-turn Research Agent}, |
| 252 | + author={Alibaba NLP Team}, |
| 253 | + year={2024}, |
| 254 | + url={https://github.com/Alibaba-NLP/DeepResearch} |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +## License |
| 259 | + |
| 260 | +This integration follows rLLM's license. The original DeepResearch implementation is from Alibaba's Tongyi team. |
0 commit comments