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

refacto: Simplify code to format system prompts

Clémence Lesné452988985360
diff --git a/README.md b/README.md
index e0a29ba..5bb47a2 100644
--- a/README.md
+++ b/README.md
@@ -290,17 +290,10 @@ prompts:
Assistant:
- Answers in {conversation_lang}, even if the employee speaks in a different language
- - Ask the employee to repeat or rephrase their question if it is not clear
- - Be proactive in the reminders you create, employee assistance is your priority
- Cannot talk about any topic other than IT support
- - Do not ask the employee more than 2 questions in a row
- - Do not have access to the employee's personal information, only the current IT support data, conversation history, and reminders
- - Each conversation message is prefixed with the action ({actions}), it adds context to the message, never add it in your answer
- - If employee contacted multiple times, continue the discussion from the previous contact
- - Is allowed to make assumptions, as the employee will correct them if they are wrong
- Is polite, helpful, and professional
- - Keep the sentences short and simple
- Rephrase the employee's questions as statements and answer them
+ - Use additional context to enhance the conversation with useful details
- When the employee says a word and then spells out letters, this means that the word is written in the way the employee spelled it (e.g. "I work in Paris PARIS", "My name is John JOHN", "My email is Clemence CLEMENCE at gmail GMAIL dot com COM")
- You work for {bot_company}, not someone else
@@ -323,9 +316,6 @@ prompts:
Reminders:
{reminders}
- Documentation and training material:
- {trainings}
-
Assistant requires data from the employee to provide IT support. The assistant's role is not over until the issue is resolved or the request is fulfilled.
```
diff --git a/helpers/config_models/prompts.py b/helpers/config_models/prompts.py
index 7cdfd17..b773ebd 100644
--- a/helpers/config_models/prompts.py
+++ b/helpers/config_models/prompts.py
@@ -1,12 +1,15 @@
from datetime import datetime
from fastapi.encoders import jsonable_encoder
+from functools import cached_property
+from logging import Logger
from models.claim import ClaimModel
from models.message import Action as MessageAction, MessageModel
from models.reminder import ReminderModel
from models.training import TrainingModel
+from pydantic import computed_field
from pydantic_settings import BaseSettings
from textwrap import dedent
-from typing import Any, List
+from typing import Any, List, Optional
import json
@@ -52,7 +55,8 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
- Is polite, helpful, and professional
- Keep the sentences short and simple
- Rephrase the customer's questions as statements and answer them
- - Use the documentation and training material as a reference to answer the customer's questions related to insurance or contract details
+ - Use additional context to enhance the conversation with useful details
+ - Use additional context as a reference to answer the customer's questions related to insurance or contract details
- When the customer says a word and then spells out letters, this means that the word is written in the way the customer spelled it (e.g. "I live in Paris PARIS", "My name is John JOHN", "My email is Clemence CLEMENCE at gmail GMAIL dot com COM")
- Will answer the customer's questions if they are related to their contract, claim, or insurance
- Won't answer if they don't know the answer
@@ -79,9 +83,6 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
Reminders:
{reminders}
- Documentation and training material:
- {trainings}
-
Assistant requires data from the customer to fill the claim. Latest claim data will be given. Assistant role is not over until all the relevant data is gathered.
"""
sms_summary_system_tpl: str = """
@@ -164,7 +165,7 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
from helpers.config import CONFIG
# TODO: Parse the date from the end-user timezone, allowing LLM to be used in multiple countries
- return dedent(
+ return self._return(
self.default_system_tpl.format(
bot_company=CONFIG.workflow.bot_company,
bot_name=CONFIG.workflow.bot_name,
@@ -182,20 +183,14 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
) -> str:
from helpers.config import CONFIG
- return dedent(
- self.chat_system_tpl.format(
- actions=", ".join([action.value for action in MessageAction]),
- bot_company=CONFIG.workflow.bot_company,
- claim=_pydantic_to_str(claim),
- conversation_lang=CONFIG.workflow.conversation_lang,
- reminders=_pydantic_to_str(reminders),
- trainings=_pydantic_to_str(
- [
- training.dict(include={"content", "title"})
- for training in trainings
- ]
- ), # Limit size of the training data to avoid a slow inference
- )
+ return self._return(
+ self.chat_system_tpl,
+ trainings=trainings,
+ actions=", ".join([action.value for action in MessageAction]),
+ bot_company=CONFIG.workflow.bot_company,
+ claim=_pydantic_to_str(claim),
+ conversation_lang=CONFIG.workflow.conversation_lang,
+ reminders=_pydantic_to_str(reminders),
)
def sms_summary_system(
@@ -206,13 +201,12 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
) -> str:
from helpers.config import CONFIG
- return dedent(
- self.sms_summary_system_tpl.format(
- claim=_pydantic_to_str(claim),
- conversation_lang=CONFIG.workflow.conversation_lang,
- messages=_pydantic_to_str(messages),
- reminders=_pydantic_to_str(reminders),
- )
+ return self._return(
+ self.sms_summary_system_tpl,
+ claim=_pydantic_to_str(claim),
+ conversation_lang=CONFIG.workflow.conversation_lang,
+ messages=_pydantic_to_str(messages),
+ reminders=_pydantic_to_str(reminders),
)
def synthesis_short_system(
@@ -223,13 +217,12 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
) -> str:
from helpers.config import CONFIG
- return dedent(
- self.synthesis_short_system_tpl.format(
- claim=_pydantic_to_str(claim),
- conversation_lang=CONFIG.workflow.conversation_lang,
- messages=_pydantic_to_str(messages),
- reminders=_pydantic_to_str(reminders),
- )
+ return self._return(
+ self.synthesis_short_system_tpl,
+ claim=_pydantic_to_str(claim),
+ conversation_lang=CONFIG.workflow.conversation_lang,
+ messages=_pydantic_to_str(messages),
+ reminders=_pydantic_to_str(reminders),
)
def synthesis_long_system(
@@ -240,15 +233,36 @@ class LlmModel(BaseSettings, env_prefix="prompts_llm_"):
) -> str:
from helpers.config import CONFIG
+ return self._return(
+ self.synthesis_long_system_tpl,
+ claim=_pydantic_to_str(claim),
+ conversation_lang=CONFIG.workflow.conversation_lang,
+ messages=_pydantic_to_str(messages),
+ reminders=_pydantic_to_str(reminders),
+ )
+
+ def _return(
+ self, prompt_tpl: str, trainings: Optional[List[TrainingModel]] = None, **kwargs
+ ) -> str:
return dedent(
- self.synthesis_long_system_tpl.format(
- claim=_pydantic_to_str(claim),
- conversation_lang=CONFIG.workflow.conversation_lang,
- messages=_pydantic_to_str(messages),
- reminders=_pydantic_to_str(reminders),
- )
+ f"""
+ {dedent(prompt_tpl.format(**kwargs))}
+
+ Additional context:
+ {_pydantic_to_str([
+ training.model_dump(include={"content", "title"})
+ for training in trainings or []
+ ])}
+ """
)
+ @computed_field
+ @cached_property
+ def _logger(self) -> Logger:
+ from helpers.logging import build_logger
+
+ return build_logger(__name__)
+
class TtsModel(BaseSettings, env_prefix="prompts_tts_"):
calltransfer_failure_tpl: str = "Il semble que je ne puisse pas vous mettre en relation avec un agent pour l'instant, mais le prochain agent disponible vous rappellera dès que possible."
@@ -278,52 +292,52 @@ class TtsModel(BaseSettings, env_prefix="prompts_tts_"):
)
def calltransfer_failure(self) -> str:
- return dedent(self.calltransfer_failure_tpl)
+ return self._return(self.calltransfer_failure_tpl)
def connect_agent(self) -> str:
- return dedent(self.connect_agent_tpl)
+ return self._return(self.connect_agent_tpl)
def end_call_to_connect_agent(self) -> str:
- return dedent(self.end_call_to_connect_agent_tpl)
+ return self._return(self.end_call_to_connect_agent_tpl)
def error(self) -> str:
- return dedent(self.error_tpl)
+ return self._return(self.error_tpl)
def goodbye(self) -> str:
from helpers.config import CONFIG
- return dedent(
- self.goodbye_tpl.format(
- bot_company=CONFIG.workflow.bot_company,
- )
+ return self._return(
+ self.goodbye_tpl,
+ bot_company=CONFIG.workflow.bot_company,
)
def hello(self) -> str:
from helpers.config import CONFIG
- return dedent(
- self.hello_tpl.format(
- bot_company=CONFIG.workflow.bot_company,
- bot_name=CONFIG.workflow.bot_name,
- )
+ return self._return(
+ self.hello_tpl,
+ bot_company=CONFIG.workflow.bot_company,
+ bot_name=CONFIG.workflow.bot_name,
)
def timeout_silence(self) -> str:
- return dedent(self.timeout_silence_tpl)
+ return self._return(self.timeout_silence_tpl)
def welcome_back(self) -> str:
from helpers.config import CONFIG
- return dedent(
- self.welcome_back_tpl.format(
- bot_company=CONFIG.workflow.bot_company,
- bot_name=CONFIG.workflow.bot_name,
- conversation_timeout_hour=CONFIG.workflow.conversation_timeout_hour,
- )
+ return self._return(
+ self.welcome_back_tpl,
+ bot_company=CONFIG.workflow.bot_company,
+ bot_name=CONFIG.workflow.bot_name,
+ conversation_timeout_hour=CONFIG.workflow.conversation_timeout_hour,
)
def timeout_loading(self) -> str:
- return dedent(self.timeout_loading_tpl)
+ return self._return(self.timeout_loading_tpl)
+
+ def _return(self, prompt_tpl: str, **kwargs) -> str:
+ return dedent(prompt_tpl.format(**kwargs))
class PromptsModel(BaseSettings, env_prefix="prompts_"):