{"id":24829432,"url":"https://github.com/sushantdhumak/langgraph-basics","last_synced_at":"2025-03-26T01:41:28.007Z","repository":{"id":275007312,"uuid":"924780445","full_name":"sushantdhumak/LangGraph-Basics","owner":"sushantdhumak","description":"Project to learn and understand LangGraph basics","archived":false,"fork":false,"pushed_at":"2025-03-14T07:54:27.000Z","size":1997,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-14T08:37:12.673Z","etag":null,"topics":["langgraph","python3","state-schema","stategraph","typing","typing-extension"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sushantdhumak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-30T16:34:31.000Z","updated_at":"2025-03-14T07:54:30.000Z","dependencies_parsed_at":"2025-02-20T18:34:40.546Z","dependency_job_id":"ab4ffd80-c18e-4065-bf85-2885da632c8e","html_url":"https://github.com/sushantdhumak/LangGraph-Basics","commit_stats":null,"previous_names":["sushantdhumak/langgraph-basics"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushantdhumak%2FLangGraph-Basics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushantdhumak%2FLangGraph-Basics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushantdhumak%2FLangGraph-Basics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushantdhumak%2FLangGraph-Basics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sushantdhumak","download_url":"https://codeload.github.com/sushantdhumak/LangGraph-Basics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245573836,"owners_count":20637670,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["langgraph","python3","state-schema","stategraph","typing","typing-extension"],"created_at":"2025-01-30T23:05:56.789Z","updated_at":"2025-03-26T01:41:27.998Z","avatar_url":"https://github.com/sushantdhumak.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LangGraph-Basics\n\n### 1. The Simplest Graph \n\nLet's build a simple graph with 3 nodes and one conditional edge. \n\n![image](https://github.com/user-attachments/assets/8c8db214-7d12-4578-8cce-4d13aa5f3a73)\n\n---\n\n### 2. A Simple chain in Graph \n\nLet's build up to a simple chain that combines 4 concepts:\n\n1. Using chat messages as our graph state\n2. Using chat models in graph nodes\n3. Binding tools to our chat model\n4. Executing tool calls in graph nodes\n\n![image](https://github.com/user-attachments/assets/57f5291e-708e-4dd7-8f7b-3e5e41cf81f2)\n\n---\n\n### 3. Route in Graph\n\nLet's build a router, where the chat model routes between a direct response or a tool call based upon the user input.\nThis is an simple example of an agent, where the LLM is directing the control flow either by calling a tool or just responding directly.\n\n![Screenshot 2025-02-03 230343](https://github.com/user-attachments/assets/363a8997-53d9-41f6-847e-e09586cd0246)\n\n---\n\n### 4. Agent in Graph\n\nLet's build an agent using a general ReAct architecture\n\n1. Act - let the model call specific tools\n2. Observe - pass the tool output back to the model\n3. Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)\n\n![Screenshot 2025-02-06 223826](https://github.com/user-attachments/assets/2a1f08b8-f01c-4fb2-bb9d-65dcbcbad6c7)\n\n---\n\n### 5. Agent in Graph with Memory\n\nLet's build an agent using a general ReAct architecture with Memory\n\n1. Act - let the model call specific tools\n2. Observe - pass the tool output back to the model\n3. Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)\n\nWe will extend our agent by introducing memory.\n\n![image](https://github.com/user-attachments/assets/1562c544-11ee-4d61-8a0d-2614ec18ceaf)\n\n---\n\n### 6. State Schema in LangGraph\n\nThe state schema represents the structure and types of data that our graph will use. All nodes are expected to communicate with that schema.\n\nWe will use\n\n1. `TypeDict` class from python's `typing` module.\n2. `DataClasses` from python\n3. `Pydantic` - data validation and settings management library using Python type annotations.\n\n---\n\n### 7. State Reducers in LangGraph\n\nThe reducers, which specify how state updates are performed on specific keys / channels in the state schema.\n\nWe will use\n\n1. `Annotated` type with reducer function like `operator.add`.\n2. `Annotated` type with custom reducer function like `reduce_list`.\n3. `MessagesState`\n4. `Re-writing` and `Removal` of messages.\n\n---\n\n### 8. Multiple Schemas in LangGraph\n\nThe multiple schemas are needed when,\n\n1. Internal nodes may pass information that is *not required* in the graph's input / output.\n2. We may also want to use different input / output schemas for the graph.\n\n---\n\n### 9. Filtering and Trimming messages in LangGraph\n\nFiltering and Trimming messages using,\n\n1. Remove Message with MessageState\n2. Filtering Messages\n3. Trimming Messages\n\n---\n\n### 10. Chatbot with Message Summarization \u0026 Checkpoint Memory\n\nLet's create a simple Chatbot with conversation summary. We'll equip that Chatbot with memory, supporting long-running conversations.\n\n---\n\n### 11. Chatbot with Message Summarization \u0026 External DB Memory\n\nLet's upgrade our Chatbot with conversation summary and external memory (SqliteSaver checkpointer), supporting long-running conversations and chat presistence.\n\n---\n\n### 12. Streaming the Output of the Graph (Graph State and Tokens)\n\n1. `.stream` and `.astream` are sync and async methods for streaming back results\n2. Streaming graph state using streaming modes - 'updates' and 'values'\n3. `.astream` events and each event is a dict with a few keys:   \n* `event`: This is the type of event that is being emitted. \n* `name`: This is the name of event.\n* `data`: This is the data associated with the event.\n* `metadata`: Contains`langgraph_node`, the node emitting the event.\n\n---\n\n### 13. Human-in-the-loop | Breakpoints\n\nFor breakpoint, we need to simply compile the graph with `interrupt_before=[\"tools\"]` where `tools` is our tools node.\nThis means that the execution will be interrupted before the node `tools`, which executes the tool call.\n\n![image](https://github.com/user-attachments/assets/8db36385-11f5-4419-abcb-794bcca180da)\n\n---\n\n### 14. Human-in-the-loop | Editing State\n\nUsing breakpoints to modify the graph state.\n\n---\n\n### 15. Human-in-the-loop | Dynamic Breakpoints\n\nThis is an internal breakpoint to allow the graph **dynamically interrupt** itself!\n\nThis has a few specific benefits: \n1. Can do it conditionally (from inside a node based on developer-defined logic).\n2. Can communicate to the user why its interrupted (by passing whatever you want to the `NodeInterrupt`).\n\n---\n\n### 16. Human-in-the-loop | Time Travel\n\nTime travel in LangGraph supports debugging by viewing, re-playing, and even forking from past states. \n\nWe can do this by: \n1. Browsing History - get_state and get_state_history.\n   ![image](https://github.com/user-attachments/assets/4295dbc1-3092-4c9e-96f9-8c3403c9edcb)\n\n   \n2. Replaying - Re-run our agent from any of the prior steps.\n   ![image](https://github.com/user-attachments/assets/1a4dc7b9-9b0a-486b-833a-50d2570a0f83)\n\n\n3. Forking - Run from that same step, but with a different input.\n   ![image](https://github.com/user-attachments/assets/5f8a6102-6019-4476-a6de-d9b36f67d281)\n\n---\n\n### 17. Parallelization\n\nWe can excecute the nodes in parallel (as required by the situation) using,\n\n1. Fan-in and Fan-out\n2. Waiting for other parallel node to finish\n3. Setting the order of the state updates\n\n![image](https://github.com/user-attachments/assets/43527675-fd50-4f07-bfc5-9582e94cdad4)\n\n\n---\n\n### 18. Sub-graphs\n\nSub-graphs allow you to create and manage different states in different parts of your graph. \nThis is particularly useful for multi-agent systems, with teams of agents that each have their own state.\n\nLet's consider an example:\n\n1. I have a system that accepts logs\n2. It performs two separate sub-tasks by different agents (summarize logs, find failure modes)\n3. I want to perform these two operations in two different sub-graphs.\n\nThe most critical thing to understand is how the graphs communicate!\n\n![image](https://github.com/user-attachments/assets/742c9f21-2e59-41c9-827f-fc6449c1c25c)\n\n\n---\n\n### 19. Map-Reduce\n\nMap-reduce operations are essential for efficient task decomposition and parallel processing. \n\nIt has two phases:\n\n1. `Map` - Break a task into smaller sub-tasks, processing each sub-task in parallel.\n2. `Reduce` - Aggregate the results across all of the completed, parallelized sub-tasks.\n\n---\n\n### 20. Research Assistant\n\nResearch is often laborious work offloaded to analysts. AI has considerable potential to assist with this.\n\nHowever, research demands customization: raw LLM outputs are often poorly suited for real-world decision-making workflows. \n\nCustomized, AI-based [research and report generation] workflows are a promising way to address this.\n\n![image](https://github.com/user-attachments/assets/1993dc5b-ea8d-4b24-b5bd-1c3a24b22d75)\n\n![output](https://github.com/user-attachments/assets/9a2e0603-a3b6-4a81-b0b3-e27dd3ce795a)\n\n---\n\n### 21. Chatbot with Memory\n\nLet's build a chatbot that uses both `short-term (within-thread)` and `long-term (across-thread)` memory.\n\nWe'll focus on long-term, which will be facts about the user. These long-term memories will be used to create a personalized chatbot that can remember facts about the user.\n\nIt will save memory, as the user is chatting with it.\n\n![image](https://github.com/user-attachments/assets/2c90b418-28b5-482a-88aa-3ed91d56b463)\n\n---\n\n### 22. Chatbot with Profile Schema\n\nOur chatbot saved memories as a string. In practice, we often want memories to have a structure. \n \nIn our case, we want this to be a single user profile. We'll extend our chatbot to save semantic memories to a single user profile\n\nWe'll also use a library, `Trustcall`, to update this schema with new information. \n\n---\n\n### 23. Chatbot with Collection Schema\n\nSometimes we want to save memories to a `collection` rather than single profile. \n\nLet's update our chatbot to save memories to a collection.\n\nWe'll also show how to use `Trustcall` to update this collection. \n\n---\n\n### 24. Memory Agent\n\nLet's pull together the pieces learned to build an agent with long-term memory.\n\n![image](https://github.com/user-attachments/assets/6ec1c208-fc57-4f72-aa18-f11e79fcd0ec)\n\n---\n\n### 25. Deployment - Creation and Connecting\n\nThe following information should be provided to create a LangGraph Platform deployment:\n\nA LangGraph API Configuration file - `langgraph.json`\nThe graphs that implement the logic of the application - e.g., `task_maistro.py`\nA file that specifies dependencies required to run the application - `requirements.txt`\nSupply environment variables needed for the application to run - `.env` or `docker-compose.yml`\n\nWe can access the deployment through:\n      \nAPI: http://localhost:8123\n\nDocs: http://localhost:8123/docs\n\nLangGraph Studio: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:8123\n\n\nLangGraph Server exposes many API endpoints for interacting with the deployed agent. These endpoints are group into a few common agent needs: \n\n**Runs**: Atomic agent executions\n\n**Threads**: Multi-turn interactions or human in the loop\n\n**Store**: Long-term memory\n\n![image](https://github.com/user-attachments/assets/d503c62a-c36c-4c13-b084-86fa2b090bf1)\n\n---\n\n### 26. Double Texting\n\nSeamless handling of double texting is important for handling real-world usage scenarios, especially in chat applications.\n\nUsers can send multiple messages in a row before the prior run(s) complete, and we want to ensure that we handle this gracefully.\n\nWe can follow below approaches to handle the different scenarios\n\n**Reject**: A simple approach is to reject any new runs until the current run completes.\n\n**Enqueue**: Enqueue any new runs until the current run completes.\n\n**Interrupt**: Interrupt to interrupt the current run, but save all the work that has been done so far up to that point.\n\n**Rollback**: Rollback to interrupt the prior run of the graph, delete it, and start a new run with the double-texted input.\n\n![image](https://github.com/user-attachments/assets/e43fce95-6dda-432e-a215-30fe8dd7dfa2)\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushantdhumak%2Flanggraph-basics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsushantdhumak%2Flanggraph-basics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushantdhumak%2Flanggraph-basics/lists"}