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: Lower serialization size for trainings

Clémence Lesné06f302213b7a
diff --git a/helpers/config_models/prompts.py b/helpers/config_models/prompts.py
index f71ede8..ff0bf55 100644
--- a/helpers/config_models/prompts.py
+++ b/helpers/config_models/prompts.py
@@ -467,8 +467,13 @@ class LlmModel(BaseModel):
# Format trainings, if any
if trainings:
+ trainings_str = (
+ TypeAdapter(list[TrainingModel])
+ .dump_json(trainings, exclude=TrainingModel.excluded_fields_for_llm())
+ .decode()
+ )
res += "\n\n# Trusted data you can use"
- res += f"\n{TypeAdapter(list[TrainingModel]).dump_json(trainings).decode()}"
+ res += f"\n{trainings_str}"
# Remove newlines to avoid hallucinations issues with GPT-4 Turbo
res = " ".join([line.strip() for line in res.splitlines()])
diff --git a/helpers/llm_tools.py b/helpers/llm_tools.py
index bd81e3f..ea15da9 100644
--- a/helpers/llm_tools.py
+++ b/helpers/llm_tools.py
@@ -335,9 +335,14 @@ class LlmPlugins:
)
# Flatten, remove duplicates, and sort by score
trainings = sorted(set(training for task in tasks for training in task or []))
+ trainings_str = (
+ TypeAdapter(list[TrainingModel])
+ .dump_json(trainings, exclude=TrainingModel.excluded_fields_for_llm())
+ .decode()
+ )
# Format results
res = "# Search results"
- res += f"\n{TypeAdapter(list[TrainingModel]).dump_json(trainings).decode()}"
+ res += f"\n{trainings_str}"
return res
async def notify_emergencies(
diff --git a/models/training.py b/models/training.py
index 4791002..77f9b11 100644
--- a/models/training.py
+++ b/models/training.py
@@ -31,3 +31,10 @@ class TrainingModel(BaseModel, frozen=True):
if not isinstance(other, TrainingModel):
return NotImplemented
return self.score < other.score
+
+ @staticmethod
+ def excluded_fields_for_llm() -> set[str]:
+ """
+ Returns fields that should be excluded from sending to LLM because they are not relevant for document understanding.
+ """
+ return {"id", "file_path", "score", "created_at"}
diff --git a/tests/search.py b/tests/search.py
index 8780d9d..ab50932 100644
--- a/tests/search.py
+++ b/tests/search.py
@@ -173,7 +173,9 @@ async def test_relevancy(
actual_output="", # Not used
input=full_input,
retrieval_context=[
- TypeAdapter(TrainingModel).dump_json(training).decode()
+ TypeAdapter(TrainingModel)
+ .dump_json(training, exclude=TrainingModel.excluded_fields_for_llm())
+ .decode()
for training in trainings
],
)