mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-10 11:47:51 +02:00
[Docs] Update notebooks to use START (#902)
This commit is contained in:
@@ -12,46 +12,21 @@
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\n",
|
||||
"%pip install --quiet -U langgraph langchain-openai"
|
||||
]
|
||||
"source": ["%%capture --no-stderr\n%pip install --quiet -U langgraph langchain-openai"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.environ.get(\"OPENAI_API_KEY\"):\n",
|
||||
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"
|
||||
]
|
||||
"source": ["import getpass\nimport os\n\nif not os.environ.get(\"OPENAI_API_KEY\"):\n os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.messages import BaseMessage, HumanMessage\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"from langgraph.graph import END, MessageGraph\n",
|
||||
"\n",
|
||||
"model = ChatOpenAI(temperature=0)\n",
|
||||
"\n",
|
||||
"graph = MessageGraph()\n",
|
||||
"\n",
|
||||
"graph.add_node(\"oracle\", model)\n",
|
||||
"graph.add_edge(\"oracle\", END)\n",
|
||||
"\n",
|
||||
"graph.set_entry_point(\"oracle\")\n",
|
||||
"\n",
|
||||
"runnable = graph.compile()"
|
||||
]
|
||||
"source": ["from langchain_core.messages import BaseMessage, HumanMessage\nfrom langchain_openai import ChatOpenAI\n\nfrom langgraph.graph import END, MessageGraph\n\nmodel = ChatOpenAI(temperature=0)\n\ngraph = MessageGraph()\n\ngraph.add_node(\"oracle\", model)\ngraph.add_edge(\"oracle\", END)\n\ngraph.add_edge(START, \"oracle\")\n\nrunnable = graph.compile()"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -69,15 +44,7 @@
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from IPython.display import Image, display\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\n",
|
||||
"except Exception:\n",
|
||||
" # This requires some extra dependencies and is optional\n",
|
||||
" pass"
|
||||
]
|
||||
"source": ["from IPython.display import Image, display\n\ntry:\n display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\nexcept Exception:\n # This requires some extra dependencies and is optional\n pass"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -96,54 +63,14 @@
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runnable.invoke(HumanMessage(\"What is 1 + 1?\"))"
|
||||
]
|
||||
"source": ["runnable.invoke(HumanMessage(\"What is 1 + 1?\"))"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import Literal\n",
|
||||
"\n",
|
||||
"from langchain_core.tools import tool\n",
|
||||
"\n",
|
||||
"from langgraph.graph import END, START\n",
|
||||
"from langgraph.prebuilt import ToolNode\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def multiply(first_number: int, second_number: int):\n",
|
||||
" \"\"\"Multiplies two numbers together.\"\"\"\n",
|
||||
" return first_number * second_number\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"model = ChatOpenAI(temperature=0)\n",
|
||||
"model_with_tools = model.bind_tools(tools=[multiply])\n",
|
||||
"\n",
|
||||
"graph = MessageGraph()\n",
|
||||
"\n",
|
||||
"graph.add_node(\"oracle\", model_with_tools)\n",
|
||||
"\n",
|
||||
"tool_node = ToolNode([multiply])\n",
|
||||
"graph.add_node(\"multiply\", tool_node)\n",
|
||||
"graph.add_edge(START, \"oracle\")\n",
|
||||
"graph.add_edge(\"multiply\", END)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def router(state: list[BaseMessage]) -> Literal[\"multiply\", \"__end__\"]:\n",
|
||||
" tool_calls = state[-1].additional_kwargs.get(\"tool_calls\", [])\n",
|
||||
" if len(tool_calls):\n",
|
||||
" return \"multiply\"\n",
|
||||
" else:\n",
|
||||
" return END\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"graph.add_conditional_edges(\"oracle\", router)\n",
|
||||
"runnable = graph.compile()"
|
||||
]
|
||||
"source": ["from typing import Literal\n\nfrom langchain_core.tools import tool\n\nfrom langgraph.graph import END, START\nfrom langgraph.prebuilt import ToolNode\n\n\n@tool\ndef multiply(first_number: int, second_number: int):\n \"\"\"Multiplies two numbers together.\"\"\"\n return first_number * second_number\n\n\nmodel = ChatOpenAI(temperature=0)\nmodel_with_tools = model.bind_tools(tools=[multiply])\n\ngraph = MessageGraph()\n\ngraph.add_node(\"oracle\", model_with_tools)\n\ntool_node = ToolNode([multiply])\ngraph.add_node(\"multiply\", tool_node)\ngraph.add_edge(START, \"oracle\")\ngraph.add_edge(\"multiply\", END)\n\n\ndef router(state: list[BaseMessage]) -> Literal[\"multiply\", \"__end__\"]:\n tool_calls = state[-1].additional_kwargs.get(\"tool_calls\", [])\n if len(tool_calls):\n return \"multiply\"\n else:\n return END\n\n\ngraph.add_conditional_edges(\"oracle\", router)\nrunnable = graph.compile()"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -161,13 +88,7 @@
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"try:\n",
|
||||
" display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\n",
|
||||
"except Exception:\n",
|
||||
" # This requires some extra dependencies and is optional\n",
|
||||
" pass"
|
||||
]
|
||||
"source": ["try:\n display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\nexcept Exception:\n # This requires some extra dependencies and is optional\n pass"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -187,9 +108,7 @@
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runnable.invoke(HumanMessage(\"What is 123 * 456?\"))"
|
||||
]
|
||||
"source": ["runnable.invoke(HumanMessage(\"What is 123 * 456?\"))"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -208,16 +127,14 @@
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runnable.invoke(HumanMessage(\"What is your name?\"))"
|
||||
]
|
||||
"source": ["runnable.invoke(HumanMessage(\"What is your name?\"))"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"source": [""]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user