Skip to content

Commit

Permalink
feat(agents-api): added mmr to chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedantsahai18 committed Jan 3, 2025
1 parent 63894e4 commit c00d08d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion agents-api/agents_api/queries/chat/gather_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from ..entries.get_history import get_history
from ..sessions.get_session import get_session
from ..utils import rewrap_exceptions
from ..docs.mmr import maximal_marginal_relevance
import numpy as np

T = TypeVar("T")

Expand Down Expand Up @@ -133,6 +135,30 @@ async def gather_messages(
connection_pool=connection_pool,
)

# TODO: Add missing MMR implementation
# Apply MMR if enabled
if (
# MMR is enabled
recall_options.mmr_strength > 0
# The number of doc references is greater than the limit
and len(doc_references) > recall_options.limit
# MMR is not applied to text search
and recall_options.mode != "text"
):
# FIXME: This is a temporary fix to ensure that the MMR algorithm works.
# We shouldn't be having references without embeddings.
doc_references = [
doc for doc in doc_references if doc.snippet.embedding is not None
]

# Apply MMR
indices = maximal_marginal_relevance(
np.asarray(query_embedding),
[doc.snippet.embedding for doc in doc_references],
k=recall_options.limit,
)
# Apply MMR
doc_references = [
doc for i, doc in enumerate(doc_references) if i in set(indices)
]

return past_messages, doc_references

0 comments on commit c00d08d

Please sign in to comment.