community[patch]: vectorstores import update (#21169)

Issue: we have several helper functions to import third-party libraries
like lancedb.import_lancedb in
[community.vectorstores](https://api.python.langchain.com/en/latest/vectorstores/langchain_community.vectorstores.lancedb.import_lancedb.html#langchain_community.vectorstores.lancedb.import_lancedb).
And we have core.utils.utils.guard_import that works exactly for this
purpose.
The import_<package> functions work inconsistently and rather be private
functions.
Change: replaced these functions with the guard_import function.

Related to #21133
This commit is contained in:
Leonid Ganeline
2024-05-13 07:45:31 -07:00
committed by GitHub
parent 3003363605
commit 500569da48
5 changed files with 34 additions and 54 deletions

View File

@@ -10,6 +10,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
import numpy as np
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.utils import guard_import
from langchain_core.vectorstores import VectorStore
from langchain_community.docstore.base import Docstore
@@ -22,14 +23,7 @@ DEFAULT_METRIC = "angular"
def dependable_annoy_import() -> Any:
"""Import annoy if available, otherwise raise error."""
try:
import annoy
except ImportError:
raise ImportError(
"Could not import annoy python package. "
"Please install it with `pip install --user annoy` "
)
return annoy
return guard_import("annoy")
class Annoy(VectorStore):
@@ -300,7 +294,7 @@ class Annoy(VectorStore):
f"Expected one of {list(INDEX_METRICS)}"
)
)
annoy = dependable_annoy_import()
annoy = guard_import("annoy")
if not embeddings:
raise ValueError("embeddings must be provided to build AnnoyIndex")
f = len(embeddings[0])
@@ -459,7 +453,7 @@ class Annoy(VectorStore):
)
path = Path(folder_path)
# load index separately since it is not picklable
annoy = dependable_annoy_import()
annoy = guard_import("annoy")
# load docstore and index_to_docstore_id
with open(path / "index.pkl", "rb") as file:
docstore, index_to_docstore_id, config_object = pickle.load(file)