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

@@ -7,20 +7,13 @@ from typing import Any, Iterable, List, Optional
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
def import_lancedb() -> Any:
"""Import lancedb package."""
try:
import lancedb
except ImportError as e:
raise ImportError(
"Could not import pinecone lancedb package. "
"Please install it with `pip install lancedb`."
) from e
return lancedb
return guard_import("lancedb")
class LanceDB(VectorStore):
@@ -64,7 +57,7 @@ class LanceDB(VectorStore):
mode: Optional[str] = "overwrite",
):
"""Initialize with Lance DB vectorstore"""
lancedb = import_lancedb()
lancedb = guard_import("lancedb")
self._embedding = embedding
self._vector_key = vector_key
self._id_key = id_key