Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/memos/api/handlers/search_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from typing import Any

_EMBED_BATCH_SIZE = 16 # max documents per embedder call to avoid API batch-size limits

from memos.api.handlers.base_handler import BaseHandler, HandlerDependencies
from memos.api.handlers.formatters_handler import rerank_knowledge_mem
from memos.api.product_models import APISearchRequest, SearchResponse
Expand Down Expand Up @@ -446,7 +448,12 @@ def _extract_embeddings(self, memories: list[dict[str, Any]]) -> list[list[float
missing_documents.append(mem.get("memory", ""))

if missing_indices:
computed = self.searcher.embedder.embed(missing_documents)
computed: list[list[float]] = []
for i in range(0, len(missing_documents), _EMBED_BATCH_SIZE):
batch = missing_documents[i : i + _EMBED_BATCH_SIZE]
batch_result = self.searcher.embedder.embed(batch)
if batch_result:
computed.extend(batch_result)
for idx, embedding in zip(missing_indices, computed, strict=False):
embeddings[idx] = embedding
memories[idx]["metadata"]["embedding"] = embedding
Expand Down
Loading