Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
AZURE_OPENAI_API_VERSION=2024-02-15-preview
AZURE_OPENAI_ENDPOINT=https://YOUR-ENDPOINT-HERE.openai.azure.com/
# Name of the Azure OpenAI GPT deployment (different from the model name)
AZURE_OPENAI_CHAT_DEPLOYMENT=gpt4o-mini
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ venv/
# Bicep build artifacts (exclude .json files except parameters)
infra/**/*.json
!infra/**/main.parameters.json

# Pip artifacts and temporary files
=*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this from gitignore, no changes to gitignore should be needed

5 changes: 0 additions & 5 deletions infra/aca.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ param serviceName string = 'aca'
param exists bool
param openAiDeploymentName string
param openAiEndpoint string
param openAiApiVersion string

resource acaIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: identityName
Expand All @@ -25,10 +24,6 @@ var env = [
name: 'AZURE_OPENAI_ENDPOINT'
value: openAiEndpoint
}
{
name: 'AZURE_OPENAI_API_VERSION'
value: openAiApiVersion
}
{
name: 'RUNNING_IN_PRODUCTION'
value: 'true'
Expand Down
3 changes: 0 additions & 3 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ param openAiResourceGroupName string = ''
})
param openAiResourceLocation string
param openAiSkuName string = ''
param openAiApiVersion string = '' // Used by the SDK in the app code
param disableKeyBasedAuth bool = true

// Parameters for the specific Azure OpenAI deployment:
Expand Down Expand Up @@ -152,7 +151,6 @@ module aca 'aca.bicep' = {
containerRegistryName: containerApps.outputs.registryName
openAiDeploymentName: openAiDeploymentName
openAiEndpoint: createAzureOpenAi ? openAi.outputs.endpoint : openAiEndpoint
openAiApiVersion: openAiApiVersion
exists: acaExists
}
}
Expand Down Expand Up @@ -185,7 +183,6 @@ output AZURE_TENANT_ID string = tenant().tenantId
output AZURE_OPENAI_RESOURCE_GROUP string = openAiResourceGroup.name
output AZURE_OPENAI_RESOURCE_NAME string = openAi.outputs.name
output AZURE_OPENAI_CHAT_DEPLOYMENT string = openAiDeploymentName
output AZURE_OPENAI_API_VERSION string = openAiApiVersion
output AZURE_OPENAI_ENDPOINT string = createAzureOpenAi ? openAi.outputs.endpoint : openAiEndpoint

