初始化项目:langgraph 学习笔记与 lg_common 通用组件包
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2026-08-01T07:55:31.602128Z",
|
||||
"start_time": "2026-08-01T07:55:29.787634Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"from typing import TypedDict\n",
|
||||
"\n",
|
||||
"from langgraph.types import RetryPolicy\n",
|
||||
"from loguru import logger\n",
|
||||
"from requests import HTTPError\n",
|
||||
"\n",
|
||||
"from langgraph.graph import StateGraph, START, END\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class OverallState(TypedDict):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def node_a(state: OverallState) -> OverallState:\n",
|
||||
" logger.info(\"node_a\")\n",
|
||||
" raise HTTPError\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"builder = StateGraph(state_schema=OverallState)\n",
|
||||
"builder.add_node(\n",
|
||||
" \"node_a\", node_a,\n",
|
||||
" retry_policy=RetryPolicy(\n",
|
||||
" max_attempts=3,\n",
|
||||
" jitter=True\n",
|
||||
" ))\n",
|
||||
"\n",
|
||||
"builder.add_edge(START,\"node_a\")\n",
|
||||
"builder.add_edge(\"node_a\",END)\n",
|
||||
"\n",
|
||||
"graph=builder.compile()\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" graph.invoke({})\n",
|
||||
"except HTTPError as e:\n",
|
||||
" logger.exception(e)"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001B[32m2026-08-01 15:55:29.797\u001B[0m | \u001B[1mINFO \u001B[0m | \u001B[36m__main__\u001B[0m:\u001B[36mnode_a\u001B[0m:\u001B[36m15\u001B[0m - \u001B[1mnode_a\u001B[0m\n",
|
||||
"\u001B[32m2026-08-01 15:55:30.395\u001B[0m | \u001B[1mINFO \u001B[0m | \u001B[36m__main__\u001B[0m:\u001B[36mnode_a\u001B[0m:\u001B[36m15\u001B[0m - \u001B[1mnode_a\u001B[0m\n",
|
||||
"\u001B[32m2026-08-01 15:55:31.562\u001B[0m | \u001B[1mINFO \u001B[0m | \u001B[36m__main__\u001B[0m:\u001B[36mnode_a\u001B[0m:\u001B[36m15\u001B[0m - \u001B[1mnode_a\u001B[0m\n",
|
||||
"\u001B[32m2026-08-01 15:55:31.563\u001B[0m | \u001B[31m\u001B[1mERROR \u001B[0m | \u001B[36m__main__\u001B[0m:\u001B[36m<module>\u001B[0m:\u001B[36m35\u001B[0m - \u001B[31m\u001B[1m\u001B[0m\n",
|
||||
"\u001B[33m\u001B[1mTraceback (most recent call last):\u001B[0m\n",
|
||||
"\n",
|
||||
" File \"<frozen runpy>\", line 198, in _run_module_as_main\n",
|
||||
" File \"<frozen runpy>\", line 88, in _run_code\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel_launcher.py\", line 18, in <module>\n",
|
||||
" app.launch_new_instance()\n",
|
||||
" │ └ <bound method Application.launch_instance of <class 'ipykernel.kernelapp.IPKernelApp'>>\n",
|
||||
" └ <module 'ipykernel.kernelapp' from '/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/i...\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/traitlets/config/application.py\", line 1082, in launch_instance\n",
|
||||
" app.start()\n",
|
||||
" │ └ <function IPKernelApp.start at 0x109e46840>\n",
|
||||
" └ <ipykernel.kernelapp.IPKernelApp object at 0x109e18d70>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/kernelapp.py\", line 807, in start\n",
|
||||
" self.io_loop.start()\n",
|
||||
" │ │ └ <function BaseAsyncIOLoop.start at 0x109e85300>\n",
|
||||
" │ └ <tornado.platform.asyncio.AsyncIOMainLoop object at 0x10a42d550>\n",
|
||||
" └ <ipykernel.kernelapp.IPKernelApp object at 0x109e18d70>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/tornado/platform/asyncio.py\", line 211, in start\n",
|
||||
" self.asyncio_loop.run_forever()\n",
|
||||
" │ │ └ <function BaseEventLoop.run_forever at 0x1035d7560>\n",
|
||||
" │ └ <_UnixSelectorEventLoop running=True closed=False debug=False>\n",
|
||||
" └ <tornado.platform.asyncio.AsyncIOMainLoop object at 0x10a42d550>\n",
|
||||
" File \"/Users/gqt/.local/share/uv/python/cpython-3.13.11-macos-aarch64-none/lib/python3.13/asyncio/base_events.py\", line 683, in run_forever\n",
|
||||
" self._run_once()\n",
|
||||
" │ └ <function BaseEventLoop._run_once at 0x1035dd3a0>\n",
|
||||
" └ <_UnixSelectorEventLoop running=True closed=False debug=False>\n",
|
||||
" File \"/Users/gqt/.local/share/uv/python/cpython-3.13.11-macos-aarch64-none/lib/python3.13/asyncio/base_events.py\", line 2050, in _run_once\n",
|
||||
" handle._run()\n",
|
||||
" │ └ <function Handle._run at 0x103572840>\n",
|
||||
" └ <Handle <_asyncio.TaskStepMethWrapper object at 0x10c6773a0>()>\n",
|
||||
" File \"/Users/gqt/.local/share/uv/python/cpython-3.13.11-macos-aarch64-none/lib/python3.13/asyncio/events.py\", line 89, in _run\n",
|
||||
" self._context.run(self._callback, *self._args)\n",
|
||||
" │ │ │ │ │ └ <member '_args' of 'Handle' objects>\n",
|
||||
" │ │ │ │ └ <Handle <_asyncio.TaskStepMethWrapper object at 0x10c6773a0>()>\n",
|
||||
" │ │ │ └ <member '_callback' of 'Handle' objects>\n",
|
||||
" │ │ └ <Handle <_asyncio.TaskStepMethWrapper object at 0x10c6773a0>()>\n",
|
||||
" │ └ <member '_context' of 'Handle' objects>\n",
|
||||
" └ <Handle <_asyncio.TaskStepMethWrapper object at 0x10c6773a0>()>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py\", line 621, in shell_main\n",
|
||||
" await self.dispatch_shell(msg, subshell_id=subshell_id)\n",
|
||||
" │ │ │ └ None\n",
|
||||
" │ │ └ [<zmq.Frame(b'e3fae01b-8f7'...36B)>, <zmq.Frame(b'\\x01')>, <zmq.Frame(b'<IDS|MSG>')>, <zmq.Frame(b'4d9dad6933cb'...64B)>, <zm...\n",
|
||||
" │ └ <function Kernel.dispatch_shell at 0x109dd2480>\n",
|
||||
" └ <ipykernel.ipkernel.IPythonKernel object at 0x109e1b380>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py\", line 478, in dispatch_shell\n",
|
||||
" await result\n",
|
||||
" └ <coroutine object IPythonKernel.execute_request at 0x10c3cb6a0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/ipkernel.py\", line 372, in execute_request\n",
|
||||
" await super().execute_request(stream, ident, parent)\n",
|
||||
" │ │ └ {'header': {'msg_id': '8c406f25-ebbb-410a-aacc-a8bb28894b9b', 'username': '', 'session': 'b96952d9-b243-44ee-8eef-3a21fc6793a...\n",
|
||||
" │ └ [b'e3fae01b-8f7d-4358-9fd2-c397a1e35c71', b'\\x01']\n",
|
||||
" └ <zmq.Socket(zmq.PAIR) at 0x10a41de10>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py\", line 834, in execute_request\n",
|
||||
" reply_content = await reply_content\n",
|
||||
" └ <coroutine object IPythonKernel.do_execute at 0x10a162dc0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/ipkernel.py\", line 460, in do_execute\n",
|
||||
" res = shell.run_cell(\n",
|
||||
" │ └ <function ZMQInteractiveShell.run_cell at 0x109e1c400>\n",
|
||||
" └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/ipykernel/zmqshell.py\", line 665, in run_cell\n",
|
||||
" return super().run_cell(*args, **kwargs)\n",
|
||||
" │ └ {'store_history': True, 'silent': False, 'cell_id': None}\n",
|
||||
" └ ('from typing import TypedDict\\n\\nfrom langgraph.types import RetryPolicy\\nfrom loguru import logger\\nfrom requests import HT...\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py\", line 3170, in run_cell\n",
|
||||
" result = self._run_cell(\n",
|
||||
" │ └ <function InteractiveShell._run_cell at 0x109385080>\n",
|
||||
" └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py\", line 3225, in _run_cell\n",
|
||||
" result = runner(coro)\n",
|
||||
" │ └ <coroutine object InteractiveShell.run_cell_async at 0x10d136260>\n",
|
||||
" └ <function _pseudo_sync_runner at 0x10935b880>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/async_helpers.py\", line 128, in _pseudo_sync_runner\n",
|
||||
" coro.send(None)\n",
|
||||
" │ └ <method 'send' of 'coroutine' objects>\n",
|
||||
" └ <coroutine object InteractiveShell.run_cell_async at 0x10d136260>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py\", line 3447, in run_cell_async\n",
|
||||
" has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n",
|
||||
" │ │ │ │ └ '/var/folders/_2/m2k52m654svgy0vz6nv68s7r0000gn/T/ipykernel_6932/1549273080.py'\n",
|
||||
" │ │ │ └ [<ast.ImportFrom object at 0x10d297cd0>, <ast.ImportFrom object at 0x10d297c50>, <ast.ImportFrom object at 0x10d297dd0>, <ast...\n",
|
||||
" │ │ └ <ast.Module object at 0x10d297d10>\n",
|
||||
" │ └ <function InteractiveShell.run_ast_nodes at 0x109385440>\n",
|
||||
" └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py\", line 3688, in run_ast_nodes\n",
|
||||
" if await self.run_code(code, result, async_=asy):\n",
|
||||
" │ │ │ │ └ False\n",
|
||||
" │ │ │ └ <ExecutionResult object at 10d1683c0, execution_count=7 error_before_exec=None error_in_exec=None info=<ExecutionInfo object ...\n",
|
||||
" │ │ └ <code object <module> at 0x10d133c30, file \"/var/folders/_2/m2k52m654svgy0vz6nv68s7r0000gn/T/ipykernel_6932/1549273080.py\", l...\n",
|
||||
" │ └ <function InteractiveShell.run_code at 0x1093854e0>\n",
|
||||
" └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py\", line 3748, in run_code\n",
|
||||
" exec(code_obj, self.user_global_ns, self.user_ns)\n",
|
||||
" │ │ │ │ └ <property object at 0x109357510>\n",
|
||||
" │ │ │ └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" │ │ └ <property object at 0x109357560>\n",
|
||||
" │ └ <ipykernel.zmqshell.ZMQInteractiveShell object at 0x10a29c2f0>\n",
|
||||
" └ <code object <module> at 0x10d133c30, file \"/var/folders/_2/m2k52m654svgy0vz6nv68s7r0000gn/T/ipykernel_6932/1549273080.py\", l...\n",
|
||||
"\n",
|
||||
"> File \"\u001B[32m/var/folders/_2/m2k52m654svgy0vz6nv68s7r0000gn/T/ipykernel_6932/\u001B[0m\u001B[32m\u001B[1m1549273080.py\u001B[0m\", line \u001B[33m33\u001B[0m, in \u001B[35m<module>\u001B[0m\n",
|
||||
" \u001B[1mgraph\u001B[0m\u001B[35m\u001B[1m.\u001B[0m\u001B[1minvoke\u001B[0m\u001B[1m(\u001B[0m\u001B[1m{\u001B[0m\u001B[1m}\u001B[0m\u001B[1m)\u001B[0m\n",
|
||||
" \u001B[36m│ └ \u001B[0m\u001B[36m\u001B[1m<function Pregel.invoke at 0x10bf97560>\u001B[0m\n",
|
||||
" \u001B[36m└ \u001B[0m\u001B[36m\u001B[1m<langgraph.graph.state.CompiledStateGraph object at 0x10d133b10>\u001B[0m\n",
|
||||
"\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/pregel/main.py\", line 3913, in invoke\n",
|
||||
" for chunk in self.stream(\n",
|
||||
" │ └ <function Pregel.stream at 0x10bf96700>\n",
|
||||
" └ <langgraph.graph.state.CompiledStateGraph object at 0x10d133b10>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/pregel/main.py\", line 2967, in stream\n",
|
||||
" for _ in runner.tick(\n",
|
||||
" │ │ └ <function PregelRunner.tick at 0x10bf4c720>\n",
|
||||
" │ └ <langgraph.pregel._runner.PregelRunner object at 0x10c43f950>\n",
|
||||
" └ None\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/pregel/_runner.py\", line 207, in tick\n",
|
||||
" run_with_retry(\n",
|
||||
" └ <function run_with_retry at 0x10bf33e20>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/pregel/_retry.py\", line 617, in run_with_retry\n",
|
||||
" return task.proc.invoke(task.input, config)\n",
|
||||
" │ │ │ │ └ {'configurable': {'__pregel_runtime': Runtime(context=None, store=None, stream_writer=<function Pregel.stream.<locals>.stream...\n",
|
||||
" │ │ │ └ <member 'input' of 'PregelExecutableTask' objects>\n",
|
||||
" │ │ └ PregelExecutableTask(name='node_a', input={}, proc=<langgraph._internal._runnable.RunnableSeq object at 0x10c023230>, writes=...\n",
|
||||
" │ └ <member 'proc' of 'PregelExecutableTask' objects>\n",
|
||||
" └ PregelExecutableTask(name='node_a', input={}, proc=<langgraph._internal._runnable.RunnableSeq object at 0x10c023230>, writes=...\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/_internal/_runnable.py\", line 684, in invoke\n",
|
||||
" input = context.run(step.invoke, input, config, **kwargs)\n",
|
||||
" │ │ │ │ │ │ └ {}\n",
|
||||
" │ │ │ │ │ └ {'configurable': {'__pregel_runtime': Runtime(context=None, store=None, stream_writer=<function Pregel.stream.<locals>.stream...\n",
|
||||
" │ │ │ │ └ {}\n",
|
||||
" │ │ │ └ <function RunnableCallable.invoke at 0x10bbca340>\n",
|
||||
" │ │ └ node_a(tags=None, recurse=True, explode_args=False, func_accepts={})\n",
|
||||
" │ └ <method 'run' of '_contextvars.Context' objects>\n",
|
||||
" └ <_contextvars.Context object at 0x10c307ac0>\n",
|
||||
" File \"/Volumes/ExternalStorageA/project/2026.7/langgraph学习/.venv/lib/python3.13/site-packages/langgraph/_internal/_runnable.py\", line 426, in invoke\n",
|
||||
" ret = self.func(*args, **kwargs)\n",
|
||||
" │ │ │ └ {}\n",
|
||||
" │ │ └ ({},)\n",
|
||||
" │ └ <function node_a at 0x10d178c20>\n",
|
||||
" └ node_a(tags=None, recurse=True, explode_args=False, func_accepts={})\n",
|
||||
"\n",
|
||||
" File \"\u001B[32m/var/folders/_2/m2k52m654svgy0vz6nv68s7r0000gn/T/ipykernel_6932/\u001B[0m\u001B[32m\u001B[1m1549273080.py\u001B[0m\", line \u001B[33m16\u001B[0m, in \u001B[35mnode_a\u001B[0m\n",
|
||||
" \u001B[35m\u001B[1mraise\u001B[0m \u001B[1mHTTPError\u001B[0m\n",
|
||||
" \u001B[36m └ \u001B[0m\u001B[36m\u001B[1m<class 'requests.exceptions.HTTPError'>\u001B[0m\n",
|
||||
"\n",
|
||||
"\u001B[31m\u001B[1mrequests.exceptions.HTTPError\u001B[0m\n",
|
||||
"During task with name 'node_a' and id '7ca2bca4-ef75-604f-f146-f6d764f7c0ef'\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 7
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user