mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, List, Literal, Optional
|
|
|
|
from langchain_core.embeddings import Embeddings
|
|
|
|
from langchain_community.vectorstores.docarray.base import (
|
|
DocArrayIndex,
|
|
_check_docarray_import,
|
|
)
|
|
|
|
|
|
class DocArrayHnswSearch(DocArrayIndex):
|
|
"""`HnswLib` storage using `DocArray` package.
|
|
|
|
To use it, you should have the ``docarray`` package with version >=0.32.0 installed.
|
|
You can install it with `pip install "langchain[docarray]"`.
|
|
"""
|
|
|
|
@classmethod
|
|
def from_params(
|
|
cls,
|
|
embedding: Embeddings,
|
|
work_dir: str,
|
|
n_dim: int,
|
|
dist_metric: Literal["cosine", "ip", "l2"] = "cosine",
|
|
max_elements: int = 1024,
|
|
index: bool = True,
|
|
ef_construction: int = 200,
|
|
ef: int = 10,
|
|
M: int = 16,
|
|
allow_replace_deleted: bool = True,
|
|
num_threads: int = 1,
|
|
**kwargs: Any,
|
|
) -> DocArrayHnswSearch:
|
|
"""Initialize DocArrayHnswSearch store.
|
|
|
|
Args:
|
|
embedding (Embeddings): Embedding function.
|
|
work_dir (str): path to the location where all the data will be stored.
|
|
n_dim (int): dimension of an embedding.
|
|
dist_metric (str): Distance metric for DocArrayHnswSearch can be one of:
|
|
"cosine", "ip", and "l2". Defaults to "cosine".
|
|
max_elements (int): Maximum number of vectors that can be stored.
|
|
Defaults to 1024.
|
|
index (bool): Whether an index should be built for this field.
|
|
Defaults to True.
|
|
ef_construction (int): defines a construction time/accuracy trade-off.
|
|
Defaults to 200.
|
|
ef (int): parameter controlling query time/accuracy trade-off.
|
|
Defaults to 10.
|
|
M (int): parameter that defines the maximum number of outgoing
|
|
connections in the graph. Defaults to 16.
|
|
allow_replace_deleted (bool): Enables replacing of deleted elements
|
|
with new added ones. Defaults to True.
|
|
num_threads (int): Sets the number of cpu threads to use. Defaults to 1.
|
|
**kwargs: Other keyword arguments to be passed to the get_doc_cls method.
|
|
"""
|
|
_check_docarray_import()
|
|
from docarray.index import HnswDocumentIndex
|
|
|
|
doc_cls = cls._get_doc_cls(
|
|
dim=n_dim,
|
|
space=dist_metric,
|
|
max_elements=max_elements,
|
|
index=index,
|
|
ef_construction=ef_construction,
|
|
ef=ef,
|
|
M=M,
|
|
allow_replace_deleted=allow_replace_deleted,
|
|
num_threads=num_threads,
|
|
**kwargs,
|
|
)
|
|
doc_index = HnswDocumentIndex[doc_cls](work_dir=work_dir) # type: ignore
|
|
return cls(doc_index, embedding)
|
|
|
|
@classmethod
|
|
def from_texts(
|
|
cls,
|
|
texts: List[str],
|
|
embedding: Embeddings,
|
|
metadatas: Optional[List[dict]] = None,
|
|
work_dir: Optional[str] = None,
|
|
n_dim: Optional[int] = None,
|
|
**kwargs: Any,
|
|
) -> DocArrayHnswSearch:
|
|
"""Create an DocArrayHnswSearch store and insert data.
|
|
|
|
|
|
Args:
|
|
texts (List[str]): Text data.
|
|
embedding (Embeddings): Embedding function.
|
|
metadatas (Optional[List[dict]]): Metadata for each text if it exists.
|
|
Defaults to None.
|
|
work_dir (str): path to the location where all the data will be stored.
|
|
n_dim (int): dimension of an embedding.
|
|
**kwargs: Other keyword arguments to be passed to the __init__ method.
|
|
|
|
Returns:
|
|
DocArrayHnswSearch Vector Store
|
|
"""
|
|
if work_dir is None:
|
|
raise ValueError("`work_dir` parameter has not been set.")
|
|
if n_dim is None:
|
|
raise ValueError("`n_dim` parameter has not been set.")
|
|
|
|
store = cls.from_params(embedding, work_dir, n_dim, **kwargs)
|
|
store.add_texts(texts=texts, metadatas=metadatas)
|
|
return store
|