123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731 |
- """Wrapper around Qdrant vector database."""
- from __future__ import annotations
- import asyncio
- import functools
- import uuid
- import warnings
- from itertools import islice
- from operator import itemgetter
- from typing import (
- TYPE_CHECKING,
- Any,
- Callable,
- Dict,
- Generator,
- Iterable,
- List,
- Optional,
- Sequence,
- Tuple,
- Type,
- Union,
- )
- import numpy as np
- from langchain.docstore.document import Document
- from langchain.embeddings.base import Embeddings
- from langchain.vectorstores import VectorStore
- from langchain.vectorstores.utils import maximal_marginal_relevance
- from qdrant_client.http.models import PayloadSchemaType
- if TYPE_CHECKING:
- from qdrant_client import grpc # noqa
- from qdrant_client.conversions import common_types
- from qdrant_client.http import models as rest
- DictFilter = Dict[str, Union[str, int, bool, dict, list]]
- MetadataFilter = Union[DictFilter, common_types.Filter]
- class QdrantException(Exception):
- """Base class for all the Qdrant related exceptions"""
- def sync_call_fallback(method: Callable) -> Callable:
- """
- Decorator to call the synchronous method of the class if the async method is not
- implemented. This decorator might be only used for the methods that are defined
- as async in the class.
- """
- @functools.wraps(method)
- async def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
- try:
- return await method(self, *args, **kwargs)
- except NotImplementedError:
- # If the async method is not implemented, call the synchronous method
- # by removing the first letter from the method name. For example,
- # if the async method is called ``aaad_texts``, the synchronous method
- # will be called ``aad_texts``.
- sync_method = functools.partial(
- getattr(self, method.__name__[1:]), *args, **kwargs
- )
- return await asyncio.get_event_loop().run_in_executor(None, sync_method)
- return wrapper
- class Qdrant(VectorStore):
- """Wrapper around Qdrant vector database.
- To use you should have the ``qdrant-client`` package installed.
- Example:
- .. code-block:: python
- from qdrant_client import QdrantClient
- from langchain import Qdrant
- client = QdrantClient()
- collection_name = "MyCollection"
- qdrant = Qdrant(client, collection_name, embedding_function)
- """
- CONTENT_KEY = "page_content"
- METADATA_KEY = "metadata"
- GROUP_KEY = "group_id"
- VECTOR_NAME = None
- def __init__(
- self,
- client: Any,
- collection_name: str,
- embeddings: Optional[Embeddings] = None,
- content_payload_key: str = CONTENT_KEY,
- metadata_payload_key: str = METADATA_KEY,
- group_payload_key: str = GROUP_KEY,
- group_id: str = None,
- distance_strategy: str = "COSINE",
- vector_name: Optional[str] = VECTOR_NAME,
- embedding_function: Optional[Callable] = None, # deprecated
- is_new_collection: bool = False
- ):
- """Initialize with necessary components."""
- try:
- import qdrant_client
- except ImportError:
- raise ValueError(
- "Could not import qdrant-client python package. "
- "Please install it with `pip install qdrant-client`."
- )
- if not isinstance(client, qdrant_client.QdrantClient):
- raise ValueError(
- f"client should be an instance of qdrant_client.QdrantClient, "
- f"got {type(client)}"
- )
- if embeddings is None and embedding_function is None:
- raise ValueError(
- "`embeddings` value can't be None. Pass `Embeddings` instance."
- )
- if embeddings is not None and embedding_function is not None:
- raise ValueError(
- "Both `embeddings` and `embedding_function` are passed. "
- "Use `embeddings` only."
- )
- self._embeddings = embeddings
- self._embeddings_function = embedding_function
- self.client: qdrant_client.QdrantClient = client
- self.collection_name = collection_name
- self.content_payload_key = content_payload_key or self.CONTENT_KEY
- self.metadata_payload_key = metadata_payload_key or self.METADATA_KEY
- self.group_payload_key = group_payload_key or self.GROUP_KEY
- self.vector_name = vector_name or self.VECTOR_NAME
- self.group_id = group_id
- self.is_new_collection= is_new_collection
- if embedding_function is not None:
- warnings.warn(
- "Using `embedding_function` is deprecated. "
- "Pass `Embeddings` instance to `embeddings` instead."
- )
- if not isinstance(embeddings, Embeddings):
- warnings.warn(
- "`embeddings` should be an instance of `Embeddings`."
- "Using `embeddings` as `embedding_function` which is deprecated"
- )
- self._embeddings_function = embeddings
- self._embeddings = None
- self.distance_strategy = distance_strategy.upper()
- @property
- def embeddings(self) -> Optional[Embeddings]:
- return self._embeddings
- def add_texts(
- self,
- texts: Iterable[str],
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- batch_size: int = 64,
- **kwargs: Any,
- ) -> List[str]:
- """Run more texts through the embeddings and add to the vectorstore.
- Args:
- texts: Iterable of strings to add to the vectorstore.
- metadatas: Optional list of metadatas associated with the texts.
- ids:
- Optional list of ids to associate with the texts. Ids have to be
- uuid-like strings.
- batch_size:
- How many vectors upload per-request.
- Default: 64
- group_id:
- collection group
- Returns:
- List of ids from adding the texts into the vectorstore.
- """
- added_ids = []
- for batch_ids, points in self._generate_rest_batches(
- texts, metadatas, ids, batch_size
- ):
- self.client.upsert(
- collection_name=self.collection_name, points=points, **kwargs
- )
- added_ids.extend(batch_ids)
- # if is new collection, create payload index on group_id
- if self.is_new_collection:
- self.client.create_payload_index(self.collection_name, self.group_payload_key,
- field_schema=PayloadSchemaType.KEYWORD,
- field_type=PayloadSchemaType.KEYWORD)
- return added_ids
- @sync_call_fallback
- async def aadd_texts(
- self,
- texts: Iterable[str],
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- batch_size: int = 64,
- **kwargs: Any,
- ) -> List[str]:
- """Run more texts through the embeddings and add to the vectorstore.
- Args:
- texts: Iterable of strings to add to the vectorstore.
- metadatas: Optional list of metadatas associated with the texts.
- ids:
- Optional list of ids to associate with the texts. Ids have to be
- uuid-like strings.
- batch_size:
- How many vectors upload per-request.
- Default: 64
- Returns:
- List of ids from adding the texts into the vectorstore.
- """
- from qdrant_client import grpc # noqa
- from qdrant_client.conversions.conversion import RestToGrpc
- added_ids = []
- for batch_ids, points in self._generate_rest_batches(
- texts, metadatas, ids, batch_size
- ):
- await self.client.async_grpc_points.Upsert(
- grpc.UpsertPoints(
- collection_name=self.collection_name,
- points=[RestToGrpc.convert_point_struct(point) for point in points],
- )
- )
- added_ids.extend(batch_ids)
- return added_ids
- def similarity_search(
- self,
- query: str,
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs most similar to query.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of Documents most similar to the query.
- """
- results = self.similarity_search_with_score(
- query,
- k,
- filter=filter,
- search_params=search_params,
- offset=offset,
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- return list(map(itemgetter(0), results))
- @sync_call_fallback
- async def asimilarity_search(
- self,
- query: str,
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs most similar to query.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- Returns:
- List of Documents most similar to the query.
- """
- results = await self.asimilarity_search_with_score(query, k, filter, **kwargs)
- return list(map(itemgetter(0), results))
- def similarity_search_with_score(
- self,
- query: str,
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs most similar to query.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of documents most similar to the query text and distance for each.
- """
- return self.similarity_search_with_score_by_vector(
- self._embed_query(query),
- k,
- filter=filter,
- search_params=search_params,
- offset=offset,
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- @sync_call_fallback
- async def asimilarity_search_with_score(
- self,
- query: str,
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs most similar to query.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of documents most similar to the query text and distance for each.
- """
- return await self.asimilarity_search_with_score_by_vector(
- self._embed_query(query),
- k,
- filter=filter,
- search_params=search_params,
- offset=offset,
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- def similarity_search_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs most similar to embedding vector.
- Args:
- embedding: Embedding vector to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of Documents most similar to the query.
- """
- results = self.similarity_search_with_score_by_vector(
- embedding,
- k,
- filter=filter,
- search_params=search_params,
- offset=offset,
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- return list(map(itemgetter(0), results))
- @sync_call_fallback
- async def asimilarity_search_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs most similar to embedding vector.
- Args:
- embedding: Embedding vector to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of Documents most similar to the query.
- """
- results = await self.asimilarity_search_with_score_by_vector(
- embedding,
- k,
- filter=filter,
- search_params=search_params,
- offset=offset,
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- return list(map(itemgetter(0), results))
- def similarity_search_with_score_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs most similar to embedding vector.
- Args:
- embedding: Embedding vector to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of documents most similar to the query text and distance for each.
- """
- if filter is not None and isinstance(filter, dict):
- warnings.warn(
- "Using dict as a `filter` is deprecated. Please use qdrant-client "
- "filters directly: "
- "https://qdrant.tech/documentation/concepts/filtering/",
- DeprecationWarning,
- )
- qdrant_filter = self._qdrant_filter_from_dict(filter)
- else:
- qdrant_filter = filter
- query_vector = embedding
- if self.vector_name is not None:
- query_vector = (self.vector_name, embedding) # type: ignore[assignment]
- results = self.client.search(
- collection_name=self.collection_name,
- query_vector=query_vector,
- query_filter=qdrant_filter,
- search_params=search_params,
- limit=k,
- offset=offset,
- with_payload=True,
- with_vectors=True, # Langchain does not expect vectors to be returned
- score_threshold=score_threshold,
- consistency=consistency,
- **kwargs,
- )
- return [
- (
- self._document_from_scored_point(
- result, self.content_payload_key, self.metadata_payload_key
- ),
- result.score,
- )
- for result in results
- ]
- @sync_call_fallback
- async def asimilarity_search_with_score_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- filter: Optional[MetadataFilter] = None,
- search_params: Optional[common_types.SearchParams] = None,
- offset: int = 0,
- score_threshold: Optional[float] = None,
- consistency: Optional[common_types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs most similar to embedding vector.
- Args:
- embedding: Embedding vector to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- filter: Filter by metadata. Defaults to None.
- search_params: Additional search params
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the
- threshold depending on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- consistency:
- Read consistency of the search. Defines how many replicas should be
- queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all
- queried replicas
- - 'majority' - query all replicas, but return values present in the
- majority of replicas
- - 'quorum' - query the majority of replicas, return values present in
- all of them
- - 'all' - query all replicas, and return values present in all replicas
- Returns:
- List of documents most similar to the query text and distance for each.
- """
- from qdrant_client import grpc # noqa
- from qdrant_client.conversions.conversion import RestToGrpc
- from qdrant_client.http import models as rest
- if filter is not None and isinstance(filter, dict):
- warnings.warn(
- "Using dict as a `filter` is deprecated. Please use qdrant-client "
- "filters directly: "
- "https://qdrant.tech/documentation/concepts/filtering/",
- DeprecationWarning,
- )
- qdrant_filter = self._qdrant_filter_from_dict(filter)
- else:
- qdrant_filter = filter
- if qdrant_filter is not None and isinstance(qdrant_filter, rest.Filter):
- qdrant_filter = RestToGrpc.convert_filter(qdrant_filter)
- response = await self.client.async_grpc_points.Search(
- grpc.SearchPoints(
- collection_name=self.collection_name,
- vector_name=self.vector_name,
- vector=embedding,
- filter=qdrant_filter,
- params=search_params,
- limit=k,
- offset=offset,
- with_payload=grpc.WithPayloadSelector(enable=True),
- with_vectors=grpc.WithVectorsSelector(enable=False),
- score_threshold=score_threshold,
- read_consistency=consistency,
- **kwargs,
- )
- )
- return [
- (
- self._document_from_scored_point_grpc(
- result, self.content_payload_key, self.metadata_payload_key
- ),
- result.score,
- )
- for result in response.result
- ]
- def max_marginal_relevance_search(
- self,
- query: str,
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- Defaults to 20.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance.
- """
- query_embedding = self._embed_query(query)
- return self.max_marginal_relevance_search_by_vector(
- query_embedding, k, fetch_k, lambda_mult, **kwargs
- )
- @sync_call_fallback
- async def amax_marginal_relevance_search(
- self,
- query: str,
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- Defaults to 20.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance.
- """
- query_embedding = self._embed_query(query)
- return await self.amax_marginal_relevance_search_by_vector(
- query_embedding, k, fetch_k, lambda_mult, **kwargs
- )
- def max_marginal_relevance_search_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- embedding: Embedding to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance.
- """
- results = self.max_marginal_relevance_search_with_score_by_vector(
- embedding=embedding, k=k, fetch_k=fetch_k, lambda_mult=lambda_mult, **kwargs
- )
- return list(map(itemgetter(0), results))
- @sync_call_fallback
- async def amax_marginal_relevance_search_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Document]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- Defaults to 20.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance and distance for
- each.
- """
- results = await self.amax_marginal_relevance_search_with_score_by_vector(
- embedding, k, fetch_k, lambda_mult, **kwargs
- )
- return list(map(itemgetter(0), results))
- def max_marginal_relevance_search_with_score_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- Defaults to 20.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance and distance for
- each.
- """
- query_vector = embedding
- if self.vector_name is not None:
- query_vector = (self.vector_name, query_vector) # type: ignore[assignment]
- results = self.client.search(
- collection_name=self.collection_name,
- query_vector=query_vector,
- with_payload=True,
- with_vectors=True,
- limit=fetch_k,
- )
- embeddings = [
- result.vector.get(self.vector_name) # type: ignore[index, union-attr]
- if self.vector_name is not None
- else result.vector
- for result in results
- ]
- mmr_selected = maximal_marginal_relevance(
- np.array(embedding), embeddings, k=k, lambda_mult=lambda_mult
- )
- return [
- (
- self._document_from_scored_point(
- results[i], self.content_payload_key, self.metadata_payload_key
- ),
- results[i].score,
- )
- for i in mmr_selected
- ]
- @sync_call_fallback
- async def amax_marginal_relevance_search_with_score_by_vector(
- self,
- embedding: List[float],
- k: int = 4,
- fetch_k: int = 20,
- lambda_mult: float = 0.5,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs selected using the maximal marginal relevance.
- Maximal marginal relevance optimizes for similarity to query AND diversity
- among selected documents.
- Args:
- query: Text to look up documents similar to.
- k: Number of Documents to return. Defaults to 4.
- fetch_k: Number of Documents to fetch to pass to MMR algorithm.
- Defaults to 20.
- lambda_mult: Number between 0 and 1 that determines the degree
- of diversity among the results with 0 corresponding
- to maximum diversity and 1 to minimum diversity.
- Defaults to 0.5.
- Returns:
- List of Documents selected by maximal marginal relevance and distance for
- each.
- """
- from qdrant_client import grpc # noqa
- from qdrant_client.conversions.conversion import GrpcToRest
- response = await self.client.async_grpc_points.Search(
- grpc.SearchPoints(
- collection_name=self.collection_name,
- vector_name=self.vector_name,
- vector=embedding,
- with_payload=grpc.WithPayloadSelector(enable=True),
- with_vectors=grpc.WithVectorsSelector(enable=True),
- limit=fetch_k,
- )
- )
- results = [
- GrpcToRest.convert_vectors(result.vectors) for result in response.result
- ]
- embeddings: List[List[float]] = [
- result.get(self.vector_name) # type: ignore
- if isinstance(result, dict)
- else result
- for result in results
- ]
- mmr_selected: List[int] = maximal_marginal_relevance(
- np.array(embedding),
- embeddings,
- k=k,
- lambda_mult=lambda_mult,
- )
- return [
- (
- self._document_from_scored_point_grpc(
- response.result[i],
- self.content_payload_key,
- self.metadata_payload_key,
- ),
- response.result[i].score,
- )
- for i in mmr_selected
- ]
- def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> Optional[bool]:
- """Delete by vector ID or other criteria.
- Args:
- ids: List of ids to delete.
- **kwargs: Other keyword arguments that subclasses might use.
- Returns:
- Optional[bool]: True if deletion is successful,
- False otherwise, None if not implemented.
- """
- from qdrant_client.http import models as rest
- result = self.client.delete(
- collection_name=self.collection_name,
- points_selector=ids,
- )
- return result.status == rest.UpdateStatus.COMPLETED
- @classmethod
- def from_texts(
- cls: Type[Qdrant],
- texts: List[str],
- embedding: Embeddings,
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- location: Optional[str] = None,
- url: Optional[str] = None,
- port: Optional[int] = 6333,
- grpc_port: int = 6334,
- prefer_grpc: bool = False,
- https: Optional[bool] = None,
- api_key: Optional[str] = None,
- prefix: Optional[str] = None,
- timeout: Optional[float] = None,
- host: Optional[str] = None,
- path: Optional[str] = None,
- collection_name: Optional[str] = None,
- distance_func: str = "Cosine",
- content_payload_key: str = CONTENT_KEY,
- metadata_payload_key: str = METADATA_KEY,
- group_payload_key: str = GROUP_KEY,
- group_id: str = None,
- vector_name: Optional[str] = VECTOR_NAME,
- batch_size: int = 64,
- shard_number: Optional[int] = None,
- replication_factor: Optional[int] = None,
- write_consistency_factor: Optional[int] = None,
- on_disk_payload: Optional[bool] = None,
- hnsw_config: Optional[common_types.HnswConfigDiff] = None,
- optimizers_config: Optional[common_types.OptimizersConfigDiff] = None,
- wal_config: Optional[common_types.WalConfigDiff] = None,
- quantization_config: Optional[common_types.QuantizationConfig] = None,
- init_from: Optional[common_types.InitFrom] = None,
- force_recreate: bool = False,
- **kwargs: Any,
- ) -> Qdrant:
- """Construct Qdrant wrapper from a list of texts.
- Args:
- texts: A list of texts to be indexed in Qdrant.
- embedding: A subclass of `Embeddings`, responsible for text vectorization.
- metadatas:
- An optional list of metadata. If provided it has to be of the same
- length as a list of texts.
- ids:
- Optional list of ids to associate with the texts. Ids have to be
- uuid-like strings.
- location:
- If `:memory:` - use in-memory Qdrant instance.
- If `str` - use it as a `url` parameter.
- If `None` - fallback to relying on `host` and `port` parameters.
- url: either host or str of "Optional[scheme], host, Optional[port],
- Optional[prefix]". Default: `None`
- port: Port of the REST API interface. Default: 6333
- grpc_port: Port of the gRPC interface. Default: 6334
- prefer_grpc:
- If true - use gPRC interface whenever possible in custom methods.
- Default: False
- https: If true - use HTTPS(SSL) protocol. Default: None
- api_key: API key for authentication in Qdrant Cloud. Default: None
- prefix:
- If not None - add prefix to the REST URL path.
- Example: service/v1 will result in
- http://localhost:6333/service/v1/{qdrant-endpoint} for REST API.
- Default: None
- timeout:
- Timeout for REST and gRPC API requests.
- Default: 5.0 seconds for REST and unlimited for gRPC
- host:
- Host name of Qdrant service. If url and host are None, set to
- 'localhost'. Default: None
- path:
- Path in which the vectors will be stored while using local mode.
- Default: None
- collection_name:
- Name of the Qdrant collection to be used. If not provided,
- it will be created randomly. Default: None
- distance_func:
- Distance function. One of: "Cosine" / "Euclid" / "Dot".
- Default: "Cosine"
- content_payload_key:
- A payload key used to store the content of the document.
- Default: "page_content"
- metadata_payload_key:
- A payload key used to store the metadata of the document.
- Default: "metadata"
- group_payload_key:
- A payload key used to store the content of the document.
- Default: "group_id"
- group_id:
- collection group id
- vector_name:
- Name of the vector to be used internally in Qdrant.
- Default: None
- batch_size:
- How many vectors upload per-request.
- Default: 64
- shard_number: Number of shards in collection. Default is 1, minimum is 1.
- replication_factor:
- Replication factor for collection. Default is 1, minimum is 1.
- Defines how many copies of each shard will be created.
- Have effect only in distributed mode.
- write_consistency_factor:
- Write consistency factor for collection. Default is 1, minimum is 1.
- Defines how many replicas should apply the operation for us to consider
- it successful. Increasing this number will make the collection more
- resilient to inconsistencies, but will also make it fail if not enough
- replicas are available.
- Does not have any performance impact.
- Have effect only in distributed mode.
- on_disk_payload:
- If true - point`s payload will not be stored in memory.
- It will be read from the disk every time it is requested.
- This setting saves RAM by (slightly) increasing the response time.
- Note: those payload values that are involved in filtering and are
- indexed - remain in RAM.
- hnsw_config: Params for HNSW index
- optimizers_config: Params for optimizer
- wal_config: Params for Write-Ahead-Log
- quantization_config:
- Params for quantization, if None - quantization will be disabled
- init_from:
- Use data stored in another collection to initialize this collection
- force_recreate:
- Force recreating the collection
- **kwargs:
- Additional arguments passed directly into REST client initialization
- This is a user-friendly interface that:
- 1. Creates embeddings, one for each text
- 2. Initializes the Qdrant database as an in-memory docstore by default
- (and overridable to a remote docstore)
- 3. Adds the text embeddings to the Qdrant database
- This is intended to be a quick way to get started.
- Example:
- .. code-block:: python
- from langchain import Qdrant
- from langchain.embeddings import OpenAIEmbeddings
- embeddings = OpenAIEmbeddings()
- qdrant = Qdrant.from_texts(texts, embeddings, "localhost")
- """
- qdrant = cls._construct_instance(
- texts,
- embedding,
- metadatas,
- ids,
- location,
- url,
- port,
- grpc_port,
- prefer_grpc,
- https,
- api_key,
- prefix,
- timeout,
- host,
- path,
- collection_name,
- distance_func,
- content_payload_key,
- metadata_payload_key,
- group_payload_key,
- group_id,
- vector_name,
- shard_number,
- replication_factor,
- write_consistency_factor,
- on_disk_payload,
- hnsw_config,
- optimizers_config,
- wal_config,
- quantization_config,
- init_from,
- force_recreate,
- **kwargs,
- )
- qdrant.add_texts(texts, metadatas, ids, batch_size)
- return qdrant
- @classmethod
- @sync_call_fallback
- async def afrom_texts(
- cls: Type[Qdrant],
- texts: List[str],
- embedding: Embeddings,
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- location: Optional[str] = None,
- url: Optional[str] = None,
- port: Optional[int] = 6333,
- grpc_port: int = 6334,
- prefer_grpc: bool = False,
- https: Optional[bool] = None,
- api_key: Optional[str] = None,
- prefix: Optional[str] = None,
- timeout: Optional[float] = None,
- host: Optional[str] = None,
- path: Optional[str] = None,
- collection_name: Optional[str] = None,
- distance_func: str = "Cosine",
- content_payload_key: str = CONTENT_KEY,
- metadata_payload_key: str = METADATA_KEY,
- vector_name: Optional[str] = VECTOR_NAME,
- batch_size: int = 64,
- shard_number: Optional[int] = None,
- replication_factor: Optional[int] = None,
- write_consistency_factor: Optional[int] = None,
- on_disk_payload: Optional[bool] = None,
- hnsw_config: Optional[common_types.HnswConfigDiff] = None,
- optimizers_config: Optional[common_types.OptimizersConfigDiff] = None,
- wal_config: Optional[common_types.WalConfigDiff] = None,
- quantization_config: Optional[common_types.QuantizationConfig] = None,
- init_from: Optional[common_types.InitFrom] = None,
- force_recreate: bool = False,
- **kwargs: Any,
- ) -> Qdrant:
- """Construct Qdrant wrapper from a list of texts.
- Args:
- texts: A list of texts to be indexed in Qdrant.
- embedding: A subclass of `Embeddings`, responsible for text vectorization.
- metadatas:
- An optional list of metadata. If provided it has to be of the same
- length as a list of texts.
- ids:
- Optional list of ids to associate with the texts. Ids have to be
- uuid-like strings.
- location:
- If `:memory:` - use in-memory Qdrant instance.
- If `str` - use it as a `url` parameter.
- If `None` - fallback to relying on `host` and `port` parameters.
- url: either host or str of "Optional[scheme], host, Optional[port],
- Optional[prefix]". Default: `None`
- port: Port of the REST API interface. Default: 6333
- grpc_port: Port of the gRPC interface. Default: 6334
- prefer_grpc:
- If true - use gPRC interface whenever possible in custom methods.
- Default: False
- https: If true - use HTTPS(SSL) protocol. Default: None
- api_key: API key for authentication in Qdrant Cloud. Default: None
- prefix:
- If not None - add prefix to the REST URL path.
- Example: service/v1 will result in
- http://localhost:6333/service/v1/{qdrant-endpoint} for REST API.
- Default: None
- timeout:
- Timeout for REST and gRPC API requests.
- Default: 5.0 seconds for REST and unlimited for gRPC
- host:
- Host name of Qdrant service. If url and host are None, set to
- 'localhost'. Default: None
- path:
- Path in which the vectors will be stored while using local mode.
- Default: None
- collection_name:
- Name of the Qdrant collection to be used. If not provided,
- it will be created randomly. Default: None
- distance_func:
- Distance function. One of: "Cosine" / "Euclid" / "Dot".
- Default: "Cosine"
- content_payload_key:
- A payload key used to store the content of the document.
- Default: "page_content"
- metadata_payload_key:
- A payload key used to store the metadata of the document.
- Default: "metadata"
- vector_name:
- Name of the vector to be used internally in Qdrant.
- Default: None
- batch_size:
- How many vectors upload per-request.
- Default: 64
- shard_number: Number of shards in collection. Default is 1, minimum is 1.
- replication_factor:
- Replication factor for collection. Default is 1, minimum is 1.
- Defines how many copies of each shard will be created.
- Have effect only in distributed mode.
- write_consistency_factor:
- Write consistency factor for collection. Default is 1, minimum is 1.
- Defines how many replicas should apply the operation for us to consider
- it successful. Increasing this number will make the collection more
- resilient to inconsistencies, but will also make it fail if not enough
- replicas are available.
- Does not have any performance impact.
- Have effect only in distributed mode.
- on_disk_payload:
- If true - point`s payload will not be stored in memory.
- It will be read from the disk every time it is requested.
- This setting saves RAM by (slightly) increasing the response time.
- Note: those payload values that are involved in filtering and are
- indexed - remain in RAM.
- hnsw_config: Params for HNSW index
- optimizers_config: Params for optimizer
- wal_config: Params for Write-Ahead-Log
- quantization_config:
- Params for quantization, if None - quantization will be disabled
- init_from:
- Use data stored in another collection to initialize this collection
- force_recreate:
- Force recreating the collection
- **kwargs:
- Additional arguments passed directly into REST client initialization
- This is a user-friendly interface that:
- 1. Creates embeddings, one for each text
- 2. Initializes the Qdrant database as an in-memory docstore by default
- (and overridable to a remote docstore)
- 3. Adds the text embeddings to the Qdrant database
- This is intended to be a quick way to get started.
- Example:
- .. code-block:: python
- from langchain import Qdrant
- from langchain.embeddings import OpenAIEmbeddings
- embeddings = OpenAIEmbeddings()
- qdrant = await Qdrant.afrom_texts(texts, embeddings, "localhost")
- """
- qdrant = cls._construct_instance(
- texts,
- embedding,
- metadatas,
- ids,
- location,
- url,
- port,
- grpc_port,
- prefer_grpc,
- https,
- api_key,
- prefix,
- timeout,
- host,
- path,
- collection_name,
- distance_func,
- content_payload_key,
- metadata_payload_key,
- vector_name,
- shard_number,
- replication_factor,
- write_consistency_factor,
- on_disk_payload,
- hnsw_config,
- optimizers_config,
- wal_config,
- quantization_config,
- init_from,
- force_recreate,
- **kwargs,
- )
- await qdrant.aadd_texts(texts, metadatas, ids, batch_size)
- return qdrant
- @classmethod
- def _construct_instance(
- cls: Type[Qdrant],
- texts: List[str],
- embedding: Embeddings,
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- location: Optional[str] = None,
- url: Optional[str] = None,
- port: Optional[int] = 6333,
- grpc_port: int = 6334,
- prefer_grpc: bool = False,
- https: Optional[bool] = None,
- api_key: Optional[str] = None,
- prefix: Optional[str] = None,
- timeout: Optional[float] = None,
- host: Optional[str] = None,
- path: Optional[str] = None,
- collection_name: Optional[str] = None,
- distance_func: str = "Cosine",
- content_payload_key: str = CONTENT_KEY,
- metadata_payload_key: str = METADATA_KEY,
- group_payload_key: str = GROUP_KEY,
- group_id: str = None,
- vector_name: Optional[str] = VECTOR_NAME,
- shard_number: Optional[int] = None,
- replication_factor: Optional[int] = None,
- write_consistency_factor: Optional[int] = None,
- on_disk_payload: Optional[bool] = None,
- hnsw_config: Optional[common_types.HnswConfigDiff] = None,
- optimizers_config: Optional[common_types.OptimizersConfigDiff] = None,
- wal_config: Optional[common_types.WalConfigDiff] = None,
- quantization_config: Optional[common_types.QuantizationConfig] = None,
- init_from: Optional[common_types.InitFrom] = None,
- force_recreate: bool = False,
- **kwargs: Any,
- ) -> Qdrant:
- try:
- import qdrant_client
- except ImportError:
- raise ValueError(
- "Could not import qdrant-client python package. "
- "Please install it with `pip install qdrant-client`."
- )
- from grpc import RpcError
- from qdrant_client.http import models as rest
- from qdrant_client.http.exceptions import UnexpectedResponse
- # Just do a single quick embedding to get vector size
- partial_embeddings = embedding.embed_documents(texts[:1])
- vector_size = len(partial_embeddings[0])
- collection_name = collection_name or uuid.uuid4().hex
- distance_func = distance_func.upper()
- is_new_collection = False
- client = qdrant_client.QdrantClient(
- location=location,
- url=url,
- port=port,
- grpc_port=grpc_port,
- prefer_grpc=prefer_grpc,
- https=https,
- api_key=api_key,
- prefix=prefix,
- timeout=timeout,
- host=host,
- path=path,
- **kwargs,
- )
- all_collection_name = []
- collections_response = client.get_collections()
- collection_list = collections_response.collections
- for collection in collection_list:
- all_collection_name.append(collection.name)
- if collection_name not in all_collection_name:
- vectors_config = rest.VectorParams(
- size=vector_size,
- distance=rest.Distance[distance_func],
- )
- # If vector name was provided, we're going to use the named vectors feature
- # with just a single vector.
- if vector_name is not None:
- vectors_config = { # type: ignore[assignment]
- vector_name: vectors_config,
- }
- client.recreate_collection(
- collection_name=collection_name,
- vectors_config=vectors_config,
- shard_number=shard_number,
- replication_factor=replication_factor,
- write_consistency_factor=write_consistency_factor,
- on_disk_payload=on_disk_payload,
- hnsw_config=hnsw_config,
- optimizers_config=optimizers_config,
- wal_config=wal_config,
- quantization_config=quantization_config,
- init_from=init_from,
- timeout=timeout, # type: ignore[arg-type]
- )
- is_new_collection = True
- if force_recreate:
- raise ValueError
- # Get the vector configuration of the existing collection and vector, if it
- # was specified. If the old configuration does not match the current one,
- # an exception is being thrown.
- collection_info = client.get_collection(collection_name=collection_name)
- current_vector_config = collection_info.config.params.vectors
- if isinstance(current_vector_config, dict) and vector_name is not None:
- if vector_name not in current_vector_config:
- raise QdrantException(
- f"Existing Qdrant collection {collection_name} does not "
- f"contain vector named {vector_name}. Did you mean one of the "
- f"existing vectors: {', '.join(current_vector_config.keys())}? "
- f"If you want to recreate the collection, set `force_recreate` "
- f"parameter to `True`."
- )
- current_vector_config = current_vector_config.get(
- vector_name
- ) # type: ignore[assignment]
- elif isinstance(current_vector_config, dict) and vector_name is None:
- raise QdrantException(
- f"Existing Qdrant collection {collection_name} uses named vectors. "
- f"If you want to reuse it, please set `vector_name` to any of the "
- f"existing named vectors: "
- f"{', '.join(current_vector_config.keys())}." # noqa
- f"If you want to recreate the collection, set `force_recreate` "
- f"parameter to `True`."
- )
- elif (
- not isinstance(current_vector_config, dict) and vector_name is not None
- ):
- raise QdrantException(
- f"Existing Qdrant collection {collection_name} doesn't use named "
- f"vectors. If you want to reuse it, please set `vector_name` to "
- f"`None`. If you want to recreate the collection, set "
- f"`force_recreate` parameter to `True`."
- )
- # Check if the vector configuration has the same dimensionality.
- if current_vector_config.size != vector_size: # type: ignore[union-attr]
- raise QdrantException(
- f"Existing Qdrant collection is configured for vectors with "
- f"{current_vector_config.size} " # type: ignore[union-attr]
- f"dimensions. Selected embeddings are {vector_size}-dimensional. "
- f"If you want to recreate the collection, set `force_recreate` "
- f"parameter to `True`."
- )
- current_distance_func = (
- current_vector_config.distance.name.upper() # type: ignore[union-attr]
- )
- if current_distance_func != distance_func:
- raise QdrantException(
- f"Existing Qdrant collection is configured for "
- f"{current_vector_config.distance} " # type: ignore[union-attr]
- f"similarity. Please set `distance_func` parameter to "
- f"`{distance_func}` if you want to reuse it. If you want to "
- f"recreate the collection, set `force_recreate` parameter to "
- f"`True`."
- )
- qdrant = cls(
- client=client,
- collection_name=collection_name,
- embeddings=embedding,
- content_payload_key=content_payload_key,
- metadata_payload_key=metadata_payload_key,
- distance_strategy=distance_func,
- vector_name=vector_name,
- group_id=group_id,
- group_payload_key=group_payload_key,
- is_new_collection=is_new_collection
- )
- return qdrant
- def _select_relevance_score_fn(self) -> Callable[[float], float]:
- """
- The 'correct' relevance function
- may differ depending on a few things, including:
- - the distance / similarity metric used by the VectorStore
- - the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
- - embedding dimensionality
- - etc.
- """
- if self.distance_strategy == "COSINE":
- return self._cosine_relevance_score_fn
- elif self.distance_strategy == "DOT":
- return self._max_inner_product_relevance_score_fn
- elif self.distance_strategy == "EUCLID":
- return self._euclidean_relevance_score_fn
- else:
- raise ValueError(
- "Unknown distance strategy, must be cosine, "
- "max_inner_product, or euclidean"
- )
- def _similarity_search_with_relevance_scores(
- self,
- query: str,
- k: int = 4,
- **kwargs: Any,
- ) -> List[Tuple[Document, float]]:
- """Return docs and relevance scores in the range [0, 1].
- 0 is dissimilar, 1 is most similar.
- Args:
- query: input text
- k: Number of Documents to return. Defaults to 4.
- **kwargs: kwargs to be passed to similarity search. Should include:
- score_threshold: Optional, a floating point value between 0 to 1 to
- filter the resulting set of retrieved docs
- Returns:
- List of Tuples of (doc, similarity_score)
- """
- return self.similarity_search_with_score(query, k, **kwargs)
- @classmethod
- def _build_payloads(
- cls,
- texts: Iterable[str],
- metadatas: Optional[List[dict]],
- content_payload_key: str,
- metadata_payload_key: str,
- group_id: str,
- group_payload_key: str
- ) -> List[dict]:
- payloads = []
- for i, text in enumerate(texts):
- if text is None:
- raise ValueError(
- "At least one of the texts is None. Please remove it before "
- "calling .from_texts or .add_texts on Qdrant instance."
- )
- metadata = metadatas[i] if metadatas is not None else None
- payloads.append(
- {
- content_payload_key: text,
- metadata_payload_key: metadata,
- group_payload_key: group_id
- }
- )
- return payloads
- @classmethod
- def _document_from_scored_point(
- cls,
- scored_point: Any,
- content_payload_key: str,
- metadata_payload_key: str,
- ) -> Document:
- return Document(
- page_content=scored_point.payload.get(content_payload_key),
- metadata=scored_point.payload.get(metadata_payload_key) or {},
- )
- @classmethod
- def _document_from_scored_point_grpc(
- cls,
- scored_point: Any,
- content_payload_key: str,
- metadata_payload_key: str,
- ) -> Document:
- from qdrant_client.conversions.conversion import grpc_to_payload
- payload = grpc_to_payload(scored_point.payload)
- return Document(
- page_content=payload[content_payload_key],
- metadata=payload.get(metadata_payload_key) or {},
- )
- def _build_condition(self, key: str, value: Any) -> List[rest.FieldCondition]:
- from qdrant_client.http import models as rest
- out = []
- if isinstance(value, dict):
- for _key, value in value.items():
- out.extend(self._build_condition(f"{key}.{_key}", value))
- elif isinstance(value, list):
- for _value in value:
- if isinstance(_value, dict):
- out.extend(self._build_condition(f"{key}[]", _value))
- else:
- out.extend(self._build_condition(f"{key}", _value))
- else:
- out.append(
- rest.FieldCondition(
- key=key,
- match=rest.MatchValue(value=value),
- )
- )
- return out
- def _qdrant_filter_from_dict(
- self, filter: Optional[DictFilter]
- ) -> Optional[rest.Filter]:
- from qdrant_client.http import models as rest
- if not filter:
- return None
- return rest.Filter(
- must=[
- condition
- for key, value in filter.items()
- for condition in self._build_condition(key, value)
- ]
- )
- def _embed_query(self, query: str) -> List[float]:
- """Embed query text.
- Used to provide backward compatibility with `embedding_function` argument.
- Args:
- query: Query text.
- Returns:
- List of floats representing the query embedding.
- """
- if self.embeddings is not None:
- embedding = self.embeddings.embed_query(query)
- else:
- if self._embeddings_function is not None:
- embedding = self._embeddings_function(query)
- else:
- raise ValueError("Neither of embeddings or embedding_function is set")
- return embedding.tolist() if hasattr(embedding, "tolist") else embedding
- def _embed_texts(self, texts: Iterable[str]) -> List[List[float]]:
- """Embed search texts.
- Used to provide backward compatibility with `embedding_function` argument.
- Args:
- texts: Iterable of texts to embed.
- Returns:
- List of floats representing the texts embedding.
- """
- if self.embeddings is not None:
- embeddings = self.embeddings.embed_documents(list(texts))
- if hasattr(embeddings, "tolist"):
- embeddings = embeddings.tolist()
- elif self._embeddings_function is not None:
- embeddings = []
- for text in texts:
- embedding = self._embeddings_function(text)
- if hasattr(embeddings, "tolist"):
- embedding = embedding.tolist()
- embeddings.append(embedding)
- else:
- raise ValueError("Neither of embeddings or embedding_function is set")
- return embeddings
- def _generate_rest_batches(
- self,
- texts: Iterable[str],
- metadatas: Optional[List[dict]] = None,
- ids: Optional[Sequence[str]] = None,
- batch_size: int = 64,
- group_id: Optional[str] = None,
- ) -> Generator[Tuple[List[str], List[rest.PointStruct]], None, None]:
- from qdrant_client.http import models as rest
- texts_iterator = iter(texts)
- metadatas_iterator = iter(metadatas or [])
- ids_iterator = iter(ids or [uuid.uuid4().hex for _ in iter(texts)])
- while batch_texts := list(islice(texts_iterator, batch_size)):
- # Take the corresponding metadata and id for each text in a batch
- batch_metadatas = list(islice(metadatas_iterator, batch_size)) or None
- batch_ids = list(islice(ids_iterator, batch_size))
- # Generate the embeddings for all the texts in a batch
- batch_embeddings = self._embed_texts(batch_texts)
- points = [
- rest.PointStruct(
- id=point_id,
- vector=vector
- if self.vector_name is None
- else {self.vector_name: vector},
- payload=payload,
- )
- for point_id, vector, payload in zip(
- batch_ids,
- batch_embeddings,
- self._build_payloads(
- batch_texts,
- batch_metadatas,
- self.content_payload_key,
- self.metadata_payload_key,
- self.group_id,
- self.group_payload_key
- ),
- )
- ]
- yield batch_ids, points
|