Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

feat: Allow using OpenAI with API key instead of RBAC

Clémence Lesné26390b745cb3
diff --git a/helpers/config_models/openai.py b/helpers/config_models/openai.py
index 4da9d62..0a247ff 100644
--- a/helpers/config_models/openai.py
+++ b/helpers/config_models/openai.py
@@ -1,7 +1,9 @@
-from pydantic import BaseModel
+from pydantic import BaseModel, SecretStr
+from typing import Optional
class OpenAiModel(BaseModel):
+ api_key: Optional[SecretStr] = None
endpoint: str
gpt_deployment: str
gpt_model: str
diff --git a/main.py b/main.py
index c2ec8b7..ac727c2 100644
--- a/main.py
+++ b/main.py
@@ -49,11 +49,17 @@ _logger.info(f'Using root path "{ROOT_PATH}"')
oai_gpt = AsyncAzureOpenAI(
api_version="2023-12-01-preview",
- azure_ad_token_provider=get_bearer_token_provider(
- AZ_CREDENTIAL, "https://cognitiveservices.azure.com/.default"
- ),
- azure_endpoint=CONFIG.openai.endpoint,
azure_deployment=CONFIG.openai.gpt_deployment,
+ azure_endpoint=CONFIG.openai.endpoint,
+ # Authentication, either RBAC or API key
+ api_key=CONFIG.openai.api_key.get_secret_value() if CONFIG.openai.api_key else None,
+ azure_ad_token_provider=(
+ get_bearer_token_provider(
+ AZ_CREDENTIAL, "https://cognitiveservices.azure.com/.default"
+ )
+ if not CONFIG.openai.api_key
+ else None
+ ),
)
eventgrid_subscription_name = f"tmp-{uuid4()}"
source_caller = PhoneNumberIdentifier(CONFIG.communication_service.phone_number)