LangGraph is an exciting new framework that allows developers to create stateful applications and workflows, particularly useful when working with language models. It provides a structured way to manage complex interactions and maintain context across multiple steps.
Let's dive into the basic syntax of LangGraph and see how we can use it in our Python projects.
First, make sure you have LangGraph installed. You can do this using pip:
pip install langgraph
Once installed, you can import the necessary components:
from langgraph.graph import StateGraph from langgraph.prebuilt import ToolExecutor
The core component of LangGraph is the StateGraph
. This represents your workflow and contains all the nodes and edges that define your application's logic.
Here's how you create a basic StateGraph:
workflow = StateGraph()
Nodes in LangGraph represent individual steps or actions in your workflow. You can add nodes to your StateGraph using the add_node
method:
def process_input(state): # Your processing logic here return state workflow.add_node("process_input", process_input)
In this example, we've added a node called "process_input" that executes the process_input
function when the workflow reaches this step.
Edges define the flow between nodes in your graph. You can add edges using the add_edge
method:
workflow.add_edge("process_input", "next_step")
This creates a directional edge from the "process_input" node to a node called "next_step".
LangGraph allows you to create conditional flows based on the state of your application. Here's an example:
def check_condition(state): if state["some_value"] > 10: return "high_value_path" else: return "low_value_path" workflow.add_conditional_edges( "check_condition", check_condition, { "high_value_path": "process_high_value", "low_value_path": "process_low_value" } )
This creates a conditional branch where the next step depends on the output of the check_condition
function.
LangGraph provides a ToolExecutor
class that can be useful when working with language models. Here's a simple example:
from langchain.tools import BaseTool class MyTool(BaseTool): name = "my_tool" description = "A custom tool" def _run(self, query: str) -> str: # Tool logic here return f"Processed: {query}" tool_executor = ToolExecutor([MyTool()]) workflow.add_node("execute_tool", tool_executor)
This creates a node that can execute a custom tool when reached in the workflow.
Once you've defined your workflow, you need to compile it before you can use it:
app = workflow.compile()
Now you can run your workflow with an initial state:
initial_state = {"input": "Hello, LangGraph!"} final_state = app.invoke(initial_state)
The final_state
will contain the result of your workflow after all steps have been executed.
This introduction to LangGraph's basic syntax should give you a solid foundation to start building your own stateful applications. As you become more familiar with the framework, you'll discover its full potential for creating complex, context-aware workflows, especially when working with language models.
25/09/2024 | Python
06/10/2024 | Python
08/12/2024 | Python
21/09/2024 | Python
05/11/2024 | Python
15/11/2024 | Python
25/09/2024 | Python
15/11/2024 | Python
08/12/2024 | Python
26/10/2024 | Python
15/10/2024 | Python
15/11/2024 | Python