Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
refacto: OTEL span attributes
Clémence Lesné98fa375d29b7
diff --git a/app/helpers/call_events.py b/app/helpers/call_events.pyindex cde56e4..d3efe9e 100644--- a/app/helpers/call_events.py+++ b/app/helpers/call_events.py@@ -35,7 +35,7 @@ from app.helpers.config import CONFIGfrom app.helpers.features import recognition_retry_max, recording_enabledfrom app.helpers.llm_worker import completion_syncfrom app.helpers.logging import logger-from app.helpers.monitoring import CallAttributes, span_attribute, tracer+from app.helpers.monitoring import SpanAttributes, span_attribute, tracerfrom app.models.call import CallStateModelfrom app.models.message import (ActionEnum as MessageActionEnum,@@ -236,7 +236,7 @@ async def on_automation_recognize_error(logger.warning("Unknown context %s, no action taken", contexts)# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "ivr")+ span_attribute(SpanAttributes.CALL_CHANNEL, "ivr")# Retry IVR recognitionlogger.info(@@ -356,7 +356,7 @@ async def on_play_started(logger.debug("Play started")# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "voice")+ span_attribute(SpanAttributes.CALL_CHANNEL, "voice")# Update last interactionasync with _db.call_transac(@@ -382,7 +382,7 @@ async def on_automation_play_completed(logger.debug("Play completed")# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "voice")+ span_attribute(SpanAttributes.CALL_CHANNEL, "voice")# Update last interactionasync with _db.call_transac(@@ -431,7 +431,7 @@ async def on_play_error(error_code: int) -> None:logger.debug("Play failed")# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "voice")+ span_attribute(SpanAttributes.CALL_CHANNEL, "voice")# Suppress known errors# See: https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/communication-services/how-tos/call-automation/play-action.md@@ -470,8 +470,8 @@ async def on_ivr_recognized(logger.info("IVR recognized: %s", label)# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "ivr")- span_attribute(CallAttributes.CALL_MESSAGE, label)+ span_attribute(SpanAttributes.CALL_CHANNEL, "ivr")+ span_attribute(SpanAttributes.CALL_MESSAGE, label)# Parse language from labeltry:@@ -554,8 +554,8 @@ async def on_sms_received(logger.info("SMS received from %s: %s", call.initiate.phone_number, message)# Enrich span- span_attribute(CallAttributes.CALL_CHANNEL, "sms")- span_attribute(CallAttributes.CALL_MESSAGE, message)+ span_attribute(SpanAttributes.CALL_CHANNEL, "sms")+ span_attribute(SpanAttributes.CALL_MESSAGE, message)# Add the SMS to the call historyasync with _db.call_transac(diff --git a/app/helpers/call_llm.py b/app/helpers/call_llm.pyindex 292e746..856a4d7 100644--- a/app/helpers/call_llm.py+++ b/app/helpers/call_llm.py@@ -39,7 +39,7 @@ from app.helpers.llm_worker import (completion_stream,)from app.helpers.logging import logger-from app.helpers.monitoring import CallAttributes, span_attribute, tracer+from app.helpers.monitoring import SpanAttributes, span_attribute, tracerfrom app.models.call import CallStateModelfrom app.models.message import (ActionEnum as MessageAction,@@ -208,8 +208,8 @@ async def _out_answer( # noqa: PLR0915, PLR0913Returns the updated call model."""# Add span attributes- span_attribute(CallAttributes.CALL_CHANNEL, "voice")- span_attribute(CallAttributes.CALL_MESSAGE, call.messages[-1].content)+ span_attribute(SpanAttributes.CALL_CHANNEL, "voice")+ span_attribute(SpanAttributes.CALL_MESSAGE, call.messages[-1].content)# Reset recognition retry counterasync with _db.call_transac(@@ -528,9 +528,16 @@ async def _execute_llm_chat( # noqa: PLR0913, PLR0911, PLR0912, PLR0915return True, True, call # Error, retry# Execute tools- tool_tasks = [tool_call.execute_function(plugins) for tool_call in tool_calls]- await asyncio.gather(*tool_tasks)- call = plugins.call # Update call model if object reference changed+ async with _db.call_transac(+ call=call,+ scheduler=scheduler,+ ):+ await asyncio.gather(+ *[plugins.execute_tool(tool_call) for tool_call in tool_calls]+ )++ # Update call model if object reference changed+ call = plugins.call# Store messageasync with _db.call_transac(diff --git a/app/helpers/monitoring.py b/app/helpers/monitoring.pyindex 204dbec..edf9a12 100644--- a/app/helpers/monitoring.py+++ b/app/helpers/monitoring.py@@ -44,14 +44,24 @@ def span_attribute(key: str, value: AttributeValue) -> None:span.set_attribute(key, value)-class CallAttributes:+class SpanAttributes:"""- OpenTelemetry attributes for a call.+ OpenTelemetry attributes.These attributes are used to track a call in the logs and metrics."""CALL_CHANNEL = "call.channel"+ """Message channel (e.g. sms, ivr, ...)."""CALL_ID = "call.id"+ """Technical call identifier."""CALL_MESSAGE = "call.message"+ """Message content as a string."""CALL_PHONE_NUMBER = "call.phone_number"+ """Phone number of the caller."""+ TOOL_ARGS = "tool.args"+ """Tool arguments being used."""+ TOOL_NAME = "tool.name"+ """Tool name being used."""+ TOOL_RESULT = "tool.result"+ """Tool result."""diff --git a/app/main.py b/app/main.pyindex e92d419..2c5b26d 100644--- a/app/main.py+++ b/app/main.py@@ -59,7 +59,7 @@ from app.helpers.call_utils import ContextEnum as CallContextEnumfrom app.helpers.config import CONFIGfrom app.helpers.http import azure_transportfrom app.helpers.logging import logger-from app.helpers.monitoring import CallAttributes, span_attribute, tracer+from app.helpers.monitoring import SpanAttributes, span_attribute, tracerfrom app.helpers.pydantic_types.phone_numbers import PhoneNumberfrom app.helpers.resources import resources_dirfrom app.models.call import CallGetModel, CallInitiateModel, CallStateModel@@ -385,8 +385,8 @@ async def call_post(request: Request) -> CallGetModel:)# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call.call_id))- span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)+ span_attribute(SpanAttributes.CALL_ID, str(call.call_id))+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)# Init SDKautomation_client = await _use_automation_client()@@ -440,8 +440,8 @@ async def call_event(callback_url, wss_url, _call = await _communicationservices_urls(phone_number)# Enrich span- span_attribute(CallAttributes.CALL_ID, str(_call.call_id))- span_attribute(CallAttributes.CALL_PHONE_NUMBER, _call.initiate.phone_number)+ span_attribute(SpanAttributes.CALL_ID, str(_call.call_id))+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, _call.initiate.phone_number)# Execute business logicawait on_new_call(@@ -478,7 +478,7 @@ async def sms_event(phone_number: str = event.data["from"]# Enrich span- span_attribute(CallAttributes.CALL_PHONE_NUMBER, phone_number)+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, phone_number)# Get callcall = await _db.call_search_one(phone_number)@@ -487,7 +487,7 @@ async def sms_event(return# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call.call_id))+ span_attribute(SpanAttributes.CALL_ID, str(call.call_id))async with get_scheduler() as scheduler:# Execute business logic@@ -535,7 +535,7 @@ async def _communicationservices_validate_call_id(secret: str,) -> CallStateModel:# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call_id))+ span_attribute(SpanAttributes.CALL_ID, str(call_id))# Validate callcall = await _db.call_get(call_id)@@ -553,7 +553,7 @@ async def _communicationservices_validate_call_id()# Enrich span- span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)return call@@ -847,8 +847,8 @@ async def training_event(call = CallStateModel.model_validate_json(training.content)# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call.call_id))- span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)+ span_attribute(SpanAttributes.CALL_ID, str(call.call_id))+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)logger.debug("Training event received")@@ -872,8 +872,8 @@ async def post_event(return# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call.call_id))- span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)+ span_attribute(SpanAttributes.CALL_ID, str(call.call_id))+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)logger.debug("Post event received")@@ -955,7 +955,7 @@ async def twilio_sms_post(Returns a 200 OK if the SMS is properly formatted. Otherwise, returns a 400 Bad Request."""# Enrich span- span_attribute(CallAttributes.CALL_PHONE_NUMBER, From)+ span_attribute(SpanAttributes.CALL_PHONE_NUMBER, From)# Get callcall = await _db.call_search_one(From)@@ -964,7 +964,7 @@ async def twilio_sms_post(logger.warning("Call for phone number %s not found", From)else:# Enrich span- span_attribute(CallAttributes.CALL_ID, str(call.call_id))+ span_attribute(SpanAttributes.CALL_ID, str(call.call_id))async with get_scheduler() as scheduler:# Execute business logic