|
1 | | -# 工具 |
| 1 | +# 内置与扩展工具(lazyllm.tools) |
2 | 2 |
|
3 | | -提供一些内置的工具用于Agent直接调用 |
| 3 | +以下工具均来自 **lazyllm.tools**,可供 Agent 直接或通过 `fc_register` 包装后调用。搜索类从 `lazyllm.tools.tools` 导入,其余从 `lazyllm.tools` 导入。 |
4 | 4 |
|
5 | | -##GoogleSearch |
| 5 | +--- |
6 | 6 |
|
7 | | -通过 Google 搜索指定的关键词。 |
| 7 | +## 搜索类工具 |
| 8 | + |
| 9 | +### GoogleSearch |
| 10 | + |
| 11 | +通过 Google Custom Search API 搜索指定关键词。 |
8 | 12 |
|
9 | 13 | 参数: |
10 | 14 |
|
11 | 15 | - custom_search_api_key (str) – 用户申请的 Google API key。 |
12 | | -- search_engine_id (str) – 用户创建的用于检索的搜索引擎 id。 |
13 | | -- timeout (int, default: 10 ) – 搜索请求的超时时间,单位是秒,默认是 10。 |
14 | | -- proxies (Dict[str, str], default: None ) – 请求时所用的代理服务。格式参考 https://www.python-httpx.org/advanced/proxies。 |
| 16 | +- search_engine_id (str) – 用户创建的搜索引擎 id。 |
| 17 | +- timeout (int, default: 10) – 请求超时时间(秒)。 |
| 18 | +- proxies (Dict[str, str], default: None) – 代理配置。 |
15 | 19 |
|
16 | 20 | ```python |
17 | 21 | from lazyllm.tools.tools import GoogleSearch |
18 | 22 |
|
19 | 23 | key = '<your_google_search_api_key>' |
20 | 24 | cx = '<your_search_engine_id>' |
21 | | - |
22 | 25 | google = GoogleSearch(custom_search_api_key=key, search_engine_id=cx) |
23 | 26 | res = google(query='商汤科技', date_restrict='m1') |
24 | 27 | ``` |
25 | 28 |
|
26 | | -## TencentSearch |
| 29 | +### TencentSearch |
27 | 30 |
|
28 | | -腾讯搜索接口封装类,用于调用腾讯云的内容搜索服务。 |
29 | | -提供对腾讯云搜索API的封装,支持关键词搜索和结果处理。 |
| 31 | +腾讯云内容搜索 API 封装,支持关键词搜索与结果处理。 |
30 | 32 |
|
31 | 33 | 参数: |
32 | 34 |
|
33 | | -- secret_id (str) – 腾讯云API密钥ID,用于身份认证 |
34 | | -- secret_key (str) – 腾讯云API密钥,用于身份认证 |
| 35 | +- secret_id (str) – 腾讯云 API 密钥 ID。 |
| 36 | +- secret_key (str) – 腾讯云 API 密钥。 |
35 | 37 |
|
36 | 38 | ```python |
37 | 39 | from lazyllm.tools.tools import TencentSearch |
38 | | -secret_id = '<your_secret_id>' |
39 | | -secret_key = '<your_secret_key>' |
40 | | -searcher = TencentSearch(secret_id, secret_key) |
| 40 | + |
| 41 | +searcher = TencentSearch(secret_id='<your_secret_id>', secret_key='<your_secret_key>') |
41 | 42 | res = searcher('calculus') |
42 | 43 | ``` |
| 44 | + |
| 45 | +### 其他搜索工具 |
| 46 | + |
| 47 | +| 工具 | 说明 | 导入方式 | |
| 48 | +|------|------|----------| |
| 49 | +| BingSearch | Bing 搜索 | `from lazyllm.tools.tools import BingSearch` | |
| 50 | +| BochaSearch | 博查搜索 API | `from lazyllm.tools.tools import BochaSearch` | |
| 51 | +| WikipediaSearch | 维基百科检索 | `from lazyllm.tools.tools import WikipediaSearch` | |
| 52 | +| ArxivSearch | arXiv 论文检索 | `from lazyllm.tools.tools import ArxivSearch` | |
| 53 | +| StackOverflowSearch | Stack Overflow 检索 | `from lazyllm.tools.tools import StackOverflowSearch` | |
| 54 | +| SemanticScholarSearch | Semantic Scholar 学术检索 | `from lazyllm.tools.tools import SemanticScholarSearch` | |
| 55 | +| GoogleBooksSearch | Google 图书检索 | `from lazyllm.tools.tools import GoogleBooksSearch` | |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 通用工具 |
| 60 | + |
| 61 | +| 工具 | 说明 | 导入方式 | |
| 62 | +|------|------|----------| |
| 63 | +| HttpTool | 发起 HTTP 请求,可封装任意 REST API 供 Agent 调用 | `from lazyllm.tools import HttpTool` | |
| 64 | +| Weather | 天气查询 | `from lazyllm.tools.tools import Weather` | |
| 65 | +| Calculator | 数学计算 | `from lazyllm.tools.tools import Calculator` | |
| 66 | +| JsonExtractor | 从文本中提取 JSON | `from lazyllm.tools.tools import JsonExtractor` | |
| 67 | +| JsonConcentrator | 合并/聚合 JSON 结构 | `from lazyllm.tools.tools import JsonConcentrator` | |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## SQL / 数据工具 |
| 72 | + |
| 73 | +### SqlManager |
| 74 | + |
| 75 | +管理数据库连接与表结构,执行 SQL、获取表信息等,常与 SqlCall 配合实现自然语言转 SQL。 |
| 76 | + |
| 77 | +- 支持 SQLite、PostgreSQL、MySQL 等;远程库需填写 user、password、host、port。 |
| 78 | +- 常用方法: `get_all_tables`, `execute_query`, `execute_commit`, `create_table`, `set_desc` 等。 |
| 79 | + |
| 80 | +```python |
| 81 | +from lazyllm.tools import SqlManager |
| 82 | + |
| 83 | +sql_manager = SqlManager(db_path='/path/to/db.sqlite') # 或指定 user/password/host/port |
| 84 | +tables = sql_manager.get_all_tables() |
| 85 | +result = sql_manager.execute_query('SELECT * FROM t LIMIT 10') |
| 86 | +``` |
| 87 | + |
| 88 | +### SqlCall |
| 89 | + |
| 90 | +将自然语言与表结构信息交给 LLM 生成 SQL,再通过 SqlManager 执行,适合与 ReactAgent 组合为 SQL Agent。 |
| 91 | + |
| 92 | +```python |
| 93 | +from lazyllm.tools import SqlManager, SqlCall, fc_register, ReactAgent |
| 94 | + |
| 95 | +sql_manager = SqlManager(db_path='/path/to/db.sqlite') |
| 96 | +sql_call = SqlCall(llm, sql_manager, use_llm_for_sql_result=False) # 可选参数见 API 文档 |
| 97 | + |
| 98 | +@fc_register('tool') |
| 99 | +def query_db(query: str) -> str: |
| 100 | + return sql_call(query) |
| 101 | + |
| 102 | +agent = ReactAgent(llm, tools=['query_db']) |
| 103 | +agent('查询销售额最高的前 5 个产品') |
| 104 | +``` |
| 105 | + |
| 106 | +更多示例见本目录 [basic.md](basic.md) 与 [agent.md](agent.md),或项目主 docs 内 Cookbook(sql_agent、tabular 等)。 |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## 代码与能力类 |
| 111 | + |
| 112 | +### CodeGenerator |
| 113 | + |
| 114 | +基于 LLM 生成代码,可与 FunctionCallAgent 等组合实现“自然语言 → 代码 → 执行”的代码 Agent。 |
| 115 | + |
| 116 | +```python |
| 117 | +from lazyllm.tools import CodeGenerator, FunctionCallAgent, fc_register |
| 118 | + |
| 119 | +gen = CodeGenerator(llm, prompt='根据用户需求生成可执行代码,只返回代码本身。') |
| 120 | + |
| 121 | +@fc_register('tool') |
| 122 | +def run_code(requirement: str) -> str: |
| 123 | + code = gen(requirement) |
| 124 | + # 可选:在沙箱中执行 code |
| 125 | + return code |
| 126 | + |
| 127 | +agent = FunctionCallAgent(llm, tools=['run_code']) |
| 128 | +agent('写一个函数计算斐波那契数列前 n 项') |
| 129 | +``` |
| 130 | + |
| 131 | +### MCPClient |
| 132 | + |
| 133 | +连接 MCP 服务器(本地或 SSE),获取 MCP 提供的工具列表,可直接作为 Agent 的 tools 使用。用法见 [基础组件 - MCPClient](../assets/agent/basic.md#4-mcpclient)。 |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## 小结 |
| 138 | + |
| 139 | +- **搜索与通用工具**: `lazyllm.tools.tools` 中的 Search、Weather、Calculator、Json*。 |
| 140 | +- **SQL / 数据**: `lazyllm.tools` 的 SqlManager、SqlCall(以及 MongoDBManager、DBManager 等)。 |
| 141 | +- **代码与 MCP**: `lazyllm.tools` 的 CodeGenerator、MCPClient;ToolManager、SkillManager 等见 Agent 模块文档。 |
| 142 | +- **自定义工具**: 使用 `fc_register('tool')` 注册函数,见 [基础组件](../assets/agent/basic.md)。 |
| 143 | + |
| 144 | +更多参数与用法见本页上文及 [references/agent.md](../../references/agent.md)。 |
0 commit comments