mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-25 04:49:17 +00:00
qdrant[patch]: Use collection_exists API instead of exceptions (#22764)
## Description Currently, the Qdrant integration relies on exceptions raised by [`get_collection` ](https://qdrant.tech/documentation/concepts/collections/#collection-info) to check if a collection exists. Using [`collection_exists`](https://qdrant.tech/documentation/concepts/collections/#check-collection-existence) is recommended to avoid missing any unhandled exceptions. This PR addresses this. ## Testing All integration and unit tests pass. No user-facing changes.
This commit is contained in:
@@ -23,14 +23,12 @@ from typing import (
|
||||
)
|
||||
|
||||
import numpy as np
|
||||
from grpc import RpcError # type: ignore
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.embeddings import Embeddings
|
||||
from langchain_core.runnables.config import run_in_executor
|
||||
from langchain_core.vectorstores import VectorStore
|
||||
from qdrant_client import AsyncQdrantClient, QdrantClient
|
||||
from qdrant_client.http import models
|
||||
from qdrant_client.http.exceptions import UnexpectedResponse
|
||||
from qdrant_client.local.async_qdrant_local import AsyncQdrantLocal
|
||||
|
||||
from langchain_qdrant._utils import maximal_marginal_relevance
|
||||
@@ -1636,14 +1634,16 @@ class Qdrant(VectorStore):
|
||||
path=path,
|
||||
**kwargs,
|
||||
)
|
||||
try:
|
||||
# Skip any validation in case of forced collection recreate.
|
||||
if force_recreate:
|
||||
raise ValueError
|
||||
collection_exists = client.collection_exists(collection_name)
|
||||
|
||||
if collection_exists and force_recreate:
|
||||
client.delete_collection(collection_name)
|
||||
collection_exists = False
|
||||
|
||||
if collection_exists:
|
||||
# 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.
|
||||
# an exception is raised.
|
||||
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:
|
||||
@@ -1700,7 +1700,7 @@ class Qdrant(VectorStore):
|
||||
f"If you want to recreate the collection, set `force_recreate` "
|
||||
f"parameter to `True`."
|
||||
)
|
||||
except (UnexpectedResponse, RpcError, ValueError):
|
||||
else:
|
||||
vectors_config = models.VectorParams(
|
||||
size=vector_size,
|
||||
distance=models.Distance[distance_func],
|
||||
@@ -1714,8 +1714,6 @@ class Qdrant(VectorStore):
|
||||
vector_name: vectors_config,
|
||||
}
|
||||
|
||||
if client.collection_exists(collection_name):
|
||||
client.delete_collection(collection_name)
|
||||
client.create_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=vectors_config,
|
||||
@@ -1795,14 +1793,17 @@ class Qdrant(VectorStore):
|
||||
path=path,
|
||||
**kwargs,
|
||||
)
|
||||
try:
|
||||
# Skip any validation in case of forced collection recreate.
|
||||
if force_recreate:
|
||||
raise ValueError
|
||||
|
||||
collection_exists = client.collection_exists(collection_name)
|
||||
|
||||
if collection_exists and force_recreate:
|
||||
client.delete_collection(collection_name)
|
||||
collection_exists = False
|
||||
|
||||
if collection_exists:
|
||||
# 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.
|
||||
# an exception is raised.
|
||||
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:
|
||||
@@ -1861,7 +1862,7 @@ class Qdrant(VectorStore):
|
||||
f"recreate the collection, set `force_recreate` parameter to "
|
||||
f"`True`."
|
||||
)
|
||||
except (UnexpectedResponse, RpcError, ValueError):
|
||||
else:
|
||||
vectors_config = models.VectorParams(
|
||||
size=vector_size,
|
||||
distance=models.Distance[distance_func],
|
||||
@@ -1875,7 +1876,7 @@ class Qdrant(VectorStore):
|
||||
vector_name: vectors_config,
|
||||
}
|
||||
|
||||
client.recreate_collection(
|
||||
client.create_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=vectors_config,
|
||||
shard_number=shard_number,
|
||||
|
Reference in New Issue
Block a user