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

官网例子就跑通了!

相关推荐
love530love3 小时前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
遇事不決洛必達3 小时前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁
CryptoPP4 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
探物 AI5 小时前
把 MambaOut 塞进 YOLOv11:会有什么样的反应
python·yolo·计算机视觉
如竟没有火炬5 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
阳区欠5 小时前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar5 小时前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构
GDAL6 小时前
使用 uv 管理 Python 版本
python·uv·版本
真实的菜6 小时前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
cup116 小时前
[开源] Meta Assistant / 告别命令行,我为一堆 Python 脚本做了一个 Windows 任务栏的“家”
windows·python·工具·nuitka·脚本运行