跳到主要内容
OpenWeatherMap 提供特定位置的所有基本天气数据
  • 当前天气
  • 未来1小时的分钟预报
  • 未来48小时的逐小时预报
  • 未来8天的每日预报
  • 国家天气警报
  • 40多年前的历史天气数据
本页介绍如何在 LangChain 中使用 OpenWeatherMap API

安装和设置

  • 安装要求:
pip install pyowm
  • 前往 OpenWeatherMap 并注册一个帐户,此处获取您的 API 密钥
  • 将您的 API 密钥设置为 OPENWEATHERMAP_API_KEY 环境变量

包装器

实用工具

存在一个 OpenWeatherMapAPIWrapper 工具,它封装了这个 API。要导入此工具:
from langchain_community.utilities.openweathermap import OpenWeatherMapAPIWrapper
有关此包装器的更详细说明,请参阅此笔记本

工具

您还可以轻松地将此包装器作为工具(与 Agent 一起使用)加载。您可以使用以下方式完成此操作
import os
from langchain_community.utilities import OpenWeatherMapAPIWrapper

os.environ["OPENWEATHERMAP_API_KEY"] = ""
weather = OpenWeatherMapAPIWrapper()
tools = [weather.run]
有关工具的更多信息,请参阅此页面
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.