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

官网例子就跑通了!

相关推荐
唐叔在学习1 小时前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽3 小时前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly3 小时前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术4 小时前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习
心在飞扬5 小时前
LangChain 工具创建方法总结
langchain
敏编程5 小时前
一天一个Python库:virtualenv - 隔离你的Python环境,保持项目整洁
python
喝茶与编码7 小时前
Python异步并发控制:asyncio.gather 与 Semaphore 协同设计解析
后端·python
zone77398 小时前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
用户8356290780518 小时前
在 PowerPoint 中用 Python 添加和定制形状的完整教程
后端·python