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

refacto: Fix Communication Services working

Clémence Lesné9b4c3b56c3e4
diff --git a/Makefile b/Makefile
index 8f703ad..ad78ae9 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ functionapp_location := swedencentral
openai_location := southcentralus
search_location := northeurope
# App configuration
-bot_phone_number ?= $(shell cat config.yaml | yq '.communication_service.phone_number')
+bot_phone_number ?= $(shell cat config.yaml | yq '.communication_services.phone_number')
event_subscription_name ?= $(shell echo '$(name)-$(bot_phone_number)' | tr -dc '[:alnum:]-')
twilio_phone_number ?= $(shell cat config.yaml | yq '.sms.twilio.phone_number')
# Bicep outputs
diff --git a/README.md b/README.md
index b336fc0..ceae050 100644
--- a/README.md
+++ b/README.md
@@ -143,7 +143,7 @@ graph LR
ai_search[("RAG\n(AI Search)")]
app["App"]
communication_service_sms["SMS gateway\n(Communication Services)"]
- communication_service["Call gateway\n(Communication Services)"]
+ communication_services["Call gateway\n(Communication Services)"]
constent_safety["Moderation\n(Content Safety)"]
db[("Conversations and claims\n(Cosmos DB or SQLite)")]
event_grid[("Broker\n(Event Grid)")]
@@ -152,7 +152,7 @@ graph LR
translation["Translation\n(Cognitive Services)"]
end
- app -- Answer with text --> communication_service
+ app -- Answer with text --> communication_services
app -- Ask for translation --> translation
app -- Few-shot training --> ai_search
app -- Generate completion --> gpt
@@ -160,16 +160,16 @@ graph LR
app -- Save conversation --> db
app -- Send SMS report --> communication_service_sms
app -- Test for profanity --> constent_safety
- app -- Transfer to agent --> communication_service
+ app -- Transfer to agent --> communication_services
app -. Watch .-> event_grid
- communication_service -- Notifies --> event_grid
- communication_service -- Transfer to --> agent
- communication_service -. Send voice .-> user
+ communication_services -- Notifies --> event_grid
+ communication_services -- Transfer to --> agent
+ communication_services -. Send voice .-> user
communication_service_sms -- Send SMS --> user
- user -- Call --> communication_service
+ user -- Call --> communication_services
```
### Sequence diagram
@@ -257,7 +257,7 @@ workflow:
bot_name: Robert
lang: {}
-communication_service:
+communication_services:
phone_number: "+33612345678"
sms: {}
@@ -300,7 +300,7 @@ workflow:
bot_company: Contoso
bot_name: Robert
-communication_service:
+communication_services:
access_key: xxx
endpoint: https://xxx.france.communication.azure.com
phone_number: "+33612345678"
diff --git a/bicep/app.bicep b/bicep/app.bicep
index 49630f4..df6db7f 100644
--- a/bicep/app.bicep
+++ b/bicep/app.bicep
@@ -51,10 +51,10 @@ var config = {
lang: localConfig.workflow.initiate.lang
}
}
- communication_service: {
+ communication_services: {
access_key: communicationServices.listKeys().primaryKey
endpoint: communicationServices.properties.hostName
- phone_number: localConfig.communication_service.phone_number
+ phone_number: localConfig.communication_services.phone_number
queue_name: communicationServicesQueue.name
}
sms: localConfig.sms
@@ -356,7 +356,7 @@ resource assignmentEventgridTopicQueueSender 'Microsoft.Authorization/roleAssign
resource eventgridSubscriptionCall 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2024-06-01-preview' = {
parent: eventgridTopic
- name: '${prefix}-${replace(localConfig.communication_service.phone_number, '+', '')}'
+ name: '${prefix}-${replace(localConfig.communication_services.phone_number, '+', '')}'
properties: {
eventDeliverySchema: 'EventGridSchema'
deliveryWithResourceIdentity: {
@@ -379,7 +379,7 @@ resource eventgridSubscriptionCall 'Microsoft.EventGrid/systemTopics/eventSubscr
operatorType: 'StringBeginsWith'
key: 'data.to.PhoneNumber.Value'
values: [
- localConfig.communication_service.phone_number
+ localConfig.communication_services.phone_number
]
}
]
@@ -391,7 +391,7 @@ resource eventgridSubscriptionCall 'Microsoft.EventGrid/systemTopics/eventSubscr
resource eventgridSubscriptionSms 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2024-06-01-preview' = {
parent: eventgridTopic
- name: '${prefix}-${replace(localConfig.communication_service.phone_number, '+', '')}-sms'
+ name: '${prefix}-${replace(localConfig.communication_services.phone_number, '+', '')}-sms'
properties: {
eventDeliverySchema: 'EventGridSchema'
deliveryWithResourceIdentity: {
@@ -414,7 +414,7 @@ resource eventgridSubscriptionSms 'Microsoft.EventGrid/systemTopics/eventSubscri
operatorType: 'StringBeginsWith'
key: 'data.to'
values: [
- localConfig.communication_service.phone_number
+ localConfig.communication_services.phone_number
]
}
]
diff --git a/function_app.py b/function_app.py
index ec6cbad..bf35d93 100644
--- a/function_app.py
+++ b/function_app.py
@@ -269,7 +269,7 @@ async def call_post(initiate: CallInitiateModel) -> CallGetModel:
@app.queue_trigger(
arg_name="msg",
connection="Storage",
- queue_name=CONFIG.communication_service.queue_name,
+ queue_name=CONFIG.communication_services.queue_name,
)
async def eventgrid_event_post(msg: func.QueueMessage) -> None:
background_tasks = BackgroundTasks()
diff --git a/helpers/config_models/communication_service.py b/helpers/config_models/communication_services.py
similarity index 81%
rename from helpers/config_models/communication_service.py
rename to helpers/config_models/communication_services.py
index 64225f2..228ff79 100644
--- a/helpers/config_models/communication_service.py
+++ b/helpers/config_models/communication_services.py
@@ -2,7 +2,7 @@ from helpers.pydantic_types.phone_numbers import PhoneNumber
from pydantic import SecretStr, BaseModel
-class CommunicationServiceModel(BaseModel):
+class CommunicationServicesModel(BaseModel):
access_key: SecretStr
endpoint: str
phone_number: PhoneNumber
diff --git a/helpers/config_models/prompts.py b/helpers/config_models/prompts.py
index f437cdb..8ba8f93 100644
--- a/helpers/config_models/prompts.py
+++ b/helpers/config_models/prompts.py
@@ -345,7 +345,7 @@ class LlmModel(BaseModel):
self.default_system_tpl.format(
bot_company=call.initiate.bot_company,
bot_name=call.initiate.bot_name,
- bot_phone_number=CONFIG.communication_service.phone_number,
+ bot_phone_number=CONFIG.communication_services.phone_number,
date=datetime.now(UTC)
.astimezone()
.strftime(
diff --git a/helpers/config_models/root.py b/helpers/config_models/root.py
index 9ae849c..8287ec5 100644
--- a/helpers/config_models/root.py
+++ b/helpers/config_models/root.py
@@ -2,7 +2,7 @@ from helpers.config_models.ai_search import AiSearchModel
from helpers.config_models.ai_translation import AiTranslationModel
from helpers.config_models.cache import CacheModel
from helpers.config_models.cognitive_service import CognitiveServiceModel
-from helpers.config_models.communication_service import CommunicationServiceModel
+from helpers.config_models.communication_services import CommunicationServicesModel
from helpers.config_models.content_safety import ContentSafetyModel
from helpers.config_models.database import DatabaseModel
from helpers.config_models.llm import LlmModel
@@ -31,7 +31,9 @@ class RootModel(BaseSettings):
ai_translation: AiTranslationModel
cache: CacheModel = CacheModel() # Object is fully defined by default
cognitive_service: CognitiveServiceModel
- communication_service: CommunicationServiceModel
+ communication_services: CommunicationServicesModel = Field(
+ serialization_alias="communication_service", # Compatibility with v5
+ )
content_safety: ContentSafetyModel
database: DatabaseModel = DatabaseModel() # Object is fully defined by default
llm: LlmModel
diff --git a/helpers/config_models/sms.py b/helpers/config_models/sms.py
index eae1275..99c8ea8 100644
--- a/helpers/config_models/sms.py
+++ b/helpers/config_models/sms.py
@@ -24,7 +24,7 @@ class CommunicationServiceModel(BaseModel, frozen=True):
from persistence.communication_services import CommunicationServicesSms
from helpers.config import CONFIG
- return CommunicationServicesSms(CONFIG.communication_service)
+ return CommunicationServicesSms(CONFIG.communication_services)
class TwilioModel(BaseModel, frozen=True):
diff --git a/persistence/communication_services.py b/persistence/communication_services.py
index 8cf0388..8c628f1 100644
--- a/persistence/communication_services.py
+++ b/persistence/communication_services.py
@@ -2,7 +2,7 @@ from azure.communication.sms import SmsSendResult
from azure.communication.sms.aio import SmsClient
from azure.core.exceptions import ClientAuthenticationError, HttpResponseError
from contextlib import asynccontextmanager
-from helpers.config_models.communication_service import CommunicationServiceModel
+from helpers.config_models.communication_services import CommunicationServicesModel
from helpers.logging import build_logger
from helpers.pydantic_types.phone_numbers import PhoneNumber
from models.readiness import ReadinessStatus
@@ -14,9 +14,9 @@ _logger = build_logger(__name__)
class CommunicationServicesSms(ISms):
- _config: CommunicationServiceModel
+ _config: CommunicationServicesModel
- def __init__(self, config: CommunicationServiceModel):
+ def __init__(self, config: CommunicationServicesModel):
_logger.info(f"Using Communication Services from number {config.phone_number}")
self._config = config