docs: reduce number of cells in streaming how-tos (#997)

This commit is contained in:
Vadym Barda
2024-07-11 17:19:58 -04:00
committed by GitHub
parent 85ec1934de
commit 39321ba042
2 changed files with 37 additions and 188 deletions
@@ -42,7 +42,7 @@
"metadata": {},
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"OPENAI_API_KEY: ········\n"
@@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "d59234f9-173e-469d-a725-c13e0979663e",
"metadata": {},
"outputs": [],
@@ -90,16 +90,7 @@
"from langchain_core.messages import AIMessageChunk\n",
"from langchain_core.runnables.config import ensure_config, get_callback_manager_for_config\n",
"\n",
"openai_client = AsyncOpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b29a083e-b753-4392-807d-3625ac85f08f",
"metadata": {},
"outputs": [],
"source": [
"openai_client = AsyncOpenAI()\n",
"# define tool schema for openai tool calling\n",
"\n",
"tool = {\n",
@@ -119,16 +110,7 @@
" ]\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7af98437-f0d8-4110-a33b-ae5d1331d509",
"metadata": {},
"outputs": [],
"source": [
"}\n",
"async def call_model(state, config=None):\n",
" config = ensure_config(config | {\"tags\": [\"agent_llm\"]})\n",
" callback_manager = get_callback_manager_for_config(config)\n",
@@ -199,11 +181,12 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "b90941d8-afe4-42ec-9262-9c3b87c3b1ec",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from langchain_core.runnables import RunnableLambda\n",
"\n",
"async def get_items(place: str) -> str:\n",
@@ -225,39 +208,13 @@
" tool_logger.invoke(token)\n",
" tokens.append(token)\n",
"\n",
" return \", \".join(tokens)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "746129da-e926-4844-8da7-bd3bac8276c3",
"metadata": {},
"outputs": [],
"source": [
" return \", \".join(tokens)\n",
"\n",
"# define mapping to look up functions when running tools\n",
"function_name_to_function = {\n",
" \"get_items\": get_items\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6829bd73-8a8e-4726-b73d-393a897b42d1",
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8fc0fbe8-691f-45b1-b506-1222c797d588",
"metadata": {},
"outputs": [],
"source": [
"}\n",
"\n",
"async def call_tools(state):\n",
" messages = state[\"messages\"]\n",
"\n",
@@ -288,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "228260be-1f9a-4195-80e0-9604f8a5dba6",
"metadata": {},
"outputs": [],
@@ -296,57 +253,24 @@
"import operator\n",
"from typing import Annotated, TypedDict, Literal\n",
"\n",
"from langgraph.graph import StateGraph, END"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "09a7c707-e088-4814-9d2f-eff68ab88771",
"metadata": {},
"outputs": [],
"source": [
"from langgraph.graph import StateGraph, END\n",
"\n",
"class State(TypedDict):\n",
" messages: Annotated[list, operator.add]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d36f2d38-024c-4be0-a79a-96cfd23f9fe5",
"metadata": {},
"outputs": [],
"source": [
" messages: Annotated[list, operator.add]\n",
"\n",
"def should_continue(state) -> Literal[\"tools\", END]:\n",
" messages = state['messages']\n",
" last_message = messages[-1]\n",
" if last_message[\"tool_calls\"]:\n",
" return \"tools\"\n",
" return END"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a53aab90-9206-46ef-be83-81c2de2c007a",
"metadata": {},
"outputs": [],
"source": [
" return END\n",
"\n",
"workflow = StateGraph(State)\n",
"workflow.set_entry_point(\"model\")\n",
"workflow.add_node(\"model\", call_model) # i.e. our \"agent\"\n",
"workflow.add_node(\"tools\", call_tools)\n",
"workflow.add_conditional_edges(\"model\", should_continue)\n",
"workflow.add_edge(\"tools\", \"model\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "5aac9d6a-f182-4ddd-af77-3e26fdf4170b",
"metadata": {},
"outputs": [],
"source": [
"workflow.add_edge(\"tools\", \"model\")\n",
"graph = workflow.compile()"
]
},
@@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "d59234f9-173e-469d-a725-c13e0979663e",
"metadata": {},
"outputs": [],
@@ -90,16 +90,7 @@
"from langchain_core.messages import AIMessageChunk\n",
"from langchain_core.runnables.config import ensure_config, get_callback_manager_for_config\n",
"\n",
"openai_client = AsyncOpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b29a083e-b753-4392-807d-3625ac85f08f",
"metadata": {},
"outputs": [],
"source": [
"openai_client = AsyncOpenAI()\n",
"# define tool schema for openai tool calling\n",
"\n",
"tool = {\n",
@@ -119,16 +110,7 @@
" ]\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7af98437-f0d8-4110-a33b-ae5d1331d509",
"metadata": {},
"outputs": [],
"source": [
"}\n",
"async def call_model(state, config=None):\n",
" config = ensure_config(config | {\"tags\": [\"agent_llm\"]})\n",
" callback_manager = get_callback_manager_for_config(config)\n",
@@ -199,11 +181,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2cb38dd9-74d8-456d-9e39-4655f2bf3f37",
"execution_count": null,
"id": "b756ea32",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"async def get_items(place: str) -> str:\n",
" \"\"\"Use this tool to look up which items are in the given place.\"\"\"\n",
" if \"bed\" in place: # For under the bed\n",
@@ -211,39 +195,13 @@
" if \"shelf\" in place: # For 'shelf'\n",
" return \"books, penciles and pictures\"\n",
" else: # if the agent decides to ask about a different place\n",
" return \"cat snacks\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "746129da-e926-4844-8da7-bd3bac8276c3",
"metadata": {},
"outputs": [],
"source": [
" return \"cat snacks\"\n",
"\n",
"# define mapping to look up functions when running tools\n",
"function_name_to_function = {\n",
" \"get_items\": get_items\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6829bd73-8a8e-4726-b73d-393a897b42d1",
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8fc0fbe8-691f-45b1-b506-1222c797d588",
"metadata": {},
"outputs": [],
"source": [
"}\n",
"\n",
"async def call_tools(state):\n",
" messages = state[\"messages\"]\n",
"\n",
@@ -274,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "228260be-1f9a-4195-80e0-9604f8a5dba6",
"metadata": {},
"outputs": [],
@@ -282,57 +240,24 @@
"import operator\n",
"from typing import Annotated, TypedDict, Literal\n",
"\n",
"from langgraph.graph import StateGraph, END"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "09a7c707-e088-4814-9d2f-eff68ab88771",
"metadata": {},
"outputs": [],
"source": [
"from langgraph.graph import StateGraph, END\n",
"\n",
"class State(TypedDict):\n",
" messages: Annotated[list, operator.add]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d36f2d38-024c-4be0-a79a-96cfd23f9fe5",
"metadata": {},
"outputs": [],
"source": [
" messages: Annotated[list, operator.add]\n",
"\n",
"def should_continue(state) -> Literal[\"tools\", END]:\n",
" messages = state['messages']\n",
" last_message = messages[-1]\n",
" if last_message[\"tool_calls\"]:\n",
" return \"tools\"\n",
" return END"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a53aab90-9206-46ef-be83-81c2de2c007a",
"metadata": {},
"outputs": [],
"source": [
" return END\n",
"\n",
"workflow = StateGraph(State)\n",
"workflow.set_entry_point(\"model\")\n",
"workflow.add_node(\"model\", call_model) # i.e. our \"agent\"\n",
"workflow.add_node(\"tools\", call_tools)\n",
"workflow.add_conditional_edges(\"model\", should_continue)\n",
"workflow.add_edge(\"tools\", \"model\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "5aac9d6a-f182-4ddd-af77-3e26fdf4170b",
"metadata": {},
"outputs": [],
"source": [
"workflow.add_edge(\"tools\", \"model\")\n",
"graph = workflow.compile()"
]
},