当前位置: 首页 > news >正文

langgraph快速搭建agent后端和react前端

官方文档

一、后端

1.安装基础依赖

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

2.下载模版项目

在终端运行

langgraph new ./example --template new-langgraph-project-python

这里是在当前文件夹下新建文件夹example,里面是下载的langgraph模版项目文件

在这里插入图片描述
显示这样就是成功。如果失败,说明网络问题。

3.安装项目依赖

cd example
pip install -e .

4.配置项目环境变量

将.env.example文件修改为.env,这样就是环境变量文件了,重要的key都放在这里,和代码文件隔离

填写LANGSMITH_API_KEY,需要去langsmith注册账号,获取key

5.部署后端服务

langgraph dev

就会看到
在这里插入图片描述

6.核心代码

是src.agent.graph.py

"""LangGraph single-node graph template.Returns a predefined response. Replace logic and configuration as needed.
"""from __future__ import annotationsfrom dataclasses import dataclass
from typing import Any, Dict, TypedDictfrom langgraph.graph import StateGraph
from langgraph.runtime import Runtime# 上下文参数
class Context(TypedDict):"""Context parameters for the agent.Set these when creating assistants OR when invoking the graph.See: https://langchain-ai.github.io/langgraph/cloud/how-tos/configuration_cloud/"""my_configurable_param: str# 状态参数,定义输入参数
@dataclass
class State:"""Input state for the agent.Defines the initial structure of incoming data.See: https://langchain-ai.github.io/langgraph/concepts/low_level/#state"""changeme: str = "example"# 一个节点,接收state和runtime,返回output
async def call_model(state: State, runtime: Runtime[Context]) -> Dict[str, Any]:"""Process input and returns output.Can use runtime context to alter behavior."""return {"changeme": "output from call_model. "f"Configured with {runtime.context.get('my_configurable_param')}"}# 定义图
graph = (StateGraph(State, context_schema=Context).add_node(call_model).add_edge("__start__", "call_model").compile(name="New Graph")
)

图形化后是
在这里插入图片描述

二、前端

官方文档

前置条件,在本地或者云端已经部署了langgraph的服务

然后本地安装前端项目,并启动

git clone https://github.com/langchain-ai/agent-chat-ui.gitcd agent-chat-uipnpm install

启动前端

pnpm dev

这样本地就有两个项目了
在这里插入图片描述
如果没有pnpm,就安装

brew install pnpm

三、最终效果

因为前端的显示是消息,所以要修改示例代码,我的简易代码

from typing import Annotatedfrom typing_extensions import TypedDictfrom langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messagesclass State(TypedDict):# Messages have the type "list". The `add_messages` function# in the annotation defines how this state key should be updated# (in this case, it appends messages to the list, rather than overwriting them)messages: Annotated[list, add_messages]def call_llm(state: State) -> State:return {"messages": [{"role": "assistant","content": "你好,我是小爱同学,请问你是谁?"}]}graph_builder = StateGraph(State)graph_builder.add_node("call_llm", call_llm)graph_builder.add_edge(START, "call_llm")
graph_builder.add_edge("call_llm", END)graph = graph_builder.compile()

在这里插入图片描述

四、本地客户端访问langgraph服务

官方文档
代码

from langgraph_sdk import get_client
import asyncioclient = get_client(url="http://localhost:2024")async def main():async for chunk in client.runs.stream(None,  # Threadless run"agent", # Name of assistant. Defined in langgraph.json.input={"messages": [{"role": "human","content": "hello",}],},):print(f"Receiving new event of type: {chunk.event}...")print(chunk.data)print("\n\n")asyncio.run(main())

效果
在这里插入图片描述

http://www.xdnf.cn/news/1341037.html

相关文章:

  • springboot 启动后get请求任意接口地址会跳到登录页
  • 【TrOCR】模型预训练权重各个文件解读
  • 【Java集合】List,Map,Set-详细讲解
  • ODDR实现多bit单边沿采样数据转为多bit双沿采样数据
  • 效率跃迁 ,亚数TrustAsia 加速证书管理迈向 CaaS 新阶段
  • 意象驱动的深层语义:感知认知统一对自然语言处理与知识图谱的影响
  • 活性数据处理与标准化
  • 在互联网大厂的Java面试:谢飞机的搞笑历险记
  • 学习 k 均值聚类算法的心得
  • 2025-08-21 Python进阶8——命名空间作用域
  • gRPC 与 HTTP 性能对比分析
  • 微算法科技(NASDAQ:MLGO)构建去中性化区块链预言机,实现跨链信息互通
  • 使用 X11 转发服务器界面
  • 整体设计 之定稿 “凝聚式中心点”原型 --整除:智能合约和DBMS的在表层挂接 能/所 依据的深层套接 之2
  • 迅为R3568开发板OpeHarmony学习开发手册-配置远程访问环境
  • Typescript入门-函数讲解
  • 面试后的跟进策略:如何提高录用几率并留下专业印象
  • Shell 变量全解析:从基础到高级技巧
  • C语言基础习题——01
  • mac的m3芯片安装JDK8、JDK17
  • QWidget/QMainWindow与QLayout的布局
  • 家里Windows,公司Linux?通过cpolar,WSL开发环境无缝切换
  • 【STM32】HAL库中的实现(九):SPI(串行外设接口)
  • 智能求职推荐系统演示说明
  • 封装FTPSClient连接ftps服务器
  • 27、设备状态监测与维护管理 (模拟电机振动) - /安全与维护组件/device-condition-monitoring
  • 【用户管理】修改文件权限
  • DeepSeek V3.1正式发布,专为下代国产芯设计
  • opencv学习:图像边缘检测
  • 8.21IPSEC安全基础后篇,IKE工作过程