mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-04 06:37:58 +00:00
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
import warnings
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from langchain_core.callbacks import CallbackManagerForRetrieverRun
|
|
from langchain_core.documents import Document
|
|
from langchain_core.embeddings import Embeddings
|
|
from langchain_core.retrievers import BaseRetriever
|
|
from pydantic import model_validator
|
|
|
|
from langchain_community.vectorstores.zilliz import Zilliz
|
|
|
|
# TODO: Update to ZillizClient + Hybrid Search when available
|
|
|
|
|
|
class ZillizRetriever(BaseRetriever):
|
|
"""`Zilliz API` retriever."""
|
|
|
|
embedding_function: Embeddings
|
|
"""The underlying embedding function from which documents will be retrieved."""
|
|
collection_name: str = "LangChainCollection"
|
|
"""The name of the collection in Zilliz."""
|
|
connection_args: Optional[Dict[str, Any]] = None
|
|
"""The connection arguments for the Zilliz client."""
|
|
consistency_level: str = "Session"
|
|
"""The consistency level for the Zilliz client."""
|
|
search_params: Optional[dict] = None
|
|
"""The search parameters for the Zilliz client."""
|
|
store: Zilliz
|
|
"""The underlying Zilliz store."""
|
|
retriever: BaseRetriever
|
|
"""The underlying retriever."""
|
|
|
|
@model_validator(mode="before")
|
|
@classmethod
|
|
def create_client(cls, values: dict) -> Any:
|
|
values["store"] = Zilliz(
|
|
values["embedding_function"],
|
|
values["collection_name"],
|
|
values["connection_args"],
|
|
values["consistency_level"],
|
|
)
|
|
values["retriever"] = values["store"].as_retriever(
|
|
search_kwargs={"param": values["search_params"]}
|
|
)
|
|
return values
|
|
|
|
def add_texts(
|
|
self, texts: List[str], metadatas: Optional[List[dict]] = None
|
|
) -> None:
|
|
"""Add text to the Zilliz store
|
|
|
|
Args:
|
|
texts (List[str]): The text
|
|
metadatas (List[dict]): Metadata dicts, must line up with existing store
|
|
"""
|
|
self.store.add_texts(texts, metadatas)
|
|
|
|
def _get_relevant_documents(
|
|
self,
|
|
query: str,
|
|
*,
|
|
run_manager: CallbackManagerForRetrieverRun,
|
|
**kwargs: Any,
|
|
) -> List[Document]:
|
|
return self.retriever.invoke(
|
|
query, run_manager=run_manager.get_child(), **kwargs
|
|
)
|
|
|
|
|
|
def ZillizRetreiver(*args: Any, **kwargs: Any) -> ZillizRetriever:
|
|
"""Deprecated ZillizRetreiver.
|
|
|
|
Please use ZillizRetriever ('i' before 'e') instead.
|
|
|
|
Args:
|
|
*args:
|
|
**kwargs:
|
|
|
|
Returns:
|
|
ZillizRetriever
|
|
"""
|
|
warnings.warn(
|
|
"ZillizRetreiver will be deprecated in the future. "
|
|
"Please use ZillizRetriever ('i' before 'e') instead.",
|
|
DeprecationWarning,
|
|
)
|
|
return ZillizRetriever(*args, **kwargs)
|