This directory contains an example of implementing an iterative workflow using LangGraph. The example demonstrates how to create a feedback loop where the output of one iteration influences the next, allowing for continuous refinement of results.
File: practice.ipynb
This example showcases an AI system that generates and iteratively refines tweets based on evaluation feedback. The workflow consists of three main components:
- Generator: Creates tweets based on a given topic
- Evaluator: Assesses the quality of the generated tweet
- Optimizer: Refines the tweet based on evaluation feedback
The process repeats until either:
- The tweet meets quality standards, or
- A maximum number of iterations is reached
-
State Management: Uses
TypedDictto maintain the workflow state including:- Current tweet text
- Evaluation results
- Iteration count
- Maximum allowed iterations
-
AI Models:
- Generator: Creative model for tweet generation
- Evaluator: Critical model for quality assessment
- Optimizer: Model for refining tweets based on feedback
-
Workflow Logic:
- Generate initial tweet
- Evaluate against quality criteria
- If approved, return the tweet
- If needs improvement, refine and repeat
- Stop after maximum iterations
-
Install the required dependencies:
pip install -r requirements.txt
-
You'll need a Google API key for the Gemini model:
- Obtain an API key from Google AI Studio
- Set it in the notebook or as an environment variable
- Open
practice.ipynbin Jupyter Notebook - Set your Google API key if not using environment variables
- Run the cells to define the workflow components
- Test with different topics and iteration limits
- Observe how the tweet evolves through iterations
- Iterative Refinement: The system improves its output through multiple feedback loops
- State Management: Maintaining context across iterations
- Model Specialization: Different AI models for generation, evaluation, and optimization
- Termination Conditions: When to stop the iteration process
- Python 3.8+
- langgraph
- langchain-google-genai
- pydantic
Check out other workflow examples in the parent directory for different LangGraph patterns and use cases.