Skip to content

Commit ad92d05

Browse files
authored
Merge pull request #37 from Azure-Samples/portfromazure
Port from Azure clients to OpenAI clients, remove azure-ai-inference examples
2 parents 3a0c9ce + da056ce commit ad92d05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+392
-724
lines changed

examples/agentframework_basic.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
import os
33

44
from agent_framework import ChatAgent
5-
from agent_framework.azure import AzureOpenAIChatClient
65
from agent_framework.openai import OpenAIChatClient
76
from azure.identity import DefaultAzureCredential
7+
from azure.identity.aio import get_bearer_token_provider
88
from dotenv import load_dotenv
99
from rich import print
1010

1111
load_dotenv(override=True)
1212
API_HOST = os.getenv("API_HOST", "github")
1313

1414
if API_HOST == "azure":
15-
client = AzureOpenAIChatClient(
16-
credential=DefaultAzureCredential(),
17-
deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
18-
endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
19-
api_version=os.environ.get("AZURE_OPENAI_VERSION"),
15+
client = OpenAIChatClient(
16+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
17+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
18+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
2019
)
2120
elif API_HOST == "github":
2221
client = OpenAIChatClient(
@@ -31,9 +30,7 @@
3130
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
3231
)
3332
else:
34-
client = OpenAIChatClient(
35-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
36-
)
33+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
3734

3835
agent = ChatAgent(chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.")
3936

examples/agentframework_magenticone.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from agent_framework import (
88
ChatAgent,
9-
MagenticAgentDeltaEvent,
109
MagenticAgentMessageEvent,
1110
MagenticBuilder,
1211
MagenticCallbackEvent,
@@ -21,7 +20,6 @@
2120
from rich.console import Console
2221
from rich.markdown import Markdown
2322
from rich.panel import Panel
24-
from rich.text import Text
2523

2624
# Setup the client to use either Azure OpenAI or GitHub Models
2725
load_dotenv(override=True)
@@ -47,20 +45,15 @@
4745
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4846
)
4947
else:
50-
client = OpenAIChatClient(
51-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
52-
)
48+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
5349

5450
# Initialize rich console
5551
console = Console()
5652

5753
# Create the agents
5854
local_agent = ChatAgent(
5955
chat_client=client,
60-
instructions=(
61-
"You are a helpful assistant that can suggest authentic and interesting local activities "
62-
"or places to visit for a user and can utilize any context information provided."
63-
),
56+
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."),
6457
name="local_agent",
6558
description="A local assistant that can suggest local activities or places to visit.",
6659
)
@@ -130,7 +123,7 @@ async def on_event(event: MagenticCallbackEvent) -> None:
130123
)
131124

132125

133-
async def main():
126+
async def main():
134127
async for event in magentic_orchestrator.run_stream("Plan a half-day trip to Costa Rica"):
135128
if isinstance(event, WorkflowOutputEvent):
136129
final_result = event.data
@@ -143,5 +136,6 @@ async def main():
143136
)
144137
)
145138

139+
146140
if __name__ == "__main__":
147141
asyncio.run(main())

examples/agentframework_supervisor.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
from typing import Annotated
77

88
from agent_framework import ChatAgent
9-
from agent_framework.azure import AzureOpenAIChatClient
109
from agent_framework.openai import OpenAIChatClient
1110
from azure.identity import DefaultAzureCredential
11+
from azure.identity.aio import get_bearer_token_provider
1212
from dotenv import load_dotenv
1313
from pydantic import Field
1414
from rich import print
1515
from rich.logging import RichHandler
1616

17-
# Logging setup
18-
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
19-
logger = logging.getLogger("supervisor_demo")
17+
# Setup logging
18+
handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False)
19+
logging.basicConfig(level=logging.WARNING, handlers=[handler], force=True, format="%(message)s")
20+
logger = logging.getLogger(__name__)
21+
logger.setLevel(logging.INFO)
2022

23+
# Configure OpenAI client based on environment
2124
load_dotenv(override=True)
2225
API_HOST = os.getenv("API_HOST", "github")
23-
2426
if API_HOST == "azure":
25-
client = AzureOpenAIChatClient(
26-
credential=DefaultAzureCredential(),
27-
deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
28-
endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
29-
api_version=os.environ.get("AZURE_OPENAI_VERSION"),
27+
client = OpenAIChatClient(
28+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
29+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
30+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
3031
)
3132
elif API_HOST == "github":
3233
client = OpenAIChatClient(
@@ -41,9 +42,7 @@
4142
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4243
)
4344
else:
44-
client = OpenAIChatClient(
45-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
46-
)
45+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4746

4847
# ----------------------------------------------------------------------------------
4948
# Sub-agent 1 tools: weekend planning
@@ -83,11 +82,7 @@ def get_current_date() -> str:
8382

8483
weekend_agent = ChatAgent(
8584
chat_client=client,
86-
instructions=(
87-
"You help users plan their weekends and choose the best activities for the given weather. "
88-
"If an activity would be unpleasant in the weather, don't suggest it. "
89-
"Include the date of the weekend in your response."
90-
),
85+
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."),
9186
tools=[get_weather, get_activities, get_current_date],
9287
)
9388

@@ -149,9 +144,7 @@ def check_fridge() -> list[str]:
149144
meal_agent = ChatAgent(
150145
chat_client=client,
151146
instructions=(
152-
"You help users plan meals and choose the best recipes. "
153-
"Include the ingredients and cooking instructions in your response. "
154-
"Indicate what the user needs to buy from the store when their fridge is missing ingredients."
147+
"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."
155148
),
156149
tools=[find_recipes, check_fridge],
157150
)

examples/agentframework_tool.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@
55
from typing import Annotated
66

77
from agent_framework import ChatAgent
8-
from agent_framework.azure import AzureOpenAIChatClient
98
from agent_framework.openai import OpenAIChatClient
109
from azure.identity import DefaultAzureCredential
10+
from azure.identity.aio import get_bearer_token_provider
1111
from dotenv import load_dotenv
1212
from pydantic import Field
1313
from rich import print
1414
from rich.logging import RichHandler
1515

16-
# Setup logging with rich
17-
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
18-
logger = logging.getLogger("weekend_planner")
16+
# Setup logging
17+
handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False)
18+
logging.basicConfig(level=logging.WARNING, handlers=[handler], force=True, format="%(message)s")
19+
logger = logging.getLogger(__name__)
20+
logger.setLevel(logging.INFO)
1921

