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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 开源盘古 Embedded-7B-DeepDiver
|
| 2 |
+
中文 | [English](README_EN.md)
|
| 3 |
+
📑[技术报告](https://ai.gitcode.com/ascend-tribe/openPangu-Embedded-7B-DeepDiver/blob/main/docs/openpangu-deepdiver-v2-tech-report.pdf)
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
## 1. 简介
|
| 7 |
+
DeepDiver是openPangu系列中定位深度信息获取与处理的Agent,支持原生 Multi-Agent System(MAS),用于复杂知识问答与长文调研报告写作。
|
| 8 |
+
|
| 9 |
+
### 特性
|
| 10 |
+
- 🔍 支持QA模式:回答100步+复杂知识性问题
|
| 11 |
+
- ✍️ 支持长文写作模式:撰写3w+字文章与报告
|
| 12 |
+
- 🔄 支持自适应模式:根据用户问题自动选择知识问答模式或长文写作模式
|
| 13 |
+
|
| 14 |
+
## 2. 评测结果
|
| 15 |
+
|
| 16 |
+
| 测评集 | 测评指标 | openPangu-7B-DeepDiver|
|
| 17 |
+
| :------------: | :-----------------: | :--------: |
|
| 18 |
+
| **BrowseComp-zh** | Acc | 18.3 |
|
| 19 |
+
| **BrowseComp-en** | Acc | 8.3 |
|
| 20 |
+
|**XBench-DeepSearch** | Acc | 39.0 |
|
| 21 |
+
|
| 22 |
+
注:上表仅展示复杂问答的结果,长文调研的评测结果请参考[技术报告](https://ai.gitcode.com/ascend-tribe/openPangu-Embedded-7B-DeepDiver/blob/main/docs/openpangu-deepdiver-v2-tech-report.pdf)
|
| 23 |
+
|
| 24 |
+
## 3. 快速部署
|
| 25 |
+
|
| 26 |
+
### 3.1 环境准备
|
| 27 |
+
|
| 28 |
+
```bash
|
| 29 |
+
# 克隆并安装
|
| 30 |
+
git clone <repository-url>
|
| 31 |
+
cd deepdiver_v2
|
| 32 |
+
pip install -r requirements.txt
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
### 3.2 部署推理服务
|
| 36 |
+
|
| 37 |
+
#### 拉取镜像
|
| 38 |
+
|
| 39 |
+
```
|
| 40 |
+
docker pull quay.io/ascend/vllm-ascend:v0.9.2rc1
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
或按照[官方文档](https://vllm-ascend.readthedocs.io/en/stable/installation.html)手动构建 docker 容器。
|
| 44 |
+
|
| 45 |
+
#### 运行容器
|
| 46 |
+
|
| 47 |
+
```
|
| 48 |
+
docker run -itd --name vllm-deepdiver \
|
| 49 |
+
--network host \
|
| 50 |
+
--device /dev/davinci0 \
|
| 51 |
+
--device /dev/davinci1 \
|
| 52 |
+
--device /dev/davinci2 \
|
| 53 |
+
--device /dev/davinci3 \
|
| 54 |
+
--device /dev/davinci4 \
|
| 55 |
+
--device /dev/davinci5 \
|
| 56 |
+
--device /dev/davinci6 \
|
| 57 |
+
--device /dev/davinci7 \
|
| 58 |
+
-u root \
|
| 59 |
+
--device /dev/davinci_manager \
|
| 60 |
+
--device /dev/devmm_svm \
|
| 61 |
+
--device /dev/hisi_hdc \
|
| 62 |
+
-v /usr/local/dcmi:/usr/local/dcmi:ro \
|
| 63 |
+
-v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/Ascend/driver/tools/hccn_tool:ro \
|
| 64 |
+
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi:ro \
|
| 65 |
+
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/:ro \
|
| 66 |
+
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info:ro \
|
| 67 |
+
-v /etc/ascend_install.info:/etc/ascend_install.info:ro \
|
| 68 |
+
-v /usr/local/Ascend/firmware:/usr/local/Ascend/firmware:ro \
|
| 69 |
+
-v /data:/data:ro \
|
| 70 |
+
-v /home/work:/home/work \ # 配置一个可读写的工作目录
|
| 71 |
+
quay.io/ascend/vllm-ascend:v0.9.2rc1
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
#### 进入容器
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
```
|
| 78 |
+
docker exec -itu root vllm-deepdiver bash
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
注意:必须使用 `-itu root`。
|
| 82 |
+
|
| 83 |
+
#### 复制 Pangu 的 modeling 文件
|
| 84 |
+
|
| 85 |
+
`open_pangu.py` 和 `__init__.py` 可以在[这里](https://ai.gitcode.com/ascend-tribe/openpangu-embedded-7b-model/tree/main/inference/vllm_ascend/models)找到。
|
| 86 |
+
|
| 87 |
+
```
|
| 88 |
+
cp ./vllm_ascend/open_pangu.py /vllm-workspace/vllm-ascend/vllm_ascend/models/
|
| 89 |
+
cp ./vllm_ascend/__init__.py /vllm-workspace/vllm-ascend/vllm_ascend/models/
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
#### 启动部署
|
| 93 |
+
|
| 94 |
+
```
|
| 95 |
+
PRECHECKPOINT_PATH="path/to/deepdiver_model"
|
| 96 |
+
|
| 97 |
+
export VLLM_USE_V1=1
|
| 98 |
+
|
| 99 |
+
export VLLM_WORKER_MULTIPROC_METHOD=fork
|
| 100 |
+
# export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
|
| 101 |
+
|
| 102 |
+
vllm serve $PRECHECKPOINT_PATH \
|
| 103 |
+
--served-model-name ${SERVED_MODEL_NAME:=pangu_auto} \
|
| 104 |
+
--tensor-parallel-size ${tensor_parallel_size:=8} \
|
| 105 |
+
--trust-remote-code \
|
| 106 |
+
--host 127.0.0.1 \
|
| 107 |
+
--port 8888 \
|
| 108 |
+
--max-num-seqs 256 \
|
| 109 |
+
--max-model-len ${MAX_MODEL_LEN:=131072} \
|
| 110 |
+
--max-num-batched-tokens ${MAX_NUM_BATCHED_TOKENS:=4096} \
|
| 111 |
+
--tokenizer-mode "slow" \
|
| 112 |
+
--dtype bfloat16 \
|
| 113 |
+
--distributed-executor-backend mp \
|
| 114 |
+
--gpu-memory-utilization 0.93 \
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
#### 测试部署
|
| 118 |
+
|
| 119 |
+
```
|
| 120 |
+
curl -X POST http://127.0.0.1:8888/v1/completions -H "Content-Type: application/json" -d '{
|
| 121 |
+
"model": "pangu_auto",
|
| 122 |
+
"prompt": ["Tell me who you are?"],
|
| 123 |
+
"max_tokens": 50
|
| 124 |
+
}'
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
### 3.3 实现所需工具
|
| 128 |
+
|
| 129 |
+
在启动服务器前,你需要为 web search 与 URL 抓取工具实现自定义逻辑。
|
| 130 |
+
|
| 131 |
+
#### Web Search(`_generic_search`)
|
| 132 |
+
|
| 133 |
+
位置:`src/tools/mcp_tools.py` - `_generic_search` 方法
|
| 134 |
+
|
| 135 |
+
将 `NotImplementedError` 替换为你的搜索工具实现:
|
| 136 |
+
|
| 137 |
+
```python
|
| 138 |
+
def _generic_search(self, query: str, max_results: int, config: Dict[str, Any]) -> MCPToolResult:
|
| 139 |
+
"""Your custom search implementation - based on the commented code example"""
|
| 140 |
+
try:
|
| 141 |
+
# Example implementation for search API:
|
| 142 |
+
url = config.get('base_url', 'https://api.search-provider.com/search')
|
| 143 |
+
payload = json.dumps({"q": query, "num": max_results})
|
| 144 |
+
api_keys = config.get('api_keys', [])
|
| 145 |
+
headers = {
|
| 146 |
+
'X-API-KEY': random.choice(api_keys),
|
| 147 |
+
'Content-Type': 'application/json'
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
response = requests.post(url, data=payload, headers=headers)
|
| 151 |
+
response.raise_for_status()
|
| 152 |
+
|
| 153 |
+
# Transform your API response to required format
|
| 154 |
+
search_results = {
|
| 155 |
+
"organic": [
|
| 156 |
+
{
|
| 157 |
+
"title": result["title"],
|
| 158 |
+
"link": result["link"],
|
| 159 |
+
"snippet": result["snippet"],
|
| 160 |
+
"date": result.get("date", "unknown")
|
| 161 |
+
}
|
| 162 |
+
for result in response.json().get("organic", [])
|
| 163 |
+
]
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
return MCPToolResult(success=True, data=search_results)
|
| 167 |
+
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return MCPToolResult(success=False, error=f"Generic search failed: {e}")
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
#### URL Crawler(`url_crawler` 与 `_content_extractor`)
|
| 173 |
+
|
| 174 |
+
位置:`src/tools/mcp_tools.py` - `_content_extractor`
|
| 175 |
+
|
| 176 |
+
将 `NotImplementedError` 部分替换为你的网页抓取工具实现:
|
| 177 |
+
|
| 178 |
+
```python
|
| 179 |
+
# Example implementation for content extractor:
|
| 180 |
+
crawler_url = f"{crawler_config.get('base_url', 'https://api.content-extractor.com')}/{url}"
|
| 181 |
+
response = requests.get(crawler_url, headers=headers, timeout=crawler_config.get('timeout', 30))
|
| 182 |
+
response.raise_for_status()
|
| 183 |
+
|
| 184 |
+
content = response.text
|
| 185 |
+
|
| 186 |
+
# Truncate if needed
|
| 187 |
+
if max_tokens and len(content.split()) > max_tokens:
|
| 188 |
+
words = content.split()[:max_tokens]
|
| 189 |
+
content = ' '.join(words) + '...'
|
| 190 |
+
|
| 191 |
+
return MCPToolResult(success=True, data=content)
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
#### ⚠️ 第三方服务提示
|
| 195 |
+
|
| 196 |
+
重要:搜索与抓取工具使用外部 API 由用户自行选择和实现。我们不对以下情况负责:
|
| 197 |
+
- 与第三方服务相关的隐私/安全问题
|
| 198 |
+
- 搜索/抓取活动的合规性
|
| 199 |
+
- 内容准确性或版权问题
|
| 200 |
+
- API 停机或变更
|
| 201 |
+
|
| 202 |
+
使用这些服务需自担风险。请查看其条款与隐私政策。
|
| 203 |
+
|
| 204 |
+
### 3.4 必要配置
|
| 205 |
+
|
| 206 |
+
#### 配置 .env 文件
|
| 207 |
+
复制 `env.template` 到 `config/.env` 并配置如下选项:
|
| 208 |
+
|
| 209 |
+
```bash
|
| 210 |
+
# LLM Service
|
| 211 |
+
MODEL_REQUEST_URL=http://localhost:8888/v1/chat/completions # 你的 LLM endpoint
|
| 212 |
+
|
| 213 |
+
# Agent 限制
|
| 214 |
+
PLANNER_MODE=auto # 在 auto、writing 或 qa 模式间切换
|
| 215 |
+
|
| 216 |
+
# 外部 API(先实现函数)
|
| 217 |
+
SEARCH_ENGINE_BASE_URL= # 搜索 API endpoint
|
| 218 |
+
SEARCH_ENGINE_API_KEYS= # 搜索 API keys
|
| 219 |
+
URL_CRAWLER_BASE_URL= # URL Crawler API endpoint
|
| 220 |
+
URL_CRAWLER_API_KEYS= # URL Crawler API keys
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
⚠️ 注意:
|
| 224 |
+
- 请将上一步部署的推理服务 URL 配置到 `MODEL_REQUEST_URL`
|
| 225 |
+
- 在 `PLANNER_MODE` 中指定模式。`auto` 会自动决策回答复杂问题或生成长文;若希望优先长文写作,可设置为 `writing`;若希望专注解决高难度问题,可设置为 `qa`
|
| 226 |
+
|
| 227 |
+
### 3.5 启动工具服务
|
| 228 |
+
|
| 229 |
+
```bash
|
| 230 |
+
python src/tools/mcp_server_standard.py
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
### 3.6 运行Demo
|
| 234 |
+
|
| 235 |
+
```bash
|
| 236 |
+
# 交互模式
|
| 237 |
+
python cli/demo.py
|
| 238 |
+
|
| 239 |
+
# 单次查询
|
| 240 |
+
python cli/demo.py -q "$your_query"
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
基于上述步骤可以快速运行DeepDiver,如果需要二次开发,可以参考[章节4](#4-自定义工具开发指南)和[5](#5-个性化配置)
|
| 244 |
+
|
| 245 |
+
## 4. 自定义工具开发指南
|
| 246 |
+
|
| 247 |
+
当前工具主要分为内置工具和外部MCP工具,内部工具主要包括分发任务,思考/反思等,外部MCP工具则是一些延伸LLM能力的工具,如搜索互联网,爬取链接,下载和读写文件等。
|
| 248 |
+
|
| 249 |
+
### 4.1 已实现的工具类别
|
| 250 |
+
|
| 251 |
+
#### A. 外部MCP工具
|
| 252 |
+
Web Search 与数据采集:
|
| 253 |
+
- `batch_web_search`:多查询 web 搜索
|
| 254 |
+
- `url_crawler`:从 URL 抽取内容
|
| 255 |
+
- `download_files`:从 URL 下载文件
|
| 256 |
+
|
| 257 |
+
文件操作:
|
| 258 |
+
- `file_read`、`file_write`:基础文件 I/O
|
| 259 |
+
- `list_workspace`:目录列表
|
| 260 |
+
|
| 261 |
+
文档处理与内容创作:
|
| 262 |
+
- `document_qa`:针对特定文档问答
|
| 263 |
+
- `document_extract`:多格式文本抽取
|
| 264 |
+
- `section_writer`:结构化内容生成
|
| 265 |
+
|
| 266 |
+
#### B. 内置工具
|
| 267 |
+
- `think`、`reflect`:推理与规划
|
| 268 |
+
- `task_done`:任务完成汇报
|
| 269 |
+
- `assign_task_xxx`: 分发任务并创建子智能体
|
| 270 |
+
|
| 271 |
+
### 4.2 开发并集成新的外部MCP工具
|
| 272 |
+
|
| 273 |
+
#### A. 实现新的MCP工具
|
| 274 |
+
位置:`src/tools/mcp_tools.py` - 在 `MCPTools` 类中添加方法
|
| 275 |
+
|
| 276 |
+
```python
|
| 277 |
+
def your_new_tool(self, param1: str, param2: int) -> MCPToolResult:
|
| 278 |
+
"""
|
| 279 |
+
Description of what your tool does.
|
| 280 |
+
|
| 281 |
+
Args:
|
| 282 |
+
param1: Description of parameter 1
|
| 283 |
+
param2: Description of parameter 2
|
| 284 |
+
|
| 285 |
+
Returns:
|
| 286 |
+
MCPToolResult: Standardized result format
|
| 287 |
+
"""
|
| 288 |
+
try:
|
| 289 |
+
# Your tool implementation here
|
| 290 |
+
result_data = {
|
| 291 |
+
"output": "Tool result",
|
| 292 |
+
"processed_items": param2
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
return MCPToolResult(
|
| 296 |
+
success=True,
|
| 297 |
+
data=result_data,
|
| 298 |
+
metadata={"tool_name": "your_new_tool"}
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
except Exception as e:
|
| 302 |
+
logger.error(f"Tool execution failed: {e}")
|
| 303 |
+
return MCPToolResult(
|
| 304 |
+
success=False,
|
| 305 |
+
error=f"Tool failed: {str(e)}"
|
| 306 |
+
)
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
#### B. 在服务器中注册工具
|
| 310 |
+
|
| 311 |
+
##### 添加工具 Schema
|
| 312 |
+
位置:`src/tools/mcp_tools.py` - 添加到 `MCP_TOOL_SCHEMAS` 字典
|
| 313 |
+
|
| 314 |
+
```python
|
| 315 |
+
MCP_TOOL_SCHEMAS = {
|
| 316 |
+
# ... existing tools ...
|
| 317 |
+
|
| 318 |
+
"your_new_tool": {
|
| 319 |
+
"name": "your_new_tool",
|
| 320 |
+
"description": "Brief description of what your tool does",
|
| 321 |
+
"inputSchema": {
|
| 322 |
+
"type": "object",
|
| 323 |
+
"properties": {
|
| 324 |
+
"param1": {
|
| 325 |
+
"type": "string",
|
| 326 |
+
"description": "Description of parameter 1"
|
| 327 |
+
},
|
| 328 |
+
"param2": {
|
| 329 |
+
"type": "integer",
|
| 330 |
+
"default": 10,
|
| 331 |
+
"description": "Description of parameter 2"
|
| 332 |
+
}
|
| 333 |
+
},
|
| 334 |
+
"required": ["param1"]
|
| 335 |
+
}
|
| 336 |
+
}
|
| 337 |
+
}
|
| 338 |
+
```
|
| 339 |
+
|
| 340 |
+
##### 注册工具函数
|
| 341 |
+
位置:`src/tools/mcp_server_standard.py` - 添加到 `get_tool_function()`
|
| 342 |
+
|
| 343 |
+
```python
|
| 344 |
+
def get_tool_function(tool_name: str):
|
| 345 |
+
"""Get the actual function for a tool"""
|
| 346 |
+
tool_map = {
|
| 347 |
+
# ... existing tools ...
|
| 348 |
+
"your_new_tool": lambda tools, **kwargs: tools.your_new_tool(**kwargs),
|
| 349 |
+
}
|
| 350 |
+
return tool_map.get(tool_name)
|
| 351 |
+
```
|
| 352 |
+
|
| 353 |
+
#### C. 让特定智能体可使用工具
|
| 354 |
+
工具对各智能体的可见性由 MCP client 中的预定义工具集控制。
|
| 355 |
+
|
| 356 |
+
位置:`src/tools/mcp_client.py` - 修改各智能体的工具集
|
| 357 |
+
|
| 358 |
+
```python
|
| 359 |
+
# Define which MCP server tools each agent can access
|
| 360 |
+
PLANNER_AGENT_TOOLS = [
|
| 361 |
+
"download_files",
|
| 362 |
+
"document_qa",
|
| 363 |
+
"file_read",
|
| 364 |
+
"file_write",
|
| 365 |
+
"str_replace_based_edit_tool",
|
| 366 |
+
"list_workspace",
|
| 367 |
+
"file_find_by_name",
|
| 368 |
+
"your_new_tool", # Add your new tool here
|
| 369 |
+
]
|
| 370 |
+
|
| 371 |
+
INFORMATION_SEEKER_TOOLS = [
|
| 372 |
+
"batch_web_search",
|
| 373 |
+
"url_crawler",
|
| 374 |
+
"document_extract",
|
| 375 |
+
"document_qa",
|
| 376 |
+
"download_files",
|
| 377 |
+
"file_read",
|
| 378 |
+
"file_write",
|
| 379 |
+
"str_replace_based_edit_tool",
|
| 380 |
+
"list_workspace",
|
| 381 |
+
"file_find_by_name",
|
| 382 |
+
"your_new_tool", # Add your new tool here if needed
|
| 383 |
+
]
|
| 384 |
+
|
| 385 |
+
WRITER_AGENT_TOOLS = [
|
| 386 |
+
"file_read",
|
| 387 |
+
"list_workspace",
|
| 388 |
+
"file_find_by_name",
|
| 389 |
+
"search_result_classifier",
|
| 390 |
+
"section_writer",
|
| 391 |
+
"concat_section_files",
|
| 392 |
+
# Add your tool if the writer agent needs it
|
| 393 |
+
]
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
### 4.3 添加内置智能体工具/函数
|
| 397 |
+
|
| 398 |
+
#### A. 带有真实返回的工具/函数
|
| 399 |
+
DeepDiver中的agent,如planner,集成了`assign_subjective_task_to_writer`, `assign_multi_objective_tasks_to_info_seeker` 等内置函数作为工具, 这类函数除了具体实现之外,还需要使用`_build_agent_specific_tool_schemas()` 添加专属的tool schema。
|
| 400 |
+
|
| 401 |
+
位置:`src/agents/your_agent.py`
|
| 402 |
+
|
| 403 |
+
```python
|
| 404 |
+
def _build_agent_specific_tool_schemas(self) -> List[Dict[str, Any]]:
|
| 405 |
+
"""Add built-in agent functions (not MCP server tools)"""
|
| 406 |
+
|
| 407 |
+
# Get base schemas from MCP server via client
|
| 408 |
+
schemas = super()._build_agent_specific_tool_schemas()
|
| 409 |
+
|
| 410 |
+
# Add agent-specific built-in functions like task assignment, completion reporting
|
| 411 |
+
builtin_functions = [
|
| 412 |
+
{
|
| 413 |
+
"type": "function",
|
| 414 |
+
"function": {
|
| 415 |
+
"name": "agent_specific_task_done",
|
| 416 |
+
"description": "Report task completion for this agent",
|
| 417 |
+
"parameters": {
|
| 418 |
+
"type": "object",
|
| 419 |
+
"properties": {
|
| 420 |
+
"result": {"type": "string", "description": "Task result"},
|
| 421 |
+
"status": {"type": "string", "description": "Completion status"}
|
| 422 |
+
},
|
| 423 |
+
"required": ["result", "status"]
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
]
|
| 428 |
+
|
| 429 |
+
schemas.extend(builtin_functions)
|
| 430 |
+
return schemas
|
| 431 |
+
```
|
| 432 |
+
|
| 433 |
+
#### B. 带有伪返回的内置工具
|
| 434 |
+
DeepDiver中的cognitive tools,比如think和reflect等,这些工具实际没有具体实现,agent在调用这些工具时通过生成工具入参,就已经完成了工具的调用。可以直接在模型生成完入参后,使用类似以下方法进行返回,继续让模型完成后续工作 (参考`planner_agent.py` 中`_execute_react_loop()`的实现):
|
| 435 |
+
|
| 436 |
+
```python
|
| 437 |
+
if tool_call["name"] in ["think", "reflect"]:
|
| 438 |
+
tool_result = {"tool_results": "You can proceed to invoke other tools if needed. "}
|
| 439 |
+
```
|
| 440 |
+
|
| 441 |
+
同理,这种内置工具也需要使用`_build_agent_specific_tool_schemas()` 添加专属的tool schema。
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
## 5. 个性化配置
|
| 445 |
+
|
| 446 |
+
### 5.1 Client 配置
|
| 447 |
+
|
| 448 |
+
复制 `env.template` 到 `config/.env` 并配置如下选项:
|
| 449 |
+
|
| 450 |
+
```bash
|
| 451 |
+
# LLM Service
|
| 452 |
+
MODEL_REQUEST_URL=http://localhost:8000 # 你的 LLM endpoint
|
| 453 |
+
MODEL_REQUEST_TOKEN=your-token # LLM auth token
|
| 454 |
+
MODEL_NAME=pangu_auto # 模型名
|
| 455 |
+
MODEL_TEMPERATURE=0.3 # 随机度(0.0-1.0)
|
| 456 |
+
MODEL_MAX_TOKENS=8192 # 最大回复长度
|
| 457 |
+
MODEL_REQUEST_TIMEOUT=60 # 请求超时(秒)
|
| 458 |
+
|
| 459 |
+
# Agent 限制
|
| 460 |
+
PLANNER_MAX_ITERATION=40 # Planner 最大 ReAct 步数
|
| 461 |
+
INFORMATION_SEEKER_MAX_ITERATION=30 # 信息搜集最大 ReAct 步数
|
| 462 |
+
WRITER_MAX_ITERATION=40 # Writer 最大 ReAct 步数
|
| 463 |
+
PLANNER_MODE=auto # auto / 长文优先 / qa 优先
|
| 464 |
+
|
| 465 |
+
# MCP Server
|
| 466 |
+
MCP_SERVER_URL=http://localhost:6274/mcp # MCP server endpoint
|
| 467 |
+
MCP_USE_STDIO=false # 使用 stdio 或 HTTP
|
| 468 |
+
|
| 469 |
+
# 外部 API(先实现函数)
|
| 470 |
+
SEARCH_ENGINE_BASE_URL= # 搜索 API endpoint
|
| 471 |
+
SEARCH_ENGINE_API_KEYS= # 搜索 API keys
|
| 472 |
+
URL_CRAWLER_BASE_URL= # URL Crawler API endpoint
|
| 473 |
+
URL_CRAWLER_API_KEYS= # URL Crawler API keys
|
| 474 |
+
URL_CRAWLER_MAX_TOKENS=100000 # URL Crawler 内容最大长度
|
| 475 |
+
|
| 476 |
+
# 存储路径
|
| 477 |
+
TRAJECTORY_STORAGE_PATH=./workspace # Agent工作目录
|
| 478 |
+
REPORT_OUTPUT_PATH=./report # 报告输出目录
|
| 479 |
+
DOCUMENT_ANALYSIS_PATH=./doc_analysis # 文档分析目录
|
| 480 |
+
|
| 481 |
+
# 系统
|
| 482 |
+
DEBUG_MODE=false # 是否开启调试日志
|
| 483 |
+
MAX_RETRIES=3 # API 重试次数
|
| 484 |
+
TIMEOUT=30 # 通用超时(秒)
|
| 485 |
+
```
|
| 486 |
+
|
| 487 |
+
### 5.2 Server 配置(server_config.yaml)
|
| 488 |
+
|
| 489 |
+
`server_config.yaml` 控制服务器行为、工具限流与运行设置:
|
| 490 |
+
|
| 491 |
+
#### 核心服务器设置
|
| 492 |
+
|
| 493 |
+
```yaml
|
| 494 |
+
server:
|
| 495 |
+
host: "127.0.0.1" # 服务器绑定地址
|
| 496 |
+
port: 6274 # 端口
|
| 497 |
+
debug_mode: false # 调试日志
|
| 498 |
+
session_ttl_seconds: 21600 # 会话过期(6小时)
|
| 499 |
+
max_sessions: 1000 # 并发会话上限
|
| 500 |
+
```
|
| 501 |
+
|
| 502 |
+
#### 工具限流
|
| 503 |
+
|
| 504 |
+
对所有会话的外部 API 使用进行控制:
|
| 505 |
+
|
| 506 |
+
```yaml
|
| 507 |
+
tool_rate_limits:
|
| 508 |
+
batch_web_search:
|
| 509 |
+
requests_per_minute: 9000 # 每分钟限制
|
| 510 |
+
burst_limit: 35 # 短时突发
|
| 511 |
+
|
| 512 |
+
url_crawler:
|
| 513 |
+
requests_per_minute: 9000
|
| 514 |
+
burst_limit: 60
|
| 515 |
+
```
|
| 516 |
+
|
| 517 |
+
#### 会话管理
|
| 518 |
+
|
| 519 |
+
```yaml
|
| 520 |
+
server:
|
| 521 |
+
cleanup_interval_seconds: 600 # 清理过期会话(5分钟)
|
| 522 |
+
enable_session_keepalive: true # 长时操作期间保活
|
| 523 |
+
keepalive_touch_interval: 300 # 保活触发间隔(秒)
|
| 524 |
+
```
|
| 525 |
+
|
| 526 |
+
#### 安全与性能
|
| 527 |
+
|
| 528 |
+
```yaml
|
| 529 |
+
server:
|
| 530 |
+
request_timeout_seconds: 1800 # 请求超时
|
| 531 |
+
max_request_size_mb: 1000 # 最大请求体
|
| 532 |
+
rate_limit_requests_per_minute: 300000 # 每 IP 限流
|
| 533 |
+
```
|
| 534 |
+
|
| 535 |
+
配置文件包含对每项设置的详细注释。请根据你的部署需求与外部 API 限额进行调整。
|
| 536 |
+
|
| 537 |
+
## 6. 模型许可证
|
| 538 |
+
|
| 539 |
+
除文件中对开源许可证另有约定外,openPangu-Embedded-7B-DeepDiver 模型根据 OPENPANGU MODEL LICENSE AGREEMENT VERSION 1.0 授权,旨在允许使用并促进人工智能技术的进一步发展。有关详细信息,请参阅模型存储库根目录中的 [LICENSE](LICENSE) 文件。
|
| 540 |
+
|
| 541 |
+
## 7. 安全提示与免责声明
|
| 542 |
+
由于 openPangu-Embedded-7B-DeepDiver 模型和框架所依赖的技术固有的技术限制,以及人工智能生成的内容是由盘古自动生成的,华为无法对以下事项做出任何保证:
|
| 543 |
+
|
| 544 |
+
- 尽管该模型的输出由 AI 算法生成,但不能排除某些信息可能存在缺陷、不合理或引起不适的可能性,生成的内容不代表华为的态度或立场;
|
| 545 |
+
- 无法保证该模型 100% 准确、可靠、功能齐全、及时、安全、无错误、不间断、持续稳定或无任何故障;
|
| 546 |
+
- 该模型的输出内容不构成任何建议或决策,也不保证生成的内容的真实性、完整性、准确性、及时性、合法性、功能性或实用性。生成的内容不能替代医疗、法律等领域的专业人士回答您的问题。生成的内容仅供参考,不代表华为的任何态度、立场或观点。您需要根据实际情况做出独立判断,华为不承担任何责任;
|
| 547 |
+
- DeepDiver MAS系统的组件间通信不包含内置的数据加密或认证(如 tokens、签名)。你需要自行评估安全需求并实施相应防护(例如运行在加密网络中、加入 SSL/TLS、强制组件身份校验);
|
| 548 |
+
- 由于缺乏加密/认证导致的任何安全事件(数据泄露、未授权访问、业务损失)由使用方自行承担。项目开发者不承担责任。
|
| 549 |
+
|
| 550 |
+
## 8. 反馈
|
| 551 |
+
|
| 552 |
+
如果有任何意见和建议,请提交issue或联系 openPangu@huawei.com。
|
| 553 |
+
|
| 554 |
+
---
|