{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain_groq import ChatGroq\n", "llm=ChatGroq(model_name=\"Gemma2-9b-It\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello! 👋 How can I help you today? 😊\\n'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "llm.invoke(\"hi\").content" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from langgraph.graph import StateGraph,MessagesState, START, END\n", "from langgraph.graph.message import add_messages\n", "from typing import Annotated, Literal, TypedDict\n", "from langchain_core.tools import tool\n", "from langchain_core.messages import HumanMessage\n", "from langgraph.checkpoint.memory import MemorySaver\n", "from langgraph.prebuilt import ToolNode" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def call_model(state:MessagesState):\n", " messages = state['messages']\n", " response = llm.invoke(messages)\n", " return {\"messages\": [response]}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "workflow=StateGraph(MessagesState)\n", "workflow.add_node(\"chatbot\",call_model)\n", "workflow.add_edge(START, \"chatbot\")\n", "workflow.add_edge(\"chatbot\",END)\n", "app=workflow.compile()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGsAAADqCAIAAAAqMSwmAAAAAXNSR0IArs4c6QAAFt9JREFUeJztnXtgE1W6wE8ySZp3miZt+n5T+qQgBQELLbY8LS21CgJlAZWVpcvuvbgruysuuF653Iou966r7F2KrlBFWAWsIgWFIm+oPGzpi77pg7Z5v1+T3D/CrSxNMpNOQk7r/P7rzJzpl1/OTM6cc+Z8FLvdDkgIQPV3AGMe0iBRSINEIQ0ShTRIFNIgUWgEy2vkFpXMotegejVqtdhttjHQNkJogEajsvkIm0cThtLZXEISKKNrD8r6TW0/6DrqdAw2Bdgp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}, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import Image, display\n", "display(Image(app.get_graph().draw_mermaid_png()))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "input={\"messages\":[\"hi, my name is Parthib\"]}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [HumanMessage(content='hi, my name is Parthib', additional_kwargs={}, response_metadata={}, id='0362fb19-f203-4a00-87f7-4ad6162c1260'),\n", " AIMessage(content=\"Hi Parthib! It's nice to meet you. 👋\\n\\nIs there anything I can help you with today?\\n\", additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 28, 'prompt_tokens': 17, 'total_tokens': 45, 'completion_time': 0.050909091, 'prompt_time': 0.001898827, 'queue_time': 0.232850566, 'total_time': 0.052807918}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-752e807b-be6f-4ba5-a277-b4ed014f6300-0', usage_metadata={'input_tokens': 17, 'output_tokens': 28, 'total_tokens': 45})]}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "app.invoke(input)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "@tool\n", "def search(query: str):\n", " \"\"\"this is my custom tool.\"\"\"\n", " if \"sf\" in query.lower() or \"san francisco\" in query.lower():\n", " return \"It's 60 degrees and foggy.\"\n", " return \"It's 90 degrees and sunny.\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"It's 60 degrees and foggy.\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "search.invoke(\"what is a temprature in sf?\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"It's 90 degrees and sunny.\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "search.invoke(\"Tell me the temperature in India\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "tools=[search]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "tool_node=ToolNode(tools)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "llm_with_tool = llm.bind_tools(tools)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "\n", "def call_model(state: MessagesState):\n", " messages = state['messages']\n", " response = llm_with_tool.invoke(messages)\n", " return {\"messages\": [response]}" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "response=call_model({\"messages\": [\"hi how are you?\"]})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_fxmj', 'function': {'arguments': '{\"query\":\"hi how are you\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 81, 'prompt_tokens': 941, 'total_tokens': 1022, 'completion_time': 0.147272727, 'prompt_time': 0.080406297, 'queue_time': 0.237967498, 'total_time': 0.227679024}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-7126749c-2fe7-466e-bb0d-459fbe3f7552-0', tool_calls=[{'name': 'search', 'args': {'query': 'hi how are you'}, 'id': 'call_fxmj', 'type': 'tool_call'}], usage_metadata={'input_tokens': 941, 'output_tokens': 81, 'total_tokens': 1022})]}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def router_function(state: MessagesState) -> Literal[\"tools\", END]:\n", " #print(f\"here is a state from should continue {state}\")\n", " messages = state['messages']\n", " last_message = messages[-1]\n", " #print(f\"here is a last message from should continue {last_message}\")\n", " if last_message.tool_calls:\n", " return \"tools\"\n", " return END" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# Define a new graph\n", "workflow = StateGraph(MessagesState)\n", "\n", "workflow.add_node(\"agent\", call_model)\n", "workflow.add_node(\"tools\", tool_node)\n", "\n", "workflow.add_edge(START, \"agent\")\n", "\n", "workflow.add_conditional_edges(\"agent\",router_function,{\"tools\": \"tools\", END: END})\n", "\n", "app = workflow.compile()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAN4AAAD5CAIAAADHrBxuAAAAAXNSR0IArs4c6QAAHVNJREFUeJzt3XlcVFXDB/BzZ1+ZGYZdFllkUVAQzFxSyyVzTdQQxDI1l8wstzSz3HIhF55cCjU1FZfwodxLK63HUksTFURgQNlRttn35f1jeoloUJxm5tx753w//gHM9hv8ce4y956LWSwWgCD4Q4EdAEFsQ9VEcApVE8EpVE0Ep1A1EZxC1URwigY7gLM01upUMpNaYdSqzXqtGXacJ8MwQGNgXD6N40H18KR7iOmwE0GGkWy/ZpVEff+Oqixf5RfC0qpNHD5NQJT/YwzoNWaVwqiWm6g0oJKbQrtxw7tzvQNZsJPBQZ5q1j7Q/HqqUehNF/szw2K5RB91Gqp19wtU0kd6o8HSd4wXYf7AHIck1bx0/FFDlb7vaHFAOBt2FgeT3FL+erIhKonf+yUx7CwuRfhqqhXGIxmVQ9N9g6M4sLM40b3f5fm/yifMD4QdxHWIXU2dxnRoXUXqkiAOn7Tbcy1q72tOZtXMXB+GYRjsLK5A4GrKmwzH/1M1bVUo7CCuo5IbD64tn50RDjuIKxB4v+aRjIr0ZSGwU7gU14M29s2A45lVsIO4AlFHzQuHH3bvL/ANdscdK0XX5c2PDM+OIPlWESFHTcktpVFnds9eAgCikjwkecrmR3rYQZyLkNX89VRD39FesFPA1He0+NdTjbBTOBfxqll0XR6VyBd4ud0u6NbC4nhMNqXugQZ2ECciXj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druIgUAAMCgO2YfH6omTlGpVH+/ANgpAMQMaF0TwSlUTQSnUDURnELVRHAKVRPBKVRNBKdQNRGcQtVEcApVE8Ep258GMViYGRD4IDSukE6h4i4/V0SnoE/f/o7OoLR3+VPboyZfRK8vt30NLEKoKFR6+uHubA0mm9JYY/vyTW7rUYWGJ7D992q7mj5BTOIeuatRGr06MXlC3A1Q/p2ZOo1bXsq3fUaj2TfE9qEq7Y6anSJYP/+3zsnBnOL7QzW9hopgp7AhNJanU5vuXG6CHQQvrp19JPCkewfavq7f4y46XXBFVpKn7DFQLPJlUGl432DSqk3yBv0vJx4Nf9XXJxi/VzH8/vBDBpsW0pXnthcFNJstjbW6wqtS3yBmUvuDyOOqCQC4X6DK+0lad19LpeF6AS/wosubDJ27cpOGikQ+uFvLbOPWz9K71+RmI1DJjbCzQECjYzwhLX6QMLIn/zF3e0I1W+g0ZsdlczyLGbC4eB/X27CYgV6H69+qkzBZlI7s/uloNRHExQg20iDuA1UTwSlUTQSnUDURnELVRHAKVRPBqf8Dr/Qwy4438NEAAAAASUVORK5CYII=", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import Image, display\n", "display(Image(app.get_graph().draw_mermaid_png()))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [HumanMessage(content='hi how are you?', additional_kwargs={}, response_metadata={}, id='ee908a18-fbee-4182-8478-0def6aae244c'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_2mrs', 'function': {'arguments': '{\"query\":\"how are you\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 82, 'prompt_tokens': 941, 'total_tokens': 1023, 'completion_time': 0.149090909, 'prompt_time': 0.054217907, 'queue_time': 0.232085166, 'total_time': 0.203308816}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-946408e0-d62c-4539-bafc-fdd22a1a9820-0', tool_calls=[{'name': 'search', 'args': {'query': 'how are you'}, 'id': 'call_2mrs', 'type': 'tool_call'}], usage_metadata={'input_tokens': 941, 'output_tokens': 82, 'total_tokens': 1023}),\n", " ToolMessage(content=\"It's 90 degrees and sunny.\", name='search', id='dafff150-754c-420a-b0cd-7d8f4dc54a65', tool_call_id='call_2mrs')]}" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "app.invoke({\"messages\": [\"hi how are you?\"]})" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [HumanMessage(content='tell me the temperature in sf??', additional_kwargs={}, response_metadata={}, id='b29b6cb9-fb9f-45c9-bbcf-f4e6f57a9648'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_tkv9', 'function': {'arguments': '{\"query\":\"temperature in san francisco\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 82, 'prompt_tokens': 943, 'total_tokens': 1025, 'completion_time': 0.149090909, 'prompt_time': 0.041112254, 'queue_time': 0.232393693, 'total_time': 0.190203163}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-3dd52d5e-dcd5-4c30-bc81-4f741419a2a8-0', tool_calls=[{'name': 'search', 'args': {'query': 'temperature in san francisco'}, 'id': 'call_tkv9', 'type': 'tool_call'}], usage_metadata={'input_tokens': 943, 'output_tokens': 82, 'total_tokens': 1025}),\n", " ToolMessage(content=\"It's 60 degrees and foggy.\", name='search', id='43c2a5a4-6d92-4561-a602-5663782cfb68', tool_call_id='call_tkv9')]}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "app.invoke({\"messages\": [\"tell me the temperature in sf??\"]})" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "workflow.add_edge(\"tools\", 'agent')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "\n", "app = workflow.compile()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAANgAAAD5CAIAAADKsmwpAAAAAXNSR0IArs4c6QAAIABJREFUeJztnWdcFNf+xs9sZTu9dxAEUVQsEYxdY4uIBQsmdm8sNyFGk5jcxMSLxhtzjbEk1mgMKpYgxnLFht3EgoUmIEgvy1K2L9vm/2L9o9ksiLizZ5Y9348vdndmzu9Z9vHMmVN+B8NxHCAQsKHAFoBAAGREBFlARkSQAmREBClARkSQAmREBCmgwRbQEZqVuvoqtUKqU0i1Wi2uVVtBDxSTRaExMDaPxuZT3XzsYMshHdZkRLlEU5gpL86WSeo1PEc6m0dl82h8Rzqwhq5QvQ7UljQrpHI6k1L2WBEQwQnszgnszoWtiyxgVtGhrdfhN0/Wi6qanTwZgRFcr2AWbEWvhUqhe5otryhUVBWrosc7denFg60IPlZgxJw/xJeP1kW/7dRriANsLWZGUq+5eaq+WaEb9Y47i0uFLQcmZDfi5aNCOzbljXHOsIUQiKi6OW1b5ejZ7t5d2LC1QIPURjyfXOseYNc9RgBbiCU4vq3yzThnZ08mbCFwIK8R036sDO7JjYi2CRcaOL6tonuMfXBPW3yCIWk/4rW0Ov9wjk25EAAQt9T7j//VN9aqYQuBABmNmJ8ppdEpPYfYwxYCgYRPfTOOCkl7myIOMhrxytG63sNs0YUAAAzD/MM5N0/WwxZiaUhnxHsXGiNi+EyW7fZl9B7mkPunRCXXwRZiUchlRBzHy/IV0eM7c2dNexg0yeXBlSbYKiwKuYxYnCVnssglCQq+oezsm2LYKiwKuX71p9nygAiOhYN+8sknJ0+e7MCFI0aMqKq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dke/tRESShrf2ac26JCx/IIgc7ObgxqDSyd32rFDqJSH3jhHD0u26uvraY6MiqecnG4U9z5A+uNNU8VVFppL5VC5zpkgaNfzinz0gHB1fUOrQ+XmLEFpqVpB6bx/XAjkP2OhvRBu01IgJBKKgWQZACZEQEKUBGRJACZEQEKUBGRJACZEQEKfg/zsZU4/1PoqEAAAAASUVORK5CYII=", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import Image, display\n", "display(Image(app.get_graph().draw_mermaid_png()))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "here is output from agent\n", "_______\n", "{'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_bad9', 'function': {'arguments': '{\"query\":\"weather in sf\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 82, 'prompt_tokens': 942, 'total_tokens': 1024, 'completion_time': 0.149090909, 'prompt_time': 0.032922777, 'queue_time': 0.23775662999999997, 'total_time': 0.182013686}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-6cb3a8df-3aae-4efe-b0eb-f4a50feeba09-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in sf'}, 'id': 'call_bad9', 'type': 'tool_call'}], usage_metadata={'input_tokens': 942, 'output_tokens': 82, 'total_tokens': 1024})]}\n", "\n", "\n", "here is output from tools\n", "_______\n", "{'messages': [ToolMessage(content=\"It's 60 degrees and foggy.\", name='search', id='024d6352-1baf-47d2-992f-af1380ecf3a5', tool_call_id='call_bad9')]}\n", "\n", "\n", "here is output from agent\n", "_______\n", "{'messages': [AIMessage(content=\"It's 60 degrees and foggy. \\n\", additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 14, 'prompt_tokens': 1020, 'total_tokens': 1034, 'completion_time': 0.025454545, 'prompt_time': 0.035590263, 'queue_time': 0.237606781, 'total_time': 0.061044808}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-b569af82-55e4-46ae-8d6e-7d2088a2bdfb-0', usage_metadata={'input_tokens': 1020, 'output_tokens': 14, 'total_tokens': 1034})]}\n", "\n", "\n" ] } ], "source": [ "for output in app.stream({\"messages\": [\"what is the weather in sf\"]}):\n", " for key,value in output.items():\n", " print(f\"here is output from {key}\")\n", " print(\"_______\")\n", " print(value)\n", " print(\"\\n\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "from langgraph.checkpoint.memory import MemorySaver\n", "\n", "memory = MemorySaver()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "workflow3 = StateGraph(MessagesState)\n", "\n", "workflow3.add_node(\"agent\", call_model)\n", "workflow3.add_node(\"tools\", tool_node)\n", "\n", "workflow3.add_edge(START, \"agent\")\n", "\n", "workflow3.add_conditional_edges(\"agent\",router_function,{\"tools\": \"tools\", END: END})\n", "\n", "workflow3.add_edge(\"tools\", 'agent')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "app3 = workflow3.compile(checkpointer = memory)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "config = {\"configurable\": {\"thread_id\": \"1\"}}" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "events = app3.stream(\n", " {\"messages\": [\"Hi there! My name is parthib.\"]}, config, stream_mode=\"values\"\n", ")" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "Hi there! My name is parthib.\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " search (call_kfx7)\n", " Call ID: call_kfx7\n", " Args:\n", " query: parthib\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", "Name: search\n", "\n", "It's 90 degrees and sunny.\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " search (call_8qkb)\n", " Call ID: call_8qkb\n", " Args:\n", " query: parthib weather\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", "Name: search\n", "\n", "It's 90 degrees and sunny.\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "That's good to know!\n" ] } ], "source": [ "for event in events:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "events = app3.stream(\n", " {\"messages\": [\"Can you please tell me My name?\"]}, config, stream_mode=\"values\"\n", ")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "Can you please tell me My name?\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "parthib\n" ] } ], "source": [ "for event in events:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'v': 1,\n", " 'ts': '2025-03-13T07:17:52.345242+00:00',\n", " 'id': '1efffdb4-6db2-6579-8008-004795bdac47',\n", " 'channel_values': {'messages': [HumanMessage(content='Hi there! My name is parthib.', additional_kwargs={}, response_metadata={}, id='f1bbde4c-4a84-499e-8983-a97067581103'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_kfx7', 'function': {'arguments': '{\"query\":\"parthib\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 82, 'prompt_tokens': 946, 'total_tokens': 1028, 'completion_time': 0.149090909, 'prompt_time': 0.080832156, 'queue_time': 0.23635677900000002, 'total_time': 0.229923065}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-7dd24c1d-ab2d-4446-b0b3-77c514cb70f9-0', tool_calls=[{'name': 'search', 'args': {'query': 'parthib'}, 'id': 'call_kfx7', 'type': 'tool_call'}], usage_metadata={'input_tokens': 946, 'output_tokens': 82, 'total_tokens': 1028}),\n", " ToolMessage(content=\"It's 90 degrees and sunny.\", name='search', id='30233758-43ce-4a8d-884a-5a0ce92a2964', tool_call_id='call_kfx7'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_8qkb', 'function': {'arguments': '{\"query\":\"parthib weather\"}', 'name': 'search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 42, 'prompt_tokens': 1026, 'total_tokens': 1068, 'completion_time': 0.076363636, 'prompt_time': 0.086929684, 'queue_time': 0.23346191, 'total_time': 0.16329332}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-077a29d8-067f-479e-9030-fa2da4bd90ef-0', tool_calls=[{'name': 'search', 'args': {'query': 'parthib weather'}, 'id': 'call_8qkb', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1026, 'output_tokens': 42, 'total_tokens': 1068}),\n", " ToolMessage(content=\"It's 90 degrees and sunny.\", name='search', id='52fcefdd-a21e-40ff-a04a-9f07e0ad487d', tool_call_id='call_8qkb'),\n", " AIMessage(content=\"That's good to know!\", additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 1107, 'total_tokens': 1116, 'completion_time': 0.016363636, 'prompt_time': 0.187245132, 'queue_time': 0.23505791699999998, 'total_time': 0.203608768}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-414769b9-fdde-4ecd-adb4-2d76016b0952-0', usage_metadata={'input_tokens': 1107, 'output_tokens': 9, 'total_tokens': 1116}),\n", " HumanMessage(content='Can you please tell me My name?', additional_kwargs={}, response_metadata={}, id='73092115-910c-4cc9-8694-56996976bc74'),\n", " AIMessage(content='parthib', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 1132, 'total_tokens': 1137, 'completion_time': 0.009090909, 'prompt_time': 0.039626394, 'queue_time': 0.237121242, 'total_time': 0.048717303}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-2889bd69-a5a6-44f8-bd14-c2b5d1bc6c08-0', usage_metadata={'input_tokens': 1132, 'output_tokens': 5, 'total_tokens': 1137})],\n", " 'agent': 'agent'},\n", " 'channel_versions': {'__start__': '00000000000000000000000000000009.0.3851235431701766',\n", " 'messages': '00000000000000000000000000000010.0.36891079962134077',\n", " 'start:agent': '00000000000000000000000000000010.0.7582001227958053',\n", " 'agent': '00000000000000000000000000000010.0.79576488464161',\n", " 'branch:agent:router_function:tools': '00000000000000000000000000000006.0.5699474757089383',\n", " 'tools': '00000000000000000000000000000007.0.4048586181949164'},\n", " 'versions_seen': {'__input__': {},\n", " '__start__': {'__start__': '00000000000000000000000000000008.0.771603223183003'},\n", " 'agent': {'start:agent': '00000000000000000000000000000009.0.3471992909319417',\n", " 'tools': '00000000000000000000000000000006.0.7804858660862368'},\n", " 'tools': {'branch:agent:router_function:tools': '00000000000000000000000000000005.0.6366621873155655'}},\n", " 'pending_sends': []}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.get(config)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }