Skip to content

Commit da5220c

Browse files
committed
remove multiple env loading, use single azure_credentials
1 parent 3a6076c commit da5220c

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

src/fastapi_app/__init__.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,21 @@
1-
import contextlib
21
import logging
32
import os
43

54
from dotenv import load_dotenv
6-
from environs import Env
75
from fastapi import FastAPI
86

9-
from fastapi_app.dependencies import get_azure_credentials
10-
from fastapi_app.postgres_engine import create_postgres_engine_from_env
11-
127
logger = logging.getLogger("ragapp")
138

149

15-
@contextlib.asynccontextmanager
16-
async def lifespan(app: FastAPI):
17-
load_dotenv(override=True)
18-
19-
azure_credential = await get_azure_credentials()
20-
engine = await create_postgres_engine_from_env(azure_credential)
21-
22-
yield
23-
24-
await engine.dispose()
25-
26-
2710
def create_app(testing: bool = False):
28-
env = Env()
29-
3011
if os.getenv("RUNNING_IN_PRODUCTION"):
3112
logging.basicConfig(level=logging.WARNING)
3213
else:
3314
if not testing:
34-
env.read_env(".env", override=True)
15+
load_dotenv(override=True)
3516
logging.basicConfig(level=logging.INFO)
3617

37-
app = FastAPI(docs_url="/docs", lifespan=lifespan)
18+
app = FastAPI(docs_url="/docs")
3819

3920
from fastapi_app.routes import api_routes, frontend_routes
4021

src/fastapi_app/dependencies.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Annotated
44

55
import azure.identity
6-
from dotenv import load_dotenv
76
from fastapi import Depends
87
from openai import AsyncAzureOpenAI, AsyncOpenAI
98
from pydantic import BaseModel
@@ -40,7 +39,6 @@ async def common_parameters():
4039
"""
4140
Get the common parameters for the FastAPI app
4241
"""
43-
load_dotenv(override=True)
4442
OPENAI_EMBED_HOST = os.getenv("OPENAI_EMBED_HOST")
4543
OPENAI_CHAT_HOST = os.getenv("OPENAI_CHAT_HOST")
4644
if OPENAI_EMBED_HOST == "azure":
@@ -69,7 +67,7 @@ async def common_parameters():
6967
)
7068

7169

72-
async def get_azure_credentials() -> azure.identity.DefaultAzureCredential | azure.identity.ManagedIdentityCredential:
70+
def get_azure_credentials() -> azure.identity.DefaultAzureCredential | azure.identity.ManagedIdentityCredential:
7371
azure_credential: azure.identity.DefaultAzureCredential | azure.identity.ManagedIdentityCredential
7472
try:
7573
if client_id := os.getenv("APP_IDENTITY_ID"):
@@ -88,10 +86,11 @@ async def get_azure_credentials() -> azure.identity.DefaultAzureCredential | azu
8886
raise e
8987

9088

89+
azure_credentials = get_azure_credentials()
90+
91+
9192
async def get_engine():
9293
"""Get the agent database engine"""
93-
load_dotenv(override=True)
94-
azure_credentials = await get_azure_credentials()
9594
engine = await create_postgres_engine_from_env(azure_credentials)
9695
return engine
9796

@@ -105,14 +104,12 @@ async def get_async_session(engine: Annotated[AsyncEngine, Depends(get_engine)])
105104

106105
async def get_openai_chat_client():
107106
"""Get the OpenAI chat client"""
108-
azure_credentials = await get_azure_credentials()
109107
chat_client = await create_openai_chat_client(azure_credentials)
110108
return OpenAIClient(client=chat_client)
111109

112110

113111
async def get_openai_embed_client():
114112
"""Get the OpenAI embed client"""
115-
azure_credentials = await get_azure_credentials()
116113
embed_client = await create_openai_embed_client(azure_credentials)
117114
return OpenAIClient(client=embed_client)
118115

src/fastapi_app/openai_clients.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def create_openai_chat_client(
2525
api_key=api_key,
2626
)
2727
else:
28-
logger.info("Authenticating to Azure OpenAI using Azure Identity...")
28+
logger.info("Authenticating to Azure OpenAI Chat using Azure Identity...")
2929
token_provider = azure.identity.get_bearer_token_provider(
3030
azure_credential, "https://cognitiveservices.azure.com/.default"
3131
)
@@ -66,7 +66,7 @@ async def create_openai_embed_client(
6666
api_key=api_key,
6767
)
6868
else:
69-
logger.info("Authenticating to Azure OpenAI using Azure Identity...")
69+
logger.info("Authenticating to Azure OpenAI Embedding using Azure Identity...")
7070
token_provider = azure.identity.get_bearer_token_provider(
7171
azure_credential, "https://cognitiveservices.azure.com/.default"
7272
)

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def mock_session_env(monkeypatch_session):
5656
# Azure OpenAI
5757
monkeypatch_session.setenv("OPENAI_CHAT_HOST", "azure")
5858
monkeypatch_session.setenv("OPENAI_EMBED_HOST", "azure")
59+
monkeypatch_session.setenv("AZURE_OPENAI_ENDPOINT", "https://api.openai.com")
5960
monkeypatch_session.setenv("AZURE_OPENAI_VERSION", "2024-03-01-preview")
6061
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_DEPLOYMENT", "gpt-35-turbo")
6162
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_MODEL", "gpt-35-turbo")

0 commit comments

Comments
 (0)