You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/tests/integration_tests/vectorstores/test_clickhouse.py

109 lines
4.0 KiB
Python

Integrate Clickhouse as Vector Store (#5650) <!-- Thank you for contributing to LangChain! Your PR will appear in our release under the title you set. Please make sure it highlights your valuable contribution. Replace this with a description of the change, the issue it fixes (if applicable), and relevant context. List any dependencies required for this change. After you're done, someone will review your PR. They may suggest improvements. If no one reviews your PR within a few days, feel free to @-mention the same people again, as notifications can get lost. Finally, we'd love to show appreciation for your contribution - if you'd like us to shout you out on Twitter, please also include your handle! --> #### Description This PR is mainly to integrate open source version of ClickHouse as Vector Store as it is easy for both local development and adoption of LangChain for enterprises who already have large scale clickhouse deployment. ClickHouse is a open source real-time OLAP database with full SQL support and a wide range of functions to assist users in writing analytical queries. Some of these functions and data structures perform distance operations between vectors, [enabling ClickHouse to be used as a vector database](https://clickhouse.com/blog/vector-search-clickhouse-p1). Recently added ClickHouse capabilities like [Approximate Nearest Neighbour (ANN) indices](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/annindexes) support faster approximate matching of vectors and provide a promising development aimed to further enhance the vector matching capabilities of ClickHouse. In LangChain, some ClickHouse based commercial variant vector stores like [Chroma](https://github.com/hwchase17/langchain/blob/master/langchain/vectorstores/chroma.py) and [MyScale](https://github.com/hwchase17/langchain/blob/master/langchain/vectorstores/myscale.py), etc are already integrated, but for some enterprises with large scale Clickhouse clusters deployment, it will be more straightforward to upgrade existing clickhouse infra instead of moving to another similar vector store solution, so we believe it's a valid requirement to integrate open source version of ClickHouse as vector store. As `clickhouse-connect` is already included by other integrations, this PR won't include any new dependencies. #### Before submitting <!-- If you're adding a new integration, please include: 1. Added a test for the integration: https://github.com/haoch/langchain/blob/clickhouse/tests/integration_tests/vectorstores/test_clickhouse.py 2. Added an example notebook and document showing its use: * Notebook: https://github.com/haoch/langchain/blob/clickhouse/docs/modules/indexes/vectorstores/examples/clickhouse.ipynb * Doc: https://github.com/haoch/langchain/blob/clickhouse/docs/integrations/clickhouse.md See contribution guidelines for more information on how to write tests, lint etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md --> 1. Added a test for the integration: https://github.com/haoch/langchain/blob/clickhouse/tests/integration_tests/vectorstores/test_clickhouse.py 2. Added an example notebook and document showing its use: * Notebook: https://github.com/haoch/langchain/blob/clickhouse/docs/modules/indexes/vectorstores/examples/clickhouse.ipynb * Doc: https://github.com/haoch/langchain/blob/clickhouse/docs/integrations/clickhouse.md #### Who can review? Tag maintainers/contributors who might be interested: <!-- For a quicker response, figure out the right person to tag with @ @hwchase17 - project lead Tracing / Callbacks - @agola11 Async - @agola11 DataLoaders - @eyurtsev Models - @hwchase17 - @agola11 Agents / Tools / Toolkits - @vowelparrot VectorStores / Retrievers / Memory - @dev2049 --> @hwchase17 @dev2049 Could you please help review? --------- Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
1 year ago
"""Test ClickHouse functionality."""
import pytest
from langchain.docstore.document import Document
from langchain.vectorstores import Clickhouse, ClickhouseSettings
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings
def test_clickhouse() -> None:
"""Test end to end construction and search."""
texts = ["foo", "bar", "baz"]
config = ClickhouseSettings()
config.table = "test_clickhouse"
docsearch = Clickhouse.from_texts(texts, FakeEmbeddings(), config=config)
output = docsearch.similarity_search("foo", k=1)
assert output == [Document(page_content="foo", metadata={"_dummy": 0})]
docsearch.drop()
@pytest.mark.asyncio
async def test_clickhouse_async() -> None:
"""Test end to end construction and search."""
texts = ["foo", "bar", "baz"]
config = ClickhouseSettings()
config.table = "test_clickhouse_async"
docsearch = Clickhouse.from_texts(
texts=texts, embedding=FakeEmbeddings(), config=config
)
output = await docsearch.asimilarity_search("foo", k=1)
assert output == [Document(page_content="foo", metadata={"_dummy": 0})]
docsearch.drop()
def test_clickhouse_with_metadatas() -> None:
"""Test end to end construction and search."""
texts = ["foo", "bar", "baz"]
metadatas = [{"page": str(i)} for i in range(len(texts))]
config = ClickhouseSettings()
config.table = "test_clickhouse_with_metadatas"
docsearch = Clickhouse.from_texts(
texts=texts,
embedding=FakeEmbeddings(),
config=config,
metadatas=metadatas,
)
output = docsearch.similarity_search("foo", k=1)
assert output == [Document(page_content="foo", metadata={"page": "0"})]
docsearch.drop()
def test_clickhouse_with_metadatas_with_relevance_scores() -> None:
"""Test end to end construction and scored search."""
texts = ["foo", "bar", "baz"]
metadatas = [{"page": str(i)} for i in range(len(texts))]
config = ClickhouseSettings()
config.table = "test_clickhouse_with_metadatas_with_relevance_scores"
docsearch = Clickhouse.from_texts(
texts=texts, embedding=FakeEmbeddings(), metadatas=metadatas, config=config
)
output = docsearch.similarity_search_with_relevance_scores("foo", k=1)
assert output[0][0] == Document(page_content="foo", metadata={"page": "0"})
docsearch.drop()
def test_clickhouse_search_filter() -> None:
"""Test end to end construction and search with metadata filtering."""
texts = ["far", "bar", "baz"]
metadatas = [{"first_letter": "{}".format(text[0])} for text in texts]
config = ClickhouseSettings()
config.table = "test_clickhouse_search_filter"
docsearch = Clickhouse.from_texts(
texts=texts, embedding=FakeEmbeddings(), metadatas=metadatas, config=config
)
output = docsearch.similarity_search(
"far", k=1, where_str=f"{docsearch.metadata_column}.first_letter='f'"
)
assert output == [Document(page_content="far", metadata={"first_letter": "f"})]
output = docsearch.similarity_search(
"bar", k=1, where_str=f"{docsearch.metadata_column}.first_letter='b'"
)
assert output == [Document(page_content="bar", metadata={"first_letter": "b"})]
docsearch.drop()
def test_clickhouse_with_persistence() -> None:
"""Test end to end construction and search, with persistence."""
config = ClickhouseSettings()
config.table = "test_clickhouse_with_persistence"
texts = [
"foo",
"bar",
"baz",
]
docsearch = Clickhouse.from_texts(
texts=texts, embedding=FakeEmbeddings(), config=config
)
output = docsearch.similarity_search("foo", k=1)
assert output == [Document(page_content="foo", metadata={"_dummy": 0})]
# Get a new VectorStore with same config
# it will reuse the table spontaneously
# unless you drop it
docsearch = Clickhouse(embedding=FakeEmbeddings(), config=config)
output = docsearch.similarity_search("foo", k=1)
# Clean up
docsearch.drop()