Skip to content

Commit 94e956b

Browse files
committed
Port from Azure clients to OpenAI clients
1 parent 010e0c8 commit 94e956b

Some content is hidden

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

57 files changed

+371
-619
lines changed

examples/agentframework_basic.py

Lines changed: 7 additions & 13 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,14 +30,9 @@
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

38-
agent = ChatAgent(
39-
chat_client=client,
40-
instructions="You're an informational agent. Answer questions cheerfully."
41-
)
35+
agent = ChatAgent(chat_client=client, instructions="You're an informational agent. Answer questions cheerfully.")
4236

4337

4438
async def main():

examples/agentframework_supervisor.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
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
@@ -22,11 +22,10 @@
2222
API_HOST = os.getenv("API_HOST", "github")
2323

2424
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"),
25+
client = OpenAIChatClient(
26+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
27+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
28+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
3029
)
3130
elif API_HOST == "github":
3231
client = OpenAIChatClient(
@@ -41,9 +40,7 @@
4140
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4241
)
4342
else:
44-
client = OpenAIChatClient(
45-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
46-
)
43+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4744

4845
# ----------------------------------------------------------------------------------
4946
# Sub-agent 1 tools: weekend planning
@@ -83,11 +80,7 @@ def get_current_date() -> str:
8380

8481
weekend_agent = ChatAgent(
8582
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-
),
83+
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."),
9184
tools=[get_weather, get_activities, get_current_date],
9285
)
9386

@@ -148,11 +141,7 @@ def check_fridge() -> list[str]:
148141

149142
meal_agent = ChatAgent(
150143
chat_client=client,
151-
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."
155-
),
144+
instructions=("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."),
156145
tools=[find_recipes, check_fridge],
157146
)
158147

@@ -170,11 +159,7 @@ async def plan_meal(query: str) -> str:
170159

171160
supervisor_agent = ChatAgent(
172161
chat_client=client,
173-
instructions=(
174-
"You are a supervisor managing two specialist agents: a weekend planning agent and a meal planning agent. "
175-
"Break down the user's request, decide which specialist (or both) to call via the available tools, "
176-
"and then synthesize a final helpful answer. When invoking a tool, provide clear, concise queries."
177-
),
162+
instructions=("You are a supervisor managing two specialist agents: a weekend planning agent and a meal planning agent. " "Break down the user's request, decide which specialist (or both) to call via the available tools, " "and then synthesize a final helpful answer. When invoking a tool, provide clear, concise queries."),
178163
tools=[plan_weekend, plan_meal],
179164
)
180165

examples/agentframework_tool.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
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
@@ -21,11 +21,10 @@
2121
API_HOST = os.getenv("API_HOST", "github")
2222

2323
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"),
24+
client = OpenAIChatClient(
25+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
26+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
27+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
2928
)
3029
elif API_HOST == "github":
3130
client = OpenAIChatClient(
@@ -40,9 +39,7 @@
4039
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4140
)
4241
else:
43-
client = OpenAIChatClient(
44-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
45-
)
42+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4643

4744

