Handled empty search result handling and updated the notebook (#27914)

- [ ] **PR title**: "community: updated Kinetica vectorstore"

  - **Description:** Handled empty search results
  - **Issue:** used to throw error if the search results were empty

@efriis
This commit is contained in:
am-kinetica
2024-11-13 02:33:49 +05:30
committed by GitHub
parent 00e7b2dada
commit a646f1c383
2 changed files with 187 additions and 252 deletions

View File

@@ -93,7 +93,7 @@ class Kinetica(VectorStore):
To use, you should have the ``gpudb`` python package installed.
Args:
kinetica_settings: Kinetica connection settings class.
config: Kinetica connection settings class.
embedding_function: Any embedding function implementing
`langchain.embeddings.base.Embeddings` interface.
collection_name: The name of the collection to use. (default: langchain)
@@ -170,7 +170,7 @@ class Kinetica(VectorStore):
except ImportError:
raise ImportError(
"Could not import Kinetica python API. "
"Please install it with `pip install gpudb==7.2.0.9`."
"Please install it with `pip install gpudb>=7.2.2.0`."
)
self.dimensions = dimensions
@@ -199,7 +199,7 @@ class Kinetica(VectorStore):
except ImportError:
raise ImportError(
"Could not import Kinetica python API. "
"Please install it with `pip install gpudb==7.2.0.9`."
"Please install it with `pip install gpudb>=7.2.2.0`."
)
options = GPUdb.Options()
@@ -290,7 +290,7 @@ class Kinetica(VectorStore):
except ImportError:
raise ImportError(
"Could not import Kinetica python API. "
"Please install it with `pip install gpudb==7.2.0.9`."
"Please install it with `pip install gpudb>=7.2.2.0`."
)
return GPUdbTable(
_type=self.table_schema,
@@ -428,7 +428,7 @@ class Kinetica(VectorStore):
k: int = 4,
filter: Optional[dict] = None,
) -> List[Tuple[Document, float]]:
from gpudb import GPUdbException
# from gpudb import GPUdbException
resp: Dict = self.__query_collection(embedding, k, filter)
if resp and resp["status_info"]["status"] == "OK" and "records" in resp:
@@ -436,9 +436,10 @@ class Kinetica(VectorStore):
results = list(zip(*list(records.values())))
return self._results_to_docs_and_scores(results)
else:
self.logger.error(resp["status_info"]["message"])
raise GPUdbException(resp["status_info"]["message"])
self.logger.error(resp["status_info"]["message"])
# raise GPUdbException(resp["status_info"]["message"])
return []
def similarity_search_by_vector(
self,
@@ -464,16 +465,20 @@ class Kinetica(VectorStore):
def _results_to_docs_and_scores(self, results: Any) -> List[Tuple[Document, float]]:
"""Return docs and scores from results."""
docs = [
(
Document(
page_content=result[0],
metadata=json.loads(result[1]),
),
result[2] if self.embedding_function is not None else None,
)
for result in results
]
docs = (
[
(
Document(
page_content=result[0],
metadata=json.loads(result[1]),
),
result[2] if self.embedding_function is not None else None,
)
for result in results
]
if len(results) > 0
else []
)
return docs
def _select_relevance_score_fn(self) -> Callable[[float], float]: