本地Ollama+Agent+LangGraph+LangSmith运行

基于LangChain官网的例子,自己本地搭建了一套智能体应用,结合本地Ollama部署模型,实现工作流+智能体交互。

官网:https://docs.langchain.com/

代码:

agent.py

复制代码
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model

def send_email(to: str, subject: str, body: str):
    """Send an email"""
    email = {
        "to": to,
        "subject": subject,
        "body": body
    }
    # ... email sending logic

    return f"Email sent to {to}"

# 先初始化模型
model = init_chat_model(
    model="ollama:llama3.2:1b",
    base_url="http://127.0.0.1:11434",
)

# 然后创建智能体
agent = create_agent(
    model=model,
    tools=[send_email],
    system_prompt="You are an email assistant. Always use the send_email tool.",
)

.env

复制代码
LANGSMITH_API_KEY=lsv2...

langgraph.json

复制代码
{
  "dependencies": ["."],
  "graphs": {
    "agent": "./agent/agent.py:agent"
  },
  "env": ".env"
}

目录结构如官网所示

复制代码
my-app/
├── src
│   └── agent.py
├── .env
└── langgraph.json

安装好必要的程序包,参考官网,启动项目

复制代码
python -m langgraph_cli dev

启动后

浏览器访问:https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024

页面如下

注册账户后页面如下,多一些功能

git下载前端会话页面源码并安装依赖启动:

复制代码
# Clone the repository
git clone https://github.com/langchain-ai/agent-chat-ui.git
cd agent-chat-ui

# Install dependencies and start
pnpm install
pnpm dev

启动后,浏览器访问:http://localhost:3000/

填写配置

URL

复制代码
http://127.0.0.1:2024

Graph ID

复制代码
agent

API Key就是.env配置的

复制代码
lsv2...

点击Continue按钮,页面如下

发送hello测试

官网例子就跑通了!

相关推荐
方知我15 分钟前
NumPy一小时速成
开发语言·python·numpy
IPdodo_19 分钟前
静态 IP 访问异常排查:403/429 归因与迁移验收
服务器·网络·数据库·python·网络协议·php·性能测试
Zengs28 分钟前
Python 脚本日志怎么分级落盘?排错时一次找准现场
python
baopixiaoz1 小时前
BeeQuant × BeeAgent:AI量化新范式
大数据·人工智能·python·区块链
IT毕设实战小研1 小时前
基于大数据的AI应用对就业与收入增长的区域差异分析及可视化
大数据·人工智能·python·随机森林·机器学习·课程设计
摆烂老大2 小时前
论文学习 | Expert Systems With Applications2026 | 可微分建模:S4D-HBV
python·学习·水文
程序员夏洛2 小时前
谈谈你了解的最常见的几种设计模式,说说他们的应用场景
开发语言·python·模板方法模式
橙露2 小时前
Cloudflare Pages 深度评测:免费静态托管的香与坑
python·部署
lk123062 小时前
【无标题】
pytorch·python
数据开发 Yang 同学2 小时前
【Python 并发编程】线程、进程、协程、同步/异步一次说清
python