├── LangGraph-Tutorial.ipynb └── README.md /LangGraph-Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | {"cells":[{"attachments":{},"cell_type":"markdown","id":"31b9bacb-bb89-431d-9bd5-d04e7c17456f","metadata":{"language":"python"},"source":"# LangGraph Chatbot\nWe’ll create a simple chatbot using LangGraph. This chatbot will respond directly to user messages. \nWe will start by creating a StateGraph. A StateGraph object defines the structure of our chatbot as a state machine."},{"cell_type":"code","execution_count":5,"id":"e83fd396-2d37-4259-8910-29a9b2a116fd","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:13:34.300632Z","iopub.status.busy":"2024-09-16T16:13:34.300298Z","iopub.status.idle":"2024-09-16T16:13:44.156270Z","shell.execute_reply":"2024-09-16T16:13:44.139262Z","shell.execute_reply.started":"2024-09-16T16:13:34.300603Z"},"language":"python","trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"Requirement already satisfied: langgraph in /opt/conda/lib/python3.11/site-packages (0.2.21)\nRequirement already satisfied: langsmith in /opt/conda/lib/python3.11/site-packages (0.1.121)\nRequirement already satisfied: langchain-core<0.4,>=0.2.39 in /opt/conda/lib/python3.11/site-packages (from langgraph) (0.3.0)\nRequirement already satisfied: langgraph-checkpoint<2.0.0,>=1.0.2 in /opt/conda/lib/python3.11/site-packages (from langgraph) (1.0.9)\nRequirement already satisfied: httpx<1,>=0.23.0 in /opt/conda/lib/python3.11/site-packages (from langsmith) (0.27.2)\nRequirement already satisfied: orjson<4.0.0,>=3.9.14 in /opt/conda/lib/python3.11/site-packages (from langsmith) (3.10.7)\nRequirement already satisfied: pydantic<3,>=1 in /opt/conda/lib/python3.11/site-packages (from langsmith) (2.9.1)\nRequirement already satisfied: requests<3,>=2 in /opt/conda/lib/python3.11/site-packages (from langsmith) (2.31.0)\nRequirement already satisfied: anyio in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith) (4.0.0)\nRequirement already satisfied: certifi in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith) (2021.10.8)\nRequirement already satisfied: httpcore==1.* in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith) (1.0.5)\nRequirement already satisfied: idna in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith) (3.3)\nRequirement already satisfied: sniffio in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith) (1.3.0)\nRequirement already satisfied: h11<0.15,>=0.13 in /opt/conda/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith) (0.14.0)\nRequirement already satisfied: PyYAML>=5.3 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4,>=0.2.39->langgraph) (6.0.1)\nRequirement already satisfied: jsonpatch<2.0,>=1.33 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4,>=0.2.39->langgraph) (1.33)\nRequirement already satisfied: packaging<25,>=23.2 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4,>=0.2.39->langgraph) (23.2)\nRequirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4,>=0.2.39->langgraph) (8.5.0)\nRequirement already satisfied: typing-extensions>=4.7 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4,>=0.2.39->langgraph) (4.12.2)\nRequirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.11/site-packages (from pydantic<3,>=1->langsmith) (0.7.0)\nRequirement already satisfied: pydantic-core==2.23.3 in /opt/conda/lib/python3.11/site-packages (from pydantic<3,>=1->langsmith) (2.23.3)\nRequirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langsmith) (2.0.7)\nRequirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langsmith) (2.2.3)\nRequirement already satisfied: jsonpointer>=1.9 in /opt/conda/lib/python3.11/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4,>=0.2.39->langgraph) (2.4)\n"}],"source":"!pip install langgraph langsmith"},{"cell_type":"code","execution_count":7,"id":"982e579d-8ed6-4dfd-9a8a-3d7048e0ce5f","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:13:49.399146Z","iopub.status.busy":"2024-09-16T16:13:49.392084Z","iopub.status.idle":"2024-09-16T16:13:59.778477Z","shell.execute_reply":"2024-09-16T16:13:59.771236Z","shell.execute_reply.started":"2024-09-16T16:13:49.395593Z"},"language":"python","trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"Requirement already satisfied: langchain in /opt/conda/lib/python3.11/site-packages (0.3.0)\nCollecting langchain_groq\n Downloading langchain_groq-0.2.0-py3-none-any.whl.metadata (2.9 kB)\nRequirement already satisfied: langchain_community in /opt/conda/lib/python3.11/site-packages (0.3.0)\nRequirement already satisfied: PyYAML>=5.3 in /opt/conda/lib/python3.11/site-packages (from langchain) (6.0.1)\nRequirement already satisfied: SQLAlchemy<3,>=1.4 in /opt/conda/lib/python3.11/site-packages (from langchain) (2.0.21)\nRequirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /opt/conda/lib/python3.11/site-packages (from langchain) (3.10.5)\nRequirement already satisfied: langchain-core<0.4.0,>=0.3.0 in /opt/conda/lib/python3.11/site-packages (from langchain) (0.3.0)\nRequirement already satisfied: langchain-text-splitters<0.4.0,>=0.3.0 in /opt/conda/lib/python3.11/site-packages (from langchain) (0.3.0)\nRequirement already satisfied: langsmith<0.2.0,>=0.1.17 in /opt/conda/lib/python3.11/site-packages (from langchain) (0.1.121)\nRequirement already satisfied: numpy<2,>=1 in /opt/conda/lib/python3.11/site-packages (from langchain) (1.26.4)\nRequirement already satisfied: pydantic<3.0.0,>=2.7.4 in /opt/conda/lib/python3.11/site-packages (from langchain) (2.9.1)\nRequirement already satisfied: requests<3,>=2 in /opt/conda/lib/python3.11/site-packages (from langchain) (2.31.0)\nRequirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /opt/conda/lib/python3.11/site-packages (from langchain) (8.5.0)\nRequirement already satisfied: groq<1,>=0.4.1 in /opt/conda/lib/python3.11/site-packages (from langchain_groq) (0.11.0)\nRequirement already satisfied: dataclasses-json<0.7,>=0.5.7 in /opt/conda/lib/python3.11/site-packages (from langchain_community) (0.6.7)\nRequirement already satisfied: pydantic-settings<3.0.0,>=2.4.0 in /opt/conda/lib/python3.11/site-packages (from langchain_community) (2.5.2)\nRequirement already satisfied: aiohappyeyeballs>=2.3.0 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.4.0)\nRequirement already satisfied: aiosignal>=1.1.2 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\nRequirement already satisfied: attrs>=17.3.0 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0)\nRequirement already satisfied: frozenlist>=1.1.1 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\nRequirement already satisfied: multidict<7.0,>=4.5 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.1.0)\nRequirement already satisfied: yarl<2.0,>=1.0 in /opt/conda/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.11.1)\nRequirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /opt/conda/lib/python3.11/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (3.22.0)\nRequirement already satisfied: typing-inspect<1,>=0.4.0 in /opt/conda/lib/python3.11/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (0.9.0)\nRequirement already satisfied: anyio<5,>=3.5.0 in /opt/conda/lib/python3.11/site-packages (from groq<1,>=0.4.1->langchain_groq) (4.0.0)\nRequirement already satisfied: distro<2,>=1.7.0 in /opt/conda/lib/python3.11/site-packages (from groq<1,>=0.4.1->langchain_groq) (1.9.0)\nRequirement already satisfied: httpx<1,>=0.23.0 in /opt/conda/lib/python3.11/site-packages (from groq<1,>=0.4.1->langchain_groq) (0.27.2)\nRequirement already satisfied: sniffio in /opt/conda/lib/python3.11/site-packages (from groq<1,>=0.4.1->langchain_groq) (1.3.0)\nRequirement already satisfied: typing-extensions<5,>=4.7 in /opt/conda/lib/python3.11/site-packages (from groq<1,>=0.4.1->langchain_groq) (4.12.2)\nRequirement already satisfied: jsonpatch<2.0,>=1.33 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4.0,>=0.3.0->langchain) (1.33)\nRequirement already satisfied: packaging<25,>=23.2 in /opt/conda/lib/python3.11/site-packages (from langchain-core<0.4.0,>=0.3.0->langchain) (23.2)\nRequirement already satisfied: orjson<4.0.0,>=3.9.14 in /opt/conda/lib/python3.11/site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.7)\nRequirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)\nRequirement already satisfied: pydantic-core==2.23.3 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.23.3)\nRequirement already satisfied: python-dotenv>=0.21.0 in /opt/conda/lib/python3.11/site-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain_community) (1.0.1)\nRequirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2.0.7)\nRequirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langchain) (3.3)\nRequirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2.2.3)\nRequirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2021.10.8)\nRequirement already satisfied: greenlet!=0.4.17 in /opt/conda/lib/python3.11/site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.0rc3)\nRequirement already satisfied: httpcore==1.* in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (1.0.5)\nRequirement already satisfied: h11<0.15,>=0.13 in /opt/conda/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (0.14.0)\nRequirement already satisfied: jsonpointer>=1.9 in /opt/conda/lib/python3.11/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.0->langchain) (2.4)\nRequirement already satisfied: mypy-extensions>=0.3.0 in /opt/conda/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community) (1.0.0)\nDownloading langchain_groq-0.2.0-py3-none-any.whl (14 kB)\nInstalling collected packages: langchain_groq\nSuccessfully installed langchain_groq-0.2.0\n"}],"source":"!pip install langchain langchain_groq langchain_community"},{"attachments":{},"cell_type":"markdown","id":"52f68d9d-7b25-4f60-ae0f-08c56be6ac0a","metadata":{"language":"python"},"source":"## Add the API keys"},{"cell_type":"code","execution_count":11,"id":"1240d18e-71b4-4745-8b07-c7f310ddba4e","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:17:37.069882Z","iopub.status.busy":"2024-09-16T16:17:37.069455Z","iopub.status.idle":"2024-09-16T16:17:37.076977Z","shell.execute_reply":"2024-09-16T16:17:37.076400Z","shell.execute_reply.started":"2024-09-16T16:17:37.069843Z"},"language":"python","trusted":true},"outputs":[],"source":"import os\nos.environ['LANGCHAIN_TRACING_V2'] = 'true'\nos.environ['LANGCHAIN_API_KEY'] = 'Add your LangChain API Key'\nos.environ['LANGCHAIN_PROJECT'] = 'LiveLanggraph'"},{"attachments":{},"cell_type":"markdown","id":"389fc469-91e3-47df-8be0-1f72bb082fc9","metadata":{"language":"python"},"source":"This will help you getting started with ChatGroq chat models. "},{"cell_type":"code","execution_count":15,"id":"371f49e8-e83a-4db1-a0a3-9f6d291944cb","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:19:34.782671Z","iopub.status.busy":"2024-09-16T16:19:34.782271Z","iopub.status.idle":"2024-09-16T16:19:34.882313Z","shell.execute_reply":"2024-09-16T16:19:34.881680Z","shell.execute_reply.started":"2024-09-16T16:19:34.782641Z"},"language":"python","trusted":true},"outputs":[],"source":"from langchain_groq import ChatGroq\n\ngroq_api_key = \"Add your Groq API Key\" # Replace with your actual API key\nllm = ChatGroq(groq_api_key=groq_api_key, model_name='Gemma2-9b-It')"},{"attachments":{},"cell_type":"markdown","id":"b80f4d10-67d7-4d76-813a-dd6beecc5d77","metadata":{"language":"python"},"source":"## Create the graph"},{"cell_type":"code","execution_count":17,"id":"e706ef7b-948c-4b6f-8ff3-0141abcba778","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:19:44.127711Z","iopub.status.busy":"2024-09-16T16:19:44.127180Z","iopub.status.idle":"2024-09-16T16:19:44.167009Z","shell.execute_reply":"2024-09-16T16:19:44.166406Z","shell.execute_reply.started":"2024-09-16T16:19:44.127679Z"},"language":"python","trusted":true},"outputs":[],"source":"from typing import Annotated\nfrom typing_extensions import TypedDict\nfrom langgraph.graph import StateGraph, START, END\nfrom langgraph.graph.message import add_messages"},{"cell_type":"code","execution_count":18,"id":"6b141672-2743-4792-8ce4-bdfc481146c2","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:19:52.980650Z","iopub.status.busy":"2024-09-16T16:19:52.980087Z","iopub.status.idle":"2024-09-16T16:19:52.985114Z","shell.execute_reply":"2024-09-16T16:19:52.984491Z","shell.execute_reply.started":"2024-09-16T16:19:52.980622Z"},"language":"python","trusted":true},"outputs":[],"source":"class State(TypedDict):\n messages:Annotated[list, add_messages]\n\ngraph_builder = StateGraph(State)"},{"cell_type":"code","execution_count":19,"id":"e733ed42-af93-4931-a78b-b84229a41af5","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:02.683334Z","iopub.status.busy":"2024-09-16T16:20:02.682947Z","iopub.status.idle":"2024-09-16T16:20:02.699532Z","shell.execute_reply":"2024-09-16T16:20:02.695199Z","shell.execute_reply.started":"2024-09-16T16:20:02.683302Z"},"language":"python","trusted":true},"outputs":[{"data":{"text/plain":""},"execution_count":19,"metadata":{},"output_type":"execute_result"}],"source":"graph_builder"},{"cell_type":"code","execution_count":20,"id":"db9c71b7-f287-4587-bb08-dee1b5be9252","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:11.752802Z","iopub.status.busy":"2024-09-16T16:20:11.752094Z","iopub.status.idle":"2024-09-16T16:20:11.784407Z","shell.execute_reply":"2024-09-16T16:20:11.777865Z","shell.execute_reply.started":"2024-09-16T16:20:11.752752Z"},"language":"python","trusted":true},"outputs":[],"source":"def chatbot(state:State):\n return {\"messages\" : llm.invoke(state['messages'])}"},{"cell_type":"code","execution_count":21,"id":"3cad2945-b5ef-4da0-b8a7-8c93dbfddd7f","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:20.290054Z","iopub.status.busy":"2024-09-16T16:20:20.289690Z","iopub.status.idle":"2024-09-16T16:20:20.295804Z","shell.execute_reply":"2024-09-16T16:20:20.295021Z","shell.execute_reply.started":"2024-09-16T16:20:20.290026Z"},"language":"python","trusted":true},"outputs":[],"source":"graph_builder.add_node(\"chatbot\",chatbot)"},{"cell_type":"code","execution_count":22,"id":"aa42798b-935e-47d1-bae4-ecaa69db4a7b","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:29.913051Z","iopub.status.busy":"2024-09-16T16:20:29.912102Z","iopub.status.idle":"2024-09-16T16:20:29.926580Z","shell.execute_reply":"2024-09-16T16:20:29.925876Z","shell.execute_reply.started":"2024-09-16T16:20:29.912711Z"},"language":"python","trusted":true},"outputs":[],"source":"graph_builder.add_edge(START, \"chatbot\")\ngraph_builder.add_edge(\"chatbot\", END)"},{"cell_type":"code","execution_count":23,"id":"63d4f583-6cc5-4d74-9ff6-49255d0d4851","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:38.678767Z","iopub.status.busy":"2024-09-16T16:20:38.678361Z","iopub.status.idle":"2024-09-16T16:20:38.695327Z","shell.execute_reply":"2024-09-16T16:20:38.692594Z","shell.execute_reply.started":"2024-09-16T16:20:38.678734Z"},"language":"python","trusted":true},"outputs":[],"source":"graph = graph_builder.compile()"},{"cell_type":"code","execution_count":24,"id":"3c2f37af-4e37-4b39-9aea-98f6e8f3cf85","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:20:47.547863Z","iopub.status.busy":"2024-09-16T16:20:47.547151Z","iopub.status.idle":"2024-09-16T16:21:03.154278Z","shell.execute_reply":"2024-09-16T16:21:03.153578Z","shell.execute_reply.started":"2024-09-16T16:20:47.547827Z"},"language":"python","trusted":true},"outputs":[{"data":{"image/jpeg":"/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADb/2wBDAAMCAgM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t6tGn75FjEvQZtyYStGVqIPx+IPjFWf33dC+WunfvVj/fVocjMuq3ltIWrxqSCa8fcMb5u1+4KzjCcIqEWrLw/wAg47FqW0aojOSLNdYV2jtr5NbsGQh5KVYB3SUkgHBBx56km4C7vcLdbWgVOTZbLACekJ3wVn0IC1fUDXpKmIe4gBLZcUEobQnwlqPQEpHEnzCtg2V7PH7U+L9d2uSnqbLcaIrBMdCulSv8agB/lGR1qrRi8VHCUXOb/l2eL/3MyjvNMpSlfNgKUpQHJdLTBvcNcS4Q2J0VfxmZDYcQfQeFVB7Ylo91RULfJYz+SxcZLafQlLgA9Aq9UrfTxFajspza4Not2ig+8bpH5rP+9pftae8bpH5rP+9pftav1K369ivqy5sXZQfeN0j81n/e0v2tPeN0j81n/e0v2tX6lNexX1Zc2LsoPvG6R+az/vaX7Wv0bDtIA8Yk8jxG7S/a1faU17FfVlzYuyB09oPT+lXC7a7UxGfI3TIIK3iPEXFEqI9NT1KVyTnKo9Kbu/EmYpSlYA//2Q==","text/plain":""},"metadata":{},"output_type":"display_data"}],"source":"from IPython.display import Image, display\n\ntry:\n display(Image(graph.get_graph().draw_mermaid_png()))\nexcept Exception:\n pass"},{"attachments":{},"cell_type":"markdown","id":"91ca7884-0e3b-41f5-a566-ab4b16e0baab","metadata":{"language":"python"},"source":"## Use the graph\nWe can now use the created chatbot."},{"cell_type":"code","execution_count":25,"id":"c1fa7cff-553e-403c-8796-8b096a048db9","metadata":{"execution":{"iopub.execute_input":"2024-09-16T16:21:16.460613Z","iopub.status.busy":"2024-09-16T16:21:16.460012Z","iopub.status.idle":"2024-09-16T16:22:50.863971Z","shell.execute_reply":"2024-09-16T16:22:50.858043Z","shell.execute_reply.started":"2024-09-16T16:21:16.460532Z"},"language":"python","trusted":true},"outputs":[{"name":"stdin","output_type":"stream","text":"User: Hello\n"},{"name":"stdout","output_type":"stream","text":"dict_values([{'messages': AIMessage(content='Hello! 👋 How can I help you today? 😊\\n', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 10, 'total_tokens': 25, 'completion_time': 0.027272727, 'prompt_time': 3.8e-07, 'queue_time': 0.014608389000000001, 'total_time': 0.027273107}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-e4c0f5b7-657a-46f7-adf3-ea4ba93260e9-0', usage_metadata={'input_tokens': 10, 'output_tokens': 15, 'total_tokens': 25})}])\ncontent='Hello! 👋 How can I help you today? 😊\\n' additional_kwargs={} response_metadata={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 10, 'total_tokens': 25, 'completion_time': 0.027272727, 'prompt_time': 3.8e-07, 'queue_time': 0.014608389000000001, 'total_time': 0.027273107}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None} id='run-e4c0f5b7-657a-46f7-adf3-ea4ba93260e9-0' usage_metadata={'input_tokens': 10, 'output_tokens': 15, 'total_tokens': 25}\nAssistant: Hello! 👋 How can I help you today? 😊\n\n"},{"name":"stdin","output_type":"stream","text":"User: Who are you?\n"},{"name":"stdout","output_type":"stream","text":"dict_values([{'messages': AIMessage(content='I am Gemma, an open-weights AI assistant. I am a large language model trained by Google DeepMind. My purpose is to help people by understanding and responding to their requests in a helpful, informative, and impartial way. I can generate text, translate languages, write different kinds of creative content, and answer your questions in an informative way. I am still under development, but I have learned to perform many kinds of tasks.\\n\\nSince I am open-weights, my weights are publicly accessible. This means that anyone can inspect, modify, or build upon me.\\n', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 118, 'prompt_tokens': 13, 'total_tokens': 131, 'completion_time': 0.214545455, 'prompt_time': 0.000120589, 'queue_time': 0.014288301, 'total_time': 0.214666044}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-25ea2df6-7646-4358-9266-8553919353df-0', usage_metadata={'input_tokens': 13, 'output_tokens': 118, 'total_tokens': 131})}])\ncontent='I am Gemma, an open-weights AI assistant. I am a large language model trained by Google DeepMind. My purpose is to help people by understanding and responding to their requests in a helpful, informative, and impartial way. I can generate text, translate languages, write different kinds of creative content, and answer your questions in an informative way. I am still under development, but I have learned to perform many kinds of tasks.\\n\\nSince I am open-weights, my weights are publicly accessible. This means that anyone can inspect, modify, or build upon me.\\n' additional_kwargs={} response_metadata={'token_usage': {'completion_tokens': 118, 'prompt_tokens': 13, 'total_tokens': 131, 'completion_time': 0.214545455, 'prompt_time': 0.000120589, 'queue_time': 0.014288301, 'total_time': 0.214666044}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None} id='run-25ea2df6-7646-4358-9266-8553919353df-0' usage_metadata={'input_tokens': 13, 'output_tokens': 118, 'total_tokens': 131}\nAssistant: I am Gemma, an open-weights AI assistant. I am a large language model trained by Google DeepMind. My purpose is to help people by understanding and responding to their requests in a helpful, informative, and impartial way. I can generate text, translate languages, write different kinds of creative content, and answer your questions in an informative way. I am still under development, but I have learned to perform many kinds of tasks.\n\nSince I am open-weights, my weights are publicly accessible. This means that anyone can inspect, modify, or build upon me.\n\n"},{"name":"stdin","output_type":"stream","text":"User: q\n"},{"name":"stdout","output_type":"stream","text":"Good Bye\n"}],"source":"while True:\n user_input=input(\"User: \")\n if user_input.lower() in [\"quit\",\"q\"]:\n print(\"Good Bye\")\n break\n for event in graph.stream({'messages':(\"user\",user_input)}):\n print(event.values())\n for value in event.values():\n print(value['messages'])\n print(\"Assistant:\",value[\"messages\"].content)"},{"cell_type":"code","execution_count":null,"id":"00374218-61f5-4357-af27-c1676195402c","metadata":{"language":"python","trusted":true},"outputs":[],"source":""}],"metadata":{"jupyterlab":{"notebooks":{"version_major":6,"version_minor":4}},"kernelspec":{"display_name":"Python 3 (ipykernel)","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.11.6"},"singlestore_cell_default_language":"python","singlestore_connection":{"connectionID":"a22b6f8b-b11b-4979-98da-98513e9876e6","defaultDatabase":""},"singlestore_row_limit":300},"nbformat":4,"nbformat_minor":5} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## LangGraph Chatbot 2 | We’ll create a simple chatbot using LangGraph. This chatbot will respond directly to user messages. 3 | We will start by creating a StateGraph. A StateGraph object defines the structure of our chatbot as a state machine. 4 | --------------------------------------------------------------------------------