-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: adding Supabase to docs #11488
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
Merged
+1,792
−1
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d452921
adding Supabase to docs
davidsbatista cc455cb
fixing links
davidsbatista e49e1fa
Merge branch 'main' into doc/adding-Supabase
davidsbatista 19f3e4a
adding missing anchors
davidsbatista 41f66cf
removing api docs, those are generated
davidsbatista d087bc1
removing warm_up()
davidsbatista bcf24b3
Merge branch 'main' into doc/adding-Supabase
davidsbatista ad9e625
adding retrievers
davidsbatista ac55346
Revert "removing api docs, those are generated"
davidsbatista 725bec8
Rely on existing supabase.md API reference — reset to main
davidsbatista 14dfb44
fixing broken anchor links
davidsbatista 9567afe
adding to choosing-a-document-store.mdx
davidsbatista 4d7a9ca
Merge branch 'main' into doc/adding-Supabase
davidsbatista 00b4700
Update docs-website/docs/concepts/document-store/choosing-a-document-…
davidsbatista 8908f68
refering Smart Pipeline connections
davidsbatista 1d25490
adding to 2.30 sidebars + Oracle DocStore and Retrievers were missing
davidsbatista 8c0d895
ElasticSearchSQLRetriever was missing in the sidebars
davidsbatista 3c76426
adding missed file
davidsbatista 34547bb
Merge branch 'main' into doc/adding-Supabase
davidsbatista 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 hidden or 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
182 changes: 182 additions & 0 deletions
182
docs-website/docs/document-stores/supabasedocumentstore.mdx
This file contains hidden or 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,182 @@ | ||
| --- | ||
| title: "SupabaseDocumentStore" | ||
| id: supabasedocumentstore | ||
| slug: "/supabasedocumentstore" | ||
| description: "Use Supabase as a document store in Haystack, with vector search (pgvector) or full-text search (PGroonga)." | ||
| --- | ||
|
|
||
| # SupabaseDocumentStore | ||
|
|
||
| <div className="key-value-table"> | ||
|
|
||
| | | | | ||
| | --- | --- | | ||
| | API reference | [Supabase](/reference/integrations-supabase) | | ||
| | GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/supabase/ | | ||
|
|
||
| </div> | ||
|
|
||
| [Supabase](https://supabase.com/) is an open-source backend platform built on PostgreSQL. The Supabase integration for Haystack provides two document stores: | ||
|
|
||
| - **`SupabasePgvectorDocumentStore`** — vector similarity search using the [pgvector](https://github.com/pgvector/pgvector) PostgreSQL extension, which comes pre-installed on Supabase. | ||
| - **`SupabaseGroongaDocumentStore`** — multilingual full-text search using the [PGroonga](https://pgroonga.github.io/) PostgreSQL extension. No embeddings required. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```shell | ||
| pip install supabase-haystack | ||
| ``` | ||
|
|
||
| ## SupabasePgvectorDocumentStore | ||
|
|
||
| `SupabasePgvectorDocumentStore` is a thin wrapper around [`PgvectorDocumentStore`](./pgvectordocumentstore.mdx) with Supabase-specific defaults: | ||
|
|
||
| - Reads the connection string from the `SUPABASE_DB_URL` environment variable. | ||
| - Defaults `create_extension` to `False` since pgvector is pre-installed on Supabase. | ||
|
|
||
| ### Connection | ||
|
|
||
| Set the `SUPABASE_DB_URL` environment variable with your Supabase database connection string. | ||
|
|
||
| :::tip[Use session mode (port 5432)] | ||
| Supabase offers two pooler ports: transaction mode (port 6543) and session mode (port 5432). For best compatibility with pgvector operations, use session mode or a direct connection. | ||
| ::: | ||
|
|
||
| ```shell | ||
| export SUPABASE_DB_URL="postgresql://postgres.[project-ref]:[password]@aws-0-[region].pooler.supabase.com:5432/postgres" | ||
| ``` | ||
|
|
||
| ### Initialization | ||
|
|
||
| ```python | ||
| from haystack_integrations.document_stores.supabase import SupabasePgvectorDocumentStore | ||
|
|
||
| document_store = SupabasePgvectorDocumentStore( | ||
| embedding_dimension=768, | ||
| vector_function="cosine_similarity", | ||
| recreate_table=True, | ||
| ) | ||
| ``` | ||
|
|
||
| To learn more about the initialization parameters, see the [API docs](/reference/integrations-supabase#supabasepgvectordocumentstore). | ||
|
|
||
| ### Supported Retrievers | ||
|
|
||
| - [`SupabasePgvectorEmbeddingRetriever`](/reference/integrations-supabase#supabasepgvectorembeddingretriever): Fetches documents from the store based on a query embedding. | ||
| - [`SupabasePgvectorKeywordRetriever`](/reference/integrations-supabase#supabasepgvectorkeywordretriever): Fetches documents matching a keyword query using PostgreSQL's `ts_rank_cd` ranking. | ||
|
|
||
| ### Example: RAG pipeline | ||
|
|
||
| ```python | ||
| from haystack import Document, Pipeline | ||
| from haystack.document_stores.types.policy import DuplicatePolicy | ||
| from haystack.components.embedders import ( | ||
| SentenceTransformersTextEmbedder, | ||
| SentenceTransformersDocumentEmbedder, | ||
| ) | ||
| from haystack.components.builders import ChatPromptBuilder | ||
| from haystack.components.generators.chat import OpenAIChatGenerator | ||
| from haystack.dataclasses import ChatMessage | ||
| from haystack.utils import Secret | ||
|
|
||
| from haystack_integrations.document_stores.supabase import SupabasePgvectorDocumentStore | ||
| from haystack_integrations.components.retrievers.supabase import ( | ||
| SupabasePgvectorEmbeddingRetriever, | ||
| ) | ||
|
|
||
| document_store = SupabasePgvectorDocumentStore( | ||
| embedding_dimension=768, | ||
| vector_function="cosine_similarity", | ||
| recreate_table=True, | ||
| ) | ||
|
|
||
| # Index documents | ||
| documents = [ | ||
| Document(content="There are over 7,000 languages spoken around the world today."), | ||
| Document( | ||
| content="Elephants have been observed to behave in a way that indicates a high level of self-awareness.", | ||
| ), | ||
| Document( | ||
| content="In certain places, you can witness the phenomenon of bioluminescent waves.", | ||
| ), | ||
| ] | ||
| embedder = SentenceTransformersDocumentEmbedder() | ||
| documents_with_embeddings = embedder.run(documents) | ||
| document_store.write_documents( | ||
| documents_with_embeddings["documents"], | ||
| policy=DuplicatePolicy.OVERWRITE, | ||
| ) | ||
|
|
||
| # Query pipeline | ||
| prompt_template = [ | ||
| ChatMessage.from_system("Answer the question based on the provided context."), | ||
| ChatMessage.from_user( | ||
| "Query: {{query}}\nDocuments:\n{% for doc in documents %}{{ doc.content }}\n{% endfor %}\nAnswer:", | ||
| ), | ||
| ] | ||
|
|
||
| query_pipeline = Pipeline() | ||
| query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder()) | ||
| query_pipeline.add_component( | ||
| "retriever", | ||
| SupabasePgvectorEmbeddingRetriever(document_store=document_store), | ||
| ) | ||
| query_pipeline.add_component( | ||
| "prompt_builder", | ||
| ChatPromptBuilder( | ||
| template=prompt_template, | ||
| required_variables=["query", "documents"], | ||
| ), | ||
| ) | ||
| query_pipeline.add_component("generator", OpenAIChatGenerator(model="gpt-4o")) | ||
| query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") | ||
| query_pipeline.connect("retriever.documents", "prompt_builder.documents") | ||
| query_pipeline.connect("prompt_builder.prompt", "generator.messages") | ||
|
|
||
| result = query_pipeline.run( | ||
| { | ||
| "text_embedder": {"text": "How many languages are there?"}, | ||
| "prompt_builder": {"query": "How many languages are there?"}, | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## SupabaseGroongaDocumentStore | ||
|
|
||
| `SupabaseGroongaDocumentStore` uses [PGroonga](https://pgroonga.github.io/), a PostgreSQL extension for fast, multilingual full-text search. Unlike the pgvector store, it works with plain text queries and requires no embeddings. | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| PGroonga must be enabled in your Supabase project. Run the following SQL in the Supabase SQL editor: | ||
|
|
||
| ```sql | ||
| CREATE EXTENSION IF NOT EXISTS pgroonga; | ||
| ``` | ||
|
|
||
| You also need to create a SQL function that PGroonga uses for search. See the [integration README](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/supabase/) for the required function definition. | ||
|
|
||
| ### Initialization | ||
|
|
||
| ```python | ||
| from haystack_integrations.document_stores.supabase import SupabaseGroongaDocumentStore | ||
| from haystack.utils import Secret | ||
|
|
||
| document_store = SupabaseGroongaDocumentStore( | ||
| supabase_url="https://<project-ref>.supabase.co", | ||
| supabase_key=Secret.from_env_var("SUPABASE_SERVICE_KEY"), | ||
| table_name="haystack_groonga_documents", | ||
| ) | ||
| document_store.warm_up() | ||
| ``` | ||
|
|
||
| :::note | ||
| `warm_up()` must be called before using the store. It initializes the Supabase client and creates the table and PGroonga index if they don't exist. | ||
| ::: | ||
|
|
||
| To learn more about the initialization parameters, see the [API docs](/reference/integrations-supabase). | ||
|
|
||
| ### Supported Retrievers | ||
|
|
||
| - [`SupabaseGroongaBM25Retriever`](/reference/integrations-supabase): Retrieves documents using PGroonga full-text search. Works without embeddings and can be combined with `SupabasePgvectorEmbeddingRetriever` for hybrid search pipelines. |
This file contains hidden or 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
152 changes: 152 additions & 0 deletions
152
docs-website/docs/pipeline-components/retrievers/supabasegroongabm25retriever.mdx
This file contains hidden or 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,152 @@ | ||
| --- | ||
| title: "SupabaseGroongaBM25Retriever" | ||
| id: supabasegroongabm25retriever | ||
| slug: "/supabasegroongabm25retriever" | ||
| description: "A full-text Retriever that fetches documents from the SupabaseGroongaDocumentStore using PGroonga search." | ||
| --- | ||
|
|
||
| # SupabaseGroongaBM25Retriever | ||
|
|
||
| A full-text Retriever that fetches documents from the SupabaseGroongaDocumentStore using PGroonga search. | ||
|
|
||
| <div className="key-value-table"> | ||
|
|
||
| | | | | ||
| | --- | --- | | ||
| | **Most common position in a pipeline** | 1. Before a [`PromptBuilder`](../builders/promptbuilder.mdx) in a RAG pipeline 2. The last component in the full-text search pipeline | | ||
| | **Mandatory init variables** | `document_store`: An instance of a [SupabaseGroongaDocumentStore](../../document-stores/supabasedocumentstore.mdx) | | ||
| | **Mandatory run variables** | `query`: A string | | ||
| | **Output variables** | `documents`: A list of documents (matching the query) | | ||
| | **API reference** | [Supabase](/reference/integrations-supabase) | | ||
| | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/supabase | | ||
| | **Package name** | `supabase-haystack` | | ||
|
|
||
| </div> | ||
|
|
||
| ## Overview | ||
|
|
||
| `SupabaseGroongaBM25Retriever` retrieves Documents from the `SupabaseGroongaDocumentStore` using [PGroonga](https://pgroonga.github.io/), a PostgreSQL extension for fast, multilingual full-text search. | ||
|
|
||
| Unlike embedding-based retrievers, this Retriever works with plain text queries and requires no embeddings. It supports a wide range of languages out of the box through PGroonga's multilingual indexing capabilities. | ||
|
|
||
| The Retriever can be combined with `SupabasePgvectorEmbeddingRetriever` and a [`DocumentJoiner`](../joiners/documentjoiner.mdx) for hybrid search pipelines that take advantage of both keyword and semantic retrieval. | ||
| You can also use of the [Smart Pipeline Connections](https://docs.haystack.deepset.ai/docs/smart-pipeline-connections) and skip the `DocumentJoiner` if you want to combine the results of both retrievers in a RAG pipeline. | ||
|
|
||
| In addition to `query`, the Retriever accepts optional parameters including `top_k` (the maximum number of Documents to retrieve) and `filters` to narrow the search space. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| PGroonga must be enabled in your Supabase project. Run the following SQL in the Supabase SQL editor: | ||
|
|
||
| ```sql | ||
| CREATE EXTENSION IF NOT EXISTS pgroonga; | ||
| ``` | ||
|
|
||
| You also need to create a SQL function that PGroonga uses for search. See the [integration README](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/supabase/) for the required function definition. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```shell | ||
| pip install supabase-haystack | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### On its own | ||
|
|
||
| This Retriever needs the `SupabaseGroongaDocumentStore` and indexed Documents to run. | ||
|
|
||
| Set the `SUPABASE_URL` and `SUPABASE_SERVICE_KEY` environment variables for your Supabase project. | ||
|
|
||
| ```python | ||
| from haystack_integrations.document_stores.supabase import SupabaseGroongaDocumentStore | ||
| from haystack_integrations.components.retrievers.supabase import ( | ||
| SupabaseGroongaBM25Retriever, | ||
| ) | ||
| from haystack.utils import Secret | ||
|
|
||
| document_store = SupabaseGroongaDocumentStore( | ||
| supabase_url="https://<project-ref>.supabase.co", | ||
| supabase_key=Secret.from_env_var("SUPABASE_SERVICE_KEY"), | ||
| table_name="haystack_groonga_documents", | ||
| ) | ||
|
|
||
| retriever = SupabaseGroongaBM25Retriever(document_store=document_store) | ||
|
|
||
| retriever.run(query="my nice query") | ||
| ``` | ||
|
|
||
| ### In a RAG pipeline | ||
|
|
||
| The prerequisites for running this code are: | ||
|
|
||
| - Set an environment variable `OPENAI_API_KEY` with your OpenAI API key. | ||
| - Set an environment variable `SUPABASE_SERVICE_KEY` with your Supabase service role key. | ||
|
|
||
| ```python | ||
| from haystack import Document, Pipeline | ||
| from haystack.components.builders.answer_builder import AnswerBuilder | ||
| from haystack.components.builders import ChatPromptBuilder | ||
| from haystack.components.generators.chat import OpenAIChatGenerator | ||
| from haystack.dataclasses import ChatMessage | ||
| from haystack.document_stores.types import DuplicatePolicy | ||
| from haystack.utils import Secret | ||
|
|
||
| from haystack_integrations.document_stores.supabase import SupabaseGroongaDocumentStore | ||
| from haystack_integrations.components.retrievers.supabase import ( | ||
| SupabaseGroongaBM25Retriever, | ||
| ) | ||
|
|
||
| document_store = SupabaseGroongaDocumentStore( | ||
| supabase_url="https://<project-ref>.supabase.co", | ||
| supabase_key=Secret.from_env_var("SUPABASE_SERVICE_KEY"), | ||
| table_name="haystack_groonga_documents", | ||
| ) | ||
|
|
||
| documents = [ | ||
| Document(content="There are over 7,000 languages spoken around the world today."), | ||
| Document( | ||
| content="Elephants have been observed to behave in a way that indicates a high level of self-awareness, such as recognizing themselves in mirrors.", | ||
| ), | ||
| Document( | ||
| content="In certain parts of the world, like the Maldives, Puerto Rico, and San Diego, you can witness the phenomenon of bioluminescent waves.", | ||
| ), | ||
| ] | ||
|
|
||
| document_store.write_documents(documents=documents, policy=DuplicatePolicy.SKIP) | ||
|
|
||
| prompt_template = [ | ||
| ChatMessage.from_user( | ||
| "Given these documents, answer the question.\nDocuments:\n" | ||
| "{% for doc in documents %}{{ doc.content }}{% endfor %}\n" | ||
| "Question: {{question}}\nAnswer:", | ||
| ), | ||
| ] | ||
|
|
||
| retriever = SupabaseGroongaBM25Retriever(document_store=document_store) | ||
| rag_pipeline = Pipeline() | ||
| rag_pipeline.add_component(name="retriever", instance=retriever) | ||
| rag_pipeline.add_component( | ||
| instance=ChatPromptBuilder( | ||
| template=prompt_template, | ||
| required_variables={"question", "documents"}, | ||
| ), | ||
| name="prompt_builder", | ||
| ) | ||
| rag_pipeline.add_component(instance=OpenAIChatGenerator(), name="llm") | ||
| rag_pipeline.add_component(instance=AnswerBuilder(), name="answer_builder") | ||
| rag_pipeline.connect("retriever", "prompt_builder.documents") | ||
| rag_pipeline.connect("prompt_builder.prompt", "llm.messages") | ||
| rag_pipeline.connect("llm.replies", "answer_builder.replies") | ||
| rag_pipeline.connect("retriever", "answer_builder.documents") | ||
|
|
||
| question = "languages spoken around the world today" | ||
| result = rag_pipeline.run( | ||
| { | ||
| "retriever": {"query": question}, | ||
| "prompt_builder": {"question": question}, | ||
| "answer_builder": {"query": question}, | ||
| }, | ||
| ) | ||
| print(result["answer_builder"]) | ||
| ``` | ||
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.
DocumentJoiner is technically not needed anymore thanks to our smart pipeline connections (but it's not wrong either to use it).
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.
That's a good point, I will add a simple sentence noting that.