初始化项目:langgraph 学习笔记与 lg_common 通用组件包
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from langchain.chat_models import init_chat_model\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"\n",
|
||||
"llm = init_chat_model(\n",
|
||||
" model=\"openai:qwen3.6-flash\",\n",
|
||||
" base_url=os.getenv(\"BASE_URL\"),\n",
|
||||
" api_key=os.getenv(\"API_KEY\")\n",
|
||||
")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"from langgraph.graph.message import MessagesState\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class OverallState(MessagesState):\n",
|
||||
" output: str\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def llm_node(state: OverallState) -> OverallState:\n",
|
||||
" messages = state[\"messages\"]\n",
|
||||
" res = llm.invoke(messages)\n",
|
||||
" return {\n",
|
||||
" \"messages\": [res]\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def output_node(state: OverallState) -> OverallState:\n",
|
||||
" return {\n",
|
||||
" \"output\": state[\"messages\"][-1].content\n",
|
||||
" }"
|
||||
],
|
||||
"id": "b443de7fa7929ba2",
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"from langgraph.graph import StateGraph, START, END\n",
|
||||
"\n",
|
||||
"builder = StateGraph(state_schema=OverallState)\n",
|
||||
"builder.add_node(\"llm_node\", llm_node)\n",
|
||||
"builder.add_node(\"output_node\", output_node)\n",
|
||||
"\n",
|
||||
"builder.add_edge(START, \"llm_node\")\n",
|
||||
"builder.add_edge(\"llm_node\", \"output_node\")\n",
|
||||
"builder.add_edge(\"output_node\", END)"
|
||||
],
|
||||
"id": "887432b11f020144",
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"from langgraph.checkpoint.postgres import PostgresSaver\n",
|
||||
"from langchain_core.runnables import RunnableConfig\n",
|
||||
"from rich import print as rp\n",
|
||||
"\n",
|
||||
"with PostgresSaver.from_conn_string(os.getenv(\"LANGGRAPH_DATABASE_URL\")) as checkpointer:\n",
|
||||
" checkpointer.setup()\n",
|
||||
" graph = builder.compile(checkpointer=checkpointer)\n",
|
||||
"\n",
|
||||
" config: RunnableConfig = {\n",
|
||||
" \"configurable\": {\n",
|
||||
" \"thread_id\": \"chapter_0804\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" messages1 = {\n",
|
||||
" \"messages\": [\n",
|
||||
" {\"role\": \"user\", \"content\": \"你好,我是gqt\"}\n",
|
||||
" ]\n",
|
||||
" }\n",
|
||||
" messages2 = {\n",
|
||||
" \"messages\": [\n",
|
||||
" {\"role\": \"user\", \"content\": \"你好,我是谁?\"}\n",
|
||||
" ]\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" res1 = graph.invoke(messages2, config=config)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" rp(res1)\n",
|
||||
" history_checkpoint = list(graph.get_state_history(config=config))\n",
|
||||
"\n",
|
||||
" # rp(history_checkpoint)"
|
||||
],
|
||||
"id": "e0f6b05e0dfeb578",
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user