Skip to content

Commit 9e8e1cc

Browse files
committed
Use Pydantic AI properly
1 parent 2aa6773 commit 9e8e1cc

File tree

5 files changed

+113
-23
lines changed

5 files changed

+113
-23
lines changed

examples/agentframework_magenticone.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,29 @@
2424
load_dotenv(override=True)
2525
API_HOST = os.getenv("API_HOST", "github")
2626

27+
async_credential = None
28+
2729
async_credential = None
2830
if API_HOST == "azure":
2931
async_credential = DefaultAzureCredential()
3032
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
31-
client = OpenAIChatClient(base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", api_key=token_provider, model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"])
33+
client = OpenAIChatClient(
34+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
35+
api_key=token_provider,
36+
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
37+
)
3238
elif API_HOST == "github":
33-
client = OpenAIChatClient(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"], model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"))
39+
client = OpenAIChatClient(
40+
base_url="https://models.github.ai/inference",
41+
api_key=os.environ["GITHUB_TOKEN"],
42+
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"),
43+
)
3444
elif API_HOST == "ollama":
35-
client = OpenAIChatClient(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none", model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"))
45+
client = OpenAIChatClient(
46+
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
47+
api_key="none",
48+
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
49+
)
3650
else:
3751
client = OpenAIChatClient(api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
3852

@@ -42,7 +56,10 @@
4256
# Create the agents
4357
local_agent = ChatAgent(
4458
chat_client=client,
45-
instructions=("You are a helpful assistant that can suggest authentic and interesting local activities " "or places to visit for a user and can utilize any context information provided."),
59+
instructions=(
60+
"You are a helpful assistant that can suggest authentic and interesting local activities "
61+
"or places to visit for a user and can utilize any context information provided."
62+
),
4663
name="local_agent",
4764
description="A local assistant that can suggest local activities or places to visit.",
4865
)

examples/agentframework_supervisor.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@
2323
load_dotenv(override=True)
2424
API_HOST = os.getenv("API_HOST", "github")
2525

26+
async_credential = None
27+
2628
async_credential = None
2729
if API_HOST == "azure":
2830
async_credential = DefaultAzureCredential()
2931
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
30-
client = OpenAIChatClient(base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", api_key=token_provider, model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"])
32+
client = OpenAIChatClient(
33+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
34+
api_key=token_provider,
35+
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
36+
)
3137
elif API_HOST == "github":
32-
client = OpenAIChatClient(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"], model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"))
38+
client = OpenAIChatClient(
39+
base_url="https://models.github.ai/inference",
40+
api_key=os.environ["GITHUB_TOKEN"],
41+
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"),
42+
)
3343
elif API_HOST == "ollama":
34-
client = OpenAIChatClient(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none", model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"))
44+
client = OpenAIChatClient(
45+
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
46+
api_key="none",
47+
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
48+
)
3549
else:
3650
client = OpenAIChatClient(api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
3751

@@ -73,7 +87,11 @@ def get_current_date() -> str:
7387

7488
weekend_agent = ChatAgent(
7589
chat_client=client,
76-
instructions=("You help users plan their weekends and choose the best activities for the given weather. " "If an activity would be unpleasant in the weather, don't suggest it. " "Include the date of the weekend in your response."),
90+
instructions=(
91+
"You help users plan their weekends and choose the best activities for the given weather. "
92+
"If an activity would be unpleasant in the weather, don't suggest it. "
93+
"Include the date of the weekend in your response."
94+
),
7795
tools=[get_weather, get_activities, get_current_date],
7896
)
7997

@@ -135,7 +153,9 @@ def check_fridge() -> list[str]:
135153
meal_agent = ChatAgent(
136154
chat_client=client,
137155
instructions=(
138-
"You help users plan meals and choose the best recipes. " "Include the ingredients and cooking instructions in your response. " "Indicate what the user needs to buy from the store when their fridge is missing ingredients."
156+
"You help users plan meals and choose the best recipes. "
157+
"Include the ingredients and cooking instructions in your response. "
158+
"Indicate what the user needs to buy from the store when their fridge is missing ingredients."
139159
),
140160
tools=[find_recipes, check_fridge],
141161
)

examples/agentframework_tool.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,29 @@
2222
load_dotenv(override=True)
2323
API_HOST = os.getenv("API_HOST", "github")
2424

25+
async_credential = None
26+
2527
async_credential = None
2628
if API_HOST == "azure":
2729
async_credential = DefaultAzureCredential()
2830
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
29-
client = OpenAIChatClient(base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", api_key=token_provider, model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"])
31+
client = OpenAIChatClient(
32+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
33+
api_key=token_provider,
34+
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
35+
)
3036
elif API_HOST == "github":
31-
client = OpenAIChatClient(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"], model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"))
37+
client = OpenAIChatClient(
38+
base_url="https://models.github.ai/inference",
39+
api_key=os.environ["GITHUB_TOKEN"],
40+
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"),
41+
)
3242
elif API_HOST == "ollama":
33-
client = OpenAIChatClient(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none", model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"))
43+
client = OpenAIChatClient(
44+
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
45+
api_key="none",
46+
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
47+
)
3448
else:
3549
client = OpenAIChatClient(api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
3650

@@ -52,7 +66,9 @@ def get_weather(
5266
}
5367

5468

55-
agent = ChatAgent(chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.", tools=[get_weather])
69+
agent = ChatAgent(
70+
chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.", tools=[get_weather]
71+
)
5672

5773

5874
async def main():

examples/agentframework_tools.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@
2323
load_dotenv(override=True)
2424
API_HOST = os.getenv("API_HOST", "github")
2525

26+
async_credential = None
27+
2628
async_credential = None
2729
if API_HOST == "azure":
2830
async_credential = DefaultAzureCredential()
2931
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
30-
client = OpenAIChatClient(base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", api_key=token_provider, model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"])
32+
client = OpenAIChatClient(
33+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
34+
api_key=token_provider,
35+
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
36+
)
3137
elif API_HOST == "github":
32-
client = OpenAIChatClient(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"], model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"))
38+
client = OpenAIChatClient(
39+
base_url="https://models.github.ai/inference",
40+
api_key=os.environ["GITHUB_TOKEN"],
41+
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"),
42+
)
3343
elif API_HOST == "ollama":
34-
client = OpenAIChatClient(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none", model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"))
44+
client = OpenAIChatClient(
45+
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
46+
api_key="none",
47+
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
48+
)
3549
else:
3650
client = OpenAIChatClient(api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
3751

@@ -74,7 +88,10 @@ def get_current_date() -> str:
7488

7589
agent = ChatAgent(
7690
chat_client=client,
77-
instructions=("You help users plan their weekends and choose the best activities for the given weather. " "If an activity would be unpleasant in weather, don't suggest it. Include date of the weekend in response."),
91+
instructions=(
92+
"You help users plan their weekends and choose the best activities for the given weather. "
93+
"If an activity would be unpleasant in weather, don't suggest it. Include date of the weekend in response."
94+
),
7895
tools=[get_weather, get_activities, get_current_date],
7996
)
8097

examples/agentframework_workflow.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@
1515
if API_HOST == "azure":
1616
async_credential = DefaultAzureCredential()
1717
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
18-
client = OpenAIChatClient(base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", api_key=token_provider, model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"])
18+
client = OpenAIChatClient(
19+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
20+
api_key=token_provider,
21+
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
22+
)
1923
elif API_HOST == "github":
20-
client = OpenAIChatClient(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"], model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"))
24+
client = OpenAIChatClient(
25+
base_url="https://models.github.ai/inference",
26+
api_key=os.environ["GITHUB_TOKEN"],
27+
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"),
28+
)
2129
elif API_HOST == "ollama":
22-
client = OpenAIChatClient(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none", model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"))
30+
client = OpenAIChatClient(
31+
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
32+
api_key="none",
33+
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
34+
)
2335
else:
2436
client = OpenAIChatClient(api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
2537

@@ -63,7 +75,11 @@ def is_approved(message: Any) -> bool:
6375
# Create Writer agent - generates content
6476
writer = client.create_agent(
6577
name="Writer",
66-
instructions=("You are an excellent content writer. " "Create clear, engaging content based on the user's request. " "Focus on clarity, accuracy, and proper structure."),
78+
instructions=(
79+
"You are an excellent content writer. "
80+
"Create clear, engaging content based on the user's request. "
81+
"Focus on clarity, accuracy, and proper structure."
82+
),
6783
)
6884

6985
# Create Reviewer agent - evaluates and provides structured feedback
@@ -98,7 +114,11 @@ def is_approved(message: Any) -> bool:
98114
# Create Publisher agent - formats content for publication
99115
publisher = client.create_agent(
100116
name="Publisher",
101-
instructions=("You are a publishing agent. " "You receive either approved content or edited content. " "Format it for publication with proper headings and structure."),
117+
instructions=(
118+
"You are a publishing agent. "
119+
"You receive either approved content or edited content. "
120+
"Format it for publication with proper headings and structure."
121+
),
102122
)
103123

104124
# Create Summarizer agent - creates final publication report
@@ -122,7 +142,7 @@ def is_approved(message: Any) -> bool:
122142
workflow = (
123143
WorkflowBuilder(
124144
name="Content Review Workflow",
125-
description="Multi-agent content creation workflow with quality-based routing (WriterReviewerEditor/Publisher)",
145+
description="Multi-agent content creation with quality-based routing (WriterReviewerEditor/Publisher)",
126146
)
127147
.set_start_executor(writer)
128148
.add_edge(writer, reviewer)

0 commit comments

Comments
 (0)