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

perf: Lowering tokens consumption by using text not JSON for trainings

Clémence Lesné2d12cd01eb5a
diff --git a/helpers/config_models/prompts.py b/helpers/config_models/prompts.py
index fb6ef6f..bce0319 100644
--- a/helpers/config_models/prompts.py
+++ b/helpers/config_models/prompts.py
@@ -402,10 +402,16 @@ class LlmModel(BaseModel):
) -> str:
# Build template
res = dedent(prompt_tpl.format(**kwargs))
- # Add trainings if any
+
+ # Format trainings, if any
if trainings:
res += "\n\n# Trusted data you can use"
- res += TypeAdapter(List[TrainingModel]).dump_json(trainings).decode()
+ for training in trainings:
+ res += f"\n- {training.title}: {training.content}"
+
+ # Remove newlines to avoid hallucinations issues with GPT-4 Turbo
+ res = " ".join(res.splitlines()).strip()
+
self._logger.debug(f"LLM prompt: {res}")
return res
diff --git a/helpers/llm_plugins.py b/helpers/llm_plugins.py
index 21092bc..0f6bc96 100644
--- a/helpers/llm_plugins.py
+++ b/helpers/llm_plugins.py
@@ -196,9 +196,14 @@ class LlmPlugins:
*[self.search.training_asearch_all(query, self.call) for query in queries]
)
# Flatten, remove duplicates, and sort by score
- res = sorted(set(training for task in tasks for training in task or []))
+ trainings = sorted(set(training for task in tasks for training in task or []))
- return f"Search results: {TypeAdapter(List[TrainingModel]).dump_json(res).decode()}"
+ # Format results
+ res = "# Search results"
+ for training in trainings:
+ res += f"\n- {training.title}: {training.content}"
+
+ return res
@staticmethod
def to_openai() -> List[ChatCompletionToolParam]: