Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community/templates: ClickHouse Template #17247

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions libs/community/langchain_community/vectorstores/clickhouse.py
efriis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
username: Optional[str] = None
password: Optional[str] = None

index_type: str = "annoy"
index_type: Optional[str] = "annoy"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this supposed to do? I think this will have unintended consequences in some string substitution stuff lower in the file

# Annoy supports L2Distance and cosineDistance.
index_param: Optional[Union[List, Dict]] = ["'L2Distance'", 100]
index_query_params: Dict[str, str] = {}
Expand Down Expand Up @@ -177,18 +177,8 @@
else self.config.index_param
)

self.schema = f"""\
CREATE TABLE IF NOT EXISTS {self.config.database}.{self.config.table}(
{self.config.column_map['id']} Nullable(String),
{self.config.column_map['document']} Nullable(String),
{self.config.column_map['embedding']} Array(Float32),
{self.config.column_map['metadata']} JSON,
{self.config.column_map['uuid']} UUID DEFAULT generateUUIDv4(),
CONSTRAINT cons_vec_len CHECK length({self.config.column_map['embedding']}) = {dim},
INDEX vec_idx {self.config.column_map['embedding']} TYPE \
{self.config.index_type}({index_params}) GRANULARITY 1000
) ENGINE = MergeTree ORDER BY uuid SETTINGS index_granularity = 8192\
"""
self.schema = self._schema(dim, index_params)

self.dim = dim
self.BS = "\\"
self.must_escape = ("\\", "'")
Expand All @@ -209,6 +199,32 @@
self.client.command(f"SET allow_experimental_{self.config.index_type}_index=1")
self.client.command(self.schema)

def _schema(self, dim, index_params):
if self.config.index_type:
return f"""\
CREATE TABLE IF NOT EXISTS {self.config.database}.{self.config.table}(
{self.config.column_map['id']} Nullable(String),
{self.config.column_map['document']} Nullable(String),
{self.config.column_map['embedding']} Array(Float32),
{self.config.column_map['metadata']} JSON,
{self.config.column_map['uuid']} UUID DEFAULT generateUUIDv4(),
CONSTRAINT cons_vec_len CHECK length({self.config.column_map['embedding']}) = {dim},

Check failure on line 211 in libs/community/langchain_community/vectorstores/clickhouse.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.8

Ruff (E501)

langchain_community/vectorstores/clickhouse.py:211:89: E501 Line too long (96 > 88)

Check failure on line 211 in libs/community/langchain_community/vectorstores/clickhouse.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.11

Ruff (E501)

langchain_community/vectorstores/clickhouse.py:211:89: E501 Line too long (96 > 88)
INDEX vec_idx {self.config.column_map['embedding']} TYPE \
{self.config.index_type}({index_params}) GRANULARITY 1000
) ENGINE = MergeTree ORDER BY uuid SETTINGS index_granularity = 8192\
"""
else:
return f"""\
CREATE TABLE IF NOT EXISTS {self.config.database}.{self.config.table}(
{self.config.column_map['id']} Nullable(String),
{self.config.column_map['document']} Nullable(String),
{self.config.column_map['embedding']} Array(Float32),
{self.config.column_map['metadata']} JSON,
{self.config.column_map['uuid']} UUID DEFAULT generateUUIDv4(),
CONSTRAINT cons_vec_len CHECK length({self.config.column_map['embedding']}) = {dim}

Check failure on line 224 in libs/community/langchain_community/vectorstores/clickhouse.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.8

Ruff (E501)

langchain_community/vectorstores/clickhouse.py:224:89: E501 Line too long (103 > 88)

Check failure on line 224 in libs/community/langchain_community/vectorstores/clickhouse.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.11

Ruff (E501)

langchain_community/vectorstores/clickhouse.py:224:89: E501 Line too long (103 > 88)
) ENGINE = MergeTree ORDER BY uuid
"""

@property
def embeddings(self) -> Embeddings:
return self.embedding_function
Expand Down
21 changes: 21 additions & 0 deletions templates/rag-clickhouse/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 LangChain, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions templates/rag-clickhouse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

# rag-clickhouse

This template performs RAG with no reliance on external APIs.

It utilizes Ollama the LLM, GPT4All for embeddings, and ClickHouse for the vectorstore.

