Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
feat: Gather all reports by phone number in a new web page
Clémence Lesnéf553855a468f
diff --git a/README.md b/README.mdindex 5bb47a2..26070bf 100644--- a/README.md+++ b/README.md@@ -63,7 +63,7 @@ Extract of the data stored during the call:### User report after the call-A report is available at `https://[your_domain]/call/report/[call_id]`. It shows the conversation history, claim data and reminders.+A report is available at `https://[your_domain]/report/[phone_number]` (like `http://localhost:8080/report/%2B133658471534`). It shows the conversation history, claim data and reminders.diff --git a/main.py b/main.pyindex 3343c76..e02b7ca 100644--- a/main.py+++ b/main.py@@ -30,6 +30,7 @@ from persistence.ai_search import AiSearchSearchfrom persistence.cosmos import CosmosStorefrom persistence.sqlite import SqliteStorefrom tenacity import retry, stop_after_attempt, wait_random_exponential+from urllib.parse import quote_plusimport asyncioimport htmlimport re@@ -50,11 +51,15 @@ import json_logger = build_logger(__name__)_logger.info(f"claim-ai v{CONFIG.version}")+# Jinja templatesjinja = Environment(autoescape=select_autoescape(),enable_async=True,loader=FileSystemLoader("public_website"),)+jinja.filters["quote_plus"] = lambda x: quote_plus(str(x)) if x else ""++# Azure OpenAI_logger.info(f"Using OpenAI GPT model {CONFIG.openai.gpt_model}")oai_gpt = AsyncAzureOpenAI(api_version="2023-12-01-preview",@@ -70,6 +75,8 @@ oai_gpt = AsyncAzureOpenAI(else None),)++# Azure Communication Servicessource_caller = PhoneNumberIdentifier(CONFIG.communication_service.phone_number)_logger.info(f"Using phone number {str(CONFIG.communication_service.phone_number)}")# Cannot place calls with RBAC, need to use access key (see: https://learn.microsoft.com/en-us/azure/communication-services/concepts/authentication#authentication-options)@@ -82,12 +89,16 @@ call_automation_client = CallAutomationClient(sms_client = SmsClient(credential=DefaultAzureCredential(), endpoint=CONFIG.communication_service.endpoint)++# Persistencedb = (SqliteStore(CONFIG.database.sqlite)if CONFIG.database.mode == DatabaseMode.SQLITEelse CosmosStore(CONFIG.database.cosmos_db))search = AiSearchSearch(CONFIG.ai_search)++# FastAPI_logger.info(f'Using root path "{CONFIG.api.root_path}"')api = FastAPI(contact={@@ -124,23 +135,41 @@ async def health_liveness_get() -> None:@api.get(- "/call/report/{call_id}",+ "/report/{phone_number}",+ description="Display the history of calls in a web page.",+)+async def report_history_get(phone_number: str) -> HTMLResponse:+ calls = await db.call_asearch_all(phone_number) or []++ template = jinja.get_template("history.html.jinja")+ render = await template.render_async(+ bot_company=CONFIG.workflow.bot_company,+ bot_name=CONFIG.workflow.bot_name,+ calls=calls,+ phone_number=phone_number,+ version=CONFIG.version,+ )+ return HTMLResponse(content=render)+++ "/report/{phone_number}/{call_id}",description="Display the call report in a web page.",)-async def call_report_get(call_id: UUID) -> HTMLResponse:+async def report_call_get(phone_number: str, call_id: UUID) -> HTMLResponse:call = await db.call_aget(call_id)- if not call:+ if not call or call.phone_number != phone_number:raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,- detail=f"Call {call_id} not found",+ detail=f"Call {call_id} for phone number {phone_number} not found",)- template = jinja.get_template("report.html")+ template = jinja.get_template("report.html.jinja")render = await template.render_async(bot_company=CONFIG.workflow.bot_company,bot_name=CONFIG.workflow.bot_name,- version=CONFIG.version,call=call,+ version=CONFIG.version,)return HTMLResponse(content=render)diff --git a/public_website/history.html.jinja b/public_website/history.html.jinjanew file mode 100644index 0000000..304d263--- /dev/null+++ b/public_website/history.html.jinja@@ -0,0 +1,19 @@+{% extends "templates/base.html.jinja" %}++{% block title %}+Historique des appels pour {{ phone_number }}+{% endblock %}++{% block content %}+<!-- History of calls -->+<div class="col-span-full space-y-4">+ <div class="p-4 space-y-4 rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">+ <h2 class="text-lg">📞 Historique des appels pour {{ phone_number }}</h2>+ {% for call in calls %}+ <div>+ <a class="hover:underline" href="/report/{{ call.phone_number | quote_plus }}/{{ call.call_id | quote_plus }}"><span class="font-mono">#{{ call.claim.claim_id }}</span> ({{ call.created_at.strftime('%d %b %Y %H:%M') }})</a>+ </div>+ {% endfor %}+ </div>+</div>+{% endblock %}diff --git a/public_website/report.html b/public_website/report.htmldeleted file mode 100644index a56dc6d..0000000--- a/public_website/report.html+++ /dev/null@@ -1,167 +0,0 @@-<!DOCTYPE html>-<html>- <head>- <meta charset="utf-8" />- <meta name="viewport" content="width=device-width, initial-scale=1.0" />- <script src="https://cdn.tailwindcss.com"></script>- <title>Déclaration de sinistre {{ call.claim.claim_id }}</title>- </head>- <body class="lg:container overflow-x-hidden mx-auto lg:p-8 text-black dark:text-white bg-white dark:bg-neutral-900 text-sm">-- <!-- Main content -->- <div class="flex flex-col lg:grid lg:grid-cols-6 gap-4">-- <!-- Top introduction -->- <header class="col-span-full my-8 px-4">- <h1>Bonjour {{ call.claim.policyholder_name | title }} 👋🏻</h1>- <p class="text-2xl">Votre sinistre <span class="font-mono">#{{ call.claim.claim_id }}</span> à propos de {{ call.synthesis.short | lower }} est en cours de traitement.</p>- </header>-- <!-- Long synthesis -->- <div class="col-span-full mb-8">- <div class="lg:rounded-md bg-gradient-to-r from-pink-300/30 dark:from-pink-700/30 to-violet-300/30 dark:to-violet-700/30 lg:ring-1 ring-transparent -mx-4 -my-2 px-8 py-6 space-y-4">- <h2 class="text-lg">Synthèse</h2>- {% if not call.synthesis.long %}- <p class="text-neutral-600 dark:text-neutral-400">Aucune synthèse pour l'instant.</p>- {% else %}- <p>{{ call.synthesis.long }}</p>- {% endif %}- </div>- </div>-- <!-- Conversation -->- <div class="col-start-1 col-end-5 order-last lg:order-none">- <div class="p-4 space-y-4 rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">- <h2 class="text-lg">💬 Conversation</h2>-- <!-- Agent and customer -->- <div class="overflow-hidden space-y-6">- {% for message in call.messages %}- {% set name = bot_name if message.persona == 'assistant' else call.claim.policyholder_name %}- <div class="flex flex-row">- <div class="basis-6 relative self-stretch">- <div class="absolute top-0 left-0 w-full h-full -z-10">- <div class="h-screen mx-auto w-0.5 bg-neutral-200/60 dark:bg-neutral-800/60"></div>- </div>- <div class="py-1.5 bg-white dark:bg-neutral-900">- {% if message.persona == 'assistant' %}- <div- class="rounded-full h-2 w-2 mx-auto bg-neutral-200/60 dark:bg-neutral-800/60 ring-1 ring-neutral-300 dark:ring-neutral-700"- ></div>- {% else %}- <div- class="rounded-full h-2 w-2 mx-auto bg-blue-200 dark:bg-blue-800 ring-1 ring-blue-400 dark:ring-blue-600"- ></div>- {% endif %}- </div>- </div>- <div class="basis-full space-y-2 px-4 py-0.5">- <div class="flex flex-row justify-between content-center text-xs">- <div>- {{ name | title }}- <span class="text-neutral-600 dark:text-neutral-400">- {% if message.action == 'call' %}- a appelé- {% elif message.action == 'hangup' %}- a raccroché- {% elif message.action == 'sms' %}- a envoyé un SMS- {% elif message.action == 'talk' %}- a dit- {% endif %}- </span>- </div>- <div class="text-neutral-600 dark:text-neutral-400">- {{ message.created_at.strftime('%d %b %Y %H:%M') }}- </div>- </div>- {% if message.content %}- <p class="text-neutral-600 dark:text-neutral-400">{{ message.content }}</p>- {% endif %}- </div>- </div>- {% else %}- <p class="text-neutral-600 dark:text-neutral-400">Aucun message pour l'instant.</p>- {% endfor %}- </div>-- <!-- TODO: Add a action for a new phone call -->- </div>- </div>-- <div class="col-start-5 col-end-7 space-y-4">- <!-- Reminders -->- <div class="rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60 p-4">- <h2 class="text-lg pb-4">⏰ Rappels</h2>-- {% if not call.reminders %}- <p class="text-neutral-600 dark:text-neutral-400">Aucun rappel pour l'instant.</p>- {% else %}- <div class="space-y-6">- <!-- TODO: Sort reminders by due date -->- {% for reminder in call.reminders %}- <div>- <div class="text-xs">- {{ reminder.owner | title }} <span class="text-neutral-600 dark:text-neutral-400">({{ reminder.due_date_time }})</span>- </div>- <p class="text-neutral-600 dark:text-neutral-400">{{ reminder.title | capitalize }}</p>- </div>- {% endfor %}- </div>- {% endif %}- </div>-- <!-- Details -->- <div class="rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">- <h2 class="text-lg px-4 pt-4">🔎 Détails</h2>- <table class="table-auto w-full">- <tbody>- {%- set details = [- {- 'title': 'Date',- 'value': call.claim.incident_date_time- },- {- 'title': 'Localisation',- 'value': call.claim.incident_location- },- {- 'title': 'Police',- 'value': call.claim.policy_number- },- {- 'title': 'Contact',- 'value': call.claim.policyholder_email | lower ~ ', ' ~ call.claim.policyholder_phone- },- {- 'title': 'Téléphone utilisé',- 'value': call.phone_number- }- ]- %}- <!-- Disable shadow to avoid overlapping with the table border -->- {% for detail in details %}- <tr {% if not loop.last %}class="border-b border-neutral-200/60 dark:border-neutral-700/60"{% endif %}>- <td class="p-4 text-neutral-600 dark:text-neutral-400">{{ detail.title }}</td>- <td class="p-4">{{ detail.value }}</td>- </tr>- {% endfor %}- </tbody>- </table>- </div>- </div>- </div>-- <!-- Footer -->- <footer class="my-16 space-y-2 text-center text-neutral-600 dark:text-neutral-400">- <p>- Call center claim AI phone bot<br>- v{{ version }}- </p>- <div>- <a class="underline" href="https://github.com/clemlesne/claim-ai-phone-bot">Created with ❤️ on GitHub</a>- </div>- </footer>- </body>-</html>diff --git a/public_website/report.html.jinja b/public_website/report.html.jinjanew file mode 100644index 0000000..19049dc--- /dev/null+++ b/public_website/report.html.jinja@@ -0,0 +1,154 @@+{% extends "templates/base.html.jinja" %}++{% block title %}+Déclaration de sinistre {{ call.claim.claim_id }}+{% endblock %}++{% block content %}+<!-- Top introduction -->+<header class="col-span-full my-8 px-4">+ <div class="mb-4 text-neutral-600 dark:text-neutral-400">+ <a class="hover:underline" href="/report/{{ call.phone_number | quote_plus }}">+ ⬅ Go back to your call history+ </a>+ </div>++ <h1>Bonjour {{ call.claim.policyholder_name | title }} 👋🏻</h1>+ <p class="text-2xl">Votre sinistre <span class="font-mono">#{{ call.claim.claim_id }}</span> à propos de {{ call.synthesis.short | lower }} est en cours de traitement.</p>+</header>++<!-- Long synthesis -->+<div class="col-span-full mb-8">+ <div class="lg:rounded-md bg-gradient-to-r from-pink-300/30 dark:from-pink-700/30 to-violet-300/30 dark:to-violet-700/30 lg:ring-1 ring-transparent -mx-4 -my-2 px-8 py-6 space-y-4">+ <h2 class="text-lg">Synthèse</h2>+ {% if not call.synthesis.long %}+ <p class="text-neutral-600 dark:text-neutral-400">Aucune synthèse pour l'instant.</p>+ {% else %}+ <p>{{ call.synthesis.long }}</p>+ {% endif %}+ </div>+</div>++<!-- Conversation -->+<div class="col-start-1 col-end-5 order-last lg:order-none">+ <div class="p-4 space-y-4 rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">+ <h2 class="text-lg">💬 Conversation</h2>++ <!-- Agent and customer -->+ <div class="overflow-hidden space-y-6">+ {% for message in call.messages %}+ {% set name = bot_name if message.persona == 'assistant' else call.claim.policyholder_name %}+ <div class="flex flex-row">+ <div class="basis-6 relative self-stretch">+ <div class="absolute top-0 left-0 w-full h-full -z-10">+ <div class="h-screen mx-auto w-0.5 bg-neutral-200/60 dark:bg-neutral-800/60"></div>+ </div>+ <div class="py-1.5 bg-white dark:bg-neutral-900">+ {% if message.persona == 'assistant' %}+ <div+ class="rounded-full h-2 w-2 mx-auto bg-neutral-200/60 dark:bg-neutral-800/60 ring-1 ring-neutral-300 dark:ring-neutral-700"+ ></div>+ {% else %}+ <div+ class="rounded-full h-2 w-2 mx-auto bg-blue-200 dark:bg-blue-800 ring-1 ring-blue-400 dark:ring-blue-600"+ ></div>+ {% endif %}+ </div>+ </div>+ <div class="basis-full space-y-2 px-4 py-0.5">+ <div class="flex flex-row justify-between content-center text-xs">+ <div>+ {{ name | title }}+ <span class="text-neutral-600 dark:text-neutral-400">+ {% if message.action == 'call' %}+ a appelé+ {% elif message.action == 'hangup' %}+ a raccroché+ {% elif message.action == 'sms' %}+ a envoyé un SMS+ {% elif message.action == 'talk' %}+ a dit+ {% endif %}+ </span>+ </div>+ <div class="text-neutral-600 dark:text-neutral-400">+ {{ message.created_at.strftime('%d %b %Y %H:%M') }}+ </div>+ </div>+ {% if message.content %}+ <p class="text-neutral-600 dark:text-neutral-400">{{ message.content }}</p>+ {% endif %}+ </div>+ </div>+ {% else %}+ <p class="text-neutral-600 dark:text-neutral-400">Aucun message pour l'instant.</p>+ {% endfor %}+ </div>++ <!-- TODO: Add a action for a new phone call -->+ </div>+</div>++<div class="col-start-5 col-end-7 space-y-4">+ <!-- Reminders -->+ <div class="rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60 p-4">+ <h2 class="text-lg pb-4">⏰ Rappels</h2>++ {% if not call.reminders %}+ <p class="text-neutral-600 dark:text-neutral-400">Aucun rappel pour l'instant.</p>+ {% else %}+ <div class="space-y-6">+ <!-- TODO: Sort reminders by due date -->+ {% for reminder in call.reminders %}+ <div>+ <div class="text-xs">+ {{ reminder.owner | title }} <span class="text-neutral-600 dark:text-neutral-400">({{ reminder.due_date_time }})</span>+ </div>+ <p class="text-neutral-600 dark:text-neutral-400">{{ reminder.title | capitalize }}</p>+ </div>+ {% endfor %}+ </div>+ {% endif %}+ </div>++ <!-- Details -->+ <div class="rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">+ <h2 class="text-lg px-4 pt-4">🔎 Détails</h2>+ <table class="table-auto w-full">+ <tbody>+ {%+ set details = [+ {+ 'title': 'Date',+ 'value': call.claim.incident_date_time+ },+ {+ 'title': 'Localisation',+ 'value': call.claim.incident_location+ },+ {+ 'title': 'Police',+ 'value': call.claim.policy_number+ },+ {+ 'title': 'Contact',+ 'value': call.claim.policyholder_email | lower ~ ', ' ~ call.claim.policyholder_phone+ },+ {+ 'title': 'Téléphone utilisé',+ 'value': call.phone_number+ }+ ]+ %}+ <!-- Disable shadow to avoid overlapping with the table border -->+ {% for detail in details %}+ <tr {% if not loop.last %}class="border-b border-neutral-200/60 dark:border-neutral-700/60"{% endif %}>+ <td class="p-4 text-neutral-600 dark:text-neutral-400">{{ detail.title }}</td>+ <td class="p-4">{{ detail.value }}</td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ </div>+</div>+{% endblock %}diff --git a/public_website/templates/base.html.jinja b/public_website/templates/base.html.jinjanew file mode 100644index 0000000..599876e--- /dev/null+++ b/public_website/templates/base.html.jinja@@ -0,0 +1,31 @@+<!DOCTYPE html>+<html>+ <head>+ {% block head %}+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <script src="https://cdn.tailwindcss.com"></script>+ <title>{% block title %}{% endblock %}</title>+ {% endblock %}+ </head>+ <body class="lg:container overflow-x-hidden mx-auto lg:p-8 text-black dark:text-white bg-white dark:bg-neutral-900 text-sm">++ <!-- Main content -->+ <div class="flex flex-col lg:grid lg:grid-cols-6 gap-4">+ {% block content %}{% endblock %}+ </div>++ <!-- Footer -->+ <footer class="my-16 space-y-2 text-center text-neutral-600 dark:text-neutral-400">+ {% block footer %}+ <p>+ Call center claim AI phone bot<br>+ v{{ version }}+ </p>+ <div>+ <a class="hover:underline" href="https://github.com/clemlesne/claim-ai-phone-bot">Created with ❤️ on GitHub</a>+ </div>+ {% endblock %}+ </footer>+ </body>+</html>