Langchain Academy translated
  • module-0
    • LangChain 学院
  • module-1
    • 智能体记忆
    • 智能体
    • 链式结构
    • 部署
    • 路由器
    • 最简单的图结构
  • module-2
    • 支持消息摘要与外部数据库记忆的聊天机器人
    • 支持消息摘要的聊天机器人
    • 多模式架构
    • 状态归约器
    • 状态模式
    • 消息过滤与修剪
  • module-3
    • 断点
    • 动态断点
    • 编辑图状态
    • 流式处理
    • 时间回溯
  • module-4
    • 映射-归约
    • 并行节点执行
    • 研究助手
    • 子图
  • module-5
    • 记忆代理
    • 具备记忆功能的聊天机器人
    • 基于集合架构的聊天机器人
    • 支持个人资料架构的聊天机器人
  • module-6
    • 助手
    • 连接 LangGraph 平台部署
    • 创建部署
    • 双重消息处理
  • Search
  • Previous
  • Next
  • 多模式架构
    • 回顾
    • 目标
    • 私有状态
    • 输入/输出模式
  • H1
    • H2
  • H1
    • H2

在 Colab 中打开 在 LangChain Academy 中打开

多模式架构¶

回顾¶

我们刚刚讨论了状态模式(state schema)和归约器(reducers)的概念。

通常情况下,所有图节点(graph nodes)都与单一模式架构进行通信。

此外,这个单一模式架构包含了图的输入/输出键(key)/通道(channel)。

目标¶

但在某些情况下,我们可能需要对此进行更多控制:

  • 内部节点传递的信息可能不需要出现在图的输入/输出中

  • 我们可能希望为图使用不同的输入/输出模式架构。例如,输出可能仅包含单个相关的输出键

接下来我们将探讨通过多模式架构自定义图的几种方法。

In [1]:
Copied!
%%capture --no-stderr
%pip install --quiet -U langgraph
%%capture --no-stderr %pip install --quiet -U langgraph

私有状态¶

首先,我们讨论在节点间传递私有状态的情况。

这种方式适用于那些在图计算中间工作逻辑中需要,但与整个图的输入或输出无关的数据。

我们将定义一个OverallState和一个PrivateState。

node_2使用PrivateState作为输入,但将结果写入OverallState。

In [2]:
Copied!
from typing_extensions import TypedDict
from IPython.display import Image, display
from langgraph.graph import StateGraph, START, END

class OverallState(TypedDict):
    foo: int

class PrivateState(TypedDict):
    baz: int

def node_1(state: OverallState) -> PrivateState:
    print("---Node 1---")
    return {"baz": state['foo'] + 1}

def node_2(state: PrivateState) -> OverallState:
    print("---Node 2---")
    return {"foo": state['baz'] + 1}

# Build graph
builder = StateGraph(OverallState)
builder.add_node("node_1", node_1)
builder.add_node("node_2", node_2)

# Logic
builder.add_edge(START, "node_1")
builder.add_edge("node_1", "node_2")
builder.add_edge("node_2", END)

# Add
graph = builder.compile()

# View
display(Image(graph.get_graph().draw_mermaid_png()))
from typing_extensions import TypedDict from IPython.display import Image, display from langgraph.graph import StateGraph, START, END class OverallState(TypedDict): foo: int class PrivateState(TypedDict): baz: int def node_1(state: OverallState) -> PrivateState: print("---Node 1---") return {"baz": state['foo'] + 1} def node_2(state: PrivateState) -> OverallState: print("---Node 2---") return {"foo": state['baz'] + 1} # Build graph builder = StateGraph(OverallState) builder.add_node("node_1", node_1) builder.add_node("node_2", node_2) # Logic builder.add_edge(START, "node_1") builder.add_edge("node_1", "node_2") builder.add_edge("node_2", END) # Add graph = builder.compile() # View display(Image(graph.get_graph().draw_mermaid_png()))
No description has been provided for this image
In [3]:
Copied!
graph.invoke({"foo" : 1})
graph.invoke({"foo" : 1})
---Node 1---
---Node 2---
Out[3]:
{'foo': 3}

baz 仅包含在 PrivateState 中。

node_2 使用 PrivateState 作为输入,但输出到 OverallState。

因此,我们可以看到 baz 被排除在图形输出之外,因为它不在 OverallState 中。

输入/输出模式¶

默认情况下,StateGraph 接收单一模式,所有节点都预期与该模式进行交互。

不过,也可以为图定义显式的输入和输出模式。

在这些场景中,我们通常会定义一个"内部"模式,其中包含与图操作相关的所有关键字段。

但我们会使用特定的 input 和 output 模式来约束输入和输出。

首先,让我们仅使用单一模式运行该图。

In [4]:
Copied!
class OverallState(TypedDict):
    question: str
    answer: str
    notes: str

def thinking_node(state: OverallState):
    return {"answer": "bye", "notes": "... his name is Lance"}

def answer_node(state: OverallState):
    return {"answer": "bye Lance"}

