feat(Agent): 新增维基百科搜索和温度查询工具并实现web界面

- 添加search_wikipedia和get_current_temperature工具函数
- 实现基于Streamlit的web交互界面
- 更新requirements.txt添加相关依赖
- 修复PROMPT_TEMPLATE变量名拼写错误
- 移除不再使用的工具函数
- 添加web界面截图到文档
This commit is contained in:
KMnO4-zx
2025-06-20 12:14:19 +08:00
parent cdf10fea16
commit 28636a0f9b
10 changed files with 249 additions and 41 deletions

View File

@@ -17,7 +17,6 @@
2. **工具使用评测集**
- **BFCL V2**:用于评测模型在复杂工具使用任务中的表现,特别是在执行多步骤操作时的正确性和效率。这些任务通常涉及与数据库交互或执行特定指令,以模拟实际工具使用场景。
- **Nexus**:用于测试模型在多步骤操作中的工具使用能力,主要评估其在多任务操作中的协调性和任务管理能力,如进行文件操作、数据整合等复杂流程。
3. **数学评测集**
- **GSM8K**GSM8K是一个包含小学数学问题的数据集用于测试模型的数学推理和逻辑分析能力。具体任务包括算术运算、简单方程求解、数字推理等。GSM8K中的问题虽然看似简单但模型需要理解问题语义并进行正确的数学运算体现了逻辑推理和语言理解的双重挑战。
@@ -481,29 +480,6 @@ def get_current_datetime() -> str:
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
return formatted_datetime
def add(a: float, b: float):
"""
计算两个浮点数的和。
:param a: 第一个浮点数。
:param b: 第二个浮点数。
:return: 两个浮点数的和。
"""
return str(a + b)
def compare(a: float, b: float):
"""
比较两个浮点数的大小。
:param a: 第一个浮点数。
:param b: 第二个浮点数。
:return: 比较结果的字符串表示。
"""
if a > b:
return f'{a} is greater than {b}'
elif a < b:
return f'{b} is greater than {a}'
else:
return f'{a} is equal to {b}'
def count_letter_in_string(a: str, b: str):
"""
统计字符串中某个字母的出现次数。
@@ -513,6 +489,28 @@ def count_letter_in_string(a: str, b: str):
"""
return str(a.count(b))
def search_wikipedia(query: str) -> str:
"""
在维基百科中搜索指定查询的前三个页面摘要。
:param query: 要搜索的查询字符串。
:return: 包含前三个页面摘要的字符串。
"""
page_titles = wikipedia.search(query)
summaries = []
for page_title in page_titles[: 3]: # 取前三个页面标题
try:
# 使用 wikipedia 模块的 page 函数,获取指定标题的维基百科页面对象。
wiki_page = wikipedia.page(title=page_title, auto_suggest=False)
# 获取页面摘要
summaries.append(f"页面: {page_title}\n摘要: {wiki_page.summary}")
except (
wikipedia.exceptions.PageError,
wikipedia.exceptions.DisambiguationError,
):
pass
if not summaries:
return "维基百科没有搜索到合适的结果"
return "\n\n".join(summaries)
# ... (可能还有其他工具函数)
```
@@ -552,7 +550,7 @@ from utils import function_to_json
# 导入定义好的工具函数
from tools import get_current_datetime, add, compare, count_letter_in_string
SYSREM_PROMPT = """
SYSTEM_PROMPT = """
你是一个叫不要葱姜蒜的人工智能助手。你的输出应该与用户的语言保持一致。
当用户的问题需要调用工具时,你可以从提供的工具列表中调用适当的工具函数。
"""
@@ -669,6 +667,11 @@ if __name__ == "__main__":
运行 `python demo.py`你可以开始提问。如果问题需要调用工具Agent 会自动处理。
<div align='center'>
<img src="https://raw.githubusercontent.com/datawhalechina/happy-llm/main/docs/images/7-images/7-3-streamlit-demo.png" alt="alt text" width="80%">
<p>图7.10 Agent 工作流程</p>
</div>
**示例交互:**
```bash
@@ -691,6 +694,10 @@ Assistant: 当前的时间是2025年4月26日17:01:33。不过我注意到
User: exit
```
另外,我们也准备了一份可以展示的 Streamlit 应用,可以运行在本地,展示 Agent 的功能。`streamlit run web_demo.py` 来运行,以下为 Agent 运行效果。
**参考文献**
[1] Hugging Face. (2023). *Open LLM Leaderboard: 开源大语言模型基准测试平台*. https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard