This directory contains examples of implementing conditional workflows using LangGraph. These examples demonstrate how to create decision-based flows where the execution path changes based on certain conditions or inputs.
File: practice.ipynb
This example implements a quadratic equation solver that demonstrates conditional branching based on the discriminant value:
- State Management: Uses
TypedDictto define and manage the equation state - Conditional Flow:
- Calculates the discriminant of a quadratic equation
- Branches to different handlers based on whether the roots are real or complex
- Demonstrates state transitions in a computational workflow
File: practice2.ipynb
A more advanced example showing sentiment analysis with conditional responses:
- Sentiment Detection: Uses Google's Gemini model to analyze text sentiment
- Structured Output: Implements Pydantic models for type-safe output
- Dynamic Response: Generates different responses based on sentiment analysis
- State Management: Maintains conversation state across multiple steps
-
Install the required dependencies:
pip install -r requirements.txt
-
For the sentiment analysis example, you'll need a Google API key:
- Get an API key from Google AI Studio
- Set it in the notebook or as an environment variable
-
Quadratic Equation Solver:
- Open
practice.ipynb - Follow the cells to see how the workflow processes different equations
- Observe how the flow changes based on the discriminant value
- Open
-
Sentiment Analysis:
- Open
practice2.ipynb - Update the API key if needed
- Run the cells to see sentiment analysis in action
- Experiment with different input texts to see conditional responses
- Open
- State Management: Using TypedDict and Pydantic models to maintain workflow state
- Conditional Branching: Directing flow based on computation results
- Model Integration: Connecting with external AI models for decision making
- Workflow Definition: Structuring complex, multi-step processes
- Python 3.8+
- langgraph
- langchain-google-genai
- pydantic
Check out other workflow examples in the parent directory for more LangGraph patterns and use cases.