LangChain和LangGraph

LangChain和LangGraph 特性 LangChain LangGraph 诞生背景 解决LLM应用开发的零散、重复问题 解决LangChain复杂智能体自定义难、扩展难、生产部署难的问题 核心理念 模块化、可组合的链 “图即工作流”,低抽象、强控制的状态机 核心组件 Components, Chains, LCEL Nodes, Edges, StateGraph, Checkpointer 执行模式 线性串联为主,LLM自主决策为辅 有向图驱动,支持循环、分支与并行处理 生态系统 丰富的社区集成,庞大的工具库 原生LangChain集成,可独立运行 设计目标 快速开发、快速原型 生产可靠、可观测、易扩展 Deep Agents是一个基于LangGraph的代理工具:规划、子代理、文件系统工具和上下文管理。 LangChain是代理框架:模型、工具和代理循环的抽象和集成。 LangGraph是编排运行时:持久执行、流、人在循环和持久性。 LangSmith是用于跨框架跟踪、评估、提示和部署的平台。 Function Calling(函数调用/工具调用) 是大语言模型(LLM)的一项核心能力——它允许模型在生成回复时,不直接输出文本,而是输出一个 结构化的函数调用请求(通常是 JSON),由开发者实际执行该函数,然后将执行结果返回给模型,模型再据此生成最终回复。 注:本文根据吴恩达AI Agent课程学习整理,模型限制未使用chatgpt,用Deekseek替换。 基于react模式的智能代理 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 import openai import re import httpx import os from dotenv import load_dotenv _ = load_dotenv() from openai import OpenAI # ================= 改动开始 ================= # 使用 DeepSeek 的 API 地址和你的 API Key # 建议在 .env 文件中设置 DEEPSEEK_API_KEY DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY") # 如果没有设置,会读取 OPENAI_API_KEY 兼容 if not DEEPSEEK_API_KEY: raise ValueError("请在环境变量中设置 DEEPSEEK_API_KEY") client = OpenAI( api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com/v1" # DeepSeek 官方 API 地址 ) # ================= 改动结束 ================= # 测试调用(可选) chat_completion = client.chat.completions.create( model="deepseek-chat", # 使用 DeepSeek 模型 messages=[{"role": "user", "content": "Hello world"}] ) print(chat_completion.choices[0].message.content) class Agent: def __init__(self, system=""): self.system = system self.messages = [] if self.system: self.messages.append({"role": "system", "content": system}) def __call__(self, message): self.messages.append({"role": "user", "content": message}) result = self.execute() self.messages.append({"role": "assistant", "content": result}) return result def execute(self): completion = client.chat.completions.create( model="deepseek-chat", # 统一使用 DeepSeek 模型 temperature=0, messages=self.messages ) return completion.choices[0].message.content prompt = """ You run in a loop of Thought, Action, PAUSE, Observation. At the end of the loop you output an Answer Use Thought to describe your thoughts about the question you have been asked. Use Action to run one of the actions available to you - then return PAUSE. Observation will be the result of running those actions. Your available actions are: calculate: e.g. calculate: 4 * 7 / 3 Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary average_dog_weight: e.g. average_dog_weight: Collie returns average weight of a dog when given the breed Example session: Question: How much does a Bulldog weigh? Thought: I should look the dogs weight using average_dog_weight Action: average_dog_weight: Bulldog PAUSE You will be called again with this: Observation: A Bulldog weights 51 lbs You then output: Answer: A bulldog weights 51 lbs """.strip() def calculate(what): return eval(what) def average_dog_weight(name): if name in "Scottish Terrier": return("Scottish Terriers average 20 lbs") elif name in "Border Collie": return("a Border Collies average weight is 37 lbs") elif name in "Toy Poodle": return("a toy poodles average weight is 7 lbs") else: return("An average dog weights 50 lbs") known_actions = { "calculate": calculate, "average_dog_weight": average_dog_weight } abot = Agent(prompt) result = abot("How much does a toy poodle weigh?") print(result) result = average_dog_weight("Toy Poodle") result next_prompt = "Observation: {}".format(result) abot(next_prompt) abot.messages abot = Agent(prompt) question = """I have 2 dogs, a border collie and a scottish terrier. \ What is their combined weight""" abot(question) next_prompt = "Observation: {}".format(average_dog_weight("Border Collie")) print(next_prompt) abot(next_prompt) next_prompt = "Observation: {}".format(average_dog_weight("Scottish Terrier")) print(next_prompt) abot(next_prompt) next_prompt = "Observation: {}".format(eval("37 + 20")) print(next_prompt) next_prompt = "Observation: {}".format(eval("37 + 20")) print(next_prompt) abot(next_prompt) action_re = re.compile('^Action: (\w+): (.*)$') # python regular expression to selection action def query(question, max_turns=5): i = 0 bot = Agent(prompt) next_prompt = question while i < max_turns: i += 1 result = bot(next_prompt) print(result) actions = [ action_re.match(a) for a in result.split('\n') if action_re.match(a) ] if actions: # There is an action to run action, action_input = actions[0].groups() if action not in known_actions: raise Exception("Unknown action: {}: {}".format(action, action_input)) print(" -- running {} {}".format(action, action_input)) observation = known_actions[action](action_input) print("Observation:", observation) next_prompt = "Observation: {}".format(observation) else: return question = """I have 2 dogs, a border collie and a scottish terrier. \ What is their combined weight""" query(question) 这段代码实现了一个基于大语言模型的 ReAct 风格智能代理,能够通过“思考-行动-观察”循环来解决需要多步推理或工具调用的问题。它使用了 DeepSeek 模型(兼容 OpenAI API),并内置了两个简单工具:数学计算和查询狗的平均体重。 ...

