Skip to content

Commit 76f2be6

Browse files
authored
docs: strengthen RAG schema and guidance (#374)
1 parent 2cacab0 commit 76f2be6

1 file changed

Lines changed: 48 additions & 21 deletions

File tree

‎docs/services/rag.md‎

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ configuration.
2828
Before deploying a RAG service, your PostgreSQL database must have the
2929
following items configured:
3030

31-
- The pgvector extension must be installed and enabled.
32-
- The database must have document tables with text and vector columns.
33-
- An HNSW index on vector columns enables fast similarity search.
34-
- A GIN index on text columns enables keyword search (BM25).
31+
- The pgvector extension must be installed and enabled
32+
- The database must have document tables with text and vector columns
33+
- An HNSW index on vector columns enables fast similarity search
34+
- A GIN index on text columns enables keyword search (BM25)
3535

3636
The Control Plane can automatically provision all of these during
3737
database creation using the `scripts.post_database_create` hook. See
@@ -74,6 +74,17 @@ In this example, `app_read_only` must be defined in `database_users`:
7474
}
7575
```
7676

77+
If the `connect_as` user is not a database owner or superuser, it must
78+
have `SELECT` privilege on each document table. Grant this privilege in
79+
the `scripts.post_database_create` array:
80+
81+
```sql
82+
GRANT SELECT ON documents_content_chunks TO app_read_only;
83+
```
84+
85+
Without this grant, the RAG service will silently return
86+
`"No relevant information found"` for all queries.
87+
7788
### Pipeline Configuration
7889

7990
The `pipelines` array (required) defines one or more RAG workflows.
@@ -187,7 +198,7 @@ CREATE EXTENSION IF NOT EXISTS vector;
187198
CREATE TABLE IF NOT EXISTS documents_content_chunks (
188199
id BIGSERIAL PRIMARY KEY,
189200
content TEXT NOT NULL,
190-
embedding vector(1536),
201+
embedding vector(1536) NOT NULL,
191202
title TEXT,
192203
source TEXT,
193204
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
@@ -259,7 +270,7 @@ that uses OpenAI for embeddings and Anthropic Claude to generate answers:
259270
"scripts": {
260271
"post_database_create": [
261272
"CREATE EXTENSION IF NOT EXISTS vector",
262-
"CREATE TABLE IF NOT EXISTS documents_content_chunks (id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, embedding vector(1536), title TEXT, source TEXT)",
273+
"CREATE TABLE IF NOT EXISTS documents_content_chunks (id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, embedding vector(1536) NOT NULL, title TEXT, source TEXT)",
263274
"CREATE INDEX ON documents_content_chunks USING hnsw (embedding vector_cosine_ops)",
264275
"CREATE INDEX ON documents_content_chunks USING gin (to_tsvector('\''english'\'', content))"
265276
]
@@ -307,8 +318,7 @@ that uses OpenAI for embeddings and Anthropic Claude to generate answers:
307318

308319
### OpenAI End-to-End
309320

310-
In the following example, OpenAI is used for both embeddings and to generate
311-
answers:
321+
In the following example, OpenAI handles both embeddings and answer generation:
312322

313323
=== "curl"
314324

@@ -504,7 +514,6 @@ required; the Ollama server URL is provided via `base_url`:
504514
In the following example, two pipelines share default values for
505515
`token_budget` and `top_n`, set with the `defaults` properties:
506516

507-
508517
=== "curl"
509518

510519
```sh
@@ -631,7 +640,7 @@ URL stays stable across container restarts.
631640
"scripts": {
632641
"post_database_create": [
633642
"CREATE EXTENSION IF NOT EXISTS vector",
634-
"CREATE TABLE IF NOT EXISTS documents_content_chunks (id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, embedding vector(1536), title TEXT, source TEXT)",
643+
"CREATE TABLE IF NOT EXISTS documents_content_chunks (id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, embedding vector(1536) NOT NULL, title TEXT, source TEXT)",
635644
"CREATE INDEX ON documents_content_chunks USING hnsw (embedding vector_cosine_ops)",
636645
"CREATE INDEX ON documents_content_chunks USING gin (to_tsvector('\''english'\'', content))",
637646
"GRANT SELECT ON documents_content_chunks TO app_read_only"
@@ -692,9 +701,9 @@ that the database is ready and the RAG service is running:
692701
In the response, look for the following items:
693702

694703
- The `state: "available"` field at the top level confirms that the
695-
database is provisioned and healthy.
704+
database is provisioned and healthy
696705
- The `service_ready: true` field inside `service_instances[].status`
697-
confirms that the RAG container is up and accepting requests.
706+
confirms that the RAG container is up and accepting requests
698707

699708
```text
700709
{
@@ -976,9 +985,9 @@ The RAG service's hybrid search combines two complementary techniques,
976985
merged using Reciprocal Rank Fusion (RRF):
977986

978987
- Vector similarity search retrieves documents semantically similar to
979-
the query using cosine distance on embeddings.
988+
the query using cosine distance on embeddings
980989
- BM25 keyword search retrieves documents with exact keyword matches
981-
using TF-IDF scoring.
990+
using TF-IDF scoring
982991

983992
This combination ensures the LLM receives context that is both
984993
semantically relevant and keyword-relevant. Documents appearing in
@@ -1000,7 +1009,8 @@ The following sections describe common issues and how to resolve them.
10001009
### About Automated Scripts
10011010

10021011
The `scripts.post_database_create` field executes SQL automatically
1003-
during database creation. The following details apply:
1012+
during database creation. The following table describes the execution
1013+
properties:
10041014

10051015
| Property | Details |
10061016
|---|---|
@@ -1023,26 +1033,43 @@ To verify that the database is accessible, run the following command:
10231033
psql -h host-1 -U admin -d knowledge_base -c "SELECT 1"
10241034
```
10251035

1026-
To verify that the service user (`app_read_only`) exists and has table
1027-
access, run the following query:
1036+
To verify that the service user (`app_read_only`) exists and has SELECT
1037+
privilege on the document table, run the following queries:
10281038

10291039
```sql
10301040
\du+ app_read_only
1031-
\dt documents_content_chunks
1041+
SELECT has_table_privilege('app_read_only', 'documents_content_chunks', 'SELECT');
1042+
```
1043+
1044+
If `has_table_privilege` returns `false`, grant the privilege:
1045+
1046+
```sql
1047+
GRANT SELECT ON documents_content_chunks TO app_read_only;
10321048
```
10331049

10341050
### Poor Query Results
10351051

10361052
To diagnose poor query results, verify that documents are loaded and
10371053
embeddings are present.
10381054

1039-
To check document counts and embedding coverage, run the following
1040-
queries:
1055+
To check document counts, run the following query:
10411056

10421057
```sql
10431058
SELECT COUNT(*) FROM documents_content_chunks;
1059+
```
1060+
1061+
The recommended schema sets `embedding NOT NULL`, so all rows should
1062+
have embeddings. If you are using an existing table without that
1063+
constraint, rows with `NULL` embeddings will cause the vector search to
1064+
fail and the service will return `"No relevant information found"` even
1065+
when documents exist. Check for and remove any such rows:
1066+
1067+
```sql
1068+
-- Check for rows missing embeddings
1069+
SELECT COUNT(*) FROM documents_content_chunks WHERE embedding IS NULL;
10441070

1045-
SELECT COUNT(*) FROM documents_content_chunks WHERE embedding IS NOT NULL;
1071+
-- Remove rows missing embeddings
1072+
DELETE FROM documents_content_chunks WHERE embedding IS NULL;
10461073
```
10471074

10481075
To find documents similar to a test query embedding, run the following

0 commit comments

Comments
 (0)