使用 LangGraph + Ollama + llama3.2 构建一个本地运行的 AI Weather Agent,无需任何 API Key。
| 工具 | 版本要求 |
|---|---|
| Python | 3.10+ 推荐(3.9 可用) |
| macOS | Homebrew 已安装 |
| 磁盘空间 | llama3.2 约 2GB |
Ollama 是本地运行大模型的工具,类似于 Docker,但专门用于 LLM。
brew install ollamaollama serve
⚠️ 保持此终端窗口开启,Ollama 服务需要持续运行。
你应该看到类似输出:
Ollama is running on http://127.0.0.1:11434
ollama pull llama3.2📦 llama3.2 约 2GB,首次下载需要等待几分钟。
ollama list预期输出:
NAME ID SIZE MODIFIED
llama3.2:latest ... 2.0 GB ...
pip install langgraph langchain-ollama langchain-core| 包 | 作用 |
|---|---|
langgraph |
提供 create_react_agent,构建 Agent 核心逻辑 |
langchain-ollama |
LangChain 对 Ollama 模型的封装 |
langchain-core |
@tool 装饰器、消息类型等基础组件 |
💡 如果看到
NotOpenSSLWarning警告,可以用pip install "urllib3<2"消除,不影响运行。
新建文件 weather_agent.py,写入以下代码:
from langchain_ollama import ChatOllama
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
# 1. 定义工具
# @tool 装饰器会将函数注册为 Agent 可调用的工具
# docstring 会作为工具描述传给模型,写清楚很重要
@tool
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"Return the weather in {city} in a joke."
# 2. 初始化模型
# llama3.2 支持 tool call,llama3 不支持
model = ChatOllama(model="llama3.2")
# 3. 创建 Agent
agent = create_react_agent(
model=model,
tools=[get_weather],
prompt="You are a helpful assistant",
)
if __name__ == "__main__":
result = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
print(result["messages"][-1].content)# 终端 1:启动 Ollama 服务(保持运行)
ollama serve
# 终端 2:运行 Agent
python weather_agent.pyWhy did the fog in San Francisco bring a sweater?
Because it heard the Golden Gate Bridge was feeling a little "sus-pended"
in the cold! 🌁😄
(In reality, SF is famously cool and foggy, especially in summer — locals
call it "Karl the Fog"!)
用户输入
│
▼
Agent 分析问题
│
▼
调用 get_weather("sf") 工具
│
▼
llama3.2 生成回答
│
▼
输出最终结果
httpx.ConnectError: [Errno 61] Connection refused
原因:Ollama 服务未启动。
解决:
ollama serveollama._types.ResponseError: registry.ollama.ai/library/llama3:latest does not support tools
原因:llama3(旧版)不支持 tool call。
解决:换成支持 tools 的模型:
ollama pull llama3.2# 修改代码中的模型名
model = ChatOllama(model="llama3.2") # ✅ImportError: cannot import name 'create_agent' from 'langchain.agents'
原因:create_agent 不存在,是错误的 API 名称。
解决:使用正确的导入:
# ❌ 错误
from langchain.agents import create_agent
# ✅ 正确
from langgraph.prebuilt import create_react_agent如果你有 Anthropic API Key,可以将模型替换为 Claude,效果更好:
pip install langchain-anthropic# 替换这两行即可,其余代码不变
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-sonnet-4-20250514")在终端中设置 API Key:
export ANTHROPIC_API_KEY="sk-ant-..."📌 Claude.ai 的付费订阅和 API Key 是独立的,需要在 console.anthropic.com 单独注册并充值。
weather-agent/
├── weather_agent.py # Agent 主程序
└── README.md # 本教程