本地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测试

官网例子就跑通了!

相关推荐
花酒锄作田11 小时前
Pydantic校验配置文件
python
hboot11 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
匹诺曹i3671 天前
docx 报表进 OpenClaw 本地 RAG:memory 索引实践与 6 个坑
ollama
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