The vectorstore is created in `chain.py` and by default indexes a [blog post about feature stores]([https://lilianweng.github.io/posts/2023-06-23-agent/](https://clickhouse.com/blog/powering-featurestores-with-clickhouse)) for question-answering.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong link text


## Environment Setup

To set up the environment, you need to download Ollama.

Follow the instructions [here](https://python.langchain.com/docs/integrations/chat/ollama).

You can choose the desired LLM with Ollama.

This template uses `mistral`, which can be accessed using `ollama pull mistral`.

There are also [other models available](https://ollama.ai/library).

This package also uses [GPT4All](https://python.langchain.com/docs/integrations/text_embedding/gpt4all) embeddings.

You'll also need to install ClickHouse:

```bash
curl https://clickhouse.com/ | sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks kinda wild but looks like you serve that script via curl!

./clickhouse server
```

## Usage

To use this package, you should first have the LangChain CLI installed:

```shell
pip install -U langchain-cli
```

To create a new LangChain project (called `my-app`) and install this as the only package, you can do:

```shell
langchain app new my-app --package rag-clickhouse
```

If you want to add this to an existing project, you can run:

```shell
langchain app add rag-clickhouse
```

And add the following code to your `server.py` file:
```python
from rag_clickhouse import chain as rag_clickhouse

add_routes(app, rag_clickhouse, path="/rag-clickhouse")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications.
LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
If you don't have access, you can skip this section

```shell
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

```shell
langchain serve
```

This will start the FastAPI app with a server is running locally at
[http://localhost:8000](http://localhost:8000)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
We can access the playground at [http://127.0.0.1:8000/rag-clickhouse/playground](http://127.0.0.1:8000/rag_clickhouse/playground)

We can access the template from code with:

```python
from langserve.client import RemoteRunnable

runnable = RemoteRunnable("http://localhost:8000/rag-clickhouse")
```

The package will create and add documents to the vector database in `chain.py`.
By default, it will load a popular blog post on agents. However, you can choose from a large number of document loaders [here](https://python.langchain.com/docs/integrations/document_loaders).
35 changes: 35 additions & 0 deletions templates/rag-clickhouse/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[tool.poetry]
name = "rag-clickhouse"
version = "0.1.0"
description = "RAG using local LLM, embeddings, vectorstore"
authors = [
"Mark Needham <[email protected]>",
]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = "^0.1"
tiktoken = ">=0.5.1"
clickhouse-connect = ">=0.7.0"
gpt4all = ">=1.0.8"
beautifulsoup4 = ">=4.12.2"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.21"

[tool.langserve]
export_module = "rag_clickhouse"
export_attr = "chain"

[tool.templates-hub]
use-case = "rag"
author = "LangChain"
integrations = ["ClickHouse", "Gpt4all", "Ollama"]
tags = ["vectordbs"]

[build-system]
requires = [
"poetry-core",
]
build-backend = "poetry.core.masonry.api"
100 changes: 100 additions & 0 deletions templates/rag-clickhouse/rag_clickhouse.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "232fd40d-cf6a-402d-bcb8-414184a8e924",
"metadata": {},
"source": [
"## ClickHouse Template\n",
"\n",
"In `server.py`, set the following:\n",
"\n",
"```python\n",
"from rag_clickhouse import chain as rag_clickhouse_chain\n",
"add_routes(app, rag_clickhouse_chain, path=\"/rag-clickhouse\")\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "5494f6a2-f809-4210-a49c-b3766ca2d83f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-02-08T16:43:34.381207Z",
"iopub.status.busy": "2024-02-08T16:43:34.380716Z",
"iopub.status.idle": "2024-02-08T16:43:34.387371Z",
"shell.execute_reply": "2024-02-08T16:43:34.386243Z",
"shell.execute_reply.started": "2024-02-08T16:43:34.381176Z"
}
},
"source": [
"And then run the following to ask a question:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ce39d358-1934-4404-bd3e-3fd497974aff",
"metadata": {
"ExecuteTime": {
"end_time": "2024-02-07T12:43:16.792520Z",
"start_time": "2024-02-07T12:43:16.717875Z"
},
"execution": {
"iopub.execute_input": "2024-02-08T16:42:37.446022Z",
"iopub.status.busy": "2024-02-08T16:42:37.445680Z",
"iopub.status.idle": "2024-02-08T16:42:39.330910Z",
"shell.execute_reply": "2024-02-08T16:42:39.330307Z",
"shell.execute_reply.started": "2024-02-08T16:42:37.445996Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"' Yes, ClickHouse can be used as a feature store. The documents suggest that ClickHouse can fulfill the role of several components in a feature store and potentially simplify the architecture due to its real-time data warehouse capabilities and performance. However, it is important to note that not all feature stores provide the same components directly, so some degree of architectural flexibility and openness may be required for ClickHouse to be integrated into a specific feature store implementation.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langserve.client import RemoteRunnable\n",
"\n",
"rag_app = RemoteRunnable(\"http://127.0.0.1:8000/rag-clickhouse/\")\n",
"rag_app.invoke(\"Can you use ClickHouse as a feature store?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa2254e7-a7ae-4b0f-bf6c-2b830b837a27",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions templates/rag-clickhouse/rag_clickhouse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from rag_clickhouse.chain import chain

__all__ = ["chain"]
60 changes: 60 additions & 0 deletions templates/rag-clickhouse/rag_clickhouse/chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Load
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.chat_models import ChatOllama
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import GPT4AllEmbeddings
from langchain_community.vectorstores import Clickhouse, ClickhouseSettings
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import RunnableParallel, RunnablePassthrough

loader = WebBaseLoader(
"https://clickhouse.com/blog/powering-featurestores-with-clickhouse"
)
data = loader.load()

# Split

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits = text_splitter.split_documents(data)

# Add to vectorDB
settings = ClickhouseSettings(table="clickhouse_vector_search_example", index_type=None)
vectorstore = Clickhouse.from_documents(
documents=all_splits, embedding=GPT4AllEmbeddings(), config=settings
)
retriever = vectorstore.as_retriever()

# Prompt
# Optionally, pull from the Hub
# from langchain import hub
# prompt = hub.pull("rlm/rag-prompt")
# Or, define your own:
template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

# LLM
# Select the LLM that you downloaded
ollama_llm = "mistral"
model = ChatOllama(model=ollama_llm)

# RAG chain
chain = (
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
| prompt
| model
| StrOutputParser()
)


# Add typing for input
class Question(BaseModel):
__root__: str


chain = chain.with_types(input_type=Question)
Empty file.
Loading