4845
def get_weather(
@@ -62,11 +59,7 @@ def get_weather(
6259
}
6360

6461

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

7164

7265
async def main():

examples/agentframework_tools.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
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
@@ -22,11 +22,10 @@
2222
API_HOST = os.getenv("API_HOST", "github")
2323

2424
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"),
25+
client = OpenAIChatClient(
26+
base_url=os.environ.get("AZURE_OPENAI_ENDPOINT") + "/openai/v1/",
27+
api_key=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
28+
model_id=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
3029
)
3130
elif API_HOST == "github":
3231
client = OpenAIChatClient(
@@ -41,9 +40,7 @@
4140
model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"),
4241
)
4342
else:
44-
client = OpenAIChatClient(
45-
api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")
46-
)
43+
client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o"))
4744

4845

4946
def get_weather(
@@ -84,10 +81,7 @@ def get_current_date() -> str:
8481

8582
agent = ChatAgent(
8683
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-
),
84+
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."),
9185
tools=[get_weather, get_activities, get_current_date],
9286
)
9387

examples/azureai_azureopenai.py

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

examples/azureai_githubmodels.py

Lines changed: 0 additions & 19 deletions
This file was deleted.
879 Bytes
Loading

examples/langchainv1_basic.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import azure.identity
44
from dotenv import load_dotenv
55
from langchain.agents import create_agent
6-
from langchain_openai import AzureChatOpenAI, ChatOpenAI
6+
from langchain_openai import ChatOpenAI
77
from rich import print
88

99
load_dotenv(override=True)
@@ -14,11 +14,10 @@
1414
azure.identity.DefaultAzureCredential(),
1515
"https://cognitiveservices.azure.com/.default",
1616
)
17-
model = AzureChatOpenAI(
18-
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
19-
azure_deployment=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
20-
openai_api_version=os.environ.get("AZURE_OPENAI_VERSION"),
21-
azure_ad_token_provider=token_provider,
17+
model = ChatOpenAI(
18+
model=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
19+
base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1",
20+
api_key=token_provider,
2221
)
2322
elif API_HOST == "github":
2423
model = ChatOpenAI(
@@ -35,7 +34,7 @@
3534
else:
3635
model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
3736

38-
agent = create_agent(model=model, prompt="You're an informational agent. Answer questions cheerfully.", tools=[])
37+
agent = create_agent(model=model, system_prompt="You're an informational agent. Answer questions cheerfully.", tools=[])
3938

4039

4140
def main():

examples/langchainv1_mcp_github.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from langchain.agents import create_agent
1818
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
1919
from langchain_mcp_adapters.client import MultiServerMCPClient
20-
from langchain_openai import AzureChatOpenAI, ChatOpenAI
20+
from langchain_openai import ChatOpenAI
2121
from pydantic import BaseModel, Field
2222
from rich import print
2323
from rich.logging import RichHandler
@@ -33,26 +33,25 @@
3333
azure.identity.DefaultAzureCredential(),
3434
"https://cognitiveservices.azure.com/.default",
3535
)
36-
base_model = AzureChatOpenAI(
37-
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
38-
azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
39-
openai_api_version=os.environ["AZURE_OPENAI_VERSION"],
40-
azure_ad_token_provider=token_provider,
36+
model = ChatOpenAI(
37+
model=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
38+
base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1/",
39+
api_key=token_provider,
4140
)
4241
elif API_HOST == "github":
43-
base_model = ChatOpenAI(
42+
model = ChatOpenAI(
4443
model=os.getenv("GITHUB_MODEL", "gpt-4o"),
4544
base_url="https://models.inference.ai.azure.com",
4645
api_key=os.environ.get("GITHUB_TOKEN"),
4746
)
4847
elif API_HOST == "ollama":
49-
base_model = ChatOpenAI(
48+
model = ChatOpenAI(
5049
model=os.environ.get("OLLAMA_MODEL", "llama3.1"),
5150
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
5251
api_key="none",
5352
)
5453
else:
55-
base_model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
54+
model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
5655

5756

5857
class IssueProposal(BaseModel):
@@ -83,12 +82,10 @@ async def main():
8382
prompt_path = Path(__file__).parent / "triager.prompt.md"
8483
with prompt_path.open("r", encoding="utf-8") as f:
8584
prompt = f.read()
86-
agent = create_agent(base_model, prompt=prompt, tools=filtered_tools, response_format=IssueProposal)
85+
agent = create_agent(model, system_prompt=prompt, tools=filtered_tools, response_format=IssueProposal)
8786

8887
user_content = "Find an open issue from Azure-samples azure-search-openai-demo that can be closed."
89-
async for step in agent.astream(
90-
{"messages": [HumanMessage(content=user_content)]}, stream_mode="updates", config={"recursion_limit": 100}
91-
):
88+
async for step in agent.astream({"messages": [HumanMessage(content=user_content)]}, stream_mode="updates", config={"recursion_limit": 100}):
9289
for step_name, step_data in step.items():
9390
last_message = step_data["messages"][-1]
9491
if isinstance(last_message, AIMessage) and last_message.tool_calls:

0 commit comments

Comments
 (0)