mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-09 06:53:59 +00:00
qdrant: test new QdrantVectorStore (#24165)
## Description This PR adds integration tests to follow up on #24164. By default, the tests use an in-memory instance. To run the full suite of tests, with both in-memory and Qdrant server: ``` $ docker run -p 6333:6333 qdrant/qdrant $ make test $ make integration_test ``` --------- Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -4,6 +4,8 @@ import requests # type: ignore
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.embeddings import Embeddings
|
||||
|
||||
from langchain_qdrant import SparseEmbeddings, SparseVector
|
||||
|
||||
|
||||
def qdrant_running_locally() -> bool:
|
||||
"""Check if Qdrant is running at http://localhost:6333."""
|
||||
@@ -55,3 +57,29 @@ class ConsistentFakeEmbeddings(Embeddings):
|
||||
"""Return consistent embeddings for the text, if seen before, or a constant
|
||||
one if the text is unknown."""
|
||||
return self.embed_documents([text])[0]
|
||||
|
||||
|
||||
class ConsistentFakeSparseEmbeddings(SparseEmbeddings):
|
||||
"""Fake sparse embeddings which remembers all the texts seen so far "
|
||||
"to return consistent vectors for the same texts."""
|
||||
|
||||
def __init__(self, dimensionality: int = 25) -> None:
|
||||
self.known_texts: List[str] = []
|
||||
self.dimensionality = 25
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[SparseVector]:
|
||||
"""Return consistent embeddings for each text seen so far."""
|
||||
out_vectors = []
|
||||
for text in texts:
|
||||
if text not in self.known_texts:
|
||||
self.known_texts.append(text)
|
||||
index = self.known_texts.index(text)
|
||||
indices = [i + index for i in range(self.dimensionality)]
|
||||
values = [1.0] * (self.dimensionality - 1) + [float(index)]
|
||||
out_vectors.append(SparseVector(indices=indices, values=values))
|
||||
return out_vectors
|
||||
|
||||
def embed_query(self, text: str) -> SparseVector:
|
||||
"""Return consistent embeddings for the text, "
|
||||
"if seen before, or a constant one if the text is unknown."""
|
||||
return self.embed_documents([text])[0]
|
||||
|
Reference in New Issue
Block a user