July 1, 2026 · 21 min · 4447 words · Taigong

信息安全管理体系方案-ISO27001

ISO/IEC 27001:2022 信息安全管理体系 全套落地方案 第一部分 项目概述 1.1 项目背景 公司处理超过1000万人的个人信息,已触发《个人信息保护合规审计管理办法》规定的每两年至少一次合规审计的法定义务。ISO/IEC 27001信息安全管理体系认证与个人信息保护合规审计具有高度协同效应——前者构建系统化的信息安全管理框架,后者聚焦具体的合规检查点。两者结合可实现“一次建设、双重收益”。 重要提示:新版国家标准GB/T 22080-2025/ISO/IEC 27001:2022已于2025年6月30日发布,2026年1月1日起正式实施,全面取代旧版GB/T 22080-2016。自2025年8月1日起,认证机构已开始受理依据新版标准的初次认证申请。任何新申请认证的企业,均须按照新版标准执行。 1.2 标准概述 ISO/IEC 27001是由国际标准化组织(ISO)与国际电工委员会(IEC)联合发布的信息安全管理体系(ISMS)要求标准。其核心框架建立在PDCA循环之上: 阶段 内容 关键活动 Plan(计划) 明确ISMS范围和边界 识别法律法规要求、评估风险、制定处置计划 Do(执行) 制定信息安全方针和策略 落实控制措施、分配安全职责、实施培训和技术防护 Check(检查) 评估体系有效性和合规性 内部审核、管理评审 Act(改进) 根据检查结果持续优化 采取纠正措施、持续改进 1.3 新版标准核心变化 ISO/IEC 27001:2022相较于2013版的主要变化: 变化维度 2013版本 2022版本 控制项数量 114项 93项 控制项分类 14个控制域 4大主题:组织控制(37项)、人员控制(8项)、物理控制(14项)、技术控制(34项) 新增控制项 — 威胁情报、云服务信息安全、ICT业务连续性、物理安全监控、数据泄露预防、配置管理等11项 1.4 项目实施总览 项目维度 说明 项目周期 约5-6个月(从启动到获证) 项目目标 通过ISO/IEC 27001:2022认证,同步支撑个人信息保护合规审计 覆盖范围 全公司信息安全管理体系(建议先从核心业务系统切入) 项目团队 跨部门项目组(IT、法务、HR、行政、业务部门) 第二部分 项目组织与团队 2.1 项目组织结构 text 项目指导委员会(高层管理层) │ 项目经理(信息安全负责人) │ ┌──────────┬──────────┬──────────┼──────────┬──────────┐ │ │ │ │ │ │ IT组 法务组 HR组 行政组 业务组 内审组 (技术落地) (合规审核)(培训考核)(物理安全)(业务对接)(体系检查) ...

