244 lines
11 KiB
Plaintext
244 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2026-08-16T14:10:13.316314Z",
|
|
"start_time": "2026-08-16T14:10:13.292407Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"from typing import TypedDict\n",
|
|
"from rich import print as rp\n",
|
|
"\n",
|
|
"from langchain_core.messages import HumanMessage, SystemMessage, AIMessage\n",
|
|
"from langgraph.graph import MessagesState, StateGraph, START, END\n",
|
|
"from langgraph.checkpoint.memory import InMemorySaver\n",
|
|
"\n",
|
|
"from lg_common import get_llm\n",
|
|
"\n",
|
|
"\n",
|
|
"llm = get_llm()\n",
|
|
"\n",
|
|
"\n",
|
|
"class ParentState(TypedDict):\n",
|
|
" input_text_list: list[str]\n",
|
|
" output_text_list: list[str]\n",
|
|
"\n",
|
|
"\n",
|
|
"# ============ 子图:处理单条输入 ============\n",
|
|
"def llm_node(state: MessagesState) -> MessagesState:\n",
|
|
" history = list(state[\"messages\"] or [])\n",
|
|
" if not any(m.type == \"system\" for m in history):\n",
|
|
" history.insert(0, SystemMessage(\"用最简短的话回答用户的提问\"))\n",
|
|
" rp(\"子图 llm_node 收到的历史:\", [m.content for m in history])\n",
|
|
" response = llm.invoke(history)\n",
|
|
" return {\"messages\": [AIMessage(content=response.content)]}\n",
|
|
"\n",
|
|
"\n",
|
|
"sub_builder = StateGraph(state_schema=MessagesState)\n",
|
|
"sub_builder.add_node(\"llm_node\", llm_node)\n",
|
|
"sub_builder.add_edge(START, \"llm_node\")\n",
|
|
"sub_builder.add_edge(\"llm_node\", END)\n",
|
|
"\n",
|
|
"# ==================================================\n",
|
|
"# 【策略切换点】只需修改这一行\n",
|
|
"# sub_graph = sub_builder.compile() # Per-invocation:每次调用状态隔离\n",
|
|
"sub_graph = sub_builder.compile(checkpointer=True) # Per-thread:多次调用共享历史\n",
|
|
"# ==================================================\n",
|
|
"\n",
|
|
"\n",
|
|
"# ============ 父图:循环多次调用同一个子图 ============\n",
|
|
"def call_sub_graph_node(state: ParentState) -> ParentState:\n",
|
|
" output_text_list = []\n",
|
|
" for input_text in state[\"input_text_list\"]:\n",
|
|
" res = sub_graph.invoke({\"messages\": [HumanMessage(input_text)]})\n",
|
|
" output_text_list.append(res[\"messages\"][-1].content)\n",
|
|
" return ParentState(output_text_list=output_text_list)\n",
|
|
"\n",
|
|
"\n",
|
|
"parent_builder = StateGraph(state_schema=ParentState)\n",
|
|
"parent_builder.add_node(\"call_sub_graph_node\", call_sub_graph_node)\n",
|
|
"parent_builder.add_edge(START, \"call_sub_graph_node\")\n",
|
|
"parent_builder.add_edge(\"call_sub_graph_node\", END)\n",
|
|
"\n",
|
|
"checkpoint_saver = InMemorySaver()\n",
|
|
"parent_graph = parent_builder.compile(checkpointer=checkpoint_saver)\n"
|
|
],
|
|
"id": "d58b69c8b0881163",
|
|
"outputs": [],
|
|
"execution_count": 36
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-08-16T14:10:13.332852Z",
|
|
"start_time": "2026-08-16T14:10:13.325726Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"q_list_1 = [\n",
|
|
" \"2*2=?\",\n",
|
|
" \"100*100=?\"\n",
|
|
"]\n",
|
|
"q_list_2 = [\n",
|
|
" \"原来基础上+100=?\",\n",
|
|
" \"原来基础上+1=?\"\n",
|
|
"]\n",
|
|
"config = {\n",
|
|
" \"configurable\": {\n",
|
|
" \"thread_id\": 12345\n",
|
|
" }\n",
|
|
"}"
|
|
],
|
|
"id": "c0b484b98164fa46",
|
|
"outputs": [],
|
|
"execution_count": 37
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-08-16T14:10:18.105814Z",
|
|
"start_time": "2026-08-16T14:10:13.333781Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"res1 = parent_graph.invoke({\"input_text_list\": q_list_1}, config=config)\n",
|
|
"rp(res1)"
|
|
],
|
|
"id": "8bf144dbd0bee503",
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"子图 llm_node 收到的历史:\n",
|
|
"\u001B[1m[\u001B[0m\u001B[32m'用最简短的话回答用户的提问'\u001B[0m, \u001B[32m'2*\u001B[0m\u001B[32m2\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">子图 llm_node 收到的历史:\n",
|
|
"<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'用最简短的话回答用户的提问'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'2*2=?'</span><span style=\"font-weight: bold\">]</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"子图 llm_node 收到的历史:\n",
|
|
"\u001B[1m[\u001B[0m\u001B[32m'用最简短的话回答用户的提问'\u001B[0m, \u001B[32m'100*\u001B[0m\u001B[32m100\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">子图 llm_node 收到的历史:\n",
|
|
"<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'用最简短的话回答用户的提问'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'100*100=?'</span><span style=\"font-weight: bold\">]</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\u001B[1m{\u001B[0m\u001B[32m'input_text_list'\u001B[0m: \u001B[1m[\u001B[0m\u001B[32m'2*\u001B[0m\u001B[32m2\u001B[0m\u001B[32m=?'\u001B[0m, \u001B[32m'100*\u001B[0m\u001B[32m100\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m, \u001B[32m'output_text_list'\u001B[0m: \u001B[1m[\u001B[0m\u001B[32m'4'\u001B[0m, \u001B[32m'10000'\u001B[0m\u001B[1m]\u001B[0m\u001B[1m}\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'input_text_list'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'2*2=?'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'100*100=?'</span><span style=\"font-weight: bold\">]</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'output_text_list'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'4'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'10000'</span><span style=\"font-weight: bold\">]}</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"execution_count": 38
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-08-16T14:10:22.474734Z",
|
|
"start_time": "2026-08-16T14:10:18.117203Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"res2 = parent_graph.invoke({\"input_text_list\": q_list_2}, config=config)\n",
|
|
"rp(res2)"
|
|
],
|
|
"id": "703d97c2191d3985",
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"子图 llm_node 收到的历史:\n",
|
|
"\u001B[1m[\u001B[0m\u001B[32m'用最简短的话回答用户的提问'\u001B[0m, \u001B[32m'2*\u001B[0m\u001B[32m2\u001B[0m\u001B[32m=?'\u001B[0m, \u001B[32m'4'\u001B[0m, \u001B[32m'原来基础上+\u001B[0m\u001B[32m100\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">子图 llm_node 收到的历史:\n",
|
|
"<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'用最简短的话回答用户的提问'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'2*2=?'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'4'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'原来基础上+100=?'</span><span style=\"font-weight: bold\">]</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"子图 llm_node 收到的历史:\n",
|
|
"\u001B[1m[\u001B[0m\u001B[32m'用最简短的话回答用户的提问'\u001B[0m, \u001B[32m'100*\u001B[0m\u001B[32m100\u001B[0m\u001B[32m=?'\u001B[0m, \u001B[32m'10000'\u001B[0m, \u001B[32m'原来基础上+\u001B[0m\u001B[32m1\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">子图 llm_node 收到的历史:\n",
|
|
"<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'用最简短的话回答用户的提问'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'100*100=?'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'10000'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'原来基础上+1=?'</span><span style=\"font-weight: bold\">]</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\u001B[1m{\u001B[0m\u001B[32m'input_text_list'\u001B[0m: \u001B[1m[\u001B[0m\u001B[32m'原来基础上+\u001B[0m\u001B[32m100\u001B[0m\u001B[32m=?'\u001B[0m, \u001B[32m'原来基础上+\u001B[0m\u001B[32m1\u001B[0m\u001B[32m=?'\u001B[0m\u001B[1m]\u001B[0m, \u001B[32m'output_text_list'\u001B[0m: \u001B[1m[\u001B[0m\u001B[32m'104'\u001B[0m, \u001B[32m'10001'\u001B[0m\u001B[1m]\u001B[0m\u001B[1m}\u001B[0m\n"
|
|
],
|
|
"text/html": [
|
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'input_text_list'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'原来基础上+100=?'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'原来基础上+1=?'</span><span style=\"font-weight: bold\">]</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'output_text_list'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'104'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'10001'</span><span style=\"font-weight: bold\">]}</span>\n",
|
|
"</pre>\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"execution_count": 39
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|