Replies: 1 comment
-
Answer generated by a 🤖 AnswerHello, Thank you for reaching out and providing a detailed explanation of your issue. It seems like you're having trouble with the
Here is how you can implement these changes in your code: def similarity_search_with_score(
self,
query: str,
k: int = 4,
filter: Optional[Dict[str, Any]] = None,
fetch_k: int = 20,
score_threshold: Optional[float] = 0.5, # Add score_threshold parameter
**kwargs: Any,
) -> List[Tuple[Document, float]]:
faiss = dependable_faiss_import()
embedding = self.embeddings.embed_query(query)
vector = np.array([embedding], dtype=np.float32)
if self._normalize_L2:
faiss.normalize_L2(vector)
scores, indices = self.index.search(vector, k if filter is None else fetch_k)
docs = []
for j, i in enumerate(indices[0]):
if i == -1 or scores[0][j] < score_threshold: # Check if score is below threshold
continue
_id = self.index_to_docstore_id[i]
doc = self.docstore.search(_id)
if not isinstance(doc, Document):
raise ValueError(f"Could not find document for id {_id}, got {doc}")
if filter is not None:
filter = {
key: [value] if not isinstance(value, list) else value
for key, value in filter.items()
}
if all(doc.metadata.get(key) in value for key, value in filter.items()):
docs.append((doc, scores[0][j]))
else:
docs.append((doc, scores[0][j]))
return docs Please note that these are general suggestions and the actual improvements might vary based on the specific use case and data. I hope this helps! If you have any other questions or need further clarification, feel free to ask. Best, Sources
This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I developed a chat app using faiss document store to find similar documents for a query. It retrieves top 5 docs, with the highest as "High_score" and the lowest as "minimum score." A condition is applied, requiring values to meet specific criteria (e.g., minimum_value_1 >= 0 and < 1.04, maximum_value_1 <= 1.45).
The application uses a condition to assess query correctness and retrieve relevant documents accordingly. Unfortunately, it classifies some relevant queries, leading to a failure in providing answers for queries that should be considered relevant.
Is there any solution that you guys have, or i am missing something in the process.
Beta Was this translation helpful? Give feedback.
All reactions