-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpage_content.txt
1 lines (1 loc) · 5.17 KB
/
webpage_content.txt
1
Mastering LangGraph: A Beginner's Guide to Building Intelligent Language Models | MediumOpen in appSign upSign inWriteSign upSign inIntroduction to LangGraph: A Beginner’s GuideCPlog·Follow3 min read·Feb 15, 2024--1ListenShareLangGraph is a powerful tool for building stateful, multi-actor applications with Large Language Models (LLMs). It extends the LangChain library, allowing you to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. In this article, we’ll introduce LangGraph, walk you through its basic concepts, and share some insights and common points of confusion for beginners.What is LangGraph?LangGraph is a library built on top of LangChain, designed to add cyclic computational capabilities to your LLM applications. While LangChain allows you to define chains of computation (Directed Acyclic Graphs or DAGs), LangGraph introduces the ability to add cycles, enabling more complex, agent-like behaviors where you can call an LLM in a loop, asking it what action to take next.Key ConceptsStateful Graph: LangGraph revolves around the concept of a stateful graph, where each node in the graph represents a step in your computation, and the graph maintains a state that is passed around and updated as the computation progresses.Nodes: Nodes are the building blocks of your LangGraph. Each node represents a function or a computation step. You define nodes to perform specific tasks, such as processing input, making decisions, or interacting with external APIs.Edges: Edges connect the nodes in your graph, defining the flow of computation. LangGraph supports conditional edges, allowing you to dynamically determine the next node to execute based on the current state of the graph.A Simple ExampleLet’s walk through a simple example where we use LangGraph to classify user input as either a “greeting” or a “search” query and respond accordingly.Step 1: Define the Graph StateFirst, we define the state structure for our graph. In this example, our state includes the user’s question, the classification of the question, and a response.from typing import Dict, TypedDict, Optionalclass GraphState(TypedDict): question: Optional[str] = None classification: Optional[str] = None response: Optional[str] = NoneStep 2: Create the GraphNext, we create a new instance of StateGraph with our GraphState structure.from langgraph.graph import StateGraphworkflow = StateGraph(GraphState)Step 3: Define NodesWe define nodes for classifying the input, handling greetings, and handling search queries.def classify_input_node(state): question = state.get('question', '').strip() classification = classify(question) # Assume a function that classifies the input return {"classification": classification}def handle_greeting_node(state): return {"response": "Hello! How can I help you today?"}def handle_search_node(state): question = state.get('question', '').strip() search_result = f"Search result for '{question}'" return {"response": search_result}Step 4: Add Nodes to the GraphWe add our nodes to the graph and define the flow using edges and conditional edges.workflow.add_node("classify_input", classify_input_node)workflow.add_node("handle_greeting", handle_greeting_node)workflow.add_node("handle_search", handle_search_node)def decide_next_node(state): return "handle_greeting" if state.get('classification') == "greeting" else "handle_search"workflow.add_conditional_edges( "classify_input", decide_next_node, { "handle_greeting": "handle_greeting", "handle_search": "handle_search" })Step 5: Set Entry and End PointsWe set the entry point for our graph and define the end points.workflow.set_entry_point("classify_input")workflow.add_edge('handle_greeting', END)workflow.add_edge('handle_search', END)Step 6: Compile and Run the GraphFinally, we compile our graph and run it with some input.app = workflow.compile()inputs = {"question": "Hello, how are you?"}result = app.invoke(inputs)print(result)Common ConfusionsState Management: Understanding how the state is passed around and updated in the graph can be tricky. Remember that each node receives the current state, can modify it, and passes it on to the next node.Conditional Edges: Setting up conditional edges requires careful consideration of the conditions and the mapping of outcomes to the next nodes. Ensure that the keys returned by the condition function match the keys in the conditional edge mapping.Dead-End Nodes: Every node in the graph should have a path leading to another node or to the END node. If a node has no outgoing edges, it's considered a dead-end, and you'll need to add an edge to avoid errors.ConclusionLangGraph is a versatile tool for building complex, stateful applications with LLMs. By understanding its core concepts and working through simple examples, beginners can start to leverage its power for their projects. Remember to pay attention to state management, conditional edges, and ensuring there are no dead-end nodes in your graph. Happy coding!LangchainLlmAIChatbotsPython----1FollowWritten by CPlog11 FollowersAI enthusiast & Principal Data Scientist at LFX & Li & Fung. I innovate with LLMs, LangChain, Stable Diffusion & ComfyUI to revolutionize supply chains.FollowHelpStatusAboutCareersBlogPrivacyTermsText to speechTeams