July 1, 2026 · 4 min · 731 words · Taigong

利用Robocopy脚本实现企业财务数据自动备份

需求是定期备份某个远程目录文件,增量备份。 以下内容为脚本,请作为批处理脚本使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @echo off chcp 65001 >nul :: Configuration set SRC="\\192.168.1.1\财务部" set DEST="D:\Backup" set LOG="D:\Backup\finance_backup.log" echo ============================================== >> %LOG% echo Start Time: %date% %time% >> %LOG% :: Check if Source is accessible before mirroring to prevent accidental wipes if not exist %SRC% ( echo ERROR: Source path not found. Aborting to protect backup. >> %LOG% exit /b 1 ) :: Run Robocopy :: /MIR : Mirroring (CAUTION: Deletes files in DEST not in SRC) :: /Z : Restartable mode (good for large files/network drops) :: /FFT : Assume FAT File Times (better for network/NAS compatibility) :: /R:2 : Retry twice on fail :: /W:5 : Wait 5 seconds between retries :: /NP : No Progress (keeps log file clean) :: /MT:8 : Multi-threaded (Optional: speeds up transfer of many small files) robocopy %SRC% %DEST% /MIR /Z /FFT /R:2 /W:5 /NP /MT:8 /LOG+:%LOG% set RC=%ERRORLEVEL% echo End Time: %date% %time% >> %LOG% echo Robocopy ExitCode=%RC% >> %LOG% echo ============================================== >> %LOG% :: Return success if RC is less than 8 if %RC% LSS 8 (exit /b 0) else (exit /b %RC%) ...

July 1, 2026 · 2 min · 217 words · Taigong

昇腾 310P 部署 GPUSTACK 集群

GPUSTACK GPUStack 是一个非常优秀的轻量级大模型多机整合与服务化框架,它支持异构算力和私有化集群。虽然它底层对华为昇腾(CANN)和鲲鹏(ARM64)的自动化原生支持可能不如传统 NVIDIA N卡那样“即插即用”,但通过其支持的 vLLM(Ascend 分支)后端 配合 Docker,我们可以非常优雅地实现多机并联。 以下是使用 GPUStack 极限部署 DeepSeek-V4-Flash 的全新实操方案: 🛠️ GPUStack 多机架构设计 由于 310P3 运行大模型必须依赖华为 CANN 环境和特定的 vLLM/MindIE 算子,在 GPUStack 体系下,我们推荐采用 「Host 宿主机 GPUStack 管理 + Container 容器内算力节点」 的架构。 节点 0 (Master 节点):运行 GPUStack Server(控制台与调度中心)+ 运行 GPUStack Worker(提供本地两块 310P3 算力)。 节点 1、2、3 (Worker 节点):仅运行 GPUStack Worker 注册到 Master,提供各自的 310P3 算力。 第一阶段:宿主机基础环境(4台机器同步执行) 无论用什么框架,底层的驱动和固件是绕不开的。请确保 4台 机器均已完成以下操作: 1. 安装昇腾 310P3 驱动与固件 参考上一篇文档,确保 npu-smi info 能够正常看到每台机器的 96GB 显存。 2. 安装 Docker 与 Ascend-Docker-Runtime 为了让 Docker 容器能够调用宿主机的 310P3 芯片,必须安装华为官方的容器运行时: ...

July 1, 2026 · 2 min · 423 words · Taigong