output SERVICE_ACA_IDENTITY_PRINCIPAL_ID string = aca.outputs.SERVICE_ACA_IDENTITY_PRINCIPAL_ID
Expand Down
2 changes: 1 addition & 1 deletion src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"httptools",
# Used by uvicorn for reload functionality
"watchfiles",
"openai",
"openai>=1.108.1",
"azure-identity",
"aiohttp>=3.11.0",
"python-dotenv",
Expand Down
11 changes: 5 additions & 6 deletions src/quartapp/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ManagedIdentityCredential,
get_bearer_token_provider,
)
from openai import AsyncAzureOpenAI
from openai import AsyncOpenAI
from quart import (
Blueprint,
Response,
Expand Down Expand Up @@ -47,11 +47,10 @@ async def configure_openai():
if not os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT"):
raise ValueError("AZURE_OPENAI_CHAT_DEPLOYMENT is required for Azure OpenAI")

# Create the Asynchronous Azure OpenAI client
bp.openai_client = AsyncAzureOpenAI(
api_version=os.getenv("AZURE_OPENAI_API_VERSION") or "2024-02-15-preview",
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
azure_ad_token_provider=token_provider,
# Create the Asynchronous OpenAI client
bp.openai_client = AsyncOpenAI(
base_url=os.getenv("AZURE_OPENAI_ENDPOINT"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This URL needs "/openai/v1" at the end

api_key=token_provider,
)
# Set the model name to the Azure OpenAI model deployment name
bp.openai_model = os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT")
Expand Down
60 changes: 30 additions & 30 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile pyproject.toml
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --output-file=src/requirements.txt src/pyproject.toml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change shouldnt be needed, as long as you run the file from inside the src folder

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an AGENTS.md that shows how to run pip-compile from inside the src folder

#
aiofiles==24.1.0
# via quart
aiohappyeyeballs==2.6.1
# via aiohttp
aiohttp==3.12.15
# via quartapp (pyproject.toml)
aiohttp==3.12.15
# via quartapp (src/pyproject.toml)
aiosignal==1.4.0
# via aiohttp
annotated-types==0.7.0
Expand All @@ -24,7 +24,7 @@ attrs==25.3.0
azure-core==1.35.1
# via azure-identity
azure-identity==1.25.0
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
blinker==1.9.0
# via
# flask
Expand Down Expand Up @@ -57,26 +57,26 @@ frozenlist==1.7.0
# aiohttp
# aiosignal
gunicorn==23.0.0
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
h11==0.16.0
# via
# httpcore
# hypercorn
# uvicorn
# wsproto
h2==4.3.0
# via hypercorn
hpack==4.1.0
h2==4.3.0
# via hypercorn
hpack==4.1.0
# via h2
httpcore==1.0.9
# via httpx
httptools==0.6.4
# via quartapp (pyproject.toml)
httpx==0.28.1
# via quartapp (src/pyproject.toml)
httpx==0.28.1
# via openai
hypercorn==0.17.3
# via quart
hyperframe==6.1.0
hyperframe==6.1.0
# via h2
idna==3.10
# via
Expand Down Expand Up @@ -110,13 +110,13 @@ multidict==6.6.4
# via
# aiohttp
# yarl
openai==1.107.2
# via quartapp (pyproject.toml)
openai==1.109.0
# via quartapp (src/pyproject.toml)
packaging==25.0
# via gunicorn
priority==2.0.0
# via hypercorn
propcache==0.3.2
propcache==0.3.2
# via
# aiohttp
# yarl
Expand All @@ -126,16 +126,16 @@ pydantic==2.11.9
# via openai
pydantic-core==2.33.2
# via pydantic
pyjwt[crypto]==2.10.1
# via
# msal
# pyjwt
python-dotenv==1.1.1
# via quartapp (pyproject.toml)
pyjwt[crypto]==2.10.1
# via
# msal
# pyjwt
python-dotenv==1.1.1
# via quartapp (src/pyproject.toml)
pyyaml==6.0.2
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
quart==0.20.0
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
requests==2.32.5
# via
# azure-core
Expand Down Expand Up @@ -163,16 +163,16 @@ typing-inspection==0.4.1
urllib3==2.5.0
# via requests
uvicorn==0.36.0
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
uvloop==0.21.0 ; sys_platform != "win32" and (sys_platform != "cygwin" and platform_python_implementation != "PyPy")
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
watchfiles==1.1.0
# via quartapp (pyproject.toml)
# via quartapp (src/pyproject.toml)
werkzeug==3.1.3
# via
# flask
# quart
# quartapp (pyproject.toml)
# quartapp (src/pyproject.toml)
wsproto==1.2.0
# via hypercorn
yarl==1.20.1
Expand Down
3 changes: 1 addition & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ async def test_openai_managedidentity(monkeypatch):
monkeypatch.setenv("AZURE_OPENAI_CLIENT_ID", "test-client-id")
monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "test-openai-service.openai.azure.com")
monkeypatch.setenv("AZURE_OPENAI_CHAT_DEPLOYMENT", "test-chatgpt")
monkeypatch.setenv("AZURE_OPENAI_VERSION", "2023-10-01-preview")

monkeypatch.setattr("azure.identity.aio.ManagedIdentityCredential", mock_cred.MockAzureCredential)

quart_app = quartapp.create_app(testing=True)

async with quart_app.test_app():
assert quart_app.blueprints["chat"].openai_client._azure_ad_token_provider is not None
assert quart_app.blueprints["chat"].openai_client.api_key is not None
Loading