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

fix: LLM hallucinations answer "content=I understood..."

Clémence Lesné9e24660c589e
diff --git a/app/helpers/call_events.py b/app/helpers/call_events.py
index 8f2f2f8..e479022 100644
--- a/app/helpers/call_events.py
+++ b/app/helpers/call_events.py
@@ -42,7 +42,6 @@ from app.models.message import (
MessageModel,
PersonaEnum as MessagePersonaEnum,
extract_message_style,
- remove_message_action,
)
from app.models.next import NextModel
from app.models.synthesis import SynthesisModel
@@ -636,7 +635,7 @@ async def _intelligence_sms(
)
# Delete action and style from the message as they are in the history and LLM hallucinates them
- _, content = extract_message_style(remove_message_action(content or ""))
+ _, content = extract_message_style(content or "")
if not content:
logger.warning("Error generating SMS report")
diff --git a/app/helpers/call_llm.py b/app/helpers/call_llm.py
index 9cc7a13..51f01c4 100644
--- a/app/helpers/call_llm.py
+++ b/app/helpers/call_llm.py
@@ -48,7 +48,6 @@ from app.models.message import (
StyleEnum as MessageStyleEnum,
ToolModel as MessageToolModel,
extract_message_style,
- remove_message_action,
)
_db = CONFIG.database.instance()
@@ -454,7 +453,7 @@ async def _generate_chat_completion( # noqa: PLR0913, PLR0911, PLR0912, PLR0915
async def _content_callback(buffer: str) -> None:
# Remove tool calls from buffer content and detect style
- style, local_content = extract_message_style(remove_message_action(buffer))
+ style, local_content = extract_message_style(buffer)
await tts_callback(local_content, style)
# Build RAG
@@ -543,9 +542,7 @@ async def _generate_chat_completion( # noqa: PLR0913, PLR0911, PLR0912, PLR0915
tool_calls = [tool_call for _, tool_call in tool_calls_buffer.items()]
# Delete action and style from the message as they are in the history and LLM hallucinates them
- last_style, content_full = extract_message_style(
- remove_message_action(content_full)
- )
+ last_style, content_full = extract_message_style(content_full)
logger.debug("Completion response: %s", content_full)
logger.debug("Completion tools: %s", tool_calls)
diff --git a/app/models/message.py b/app/models/message.py
index f1051cc..842a0c8 100644
--- a/app/models/message.py
+++ b/app/models/message.py
@@ -163,9 +163,11 @@ class MessageModel(BaseModel):
return res
-def remove_message_action(text: str) -> str:
+def _filter_action(text: str) -> str:
"""
- Remove action from content. AI often adds it by mistake event if explicitly asked not to.
+ Remove action from content.
+
+ AI often adds it by mistake event if explicitly asked not to.
Example:
- Input: "action=talk Hello!"
@@ -182,6 +184,19 @@ def remove_message_action(text: str) -> str:
return text
+def _filter_content(text: str) -> str:
+ """
+ Remove content from text.
+
+ AI often adds it by mistake event if explicitly asked not to.
+
+ Example:
+ - Input: "content=Hello!"
+ - Output: "Hello!"
+ """
+ return text.replace("content=", "")
+
+
def extract_message_style(text: str) -> tuple[StyleEnum, str]:
"""
Detect the style of a message and extract it from the text.
@@ -190,6 +205,11 @@ def extract_message_style(text: str) -> tuple[StyleEnum, str]:
- Input: "style=cheerful Hello!"
- Output: (StyleEnum.CHEERFUL, "Hello!")
"""
+ # Apply hallucination filters
+ text = _filter_action(text)
+ text = _filter_content(text)
+
+ # Extract style
default_style = StyleEnum.NONE
res = re.match(_MESSAGE_STYLE_R, text)
if not res:
@@ -199,6 +219,7 @@ def extract_message_style(text: str) -> tuple[StyleEnum, str]:
StyleEnum(res.group(1)), # style
(res.group(2) or ""), # content
)
+
# Regex failed, return original text
except ValueError:
return default_style, text