In [1]:
Copied!
%%capture --no-stderr
%pip install --quiet -U langgraph
%%capture --no-stderr
%pip install --quiet -U langgraph
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()))
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()))
请注意,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"})
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¶

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)
图片¶

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