File tree 1 file changed +27
-1
lines changed
agents-api/agents_api/queries/chat
1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change 16
16
from ..entries .get_history import get_history
17
17
from ..sessions .get_session import get_session
18
18
from ..utils import rewrap_exceptions
19
+ from ..docs .mmr import maximal_marginal_relevance
20
+ import numpy as np
19
21
20
22
T = TypeVar ("T" )
21
23
@@ -133,6 +135,30 @@ async def gather_messages(
133
135
connection_pool = connection_pool ,
134
136
)
135
137
136
- # TODO: Add missing MMR implementation
138
+ # Apply MMR if enabled
139
+ if (
140
+ # MMR is enabled
141
+ recall_options .mmr_strength > 0
142
+ # The number of doc references is greater than the limit
143
+ and len (doc_references ) > recall_options .limit
144
+ # MMR is not applied to text search
145
+ and recall_options .mode != "text"
146
+ ):
147
+ # FIXME: This is a temporary fix to ensure that the MMR algorithm works.
148
+ # We shouldn't be having references without embeddings.
149
+ doc_references = [
150
+ doc for doc in doc_references if doc .snippet .embedding is not None
151
+ ]
152
+
153
+ # Apply MMR
154
+ indices = maximal_marginal_relevance (
155
+ np .asarray (query_embedding ),
156
+ [doc .snippet .embedding for doc in doc_references ],
157
+ k = recall_options .limit ,
158
+ )
159
+ # Apply MMR
160
+ doc_references = [
161
+ doc for i , doc in enumerate (doc_references ) if i in set (indices )
162
+ ]
137
163
138
164
return past_messages , doc_references
You can’t perform that action at this time.
0 commit comments