LangChain + Ollama 本地 Agent
# LangChain + Ollama 本地 Agent
基于 my-local-agent 项目实践,用 LangChain + LangGraph + Ollama 搭建本地运行的 Agent。
项目路径:/Users/yangjianfei/myWidget/values/others/my-local-agent
# 环境准备
# 创建虚拟环境
python -m venv .venv
source .venv/bin/activate
# 安装依赖
pip install langchain-ollama langchain langgraph
1
2
3
4
5
6
2
3
4
5
6
确保 Ollama 服务已启动并拉取了模型:
ollama pull deepseek-r1:14b
1
# 核心代码结构
# 1. 初始化本地模型
from langchain_ollama import ChatOllama
llm = ChatOllama(
model="deepseek-r1:14b",
temperature=0,
base_url="http://localhost:11434",
num_ctx=8192, # 上下文窗口,内存小可设 4096
num_predict=2048 # 最大输出长度
)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 2. 定义工具(@tool 装饰器)
from langchain_core.tools import tool
@tool
def calculator(expression: str) -> str:
"""计算数学表达式,如 '2 + 3 * 4'"""
try:
return f"计算结果: {eval(expression)}"
except Exception as e:
return f"计算错误: {str(e)}"
@tool
def get_current_time() -> str:
"""获取当前日期和时间"""
from datetime import datetime
return f"当前时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')"
tools = [calculator, get_current_time]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 3. 创建 Agent(带记忆)
from langchain.agents import create_agent
from langgraph.checkpoint.memory import MemorySaver
memory = MemorySaver()
agent = create_agent(
model=llm,
tools=tools,
system_prompt="你是一个有用的AI助手,使用工具来帮助用户完成任务。",
checkpointer=memory
)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 4. 流式运行
config = {"configurable": {"thread_id": "user-123"}}
for event in agent.stream(
{"messages": [{"role": "user", "content": user_input}]},
config,
stream_mode="values"
):
last_message = event["messages"][-1]
if last_message.type == "ai":
content = last_message.content or ""
# 过滤 deepseek-r1 思考过程
if "</think>" in content:
content = content.split("</think>")[-1].strip()
print(content, end="", flush=True)
# 显示工具调用
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
for tc in last_message.tool_calls:
print(f"\n [调用工具: {tc['name']}({tc['args']})]")
elif last_message.type == "tool":
print(f"\n [工具返回: {str(last_message.content)[:100]}]")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 调试技巧
# 查看完整事件流
用 debug_agent.py 思路,打印每个事件的详细信息:
event_count = 0
for event in agent.stream(
{"messages": [{"role": "user", "content": "计算 2+3"}]},
config,
stream_mode="values"
):
event_count += 1
messages = event.get("messages", [])
last_msg = messages[-1] if messages else None
print(f"--- 事件 {event_count} ---")
print(f"消息类型: {type(last_msg).__name__}")
print(f"role: {getattr(last_msg, 'role', 'N/A')}")
print(f"内容前100字: {str(getattr(last_msg, 'content', ''))[:100]}")
if hasattr(last_msg, 'tool_calls') and last_msg.tool_calls:
print(f"工具调用: {last_msg.tool_calls}")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 常见问题
# Agent 不调用工具
- 检查模型是否支持 function calling(DeepSeek-R1、Qwen2.5 支持较好)
temperature设为 0 提高确定性- tool 的 docstring 要写清楚用途和参数
- system_prompt 明确告知"使用工具来完成任务"
# 输出截断
- 增大
num_predict参数 - 检查
num_ctx是否足够容纳历史消息
# 内存压力大
num_ctx从 8192 降到 4096- 换更小的模型(7B 代替 14B)
# 记忆不生效
- 确保传入了
checkpointer=memory - 确保每次调用传入相同的
thread_id
# 依赖版本参考
langchain-ollama
langchain
langgraph
1
2
3
2
3
create_agent是 LangChain 新版 API(0.2+),旧版用initialize_agent。
上次更新: 2026/08/12, 13:57:01