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

dev: Debug script

Clémence Lesné9393c733fce0
diff --git a/README.md b/README.md
index 2cc0485..9fe57b9 100644
--- a/README.md
+++ b/README.md
@@ -390,6 +390,16 @@ Finally, run:
make dev
```
+### Debug
+
+Breakpoints can be added in the code to debug the application with your favorite IDE.
+
+Also, `local.py` script is available to test the application without the need of a phone call (= without Communication Services). Run the script with:
+
+```bash
+python3 -m tests.local
+```
+
## Advanced usage
### Add my custom training data with AI Search
diff --git a/tests/llm.py b/tests/llm.py
index ad9678f..c5f1212 100644
--- a/tests/llm.py
+++ b/tests/llm.py
@@ -268,12 +268,8 @@ async def test_llm(
call=call,
client=automation_client,
label=call.lang.short_code,
- post_callback=lambda _call: post_event(
- QueueMessage(body=_call.model_dump_json())
- ),
- trainings_callback=lambda _call: trainings_event(
- QueueMessage(body=_call.model_dump_json())
- ),
+ post_callback=lambda _: None,
+ trainings_callback=lambda _: None,
)
# Simulate conversation with speech recognition
@@ -282,22 +278,18 @@ async def test_llm(
await on_speech_recognized(
call=call,
client=automation_client,
- post_callback=lambda _call: post_event(
- QueueMessage(body=_call.model_dump_json())
- ),
+ post_callback=lambda _: None,
text=input,
- trainings_callback=lambda _call: trainings_event(
- QueueMessage(body=_call.model_dump_json())
- ),
+ trainings_callback=lambda _: None,
)
+ # Mock trainings callback
+ await call.trainings(cache_only=False)
# Receip
await on_play_completed(
call=call,
client=automation_client,
contexts=call_client.last_contexts,
- post_callback=lambda _call: post_event(
- QueueMessage(body=_call.model_dump_json())
- ),
+ post_callback=lambda _: None,
)
# Reset contexts
call_client.last_contexts.clear()
diff --git a/tests/local.py b/tests/local.py
new file mode 100644
index 0000000..349dc7b
--- /dev/null
+++ b/tests/local.py
@@ -0,0 +1,100 @@
+# Mock environment variables
+from os import environ
+
+
+environ["PUBLIC_DOMAIN"] = "dummy"
+
+
+# General imports
+from helpers.config import CONFIG
+from helpers.logging import logger
+from models.call import CallStateModel, CallInitiateModel
+from tests.conftest import CallAutomationClientMock
+from helpers.call_events import (
+ on_call_connected,
+ on_ivr_recognized,
+ on_play_completed,
+ on_speech_recognized,
+)
+import asyncio
+from function_app import trainings_event, post_event
+from azure.functions import QueueMessage
+
+
+async def main() -> None:
+ continue_conversation = True
+
+ def _play_media_callback(text: str) -> None:
+ logger.info(f"🤖 {text}")
+
+ def _hang_up_callback() -> None:
+ nonlocal continue_conversation
+ continue_conversation = False
+ logger.info("🤖 Hanging up")
+
+ def _transfer_callback() -> None:
+ nonlocal continue_conversation
+ continue_conversation = False
+ logger.info("🤖 Transfering")
+
+ # Mocks
+ call = CallStateModel(
+ initiate=CallInitiateModel(
+ **CONFIG.conversation.initiate.model_dump(),
+ phone_number="+33612345678", # type: ignore
+ ),
+ lang_shmediumort_code="fr-FR",
+ voice_id="dummy",
+ )
+ automation_client = CallAutomationClientMock(
+ hang_up_callback=_hang_up_callback,
+ play_media_callback=_play_media_callback,
+ transfer_callback=_transfer_callback,
+ )
+ call_client = automation_client.get_call_connection()
+
+ # Connect call
+ await on_call_connected(
+ call=call,
+ client=automation_client,
+ )
+
+ # First IVR
+ await on_ivr_recognized(
+ call=call,
+ client=automation_client,
+ label=call.lang.short_code,
+ post_callback=lambda _: None,
+ trainings_callback=lambda _: None,
+ )
+
+ # Simulate conversation
+ while continue_conversation:
+ message = input("Customer: ")
+ if message.strip().lower() == "exit":
+ break
+ # Answer
+ await on_speech_recognized(
+ call=call,
+ client=automation_client,
+ post_callback=lambda _: None,
+ text=message,
+ trainings_callback=lambda _: None,
+ )
+ # Mock trainings callback
+ await call.trainings(cache_only=False)
+ # Receip
+ await on_play_completed(
+ call=call,
+ client=automation_client,
+ contexts=call_client.last_contexts,
+ post_callback=lambda _: None,
+ )
+ # Reset contexts
+ call_client.last_contexts.clear()
+
+ logger.info("Conversation ended, bye bye")
+
+
+if __name__ == "__main__":
+ asyncio.run(main())