22+
# Configure OpenAI client based on environment
2023
load_dotenv(override=True)
2124
API_HOST = os.getenv("API_HOST", "github")
22-
2325
if API_HOST == "azure":
24-
client = AzureOpenAIChatClient(
25-
credential=DefaultAzureCredential(),
26-
deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
27-
endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
28-
api_version=os.environ.get("AZURE_OPENAI_VERSION"),
26+
client = OpenAIChatClient(
27+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
28+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
29+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
2930
)
3031
elif API_HOST == "github":
3132
client = OpenAIChatClient(
@@ -40,9 +41,7 @@
4041
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4142
)
4243
else:
43-
client = OpenAIChatClient(
44-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
45-
)
44+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4645

4746

4847
def get_weather(
@@ -62,9 +61,7 @@ def get_weather(
6261
}
6362

6463

65-
agent = ChatAgent(
66-
chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.", tools=[get_weather]
67-
)
64+
agent = ChatAgent(chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.", tools=[get_weather])
6865

6966

7067
async def main():

examples/agentframework_tools.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
from typing import Annotated
77

88
from agent_framework import ChatAgent
9-
from agent_framework.azure import AzureOpenAIChatClient
109
from agent_framework.openai import OpenAIChatClient
1110
from azure.identity import DefaultAzureCredential
11+
from azure.identity.aio import get_bearer_token_provider
1212
from dotenv import load_dotenv
1313
from pydantic import Field
1414
from rich import print
1515
from rich.logging import RichHandler
1616

17-
# Setup logging with rich
18-
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
19-
logger = logging.getLogger("weekend_planner")
17+
# Setup logging
18+
handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False)
19+
logging.basicConfig(level=logging.WARNING, handlers=[handler], force=True, format="%(message)s")
20+
logger = logging.getLogger(__name__)
21+
logger.setLevel(logging.INFO)
2022

23+
# Configure OpenAI client based on environment
2124
load_dotenv(override=True)
2225
API_HOST = os.getenv("API_HOST", "github")
23-
2426
if API_HOST == "azure":
25-
client = AzureOpenAIChatClient(
26-
credential=DefaultAzureCredential(),
27-
deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
28-
endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
29-
api_version=os.environ.get("AZURE_OPENAI_VERSION"),
27+
client = OpenAIChatClient(
28+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
29+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
30+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
3031
)
3132
elif API_HOST == "github":
3233
client = OpenAIChatClient(
@@ -41,9 +42,7 @@
4142
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4243
)
4344
else:
44-
client = OpenAIChatClient(
45-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
46-
)
45+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4746

4847

4948
def get_weather(
@@ -84,10 +83,7 @@ def get_current_date() -> str:
8483

8584
agent = ChatAgent(
8685
chat_client=client,
87-
instructions=(
88-
"You help users plan their weekends and choose the best activities for the given weather. "
89-
"If an activity would be unpleasant in weather, don't suggest it. Include date of the weekend in response."
90-
),
86+
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."),
9187
tools=[get_weather, get_activities, get_current_date],
9288
)
9389

examples/agentframework_workflow.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
from __future__ import annotations
2-
1+
# pip install agent-framework-devui==1.0.0b251016
32
import os
43
from typing import Any
54

6-
from agent_framework import (
7-
AgentExecutorResponse,
8-
WorkflowBuilder,
9-
)
10-
from agent_framework.azure import AzureOpenAIChatClient
5+
from agent_framework import AgentExecutorResponse, WorkflowBuilder
116
from agent_framework.openai import OpenAIChatClient
127
from azure.identity import DefaultAzureCredential
8+
from azure.identity.aio import get_bearer_token_provider
139
from dotenv import load_dotenv
1410
from pydantic import BaseModel
1511

12+
# Configure OpenAI client based on environment
1613
load_dotenv(override=True)
1714
API_HOST = os.getenv("API_HOST", "github")
18-
1915
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"),
16+
client = OpenAIChatClient(
17+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
18+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
19+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
2520
)
2621
elif API_HOST == "github":
2722
client = OpenAIChatClient(
@@ -36,9 +31,7 @@
3631
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
3732
)
3833
else:
39-
client = OpenAIChatClient(
40-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
41-
)
34+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4235

4336

4437
# Define structured output for review results
@@ -80,11 +73,7 @@ def is_approved(message: Any) -> bool:
8073
# Create Writer agent - generates content
8174
writer = client.create_agent(
8275
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-
),
76+
instructions=("You are an excellent content writer. " "Create clear, engaging content based on the user's request. " "Focus on clarity, accuracy, and proper structure."),
8877
)
8978

9079
# Create Reviewer agent - evaluates and provides structured feedback
@@ -119,11 +108,7 @@ def is_approved(message: Any) -> bool:
119108
# Create Publisher agent - formats content for publication
120109
publisher = client.create_agent(
121110
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-
),
111+
instructions=("You are a publishing agent. " "You receive either approved content or edited content. " "Format it for publication with proper headings and structure."),
127112
)
128113

129114
# Create Summarizer agent - creates final publication report

examples/azureai_azureopenai.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)