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

人工智能-LangGraph+ChatUI+DeepSeek API搭建本地智能助手

人工智能-LangGraph+ChatUI+DeepSeek API搭建本地智能助手

    • 0 环境说明
    • 1 LangGraph
    • 2 Agent Chat UI

0 环境说明

环境项环境说明
操作系统Windows11 专业版
硬件信息联想拯救者Y9000P
condaAnanconda
Python版本3.12
NodeJs18.20.0
# 使用conda创建python环境
conda create -n langgraph python=3.12

# 激活环境
conda activate langgraph

# 下载依赖
pip install langchain-core langgraph -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install "langgraph-cli[inmem]" --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple

1 LangGraph

下载一个LangGraph Template项目

git clone https://github.com/langchain-ai/new-langgraph-project.git

修改配置文件

cd new-langgraph-project
cp .env.example .env

因为我使用的是DeepSeek API,所以需要配置 DEEPSEEK_API_KEY(上一步创建的DeepSeek API Key)

# To separate your traces from other application
LANGSMITH_PROJECT=new-agent

# The following depend on your selected configuration

## LLM choice:
# ANTHROPIC_API_KEY=....
# FIREWORKS_API_KEY=...
# OPENAI_API_KEY=...

DEEPSEEK_API_KEY=your-deepseek-api-key

修改代码
src/agent/state.py

from __future__ import annotations

from typing_extensions import TypedDict

from dataclasses import dataclass
from langchain_core.messages import AnyMessage
from langgraph.graph.message import add_messages
from typing import Annotated


@dataclass
class State(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]

src/agent/configuration.py

"""Define the configurable parameters for the agent."""

from __future__ import annotations

from dataclasses import dataclass, fields
from typing import Optional

from langchain_core.runnables import RunnableConfig


@dataclass(kw_only=True)
class Configuration:


    model_name: str = "deepseek-chat"

    @classmethod
    def from_runnable_config(
        cls, config: Optional[RunnableConfig] = None
    ) -> Configuration:
        """Create a Configuration instance from a RunnableConfig object."""
        configurable = (config.get("configurable") or {}) if config else {}
        _fields = {f.name for f in fields(cls) if f.init}
        return cls(**{k: v for k, v in configurable.items() if k in _fields})


src/agent/graph.py



from typing import Any, Dict

from langchain_core.runnables import RunnableConfig
from langgraph.graph import StateGraph
from langchain_deepseek import ChatDeepSeek
from agent.configuration import Configuration
from agent.state import State

model = ChatDeepSeek(
    model="deepseek-chat",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
)

def call_model(state, config):
    response = model.invoke(state["messages"])
    # We return a list, because this will get added to the existing list
    return {"messages": [response]}


# Define a new graph
workflow = StateGraph(State, config_schema=Configuration)

# Add the node to the graph
workflow.add_node("call_model", call_model)

# Set the entrypoint as `call_model`
workflow.add_edge("__start__", "call_model")

# Compile the workflow into an executable graph
graph = workflow.compile()
graph.name = "New Graph"  # This defines the custom name in LangSmith


接下来就可以启动项目了:

# 安装依赖
pip install -qU langchain-deepseek -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple

# 启动项目
langgraph dev

启动的运行日志如下图:
在这里插入图片描述
启动完毕后,会自动弹出浏览器打开langsmith跟踪页面,如下图:
在这里插入图片描述

API文档地址(http://127.0.0.1:2024/docs)访问如下图:
在这里插入图片描述

2 Agent Chat UI

前端项目基于NodeJS。

  • NodeJs版本 ≥ 18.12 我用的版本是 18.20.0

下载agent-chat-ui项目

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

cd agent-chat-ui

# 先安装pnpm
npm install -g pnpm

# 安装项目依赖
pnpm install

# 启动项目
pnpm dev

安装项目依赖过程如下图:
在这里插入图片描述
在这里插入图片描述

项目启动日志如下:
在这里插入图片描述
启动后可以通过浏览器输入: http://localhost:5173访问,如下图:
在这里插入图片描述

点击 Continue 进入对话界面,如下图:
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 搭建开源笔记平台:outline
  • 如何在 Unity3D 导入 Spine 动画
  • 【NLP】15. NLP推理方法详解 --- 动态规划:序列标注,语法解析,共同指代
  • JavaScript 库:全面解析与推荐
  • 13-SpringBoot3入门-整合MyBatis-Plus
  • 【Docker镜像】Python项目之使用Dockerfile构建镜像(一)
  • 如何用Postman实现自动化测试?
  • 【Jira 查询与 JQL 使用详解】
  • 【数据结构】导航
  • Ubuntu 24.04.2 LTS 系统安装python,创建虚拟环境
  • 解决 CMS Old GC 频繁触发线上问题技术方案
  • 本地部署vanna ai+通过http请求调用vanna
  • 虚幻5入门
  • Docker学习--本地镜像管理相关命令--docker save 命令
  • 【Easylive】TokenUserInfoDto中@JsonIgnoreProperties和 Serializable 接口作用
  • git 按行切割 csv文件
  • MCP协议的Streamable HTTP:革新数据传输的未来
  • 企业常用Linux服务搭建
  • 数字IC后端项目常见问题之streamOut layermap和innovus drc violation
  • 智能设备操作系统闭源化浪潮下,小程序生态重构与技术创新
  • 东莞“超级”音乐节五一出圈背后:文旅热力何以澎湃经济脉动
  • 青岛双星名人集团管理权之争:公司迁址,管理层更迭
  • 国防部:正告菲方停止以任何方式冲撞中方核心利益
  • 司法部:持续规范行政执法行为,加快制定行政执法监督条例
  • 自然资源部印发地理信息数据分类分级指南
  • 潘功胜:坚定支持汇金公司在必要时实施对股票市场指数基金的增持