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

quality: Code quality

Clémence Lesné37cf51602495
diff --git a/helpers/call_events.py b/helpers/call_events.py
index 700eb04..422d656 100644
--- a/helpers/call_events.py
+++ b/helpers/call_events.py
@@ -119,7 +119,12 @@ async def on_speech_recognized(
trainings_callback: Callable[[CallStateModel], None],
) -> None:
logger.info(f"Voice recognition: {text}")
- call.messages.append(MessageModel(content=text, persona=MessagePersonaEnum.HUMAN))
+ call.messages.append(
+ MessageModel(
+ content=text,
+ persona=MessagePersonaEnum.HUMAN,
+ )
+ )
call.recognition_retry = 0 # Reset recognition retry counter
await asyncio.gather(
handle_clear_queue(
@@ -390,9 +395,9 @@ async def _handle_hangup(
await handle_hangup(client=client, call=call)
call.messages.append(
MessageModel(
+ action=MessageActionEnum.HANGUP,
content="",
persona=MessagePersonaEnum.HUMAN,
- action=MessageActionEnum.HANGUP,
)
)
post_callback(call)
diff --git a/helpers/call_llm.py b/helpers/call_llm.py
index a7f7772..77795ea 100644
--- a/helpers/call_llm.py
+++ b/helpers/call_llm.py
@@ -330,7 +330,7 @@ async def _execute_llm_chat(
1. `bool`, notify error
2. `bool`, should retry chat
- 4. `CallStateModel`, the updated model
+ 3. `CallStateModel`, the updated model
"""
logger.debug("Running LLM chat")
content_full = ""
@@ -460,13 +460,14 @@ async def _execute_llm_chat(
message.style = plugins.style
message.tool_calls = tool_calls
else:
- message = MessageModel(
- content=content_full.strip(),
- persona=MessagePersonaEnum.ASSISTANT,
- style=plugins.style,
- tool_calls=tool_calls,
+ call.messages.append(
+ MessageModel(
+ content=content_full.strip(),
+ persona=MessagePersonaEnum.ASSISTANT,
+ style=plugins.style,
+ tool_calls=tool_calls,
+ )
)
- call.messages.append(message)
if tool_calls: # Recusive call if needed
return False, True, call
diff --git a/helpers/llm_worker.py b/helpers/llm_worker.py
index e9c14aa..ce3f42c 100644
--- a/helpers/llm_worker.py
+++ b/helpers/llm_worker.py
@@ -506,15 +506,18 @@ def _contentsafety_category_test(
"""
Returns `True` if the category is safe or the severity is low, `False` otherwise, meaning the category is unsafe.
"""
- if score == 0:
- return True # No need to check severity
-
- detection = next((item for item in res if item.category == category), None)
-
- if detection and detection.severity and detection.severity > score:
+ if score == 0: # No need to check severity
+ return True
+
+ if (
+ (detection := next((item for item in res if item.category == category), None))
+ and detection.severity
+ and detection.severity > score
+ ): # Unsafe category detected
logger.debug(f"Matched {category} with severity {detection.severity}")
return False
- return True
+
+ return True # Default to safe
@lru_cache # Cache results in memory as token count is done many times on the same content
diff --git a/tests/search.py b/tests/search.py
index 9cc8618..ef9fc06 100644
--- a/tests/search.py
+++ b/tests/search.py
@@ -138,7 +138,10 @@ async def test_relevancy(
# Fill call with messages
for input in inputs:
call.messages.append(
- MessageModel(content=input, persona=MessagePersonaEnum.HUMAN)
+ MessageModel(
+ content=input,
+ persona=MessagePersonaEnum.HUMAN,
+ )
)
# Get trainings