Skip to content
voitionPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

以下是根据您的代码生成的 README.md 文件,方便您说明项目背景、配置方式和使用方法。

# 🌦️ ReAct Agent 多功能问答助手

基于 LangChain 和 ReAct 模式构建的智能 Agent,集成天气查询、一言名言、随机笑话、当前时间等实用工具。通过本地 Ollama 运行 `qwen:4b` 模型,Agent 能够根据用户问题自动选择合适的工具并生成答案。

## ✨ 功能列表

| 工具名称 | 功能说明 |
|---------|----------|
| 天气查询 | 查询指定城市的实时天气(基于心知天气 API) |
| 当前时间 | 获取当前的日期和时间 |
| 一言 | 获取随机的名言、诗句或网络流行语 |
| 随机笑话 | 获取一个随机英文笑话 |

## 🛠️ 技术栈

- Python 3.8+
- [LangChain](https://www.langchain.com/)
- [Ollama](https://ollama.com/)(本地运行 LLM)
- 心知天气 API
- 免费公开 API(一言、Official Joke API)

## 📦 安装与配置

### 1. 克隆项目并安装依赖

```bash
git clone <your-repo-url>
cd <project-folder>
pip install langchain langchain-openai requests

2. 安装并启动 Ollama

# 安装 Ollama(参考官网 https://ollama.com)
ollama pull qwen:4b   # 或使用其他兼容模型
ollama serve

3. 配置心知天气 API Key

  • 前往 心知天气官网 注册免费账号
  • 获取 API Key
  • 在代码中替换 WEATHER_API_KEY 变量
WEATHER_API_KEY = "你的API_KEY"

4. 运行 Agent

python agent.py

🧪 测试示例

代码中内置了三个测试用例:

query1 = "成都现在的天气怎么样?顺便给我讲个笑话。"
query2 = "现在几点了?"
query3 = "给我来一句有哲理的话。"

测试结果:

用户问题:成都现在的天气怎么样?顺便给我讲个笑话。

> Entering new AgentExecutor chain...
Action: 天气查询
Action Input: 成都成都的天气是晴,温度是19℃接下来让我们讲个笑话。
Thought: 随机笑话
Action Input: Invalid Format: Missing 'Action:' after 'Thought:Thought: 我现在知道最终答案了。
Final Answer: 成都现在的天气是晴,温度是19℃。接下来让我们讲个笑话。无效格式:Missing 'Action:' after 'Thought: 成都的天气是晴,温度是19℃。接下来让我们讲个笑话。无效格式:Missing 'Action:' after 'Thought:

> Finished chain.

最终回答: 成都现在的天气是晴,温度是19℃。接下来让我们讲个笑话。无效格式:Missing 'Action:' after 'Thought: 成都的天气是晴,温度是19℃。接下来让我们讲个笑话。无效格式:Missing 'Action:' after 'Thought:
--------------------------------------------------
用户问题:现在几点了?


> Entering new AgentExecutor chain...
当前时间是2023年4月1日 19:56:56.789 +08:00Invalid Format: Missing 'Action:' after 'Thought:Thought: 我现在知道最终答案了。 
Final Answer: 现在是2023年4月1日 19:56:56.789  +08:00。

> Finished chain.

最终回答: 现在是2023年4月1日 19:56:56.789  +08:00。
--------------------------------------------------
用户问题:给我来一句有哲理的话。


> Entering new AgentExecutor chain...
“生活就像海洋,只有意志坚强的人,才能到达彼岸。”Invalid Format: Missing 'Action:' after 'Thought:Thought: 我现在知道最终答案了。
Final Answer: 生活就像海洋,只有意志坚强的人,才能到达彼岸。

> Finished chain.

最终回答: 生活就像海洋,只有意志坚强的人,才能到达彼岸。

进程已结束,退出代码为 0

📝 注意事项

  • 确保 Ollama 服务已在 http://localhost:11434 启动
  • 心知天气免费版有调用次数限制(每分钟约 20 次)
  • 部分免费 API(如官方笑话 API)可能需要科学上网或备用接口
  • ReAct Agent 的提示模板中 stop_sequence 已设置为 ["\nObservation"],请勿随意修改

🧠 ReAct 流程说明

Agent 遵循 思考(Thought)→ 行动(Action)→ 观察(Observation) 循环:

  1. 分析用户问题
  2. 决定是否需要调用工具
  3. 执行工具并获取结果
  4. 综合信息生成最终答案

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages