-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathworkflow_agents_sequential.py
More file actions
104 lines (85 loc) · 3.58 KB
/
workflow_agents_sequential.py
File metadata and controls
104 lines (85 loc) · 3.58 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
"""Writer → Reviewer workflow using SequentialBuilder orchestration.
Demonstrates: SequentialBuilder(participants=[...]) with AI agents and
collecting the final conversation from workflow.run() + get_outputs().
Each participant receives the full conversation history generated so far.
Reference:
https://learn.microsoft.com/en-us/agent-framework/workflows/orchestrations/sequential?pivots=programming-language-python
Run:
uv run examples/workflow_agents_sequential.py
uv run examples/workflow_agents_sequential.py --devui (opens DevUI at http://localhost:8096)
"""
import asyncio
import logging
import os
import sys
from agent_framework import Agent, Message
from agent_framework.openai import OpenAIChatClient
from agent_framework.orchestrations import SequentialBuilder
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from rich.logging import RichHandler
log_handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False)
logging.basicConfig(level=logging.WARNING, handlers=[log_handler], force=True, format="%(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
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-4o-mini")
)
writer = Agent(
client=client,
name="Writer",
instructions=(
"You are a concise content writer. "
"Write a clear, engaging short article (2-3 paragraphs) based on the user's topic. "
"Focus on accuracy and readability."
),
)
reviewer = Agent(
client=client,
name="Reviewer",
instructions=(
"You are a thoughtful content reviewer. "
"Read the writer's draft and provide specific, constructive feedback. "
"Comment on clarity, accuracy, and structure. Keep your review concise."
),
)
# Build the sequential workflow: Writer → Reviewer
workflow = SequentialBuilder(participants=[writer, reviewer]).build()
async def main():
prompt = 'Write a one-paragraph LinkedIn post: "The AI workflow mistake almost every team makes."'
logger.info("Prompt: %s", prompt)
events = await workflow.run(prompt)
outputs = events.get_outputs()
for conversation in outputs:
logger.info("===== Final Conversation =====")
messages: list[Message] = conversation
for index, message in enumerate(messages, start=1):
author = message.author_name or ("assistant" if message.role == "assistant" else "user")
logger.info("%02d [%s]\n%s", index, author, message.text)
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=8096, auto_open=True)
else:
asyncio.run(main())