This commit is contained in:
vbarda
2024-06-13 19:24:37 -04:00
parent cfbb6ba7ee
commit fabade8ff5
+20 -19
View File
@@ -34,7 +34,7 @@
"metadata": {},
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"OPENAI_API_KEY: ········\n"
@@ -100,7 +100,6 @@
"from langchain_core.tools import tool\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
"\n",
"\n",
@@ -114,6 +113,7 @@
" else:\n",
" raise AssertionError(\"Unknown city\")\n",
"\n",
"\n",
"tools = [get_weather]"
]
},
@@ -133,6 +133,7 @@
"outputs": [],
"source": [
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"graph = create_react_agent(model, tools=tools)"
]
},
@@ -154,7 +155,7 @@
}
],
"source": [
"from IPython.display import display, Image\n",
"from IPython.display import Image, display\n",
"\n",
"display(Image(graph.get_graph().draw_mermaid_png()))"
]
@@ -183,16 +184,6 @@
"Let's run the app with an input that needs a tool call"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "46aa3f8f-067d-4178-99f8-806071ea78dd",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.tools import Tool"
]
},
{
"cell_type": "code",
"execution_count": 8,
@@ -353,16 +344,21 @@
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"prompt = ChatPromptTemplate.from_messages([\n",
" (\"system\", \"You are a helpful bot named Fred.\"),\n",
" (\"user\", \"My name is Joe\"),\n",
" (\"placeholder\", \"{messages}\"),\n",
"])\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\"system\", \"You are a helpful bot named Fred.\"),\n",
" (\"user\", \"My name is Joe\"),\n",
" (\"placeholder\", \"{messages}\"),\n",
" ]\n",
")\n",
"\n",
"\n",
"def modify_messages(messages: list):\n",
" # You can do more complex modifications here\n",
" return prompt.invoke({\"messages\": messages})\n",
"\n",
"\n",
"graph = create_react_agent(model, tools, messages_modifier=modify_messages)\n",
"\n",
"inputs = {\"messages\": [(\"user\", \"What's my name? And what's the weather in SF?\")]}\n",
@@ -416,6 +412,7 @@
],
"source": [
"from langgraph.checkpoint import MemorySaver\n",
"\n",
"graph = create_react_agent(model, tools, checkpointer=MemorySaver())\n",
"\n",
"config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
@@ -524,7 +521,11 @@
],
"source": [
"inputs = {\"messages\": [(\"user\", \"What's it known for?\")]}\n",
"print_stream(graph.stream(inputs, config={\"configurable\": {\"thread_id\": 2}}, stream_mode=\"values\"))"
"print_stream(\n",
" graph.stream(\n",
" inputs, config={\"configurable\": {\"thread_id\": 2}}, stream_mode=\"values\"\n",
" )\n",
")"
]
},
{