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é8f414880f799
diff --git a/examples/blocklist.ipynb b/examples/blocklist.ipynb
index fb3e77e..676a418 100644
--- a/examples/blocklist.ipynb
+++ b/examples/blocklist.ipynb
@@ -64,12 +64,18 @@
"outputs": [],
"source": [
"from azure.ai.contentsafety import BlocklistClient\n",
- "from azure.ai.contentsafety.models import TextBlocklist, TextBlocklistItem, AddOrUpdateTextBlocklistItemsOptions\n",
+ "from azure.ai.contentsafety.models import (\n",
+ " TextBlocklist,\n",
+ " TextBlocklistItem,\n",
+ " AddOrUpdateTextBlocklistItemsOptions,\n",
+ ")\n",
"from azure.core.credentials import AzureKeyCredential\n",
"import pandas as pd\n",
"\n",
"key = AzureKeyCredential(\"19cbfb37271e4d5a9fafba91b35897a5\")\n",
- "client = BlocklistClient(\"https://groupama-diva-poc-contentsafety.cognitiveservices.azure.com/\", key)"
+ "client = BlocklistClient(\n",
+ " \"https://groupama-diva-poc-contentsafety.cognitiveservices.azure.com/\", key\n",
+ ")"
]
},
{
@@ -136,9 +142,7 @@
" )\n",
" client.add_or_update_blocklist_items(\n",
" blocklist_name=blocklist,\n",
- " options=AddOrUpdateTextBlocklistItemsOptions(\n",
- " blocklist_items=words\n",
- " )\n",
+ " options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=words),\n",
" )"
]
}
diff --git a/examples/import-training.ipynb b/examples/import-training.ipynb
index 463bbde..fd71273 100644
--- a/examples/import-training.ipynb
+++ b/examples/import-training.ipynb
@@ -64,16 +64,16 @@
"doc_endpoint = \"https://claim-ai.cognitiveservices.azure.com\"\n",
"doc_credential = AzureKeyCredential(\"xxx\")\n",
"doc_client = DocumentIntelligenceClient(\n",
- " endpoint=doc_endpoint,\n",
- " credential=doc_credential,\n",
+ " endpoint=doc_endpoint,\n",
+ " credential=doc_credential,\n",
")\n",
"\n",
"search_endpoint = \"https://claim-ai.search.windows.net\"\n",
"search_credential = AzureKeyCredential(\"xxx\")\n",
"search_client = SearchClient(\n",
- " endpoint=search_endpoint,\n",
- " index_name=\"trainings\",\n",
- " credential=search_credential,\n",
+ " endpoint=search_endpoint,\n",
+ " index_name=\"trainings\",\n",
+ " credential=search_credential,\n",
")"
]
},
@@ -185,7 +185,7 @@
" \"content\": content,\n",
" \"id\": f\"{'_'.join(re.sub('[^a-z0-9]', ' ', unidecode(source_uri).lower()).split())}-{iterator}\",\n",
" \"source_uri\": unidecode(source_uri).lower(),\n",
- " \"title\": ' '.join(re.sub('[^a-z0-9]', ' ', unidecode(title).lower()).split()),\n",
+ " \"title\": \" \".join(re.sub(\"[^a-z0-9]\", \" \", unidecode(title).lower()).split()),\n",
" }\n",
"\n",
"\n",
@@ -193,43 +193,45 @@
"iterator = 0\n",
"\n",
"for source, content in doc_results.items():\n",
- " # Skip empty documents\n",
- " if not content:\n",
- " continue\n",
+ " # Skip empty documents\n",
+ " if not content:\n",
+ " continue\n",
"\n",
- " # Split the document into sections\n",
- " for section in content.split(\"\\n#\"):\n",
- " lines = section.split(\"\\n\")\n",
- " title = lines[0].strip()\n",
- " paragraph = \" \".join(lines[1:])\n",
+ " # Split the document into sections\n",
+ " for section in content.split(\"\\n#\"):\n",
+ " lines = section.split(\"\\n\")\n",
+ " title = lines[0].strip()\n",
+ " paragraph = \" \".join(lines[1:])\n",
"\n",
- " # Skip empty sections\n",
- " if not section:\n",
- " continue\n",
+ " # Skip empty sections\n",
+ " if not section:\n",
+ " continue\n",
"\n",
- " # Split the paragraph into sentences and group them into groups of 300 characters\n",
- " groups = []\n",
- " group = \"\"\n",
- " separators = re.findall(sentence_r, paragraph)\n",
- " splits = re.split(sentence_r, paragraph)\n",
- " for i, separator in enumerate(separators):\n",
- " sentence = compress_and_clean(splits[i] + separator)\n",
- " if len(group) + len(sentence) >= 300:\n",
+ " # Split the paragraph into sentences and group them into groups of 300 characters\n",
+ " groups = []\n",
+ " group = \"\"\n",
+ " separators = re.findall(sentence_r, paragraph)\n",
+ " splits = re.split(sentence_r, paragraph)\n",
+ " for i, separator in enumerate(separators):\n",
+ " sentence = compress_and_clean(splits[i] + separator)\n",
+ " if len(group) + len(sentence) >= 300:\n",
+ " groups.append(group)\n",
+ " group = \"\"\n",
+ " group += sentence\n",
+ " if group:\n",
" groups.append(group)\n",
- " group = \"\"\n",
- " group += sentence\n",
- " if group:\n",
- " groups.append(group)\n",
"\n",
- " # Create a data object for each group\n",
- " for group in groups:\n",
- " chuncks.append(data(\n",
- " f\"# {title} {group}\",\n",
- " source,\n",
- " title,\n",
- " iterator,\n",
- " ))\n",
- " iterator += 1\n",
+ " # Create a data object for each group\n",
+ " for group in groups:\n",
+ " chuncks.append(\n",
+ " data(\n",
+ " f\"# {title} {group}\",\n",
+ " source,\n",
+ " title,\n",
+ " iterator,\n",
+ " )\n",
+ " )\n",
+ " iterator += 1\n",
"\n",
"print(chuncks[:5])\n",
"print(f\"Created {len(chuncks)} chunks\")"
@@ -286,7 +288,7 @@
" break\n",
" print(ids[:5])\n",
" search_client.delete_documents(ids)\n",
- " print(f\"Deleted {len(ids)} documents\")\n"
+ " print(f\"Deleted {len(ids)} documents\")"
]
}
],
diff --git a/main.py b/main.py
index 66cf4e8..553e40c 100644
--- a/main.py
+++ b/main.py
@@ -55,7 +55,7 @@ from persistence.cosmos import CosmosStore
from persistence.memory import MemoryCache
from persistence.redis import RedisCache
from persistence.sqlite import SqliteStore
-from urllib.parse import quote_plus
+from urllib.parse import quote_plus, urljoin
import asyncio
import html
import re
@@ -108,7 +108,8 @@ call_automation_client = CallAutomationClient(
),
)
sms_client = SmsClient(
- credential=DefaultAzureCredential(), endpoint=CONFIG.communication_service.endpoint
+ credential=DefaultAzureCredential(),
+ endpoint=CONFIG.communication_service.endpoint,
)
# Persistence
@@ -141,11 +142,13 @@ api = FastAPI(
)
-CALL_EVENT_URL = f'{CONFIG.api.events_domain.strip("/")}/call/event/{{phone_number}}/{{callback_secret}}'
-SENTENCE_R = r"[^\w\s+\-–—’/'\",:;()@=]"
-MESSAGE_ACTION_R = rf"action=([a-z_]*)( .*)?"
-_MESSAGE_STYLE_R = rf"style=([a-z_]*)( .*)?"
-FUNC_NAME_SANITIZER_R = r"[^a-zA-Z0-9_-]"
+_CALL_EVENT_URL = urljoin(
+ str(CONFIG.api.events_domain), "/call/event/{phone_number}/{callback_secret}"
+)
+_logger.info(f"Using call event URL {_CALL_EVENT_URL}")
+
+_MESSAGE_ACTION_R = r"action=([a-z_]*)( .*)?"
+_MESSAGE_STYLE_R = r"style=([a-z_]*)( .*)?"
@api.get(
@@ -717,7 +720,7 @@ async def execute_llm_chat(
"""
Remove action from content. AI often adds it by mistake event if explicitly asked not to.
"""
- res = re.match(MESSAGE_ACTION_R, text)
+ res = re.match(_MESSAGE_ACTION_R, text)
if not res:
return text.strip()
content = res.group(2)
@@ -1016,7 +1019,7 @@ async def callback_url(caller_id: str) -> str:
if not call:
call = CallModel(phone_number=caller_id)
await db.call_aset(call)
- return CALL_EVENT_URL.format(
+ return _CALL_EVENT_URL.format(
callback_secret=html.escape(call.callback_secret),
phone_number=html.escape(call.phone_number),
)