-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathworkflow_aggregator_voting.py
More file actions
187 lines (153 loc) · 6.64 KB
/
workflow_aggregator_voting.py
File metadata and controls
187 lines (153 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Fan-out/fan-in with majority-vote aggregation.
Three classifier agents use different reasoning strategies (keyword,
sentiment, intent) to independently categorize a support ticket. The
fan-in aggregator tallies votes and picks the majority label.
Aggregation technique: majority vote (pure logic, no LLM in aggregator).
Note: In production, using different models per branch would strengthen
the ensemble. Here we simulate diversity via different prompting
strategies on the same model.
Run:
uv run examples/workflow_aggregator_voting.py
uv run examples/workflow_aggregator_voting.py --devui (opens DevUI at http://localhost:8103)
"""
import asyncio
import os
import sys
from collections import Counter
from enum import Enum
from agent_framework import Agent, AgentExecutorResponse, Executor, WorkflowBuilder, WorkflowContext, handler
from agent_framework.openai import OpenAIChatClient
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from pydantic import BaseModel
from typing_extensions import Never
load_dotenv(override=True)
API_HOST = os.getenv("API_HOST", "github")
# Configure the chat client based on the API host
async_credential = None
if API_HOST == "azure":
async_credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
client = OpenAIChatClient(
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
api_key=token_provider,
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
)
elif API_HOST == "github":
client = OpenAIChatClient(
base_url="https://models.github.ai/inference",
api_key=os.environ["GITHUB_TOKEN"],
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4.1-mini"),
)
else:
client = OpenAIChatClient(
api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-5-mini")
)
class Category(str, Enum):
BUG = "bug"
BILLING = "billing"
FEATURE_REQUEST = "feature_request"
GENERAL = "general"
class Classification(BaseModel):
"""Structured output for each classifier agent."""
category: Category
class DispatchPrompt(Executor):
"""Emit the ticket text downstream for fan-out broadcast."""
@handler
async def dispatch(self, prompt: str, ctx: WorkflowContext[str]) -> None:
await ctx.send_message(prompt)
class TallyVotes(Executor):
"""Fan-in aggregator that counts votes and picks the majority label."""
@handler
async def tally(
self,
results: list[AgentExecutorResponse],
ctx: WorkflowContext[Never, str],
) -> None:
"""Count classifier votes and yield the winning category."""
votes: list[tuple[str, str]] = []
for result in results:
classification = Classification.model_validate_json(result.agent_response.text)
votes.append((result.executor_id, classification.category.value))
labels = [label for _, label in votes]
counter = Counter(labels)
winner, count = counter.most_common(1)[0]
report = f" Result: {winner} ({count}/{len(votes)} votes)\n"
for agent_id, label in votes:
report += f" {agent_id}: {label}\n"
await ctx.yield_output(report)
dispatcher = DispatchPrompt(id="dispatcher")
keyword_classifier = Agent(
client=client,
name="KeywordClassifier",
instructions=(
"Classify the support ticket into exactly one category: bug, billing, feature_request, or general.\n"
"Rules:\n"
"- If the message mentions error, crash, bug, broken, or fail → bug\n"
"- If the message mentions invoice, charge, payment, refund, or subscription → billing\n"
"- If the message mentions add, wish, suggest, request, or would be nice → feature_request\n"
"- Otherwise → general"
),
default_options={"response_format": Classification},
)
sentiment_classifier = Agent(
client=client,
name="SentimentClassifier",
instructions=(
"Classify the support ticket into exactly one category: bug, billing, feature_request, or general.\n"
"Analyze the emotional tone:\n"
"- Frustrated or angry about something not working → bug\n"
"- Confused or upset about money/charges → billing\n"
"- Enthusiastic or hopeful about new capabilities → feature_request\n"
"- Neutral informational inquiry → general"
),
default_options={"response_format": Classification},
)
intent_classifier = Agent(
client=client,
name="IntentClassifier",
instructions=(
"Classify the support ticket into exactly one category: bug, billing, feature_request, or general.\n"
"Focus on what the user wants to accomplish:\n"
"- Wants something fixed or repaired → bug\n"
"- Wants a refund, explanation of charges, or account adjustment → billing\n"
"- Wants a new feature or improvement → feature_request\n"
"- Wants general information or has a question → general"
),
default_options={"response_format": Classification},
)
tally = TallyVotes(id="tally")
workflow = (
WorkflowBuilder(
name="FanOutFanInVoting",
description="Ensemble classification with majority-vote aggregation.",
start_executor=dispatcher,
output_executors=[tally],
)
.add_fan_out_edges(dispatcher, [keyword_classifier, sentiment_classifier, intent_classifier])
.add_fan_in_edges([keyword_classifier, sentiment_classifier, intent_classifier], tally)
.build()
)
async def main() -> None:
"""Run several sample tickets and show vote breakdowns."""
samples = [
# Clear-cut: all three should agree
"The app crashes every time I try to upload a photo. Error code 500.",
# Keyword → feature_request (add, wish), Sentiment → bug (angry), Intent → bug (fix broken)
"I wish the export button actually worked. Please add a fix — I'm losing data daily!",
# Keyword → bug (error, fail), Sentiment → feature_request (hopeful), Intent → feature_request (new ability)
"The current search fails on long queries — it would be amazing if you could add fuzzy matching.",
]
for sample in samples:
print(f"Ticket: {sample}")
events = await workflow.run(sample)
for output in events.get_outputs():
print(output)
if async_credential:
await async_credential.close()
if __name__ == "__main__":
if "--devui" in sys.argv:
from agent_framework.devui import serve
serve(entities=[workflow], port=8103, auto_open=True)
else:
asyncio.run(main())