-
Notifications
You must be signed in to change notification settings - Fork 16.1k
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c1a8c37
ClickHouse RAG template
mneedham 2c38c7c
update
mneedham f806bf8
add none type
mneedham 498c70b
optional type
mneedham a7a2dc3
updates
mneedham 52355ac
add jupyter notebook
mneedham 5df78f5
Update pyproject.toml
mneedham a34f94b
Update README.md
mneedham f63c5ab
Merge branch 'master' into clickhouse-template
efriis 7de5711
fmt
efriis 9abfcb2
Merge branch 'master' into clickhouse-template
baskaryan 918b945
cr
baskaryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
## 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from rag_clickhouse.chain import chain | ||
|
||
__all__ = ["chain"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong link text