Skip to content

Commit 827bc07

Browse files
suluyanasuluyangemini-code-assist[bot]
authored
Feat/search sirchmunk (#882)
Co-authored-by: suluyan <suluyan.sly@alibaba-inc.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent de58fa5 commit 827bc07

File tree

13 files changed

+1049
-70
lines changed

13 files changed

+1049
-70
lines changed

docs/en/Components/Config.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,20 @@ In addition to yaml configuration, MS-Agent also supports several additional com
166166
```
167167

168168
> Any configuration in agent.yaml can be passed in with new values via command line, and also supports reading from environment variables with the same name (case insensitive), for example `--llm.modelscope_api_key xxx-xxx`.
169+
170+
- knowledge_search_paths: Knowledge search paths, comma-separated multiple paths. When provided, automatically enables SirchmunkSearch for knowledge retrieval, with LLM configuration automatically inherited from the `llm` module.
171+
172+
### Quick Start for Knowledge Search
173+
174+
Use the `--knowledge_search_paths` parameter to quickly enable knowledge search based on local documents:
175+
176+
```bash
177+
# Using default agent.yaml configuration, automatically reuses LLM settings
178+
ms-agent run --query "How to implement user authentication?" --knowledge_search_paths "./src,./docs"
179+
180+
# Specify configuration file
181+
ms-agent run --config /path/to/agent.yaml --query "your question" --knowledge_search_paths "/path/to/docs"
182+
```
183+
184+
LLM-related parameters (api_key, base_url, model) are automatically inherited from the `llm` module in the configuration file, no need to configure them repeatedly.
185+
If you need to use independent LLM configuration in the `knowledge_search` module, you can explicitly configure `knowledge_search.llm_api_key` and other parameters in the yaml.

docs/zh/Components/config.md

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
slug: config
33
title: 配置与参数
4-
description: Ms-Agent 配置与参数:类型配置、自定义代码、LLM配置、推理配置、system和query、callbacks、工具配置、其他、config_handler、命令行配置
4+
description: Ms-Agent 配置与参数:类型配置、自定义代码、LLM 配置、推理配置、system 和 query、callbacks、工具配置、其他、config_handler、命令行配置
55
---
66

77
# 配置与参数
88

9-
MS-Agent使用一个yaml文件进行配置管理,通常这个文件被命名为`agent.yaml`,这样的设计使不同场景可以读取不同的配置文件。该文件具体包含的字段有:
9+
MS-Agent 使用一个 yaml 文件进行配置管理,通常这个文件被命名为 `agent.yaml`,这样的设计使不同场景可以读取不同的配置文件。该文件具体包含的字段有:
1010

1111
## 类型配置
1212

@@ -17,31 +17,31 @@ MS-Agent使用一个yaml文件进行配置管理,通常这个文件被命名
1717
type: llmagent
1818
```
1919
20-
标识本配置对应的agent类型,支持`llmagent``codeagent`两类。默认为`llmagent`。如果yaml中包含了code_file字段,则code_file优先生效
20+
标识本配置对应的 agent 类型,支持 `llmagent``codeagent` 两类。默认为 `llmagent`。如果 yaml 中包含了 code_file 字段,则 code_file 优先生效
2121

2222
## 自定义代码
2323

24-
> 可选,在需要自定义LLMAgent时使用
24+
> 可选,在需要自定义 LLMAgent 时使用
2525

2626
```yaml
2727
code_file: custom_agent
2828
```
2929

30-
可以使用一个外部agent类,该类需要继承自`LLMAgent`。可以复写其中的若干方法,如果code_file有值,则`type`字段不生效。
30+
可以使用一个外部 agent 类,该类需要继承自 `LLMAgent`。可以复写其中的若干方法,如果 code_file 有值,则 `type` 字段不生效。
3131

32-
## LLM配置
32+
## LLM 配置
3333

3434
> 必须存在
3535

3636
```yaml
3737
llm:
38-
# 大模型服务backend
38+
# 大模型服务 backend
3939
service: modelscope
40-
# 模型id
40+
# 模型 id
4141
model: Qwen/Qwen3-235B-A22B-Instruct-2507
42-
# 模型api_key
42+
# 模型 api_key
4343
modelscope_api_key:
44-
# 模型base_url
44+
# 模型 base_url
4545
modelscope_base_url: https://api-inference.modelscope.cn/v1
4646
```
4747

@@ -51,7 +51,7 @@ llm:
5151

5252
```yaml
5353
generation_config:
54-
# 下面的字段均为OpenAI sdk的标准参数,你也可以配置OpenAI支持的其他参数在这里
54+
# 下面的字段均为 OpenAI sdk 的标准参数,你也可以配置 OpenAI 支持的其他参数在这里
5555
top_p: 0.6
5656
temperature: 0.2
5757
top_k: 20
@@ -60,25 +60,25 @@ generation_config:
6060
enable_thinking: false
6161
```
6262

63-
## system和query
63+
## system 和 query
6464

65-
> 可选,但推荐传入system
65+
> 可选,但推荐传入 system
6666

6767
```yaml
6868
prompt:
69-
# LLM system,如果不传递则使用默认的`you are a helpful assistant.`
69+
# LLM system,如果不传递则使用默认的 `you are a helpful assistant.`
7070
system:
71-
# LLM初始query,通常来说可以不使用
71+
# LLM 初始 query,通常来说可以不使用
7272
query:
7373
```
7474
7575
## callbacks
7676
77-
> 可选,推荐自定义callbacks
77+
> 可选,推荐自定义 callbacks
7878
7979
```yaml
8080
callbacks:
81-
# 用户输入callback,该callback在assistant回复后自动等待用户输入
81+
# 用户输入 callback,该 callback 在 assistant 回复后自动等待用户输入
8282
- input_callback
8383
```
8484
@@ -90,9 +90,9 @@ callbacks:
9090
tools:
9191
# 工具名称
9292
file_system:
93-
# 是否是mcp
93+
# 是否是 mcp
9494
mcp: false
95-
# 排除的function,可以为空
95+
# 排除的 function,可以为空
9696
exclude:
9797
- create_directory
9898
- write_file
@@ -104,20 +104,20 @@ tools:
104104
- map_geo
105105
```
106106
107-
支持的完整工具列表,以及自定义工具请参考[这里](./tools)
107+
支持的完整工具列表,以及自定义工具请参考 [这里](./tools)
108108
109109
## 其他
110110
111111
> 可选,按需配置
112112
113113
```yaml
114-
# 自动对话轮数,默认为20轮
114+
# 自动对话轮数,默认为 20 轮
115115
max_chat_round: 9999
116116

117117
# 工具调用超时时间,单位秒
118118
tool_call_timeout: 30000
119119

120-
# 输出artifact目录
120+
# 输出 artifact 目录
121121
output_dir: output
122122

123123
# 帮助信息,通常在运行错误后出现
@@ -127,13 +127,13 @@ help: |
127127
128128
## config_handler
129129
130-
为了便于在任务开始时对config进行定制化,MS-Agent构建了一个名为`ConfigLifecycleHandler`的机制。这是一个callback类,开发者可以在yaml文件中增加这样一个配置
130+
为了便于在任务开始时对 config 进行定制化,MS-Agent 构建了一个名为 `ConfigLifecycleHandler` 的机制。这是一个 callback 类,开发者可以在 yaml 文件中增加这样一个配置
131131

132132
```yaml
133133
handler: custom_handler
134134
```
135135

136-
这代表和yaml文件同级有一个custom_handler.py文件,该文件的类继承自`ConfigLifecycleHandler`,分别有两个方法:
136+
这代表和 yaml 文件同级有一个 custom_handler.py 文件,该文件的类继承自 `ConfigLifecycleHandler`,分别有两个方法:
137137

138138
```python
139139
def task_begin(self, config: DictConfig, tag: str) -> DictConfig:
@@ -143,18 +143,18 @@ handler: custom_handler
143143
return config
144144
```
145145

146-
`task_begin`在LLMAgent类构造时生效,在该方法中可以对config进行一些修改。如果你的工作流中下游任务会继承上游的yaml配置,这个机制会有帮助。值得注意的是`tag`参数,该参数会传入当前LLMAgent的名字,方便分辨当前工作流的节点。
146+
`task_begin` 在 LLMAgent 类构造时生效,在该方法中可以对 config 进行一些修改。如果你的工作流中下游任务会继承上游的 yaml 配置,这个机制会有帮助。值得注意的是 `tag` 参数,该参数会传入当前 LLMAgent 的名字,方便分辨当前工作流的节点。
147147

148148

149149
## 命令行配置
150150

151-
在yaml配置之外,MS-Agent还支持若干额外的命令行参数
151+
在 yaml 配置之外,MS-Agent 还支持若干额外的命令行参数
152152

153-
- query: 初始query,这个query的优先级高于yaml中的prompt.query
154-
- config: 配置文件路径,支持modelscope model-id
155-
- trust_remote_code: 是否信任外部代码。如果某个配置包含了一些外部代码,需要将这个参数置为true才会生效
156-
- load_cache: 从历史messages继续对话。cache会被自动存储在`output`配置中。默认为`False`
157-
- mcp_server_file: 可以读取一个外部的mcp工具配置,格式为:
153+
- query: 初始 query,这个 query 的优先级高于 yaml 中的 prompt.query
154+
- config: 配置文件路径,支持 modelscope model-id
155+
- trust_remote_code: 是否信任外部代码。如果某个配置包含了一些外部代码,需要将这个参数置为 true 才会生效
156+
- load_cache: 从历史 messages 继续对话。cache 会被自动存储在 `output` 配置中。默认为 `False`
157+
- mcp_server_file: 可以读取一个外部的 mcp 工具配置,格式为:
158158
```json
159159
{
160160
"mcpServers": {
@@ -165,5 +165,21 @@ handler: custom_handler
165165
}
166166
}
167167
```
168+
- knowledge_search_paths: 知识搜索路径,逗号分隔的多个路径。传入后会自动启用 SirchmunkSearch 进行知识检索,LLM 配置自动从 `llm` 模块复用
168169

169-
> agent.yaml中的任意一个配置,都可以使用命令行传入新的值, 也支持从同名(大小写不敏感)环境变量中读取,例如`--llm.modelscope_api_key xxx-xxx`。
170+
> agent.yaml 中的任意一个配置,都可以使用命令行传入新的值,也支持从同名(大小写不敏感)环境变量中读取,例如 `--llm.modelscope_api_key xxx-xxx`。
171+
172+
### 知识搜索快速使用
173+
174+
通过 `--knowledge_search_paths` 参数,可以快速启用基于本地文档的知识搜索:
175+
176+
```bash
177+
# 使用默认 agent.yaml 配置,自动复用 LLM 设置
178+
ms-agent run --query "如何实现用户认证?" --knowledge_search_paths "./src,./docs"
179+
180+
# 指定配置文件
181+
ms-agent run --config /path/to/agent.yaml --query "你的问题" --knowledge_search_paths "/path/to/docs"
182+
```
183+
184+
LLM 相关参数(api_key, base_url, model)会自动从配置文件的 `llm` 模块继承,无需重复配置。
185+
如果需要在 `knowledge_search` 模块中使用独立的 LLM 配置,可以在 yaml 中显式配置 `knowledge_search.llm_api_key` 等参数。
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Sirchmunk Knowledge Search 配置示例
2+
# Sirchmunk Knowledge Search Configuration Example
3+
4+
# 在您的 agent.yaml 或 workflow.yaml 中添加以下配置:
5+
6+
llm:
7+
service: modelscope
8+
model: Qwen/Qwen3-235B-A22B-Instruct-2507
9+
modelscope_api_key: <your-api-key>
10+
modelscope_base_url: https://api-inference.modelscope.cn/v1
11+
12+
generation_config:
13+
temperature: 0.3
14+
top_k: 20
15+
stream: true
16+
17+
# Knowledge Search 配置(可选)
18+
# 用于在本地代码库中搜索相关信息
19+
knowledge_search:
20+
# 必选:要搜索的路径列表
21+
paths:
22+
- ./src
23+
- ./docs
24+
25+
# 可选:sirchmunk 工作目录,用于缓存
26+
work_path: ./.sirchmunk
27+
28+
# 可选:LLM 配置(如不配置则使用上面 llm 的配置)
29+
llm_api_key: <your-api-key>
30+
llm_base_url: https://api.openai.com/v1
31+
llm_model_name: gpt-4o-mini
32+
33+
# 可选:Embedding 模型
34+
embedding_model: text-embedding-3-small
35+
36+
# 可选:聚类相似度阈值
37+
cluster_sim_threshold: 0.85
38+
39+
# 可选:聚类 TopK
40+
cluster_sim_top_k: 3
41+
42+
# 可选:是否重用之前的知识
43+
reuse_knowledge: true
44+
45+
# 可选:搜索模式 (DEEP, FAST, FILENAME_ONLY)
46+
mode: FAST
47+
48+
# 可选:最大循环次数
49+
max_loops: 10
50+
51+
# 可选:最大 token 预算
52+
max_token_budget: 128000
53+
54+
prompt:
55+
system: |
56+
You are an assistant that helps me complete tasks.
57+
58+
max_chat_round: 9999
59+
60+
# 使用说明:
61+
# 1. 配置 knowledge_search 后,LLMAgent 会在处理用户请求时自动搜索本地代码库
62+
# 2. 搜索结果会自动添加到 user message 的 search_result 和 searching_detail 字段
63+
# 3. search_result 包含搜索到的相关文档,会作为上下文提供给 LLM
64+
# 4. searching_detail 包含搜索日志和元数据,可用于前端展示
65+
#
66+
# Python 使用示例:
67+
# ```python
68+
# from ms_agent import LLMAgent
69+
# from ms_agent.config import Config
70+
#
71+
# config = Config.from_task('path/to/agent.yaml')
72+
# agent = LLMAgent(config=config)
73+
# result = await agent.run('如何实现用户认证功能?')
74+
#
75+
# # 获取搜索详情(用于前端展示)
76+
# for msg in result:
77+
# if msg.role == 'user':
78+
# print(f"Search logs: {msg.searching_detail}")
79+
# print(f"Search results: {msg.search_result}")
80+
# ```
81+
#
82+
# CLI 测试命令:
83+
# export LLM_API_KEY="your-api-key"
84+
# export LLM_BASE_URL="https://api.openai.com/v1"
85+
# export LLM_MODEL_NAME="gpt-4o-mini"
86+
# python tests/knowledge_search/test_cli.py --query "你的问题"

0 commit comments

Comments
 (0)