本地部署Agent以及企业部署Agent

本地部署:

1. 安装LangGraph CLI

pip install --upgrade "langgraph-cli[inmem]"

2. 配置LangSmith的环境变量

LANGSMITH_API_KEY="${LANGSMITH_API_KEY}"

3. 在项目根目录下新建 langgraph.json,内容如下:

{
  "dependencies": ["."],
  "graphs": {
    "agent": "./src/agent/my_agent1.py:agent"
  },
  "env": ".env"
}

4. 编写智能体代码

from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_core.prompts import PromptTemplate
from pydantic import BaseModel, Field

from agent.my_llm import llm


def send_email(to: str, subject: str, body: str):
    """发送邮件"""
    email = {
        "to": to,
        "subject": subject,
        "body": body
    }
    # ... 邮件发送逻辑
    
agent = create_agent(
    llm,
    tools=[send_email],
    system_prompt="你是一个邮件助手。请始终使用 send_email 工具。",
)

 5. 项目下新建 pyproject.toml 文件,内容如下:

[project]
name = "agent"
version = "0.0.1"
description = "Starter template for making a new agent LangGraph."
authors = [
    { name = "William Fu-Hinthorn", email = "13333726+hinthornw@users.noreply.github.com" },
]
readme = "README.md"
license = { text = "MIT" }
requires-python = ">=3.10"
dependencies = [
    "langgraph>=1.0.0",
    "python-dotenv>=1.0.1",
]


[project.optional-dependencies]
dev = ["mypy>=1.11.1", "ruff>=0.6.1"]

[build-system]
requires = ["setuptools>=73.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
packages = ["langgraph.templates.agent", "agent"]
[tool.setuptools.package-dir]
"langgraph.templates.agent" = "src/agent"
"agent" = "src/agent"


[tool.setuptools.package-data]
"*" = ["py.typed"]

[tool.ruff]
lint.select = [
    "E",    # pycodestyle
    "F",    # pyflakes
    "I",    # isort
    "D",    # pydocstyle
    "D401", # First line should be in imperative mood
    "T201",
    "UP",
]
lint.ignore = [
    "UP006",
    "UP007",
    # We actually do want to import from typing_extensions
    "UP035",
    # Relax the convention by _not_ requiring documentation for every function parameter.
    "D417",
    "E501",
]
[tool.ruff.lint.per-file-ignores]
"tests/*" = ["D", "UP"]
[tool.ruff.lint.pydocstyle]
convention = "google"

[dependency-groups]
dev = [
    "anyio>=4.7.0",
    "langgraph-cli[inmem]>=0.4.7",
    "mypy>=1.13.0",
    "pytest>=8.3.5",
    "ruff>=0.8.2",
]

image

 在项目目录下敲以下命令,导入依赖:

pip install -e .

image

 6. 在Studio中启动和运行智能体:

langgraph dev

 

企业中,使用FastAPI部署

posted @ 2026-11-19 23:05  君莫笑我十年游  阅读(11)  评论(0)    收藏  举报