天气查询代理

利用LlamaIndex和MCP工具集成,通过OpenWeather API获取实时天气信息的AI代理服务。

  • by teddytesfa
  • Jupyter Notebook
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 teddytesfa/llamaindex-mcp-openweather-agent

使用说明

MCP + LlamaIndex 示例:使用 OpenWeather API 的天气代理

概述

这个项目展示了如何使用 LlamaIndex(注:这是一个专有名词,直接翻译为“Llama索引”可能不够准确,因为“LlamaIndex”本身是一个项目或系统的名称,具体翻译可能需要根据上下文或官方介绍来确定,但在此处保留原样以体现其专有性。) (与)……一起,带着 MCP工具集成 使大型语言模型(LLM)代理能够获取 实时天气信息 使用 开放天气API。

特点/特性

  • 实时天气查询:
    该代理通过MCP服务器动态访问OpenWeather API,能够回答任何基于位置的天气查询。

  • LlamaIndex与MCP集成:
    利用FunctionAgent工作流结合外部工具调用,实现大型语言模型(LLM)代理的灵活扩展和可扩展增强。

  • 完全可定制:
    只需通过暴露新的MCP工具,即可让代理适应任何外部API——展示了快速的AI工程能力。


使用方法

# get the code
git clone https://github.com/teddytesfa/llamaindex-mcp-openweather-agent
cd llamaindex-mcp-openweather-agent

# create a virtual environment
python3 -m venv venv
source venv/bin/activate

# install dependencies
pip install  "mcp>=1.9.1,<2" python-dotenv llama_index llama-index-tools-mcp llama-index-llms-google-genai

cp .env.example .env
# NOTE: edit the .env file to have the correct values!

# run the server
python mcp_server.py --server_type=sse
  • 设置 .env 包含您的OpenAI API密钥和OpenWeather API密钥。

  • 确保一个有效的MCP服务器正在运行并且对外提供服务 fetch_weather 使用OpenWeather API的工具。

  • 运行笔记本(mcp_openweather.ipynb)并互动地提出与天气相关的问题。


示例

用户:亚的斯亚贝巴的天气怎么样? 客服:亚的斯亚贝巴当前天气晴朗,气温为21.7摄氏度,湿度为37%。


作者 泰迪·特斯法 | 人工智能工程师
更多内容请访问:https://github.com/teddytesfa