Clickhouse(
self,
embedding: Embeddings,
config: Optional[ClickhouseSettings] = None,
**kwargs| Name | Type | Description |
|---|---|---|
embedding_function* | Embeddings | |
config | ClickHouseSettings | Default: None |
kwargs | any | Default: {} |
| Name | Type |
|---|---|
| embedding | Embeddings |
| config | Optional[ClickhouseSettings] |
ClickHouse vector store integration.
Setup:
Install langchain_community and clickhouse-connect:
.. code-block:: bash
pip install -qU langchain_community clickhouse-connect
Key init args — indexing params: embedding: Embeddings Embedding function to use.
Key init args — client params: config: Optional[ClickhouseSettings] ClickHouse client configuration.
Instantiate:
.. code-block:: python
from langchain_community.vectorstores import Clickhouse, ClickhouseSettings from langchain_openai import OpenAIEmbeddings
settings = ClickhouseSettings(table="clickhouse_example") vector_store = Clickhouse(embedding=OpenAIEmbeddings(), config=settings)
Add Documents:
.. code-block:: python
from langchain_core.documents import Document
document_1 = Document(page_content="foo", metadata={"baz": "bar"}) document_2 = Document(page_content="thud", metadata={"bar": "baz"}) document_3 = Document(page_content="i will be deleted :(")
documents = [document_1, document_2, document_3] ids = ["1", "2", "3"] vector_store.add_documents(documents=documents, ids=ids)
Delete Documents:
.. code-block:: python
vector_store.delete(ids=["3"])
Search: .. code-block:: python
results = vector_store.similarity_search(query="thud",k=1)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
.. code-block:: python
# TODO: Example output
Search with filter: .. code-block:: python
# TODO: Edit filter if needed
results = vector_store.similarity_search(query="thud",k=1,filter="metadata.baz='bar'")
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
.. code-block:: python
# TODO: Example output
Search with score: .. code-block:: python
results = vector_store.similarity_search_with_score(query="qux",k=1)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
.. code-block:: python
# TODO: Example output
Async: .. code-block:: python
# add documents
# await vector_store.aadd_documents(documents=documents, ids=ids)
# delete documents
# await vector_store.adelete(ids=["3"])
# search
# results = vector_store.asimilarity_search(query="thud",k=1)
# search with score
results = await vector_store.asimilarity_search_with_score(query="qux",k=1)
for doc,score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
.. code-block:: python
# TODO: Example output
Use as Retriever: .. code-block:: python
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
.. code-block:: python
# TODO: Example output
embedding function to use
Configuration to ClickHouse Client
Other keyword arguments will pass into clickhouse-connect
Provides access to the embedding mechanism used by the Clickhouse instance.
This property allows direct access to the embedding function or model being used by the Clickhouse instance to convert text documents into embedding vectors for vector similarity search.
Escape special characters in a string for Clickhouse SQL queries.
This method is used internally to prepare strings for safe insertion into SQL queries by escaping special characters that might otherwise interfere with the query syntax.
Insert more texts through the embeddings and add to the VectorStore.
Create ClickHouse wrapper with existing texts
Perform a similarity search with ClickHouse
Perform a similarity search with ClickHouse by vectors
Perform a similarity search with ClickHouse
Helper function: Drop data