|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from agent_framework import ( |
| 7 | + AgentExecutorResponse, |
| 8 | + WorkflowBuilder, |
| 9 | +) |
| 10 | +from agent_framework.azure import AzureOpenAIChatClient |
| 11 | +from agent_framework.openai import OpenAIChatClient |
| 12 | +from azure.identity import DefaultAzureCredential |
| 13 | +from dotenv import load_dotenv |
| 14 | +from pydantic import BaseModel |
| 15 | + |
| 16 | +load_dotenv(override=True) |
| 17 | +API_HOST = os.getenv("API_HOST", "github") |
| 18 | + |
| 19 | +if API_HOST == "azure": |
| 20 | + client = AzureOpenAIChatClient( |
| 21 | + credential=DefaultAzureCredential(), |
| 22 | + deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"), |
| 23 | + endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
| 24 | + api_version=os.environ.get("AZURE_OPENAI_VERSION"), |
| 25 | + ) |
| 26 | +elif API_HOST == "github": |
| 27 | + client = OpenAIChatClient( |
| 28 | + base_url="https://models.github.ai/inference", |
| 29 | + api_key=os.environ["GITHUB_TOKEN"], |
| 30 | + model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"), |
| 31 | + ) |
| 32 | +elif API_HOST == "ollama": |
| 33 | + client = OpenAIChatClient( |
| 34 | + base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), |
| 35 | + api_key="none", |
| 36 | + model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"), |
| 37 | + ) |
| 38 | +else: |
| 39 | + client = OpenAIChatClient( |
| 40 | + api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o") |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +# Define structured output for review results |
| 45 | +class ReviewResult(BaseModel): |
| 46 | + """Review evaluation with scores and feedback.""" |
| 47 | + |
| 48 | + score: int # Overall quality score (0-100) |
| 49 | + feedback: str # Concise, actionable feedback |
| 50 | + clarity: int # Clarity score (0-100) |
| 51 | + completeness: int # Completeness score (0-100) |
| 52 | + accuracy: int # Accuracy score (0-100) |
| 53 | + structure: int # Structure score (0-100) |
| 54 | + |
| 55 | + |
| 56 | +# Condition function: route to editor if score < 80 |
| 57 | +def needs_editing(message: Any) -> bool: |
| 58 | + """Check if content needs editing based on review score.""" |
| 59 | + if not isinstance(message, AgentExecutorResponse): |
| 60 | + return False |
| 61 | + try: |
| 62 | + review = ReviewResult.model_validate_json(message.agent_run_response.text) |
| 63 | + return review.score < 80 |
| 64 | + except Exception: |
| 65 | + return False |
| 66 | + |
| 67 | + |
| 68 | +# Condition function: content is approved (score >= 80) |
| 69 | +def is_approved(message: Any) -> bool: |
| 70 | + """Check if content is approved (high quality).""" |
| 71 | + if not isinstance(message, AgentExecutorResponse): |
| 72 | + return True |
| 73 | + try: |
| 74 | + review = ReviewResult.model_validate_json(message.agent_run_response.text) |
| 75 | + return review.score >= 80 |
| 76 | + except Exception: |
| 77 | + return True |
| 78 | + |
| 79 | + |
| 80 | +# Create Writer agent - generates content |
| 81 | +writer = client.create_agent( |
| 82 | + name="Writer", |
| 83 | + instructions=( |
| 84 | + "You are an excellent content writer. " |
| 85 | + "Create clear, engaging content based on the user's request. " |
| 86 | + "Focus on clarity, accuracy, and proper structure." |
| 87 | + ), |
| 88 | +) |
| 89 | + |
| 90 | +# Create Reviewer agent - evaluates and provides structured feedback |
| 91 | +reviewer = client.create_agent( |
| 92 | + name="Reviewer", |
| 93 | + instructions=( |
| 94 | + "You are an expert content reviewer. " |
| 95 | + "Evaluate the writer's content based on:\n" |
| 96 | + "1. Clarity - Is it easy to understand?\n" |
| 97 | + "2. Completeness - Does it fully address the topic?\n" |
| 98 | + "3. Accuracy - Is the information correct?\n" |
| 99 | + "4. Structure - Is it well-organized?\n\n" |
| 100 | + "Return a JSON object with:\n" |
| 101 | + "- score: overall quality (0-100)\n" |
| 102 | + "- feedback: concise, actionable feedback\n" |
| 103 | + "- clarity, completeness, accuracy, structure: individual scores (0-100)" |
| 104 | + ), |
| 105 | + response_format=ReviewResult, |
| 106 | +) |
| 107 | + |
| 108 | +# Create Editor agent - improves content based on feedback |
| 109 | +editor = client.create_agent( |
| 110 | + name="Editor", |
| 111 | + instructions=( |
| 112 | + "You are a skilled editor. " |
| 113 | + "You will receive content along with review feedback. " |
| 114 | + "Improve the content by addressing all the issues mentioned in the feedback. " |
| 115 | + "Maintain the original intent while enhancing clarity, completeness, accuracy, and structure." |
| 116 | + ), |
| 117 | +) |
| 118 | + |
| 119 | +# Create Publisher agent - formats content for publication |
| 120 | +publisher = client.create_agent( |
| 121 | + name="Publisher", |
| 122 | + instructions=( |
| 123 | + "You are a publishing agent. " |
| 124 | + "You receive either approved content or edited content. " |
| 125 | + "Format it for publication with proper headings and structure." |
| 126 | + ), |
| 127 | +) |
| 128 | + |
| 129 | +# Create Summarizer agent - creates final publication report |
| 130 | +summarizer = client.create_agent( |
| 131 | + name="Summarizer", |
| 132 | + instructions=( |
| 133 | + "You are a summarizer agent. " |
| 134 | + "Create a final publication report that includes:\n" |
| 135 | + "1. A brief summary of the published content\n" |
| 136 | + "2. The workflow path taken (direct approval or edited)\n" |
| 137 | + "3. Key highlights and takeaways\n" |
| 138 | + "Keep it concise and professional." |
| 139 | + ), |
| 140 | +) |
| 141 | + |
| 142 | +# Build workflow with branching and convergence: |
| 143 | +# Writer → Reviewer → [branches]: |
| 144 | +# - If score >= 80: → Publisher → Summarizer (direct approval path) |
| 145 | +# - If score < 80: → Editor → Publisher → Summarizer (improvement path) |
| 146 | +# Both paths converge at Summarizer for final report |
| 147 | +workflow = ( |
| 148 | + WorkflowBuilder( |
| 149 | + name="Content Review Workflow", |
| 150 | + description="Multi-agent content creation workflow with quality-based routing (Writer → Reviewer → Editor/Publisher)", |
| 151 | + ) |
| 152 | + .set_start_executor(writer) |
| 153 | + .add_edge(writer, reviewer) |
| 154 | + # Branch 1: High quality (>= 80) goes directly to publisher |
| 155 | + .add_edge(reviewer, publisher, condition=is_approved) |
| 156 | + # Branch 2: Low quality (< 80) goes to editor first, then publisher |
| 157 | + .add_edge(reviewer, editor, condition=needs_editing) |
| 158 | + .add_edge(editor, publisher) |
| 159 | + # Both paths converge: Publisher → Summarizer |
| 160 | + .add_edge(publisher, summarizer) |
| 161 | + .build() |
| 162 | +) |
| 163 | + |
| 164 | + |
| 165 | +def main(): |
| 166 | + from agent_framework.devui import serve |
| 167 | + |
| 168 | + serve(entities=[workflow], port=8093, auto_open=True) |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments