|
| 1 | +--- |
| 2 | +title: Multi-LLM Workflows |
| 3 | +description: Leverage different LLM models for specific tasks within a workflow. |
| 4 | +icon: network-wired |
| 5 | +--- |
| 6 | + |
| 7 | +This example demonstrates how to use multiple LLM models within a single ControlFlow workflow. We'll use GPT-4o-mini models for efficient classification tasks and GPT-4o for more complex synthesis. This approach allows us to optimize for both speed and quality in our AI-powered workflows. |
| 8 | + |
| 9 | +In this scenario, we'll create a workflow that analyzes customer feedback for a product. The workflow will: |
| 10 | + |
| 11 | +1. Classify the sentiment of each piece of feedback (using GPT-4o-mini) |
| 12 | +2. Categorize the topic of each piece of feedback (using GPT-4o-mini) |
| 13 | +3. Generate a comprehensive summary of the feedback (using GPT-4o) |
| 14 | + |
| 15 | +## Code |
| 16 | + |
| 17 | +```python |
| 18 | +import controlflow as cf |
| 19 | +from langchain_openai import ChatOpenAI |
| 20 | +from pydantic import BaseModel |
| 21 | +from typing import Literal |
| 22 | + |
| 23 | +# Define our models |
| 24 | +gpt4_mini = ChatOpenAI(model="gpt-4o-mini") |
| 25 | +gpt4 = ChatOpenAI(model="gpt-4o") |
| 26 | + |
| 27 | +# Create specialized agents |
| 28 | +classifier = cf.Agent(name="Classifier", model=gpt4_mini) |
| 29 | +summarizer = cf.Agent(name="Summarizer", model=gpt4) |
| 30 | + |
| 31 | +# Define our data models |
| 32 | +class Feedback(BaseModel): |
| 33 | + text: str |
| 34 | + sentiment: Literal["positive", "neutral", "negative"] |
| 35 | + topic: Literal["user interface", "performance", "features", "other"] |
| 36 | + |
| 37 | +class FeedbackSummary(BaseModel): |
| 38 | + overall_sentiment: str |
| 39 | + key_points: list[str] |
| 40 | + recommendations: list[str] |
| 41 | + |
| 42 | +@cf.flow |
| 43 | +def analyze_customer_feedback(feedback_list: list[str]) -> FeedbackSummary: |
| 44 | + analyzed_feedback = [] |
| 45 | + |
| 46 | + for feedback in feedback_list: |
| 47 | + |
| 48 | + # Classify sentiment |
| 49 | + sentiment = cf.run( |
| 50 | + "Classify the sentiment of this feedback", |
| 51 | + agents=[classifier], |
| 52 | + result_type=["positive", "neutral", "negative"], |
| 53 | + context={"feedback": feedback} |
| 54 | + ) |
| 55 | + |
| 56 | + # Classify topic |
| 57 | + topic = cf.run( |
| 58 | + "Categorize this feedback into one of the predefined topics", |
| 59 | + agents=[classifier], |
| 60 | + result_type=["user interface", "performance", "features", "other"], |
| 61 | + context={"feedback": feedback} |
| 62 | + ) |
| 63 | + |
| 64 | + analyzed_feedback.append( |
| 65 | + Feedback(text=feedback, sentiment=sentiment, topic=topic) |
| 66 | + ) |
| 67 | + |
| 68 | + # Generate summary |
| 69 | + summary = cf.run( |
| 70 | + "Generate a comprehensive summary of the analyzed feedback", |
| 71 | + agents=[summarizer], |
| 72 | + result_type=FeedbackSummary, |
| 73 | + context={"feedback": analyzed_feedback} |
| 74 | + ) |
| 75 | + |
| 76 | + return summary |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | +### Example usage |
| 81 | + |
| 82 | +<CodeGroup> |
| 83 | + |
| 84 | +```python Code |
| 85 | +feedback_list = [ |
| 86 | + "The new user interface is intuitive and easy to use. Great job!", |
| 87 | + "The app crashes frequently when I try to save my work. This is frustrating.", |
| 88 | + "I love the new feature that allows collaboration in real-time.", |
| 89 | + "The performance has improved, but there's still room for optimization." |
| 90 | +] |
| 91 | + |
| 92 | +result = analyze_customer_feedback(feedback_list) |
| 93 | +print(result) |
| 94 | +``` |
| 95 | +```python Result |
| 96 | +FeedbackSummary( |
| 97 | + overall_sentiment='mixed', |
| 98 | + key_points=[ |
| 99 | + 'The new user interface is intuitive and easy to use.', |
| 100 | + 'The app crashes frequently when trying to save work, causing frustration.', |
| 101 | + 'The new feature allows collaboration in real-time and is well-received.', |
| 102 | + 'Performance has improved but still needs further optimization.' |
| 103 | + ], |
| 104 | + recommendations=[ |
| 105 | + 'Investigate and fix the app crashing issue.', |
| 106 | + 'Continue improving performance.', |
| 107 | + 'Maintain the intuitive design of the user interface.', |
| 108 | + 'Expand on real-time collaboration features.' |
| 109 | + ] |
| 110 | +) |
| 111 | +``` |
| 112 | +</CodeGroup> |
| 113 | + |
| 114 | +## Key points |
| 115 | + |
| 116 | +1. **Multiple LLM Models**: We use GPT-4o-mini for quick classification tasks (sentiment and topic) and GPT-4o for the more complex task of summarization. |
| 117 | + |
| 118 | +2. **Specialized Agents**: We create separate agents for different tasks, each with its own LLM model. This allows us to optimize for both speed and quality. |
| 119 | + |
| 120 | +3. **Structured Data**: We use Pydantic models (`Feedback` and `FeedbackSummary`) to ensure type safety and consistent data structures throughout the workflow. |
| 121 | + |
| 122 | +4. **Task-Specific Result Types**: Each task has a specific `result_type` that matches the expected output, ensuring that the agents provide the correct type of information. |
| 123 | + |
| 124 | +5. **Workflow Composition**: The `analyze_customer_feedback` flow composes multiple tasks into a cohesive workflow, demonstrating how ControlFlow can manage complex, multi-step processes that include loops and conditional logic. |
| 125 | + |
| 126 | +This example showcases how ControlFlow allows you to leverage the strengths of different LLM models within a single workflow. By using more efficient models for simpler tasks and more powerful models for complex analysis, you can create workflows that are both fast and capable of high-quality output. |
0 commit comments