graph = StateGraph(OverallState)
graph.add_node("answer_node", answer_node)
graph.add_node("thinking_node", thinking_node)
graph.add_edge(START, "thinking_node")
graph.add_edge("thinking_node", "answer_node")
graph.add_edge("answer_node", END)

graph = graph.compile()

# View
display(Image(graph.get_graph().draw_mermaid_png()))
class OverallState(TypedDict): question: str answer: str notes: str def thinking_node(state: OverallState): return {"answer": "bye", "notes": "... his name is Lance"} def answer_node(state: OverallState): return {"answer": "bye Lance"} graph = StateGraph(OverallState) graph.add_node("answer_node", answer_node) graph.add_node("thinking_node", thinking_node) graph.add_edge(START, "thinking_node") graph.add_edge("thinking_node", "answer_node") graph.add_edge("answer_node", END) graph = graph.compile() # View display(Image(graph.get_graph().draw_mermaid_png()))
No description has been provided for this image

请注意,invoke 的输出包含 OverallState 中的所有键值。

In [5]:
Copied!
graph.invoke({"question":"hi"})
graph.invoke({"question":"hi"})
Out[5]:
{'question': 'hi', 'answer': 'bye Lance', 'notes': '... his name is Lance'}

现在,让我们在图中使用特定的 input 和 output 模式。

这里的 input / output 模式会对图中输入和输出允许的键进行过滤。

此外,我们可以使用类型提示 state: InputState 来指定每个节点的输入模式。

当图中使用多个模式时,这一点非常重要。

我们在下方使用类型提示,例如表明 answer_node 的输出将被过滤为 OutputState。

In [ ]:
Copied!
class InputState(TypedDict):
    question: str

class OutputState(TypedDict):
    answer: str

class OverallState(TypedDict):
    question: str
    answer: str
    notes: str

def thinking_node(state: InputState):
    return {"answer": "bye", "notes": "... his is name is Lance"}

def answer_node(state: OverallState) -> OutputState:
    return {"answer": "bye Lance"}

graph = StateGraph(OverallState, input_schema=InputState, output_schema=OutputState)
graph.add_node("answer_node", answer_node)
graph.add_node("thinking_node", thinking_node)
graph.add_edge(START, "thinking_node")
graph.add_edge("thinking_node", "answer_node")
graph.add_edge("answer_node", END)

graph = graph.compile()

# View
display(Image(graph.get_graph().draw_mermaid_png()))

graph.invoke({"question":"hi"})
class InputState(TypedDict): question: str class OutputState(TypedDict): answer: str class OverallState(TypedDict): question: str answer: str notes: str def thinking_node(state: InputState): return {"answer": "bye", "notes": "... his is name is Lance"} def answer_node(state: OverallState) -> OutputState: return {"answer": "bye Lance"} graph = StateGraph(OverallState, input_schema=InputState, output_schema=OutputState) graph.add_node("answer_node", answer_node) graph.add_node("thinking_node", thinking_node) graph.add_edge(START, "thinking_node") graph.add_edge("thinking_node", "answer_node") graph.add_edge("answer_node", END) graph = graph.compile() # View display(Image(graph.get_graph().draw_mermaid_png())) graph.invoke({"question":"hi"})
No description has been provided for this image
Out[ ]:
{'question': 'hi', 'answer': 'bye Lance', 'notes': '... his is name is Lance'}

我们可以看到 output 模式将输出限制为仅包含 answer 键。

# Getting Started with Markdown

Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. Created by John Gruber in 2004, Markdown is now one of the world's most popular markup languages.

## Basic Syntax

Here are some of the most commonly used Markdown syntax elements:

### Headers

```markdown
# H1
## H2
### H3

Emphasis¶

*italic* or _italic_
**bold** or __bold__

Lists¶

Unordered¶

- Item 1
- Item 2
  - Subitem 2.1

Ordered¶

1. First item
2. Second item
   1. Subitem 2.1

Links¶

[Google](https://www.google.com)

Images¶

![alt text](image.jpg)

Note: For more advanced formatting, refer to the Markdown Guide.


以下是翻译后的中文 Markdown 内容:

```markdown
# Markdown 入门指南

Markdown 是一种轻量级标记语言,可用于为纯文本文档添加格式元素。由 John Gruber 于 2004 年创建,现已成为全球最流行的标记语言之一。

## 基础语法

以下是最常用的 Markdown 语法元素:

### 标题

```markdown
# H1
## H2
### H3

强调¶

*斜体* 或 _斜体_
**粗体** 或 __粗体__

列表¶

无序列表¶

- 项目 1
- 项目 2
  - 子项目 2.1

有序列表¶

1. 第一项
2. 第二项
   1. 子项 2.1

链接¶

[Google](https://www.google.com)

图片¶

![替代文本](image.jpg)

注意:如需更高级的格式设置,请参考 Markdown 指南。


Documentation built with MkDocs.

Search

From here you can search these documents. Enter your search terms below.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search