This commit is contained in:
Eugene Yurtsev
2024-04-09 15:06:03 -04:00
parent 52621bb803
commit 40a0f8c9ca
+132 -39
View File
@@ -22,20 +22,12 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!pip install --quiet -U langchain langchain_openai tavily-python"
]
@@ -52,7 +44,9 @@
"cell_type": "code",
"execution_count": 2,
"id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdin",
@@ -106,7 +100,9 @@
"cell_type": "code",
"execution_count": 1,
"id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
@@ -128,7 +124,9 @@
"cell_type": "code",
"execution_count": 2,
"id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langgraph.prebuilt import ToolExecutor\n",
@@ -156,7 +154,9 @@
"cell_type": "code",
"execution_count": 3,
"id": "892b54b9-75f0-4804-9ed0-88b5e5532989",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
@@ -180,7 +180,9 @@
"cell_type": "code",
"execution_count": 4,
"id": "cd3cbae5-d92c-4559-a4aa-44721b80d107",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_core.utils.function_calling import convert_to_openai_function\n",
@@ -220,7 +222,9 @@
"cell_type": "code",
"execution_count": 5,
"id": "3b541bb9-900c-40d0-964d-7b5dfee30667",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langgraph.prebuilt import ToolInvocation\n",
@@ -280,7 +284,9 @@
"cell_type": "code",
"execution_count": 6,
"id": "812b4e70-4956-4415-8880-db48b3dcbad2",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langgraph.graph import MessageGraph, END\n",
@@ -334,23 +340,79 @@
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6845ed6a-d155-4105-9160-28849877248b",
"execution_count": null,
"id": "d2219e9d-a56f-4a93-b4b0-4c9adf3b1fd5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"id": "14964e27-3326-49ef-9fc9-d646af7f43f8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langgraph.checkpoint.sqlite import SqliteSaver\n",
"\n",
"memory = SqliteSaver.from_conn_string(\":memory:\")"
"pool = AsyncConnectionPool(\n",
" # Example configuration\n",
" conninfo=\"postgresql://langchain:langchain@localhost:6024/langchain\",\n",
" max_size=20,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "79d29875-8aa8-434c-9f20-1c58346a6249",
"metadata": {},
"execution_count": 18,
"id": "15a565f9-639a-462e-b930-16468503ee2a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await pool.close()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d29cc0d4-5864-4a8c-8020-ce38f688d5c7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langgraph.checkpoint.sqlite import SqliteSaver\n",
"from langchain_postgres import PostgresSaver, PickleCheckpointSerializer\n",
"from psycopg_pool import AsyncConnectionPool, ConnectionPool\n",
"from langchain_postgres import (\n",
" PostgresSaver, PickleCheckpointSerializer\n",
")\n",
"\n",
"# pool = AsyncConnectionPool(\n",
"# # Example configuration\n",
"# conninfo=\"postgresql://langchain:langchain@localhost:6024/langc0\n",
"# hain\",\n",
"# max_size=20,\n",
"# )\n",
"\n",
"pool = ConnectionPool(\n",
" # Example configuration\n",
" conninfo=\"postgresql://langchain:langchain@localhost:6024/langchain\",\n",
" max_size=20,\n",
")\n",
"\n",
"\n",
"PostgresSaver.create_tables(pool)\n",
"\n",
"memory = PostgresSaver(\n",
" serializer=PickleCheckpointSerializer(),\n",
" sync_connection=pool,\n",
")\n",
"\n",
"# memory = SqliteSaver.from_conn_string(\":memory:\")\n",
"\n",
"# Finally, we compile it!\n",
"# This compiles it into a LangChain Runnable,\n",
"# meaning you can use it as you would any other runnable\n",
@@ -369,15 +431,17 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"id": "cfd140f0-a5a6-4697-8115-322242f197b5",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content='Hello Bob! How can I assist you today?' id='a34fcaf6-a812-47f8-921d-4ba57b8271cd'\n"
"content='Hello Bob! It seems like you mentioned your name twice. How can I assist you today?' response_metadata={'finish_reason': 'stop'} id='run-95013bc6-6893-47bb-9640-c9a32e6213be-0'\n"
]
}
],
@@ -393,15 +457,40 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "16849d25-d86e-4984-b4d7-c92076d867d5",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"{'agent': AIMessage(content=\"I found a website for toothpaste products. You can visit [Colgate's website](https://www.colgate.com/en-us/products) to explore a range of dental care products, including toothpaste.\", response_metadata={'finish_reason': 'stop'}, id='run-4c5770e9-46ef-427a-958b-3453893e87b8-0')}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"event"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "08ae8246-11d5-40e1-8567-361e5bef8917",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content='Your name is Bob. How can I help you, Bob?' id='dcf332e8-f9be-41c1-8a09-7a815a7de6d0'\n"
"content='Your name is Bob! How can I help you, Bob?' response_metadata={'finish_reason': 'stop'} id='run-7b182ace-d276-48d5-b6f6-037614b4aa8e-0'\n"
]
}
],
@@ -422,20 +511,24 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"id": "273d56a8-f40f-4a51-a27f-7c6bb2bda0ba",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content=\"I'm sorry, but I don't have access to your personal information, including your name. How can I assist you today?\" id='d7ef71f4-0393-4770-b8aa-969756791ec6'\n"
"content='' additional_kwargs={'function_call': {'arguments': '{\"query\":\"toothpaste website\"}', 'name': 'tavily_search_results_json'}} response_metadata={'finish_reason': 'function_call'} id='run-c24abecf-e217-4553-9d53-a3c8ef258ecd-0'\n",
"content=\"[{'url': 'https://www.colgate.com/en-us/products', 'content': 'Get a brighter, whiter smile when you use Colgate Optic White toothpaste, mouthwash, and toothbrush products during your oral care routine. View Products Colgate Total 360º'}]\" name='tavily_search_results_json' id='6ad0fc6f-94e2-449a-92a9-94d26fb444ec'\n",
"content=\"I found a website for toothpaste products. You can visit [Colgate's website](https://www.colgate.com/en-us/products) to explore a range of dental care products, including toothpaste.\" response_metadata={'finish_reason': 'stop'} id='run-4c5770e9-46ef-427a-958b-3453893e87b8-0'\n"
]
}
],
"source": [
"inputs = HumanMessage(content=\"what is my name?\")\n",
"inputs = HumanMessage(content=\"can you find website for toothpaste?\")\n",
"for event in app.stream(inputs, {\"configurable\": {\"thread_id\": \"3\"}}):\n",
" for v in event.values():\n",
" print(v)"
@@ -466,7 +559,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.19"
}
},
"nbformat": 4,