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: Limit hallucinations for completions by removing message history

Clémence Lesné49668f8256d3
diff --git a/helpers/call_llm.py b/helpers/call_llm.py
index de91a81..2a79028 100644
--- a/helpers/call_llm.py
+++ b/helpers/call_llm.py
@@ -53,7 +53,6 @@ async def llm_completion(text: Optional[str], call: CallStateModel) -> Optional[
try:
content = await completion_sync(
max_tokens=1000,
- messages=call.messages,
system=system,
)
except APIError as e:
@@ -83,7 +82,6 @@ async def llm_model(
try:
res = await completion_model_sync(
max_tokens=1000,
- messages=call.messages,
model=model,
system=system,
)
diff --git a/helpers/config_models/prompts.py b/helpers/config_models/prompts.py
index 83a63e4..614ea4e 100644
--- a/helpers/config_models/prompts.py
+++ b/helpers/config_models/prompts.py
@@ -198,6 +198,9 @@ class LlmModel(BaseModel):
# Reminders
{reminders}
+ # Conversation history
+ {messages}
+
# Answer examples
- "the breakdown of your scooter"
- "the flooding in your field"
@@ -294,6 +297,9 @@ class LlmModel(BaseModel):
# Reminders
{reminders}
+ # Conversation history
+ {messages}
+
# Response format
{{
"action": "[action]",
@@ -389,6 +395,9 @@ class LlmModel(BaseModel):
return self._return(
self.synthesis_short_system_tpl,
claim=json.dumps(jsonable_encoder(call.claim, exclude_none=True)),
+ messages=TypeAdapter(list[MessageModel])
+ .dump_json(call.messages, exclude_none=True)
+ .decode(),
reminders=TypeAdapter(list[ReminderModel])
.dump_json(call.reminders, exclude_none=True)
.decode(),
@@ -433,6 +442,9 @@ class LlmModel(BaseModel):
self.next_system_tpl,
actions=", ".join([action.value for action in NextActionEnum]),
claim=json.dumps(jsonable_encoder(call.claim, exclude_none=True)),
+ messages=TypeAdapter(list[MessageModel])
+ .dump_json(call.messages, exclude_none=True)
+ .decode(),
reminders=TypeAdapter(list[ReminderModel])
.dump_json(call.reminders, exclude_none=True)
.decode(),
diff --git a/helpers/llm_worker.py b/helpers/llm_worker.py
index 163ef32..95d43e0 100644
--- a/helpers/llm_worker.py
+++ b/helpers/llm_worker.py
@@ -228,7 +228,6 @@ async def _completion_stream_worker(
@TRACER.start_as_current_span("completion_sync")
async def completion_sync(
max_tokens: int,
- messages: list[MessageModel],
system: list[ChatCompletionSystemMessageParam],
json_output: bool = False,
) -> Optional[str]:
@@ -242,7 +241,6 @@ async def completion_sync(
return await _completion_sync_worker(
is_backup=False,
max_tokens=max_tokens,
- messages=messages,
system=system,
json_output=json_output,
)
@@ -267,7 +265,6 @@ async def completion_sync(
return await _completion_sync_worker(
is_backup=True,
max_tokens=max_tokens,
- messages=messages,
system=system,
json_output=json_output,
)
@@ -276,7 +273,6 @@ async def completion_sync(
async def _completion_sync_worker(
is_backup: bool,
max_tokens: int,
- messages: list[MessageModel],
system: list[ChatCompletionSystemMessageParam],
json_output: bool = False,
) -> Optional[str]:
@@ -284,7 +280,7 @@ async def _completion_sync_worker(
Returns a completion.
"""
# Try cache
- cache_key = f"{__name__}-completion_sync-{system}-{messages}-{max_tokens}"
+ cache_key = f"{__name__}-completion_sync-{system}-{max_tokens}"
cached = await _cache.aget(cache_key)
if cached:
return cached.decode()
@@ -299,7 +295,7 @@ async def _completion_sync_worker(
prompt = _limit_messages(
context=platform.context,
max_tokens=max_tokens,
- messages=messages,
+ messages=[],
model=platform.model,
system=system,
)
@@ -334,7 +330,6 @@ async def _completion_sync_worker(
@TRACER.start_as_current_span("completion_model_sync")
async def completion_model_sync(
max_tokens: int,
- messages: list[MessageModel],
model: Type[ModelType],
system: list[ChatCompletionSystemMessageParam],
) -> Optional[ModelType]:
@@ -346,7 +341,6 @@ async def completion_model_sync(
res = await completion_sync(
json_output=True,
max_tokens=max_tokens,
- messages=messages,
system=system,
)
if not res: