Skip to content

The agent engineering platform.

community: Opensearch - added score function for similarity_score_threshold (#23928)

Djordje22f9ae489fb7
diff --git a/libs/community/langchain_community/vectorstores/opensearch_vector_search.py b/libs/community/langchain_community/vectorstores/opensearch_vector_search.py
index c7372f65fc..da5d38db6d 100644
--- a/libs/community/langchain_community/vectorstores/opensearch_vector_search.py
+++ b/libs/community/langchain_community/vectorstores/opensearch_vector_search.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import uuid
import warnings
-from typing import Any, Dict, Iterable, List, Optional, Tuple
+from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
import numpy as np
from langchain_core.documents import Document
@@ -733,6 +733,23 @@ class OpenSearchVectorSearch(VectorStore):
item.get("delete", {}).get("error") for item in response["items"]
)
+ @staticmethod
+ def _identity_fn(score: float) -> float:
+ return score
+
+ def _select_relevance_score_fn(self) -> Callable[[float], float]:
+ """
+ The 'correct' relevance function
+ may differ depending on a few things, including:
+ - the distance / similarity metric used by the VectorStore
+ - the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
+ - embedding dimensionality
+ - etc.
+
+ Vectorstores should define their own selection based method of relevance.
+ """
+ return self._identity_fn
+
def similarity_search(
self,
query: str,