Skip to content

Commit fd74954

Browse files
committed
Switch from OpenAI to Azure OpenAI
1 parent eee5e87 commit fd74954

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

samples-v2/openai_agents/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ dmypy.json
126126
bin
127127
obj
128128
appsettings.json
129-
# local.settings.json
129+
local.settings.json
130130

131131
# Azurite artifacts
132132
__blobstorage__

samples-v2/openai_agents/function_app.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1+
import os
12
import azure.functions as func
2-
from agents import Agent, Runner
3+
from openai import AsyncAzureOpenAI
4+
from agents import Agent, Runner, set_default_openai_client
35
from durable_decorators import durable_openai_agent_orchestrator
46
from model_activity import create_invoke_model_activity
7+
from azure.identity import DefaultAzureCredential
8+
9+
10+
#region Regular Azure OpenAI setup
11+
12+
# Initialize Azure credential
13+
credential = DefaultAzureCredential()
14+
15+
# Token provider function that returns the token
16+
def get_azure_token():
17+
return credential.get_token("https://cognitiveservices.azure.com/.default").token
18+
19+
# Initialize Azure OpenAI client with DefaultAzureCredential
20+
openai_client = AsyncAzureOpenAI(
21+
azure_ad_token_provider=get_azure_token,
22+
api_version=os.getenv("AZURE_OPENAI_API_VERSION", "2024-02-01"),
23+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
24+
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-4")
25+
)
26+
27+
# Set the default OpenAI client for the Agents SDK
28+
set_default_openai_client(openai_client)
29+
30+
# endregion
31+
532

633
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
734

samples-v2/openai_agents/local.settings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
"FUNCTIONS_WORKER_RUNTIME": "python",
6+
"AZURE_OPENAI_ENDPOINT": "https://your-openai-service.openai.azure.com/",
7+
"AZURE_OPENAI_DEPLOYMENT": "your-gpt-deployment-name",
8+
"AZURE_OPENAI_API_VERSION": "2025-03-01-preview"
9+
}
10+
}

samples-v2/openai_agents/model_invoker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ def from_json(cls, json_str: str) -> 'ActivityModelInput':
204204
class ModelInvoker:
205205
def __init__(self, model_provider: Optional[ModelProvider] = None):
206206
"""Initialize the activity with a model provider."""
207-
self._model_provider = model_provider or OpenAIProvider(
208-
openai_client=AsyncOpenAI(max_retries=0)
209-
)
207+
self._model_provider = model_provider or OpenAIProvider()
210208

211209
async def invoke_model_activity(self, input: ActivityModelInput) -> ModelResponse:
212210
"""Activity that invokes a model with the given input."""

samples-v2/openai_agents/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
azure-functions
66
azure-functions-durable
7+
azure-identity
8+
openai
79
openai-agents

0 commit comments

Comments
 (0)