mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-08 06:23:20 +00:00
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:
@@ -5,6 +5,7 @@ from typing import Any, 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 AddableMixin, Docstore
|
||||
@@ -15,14 +16,7 @@ def dependable_usearch_import() -> Any:
|
||||
"""
|
||||
Import usearch if available, otherwise raise error.
|
||||
"""
|
||||
try:
|
||||
import usearch.index
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Could not import usearch python package. "
|
||||
"Please install it with `pip install usearch` "
|
||||
)
|
||||
return usearch.index
|
||||
return guard_import("usearch.index")
|
||||
|
||||
|
||||
class USearch(VectorStore):
|
||||
@@ -170,7 +164,7 @@ class USearch(VectorStore):
|
||||
documents.append(Document(page_content=text, metadata=metadata))
|
||||
|
||||
docstore = InMemoryDocstore(dict(zip(ids, documents)))
|
||||
usearch = dependable_usearch_import()
|
||||
usearch = guard_import("usearch.index")
|
||||
index = usearch.Index(ndim=len(embeddings[0]), metric=metric)
|
||||
index.add(np.array(ids), np.array(embeddings))
|
||||
return cls(embedding, index, docstore, ids.tolist())
|
||||
|
Reference in New Issue
Block a user