You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ADD COLUMN context TEXT GENERATED ALWAYS AS (product_name ||': '|| description) STORED;
185
185
```
186
186
187
+
Initialize the RAG project.
188
+
We'll use the `sentence-transformers/all-MiniLM-L12-v2` model to generate embeddings on our source documents.
189
+
187
190
```sql
188
191
SELECTvectorize.init_rag(
189
192
agent_name =>'product_chat',
@@ -194,17 +197,35 @@ SELECT vectorize.init_rag(
194
197
);
195
198
```
196
199
200
+
Now we can ask questions of the `products` table and get responses from the `product_chat` agent using the `openai/gpt-3.5-turbo` generative model.
201
+
197
202
```sql
198
203
SELECTvectorize.rag(
199
204
agent_name =>'product_chat',
200
-
query =>'What is a pencil?'
205
+
query =>'What is a pencil?',
206
+
chat_model =>'openai/gpt-3.5-turbo'
201
207
) ->'chat_response';
202
208
```
203
209
204
210
```text
205
211
"A pencil is an item that is commonly used for writing and is known to be most effective on paper."
206
212
```
207
213
214
+
And to use a locally hosted Ollama service, change the `chat_model` parameter:
215
+
216
+
```sql
217
+
SELECTvectorize.rag(
218
+
agent_name =>'product_chat',
219
+
query =>'What is a pencil?',
220
+
chat_model =>'ollama/wizardlm2:7b'
221
+
) ->'chat_response';
222
+
```
223
+
224
+
```text
225
+
" A pencil is a writing instrument that consists of a solid or gelignola wood core, known as the \"lead,\" encased in a cylindrical piece of breakable material (traditionally wood or plastic), which serves as the body of the pencil. The tip of the body is tapered to a point for writing, and it can mark paper with the imprint of the lead. When used on a sheet of paper, the combination of the pencil's lead and the paper creates a visible mark that is distinct from unmarked areas of the paper. Pencils are particularly well-suited for writing on paper, as they allow for precise control over the marks made."
226
+
```
227
+
228
+
208
229
:bulb: Note that the `-> 'chat_response'` addition selects for that field of the JSON object output. Removing it will show the full JSON object, including information on which documents were included in the contextual prompt.
pg vectorize provides tools for two closely related tasks; vector search and retrieval augmented generation (RAG), and there are APIs dedicated to both of these tasks. Vector search is an important component of RAG and the RAG APIs depend on the vector search APIs. It could be helpful to think of the vector search APIs as lower level than RAG. However, relative to Postgres's APIs, both of these vectorize APIs are very high level.
| schema | text | The name of the schema where the table is located. Defaults to 'public'. |
33
33
| update_col | text | Column specifying the last time the record was updated. Required for cron-like schedule. Defaults to `last_updated_at`|
34
34
| transformer | text | The name of the transformer to use for the embeddings. Defaults to 'text-embedding-ada-002'. |
35
-
|search_alg|SimilarityAlg| The name of the search algorithm to use. Defaults to 'pgv_cosine_similarity'. |
35
+
|index_dist_type|IndexDist| The name of index type to build. Defaults to 'pgv_hnsw_cosine'. |
36
36
| table_method | TableMethod |`join` to store embeddings in a new table in the vectorize schema. `append` to create columns for embeddings on the source table. Defaults to `join`. |
37
37
| schedule | text | Accepts a cron-like input for a cron based updates. Or `realtime` to set up a trigger. |
38
38
@@ -47,12 +47,12 @@ Pass the API key into the function call via `args`.
47
47
48
48
```sql
49
49
selectvectorize.table(
50
-
job_name =>'product_search',
51
-
"table"=>'products',
50
+
job_name =>'product_search',
51
+
"table"=>'products',
52
52
primary_key =>'product_id',
53
-
columns => ARRAY['product_name', 'description'],
54
-
transformer =>'text-embedding-ada-002',
55
-
args =>'{"api_key": "my-openai-key"}'
53
+
columns => ARRAY['product_name', 'description'],
54
+
transformer =>'openai/text-embedding-ada-002',
55
+
args =>'{"api_key": "my-openai-key"}'
56
56
);
57
57
```
58
58
@@ -67,11 +67,11 @@ Then call `vectorize.table()` without providing the API key.
Configure `vectorize` to run on a database other than the default `postgres`.
39
39
40
-
Note that when making this change, it's also required to update `pg_cron` such that its corresponding background workers also connect to the appropriate database.
40
+
Note that when making this change, it's also required to update `pg_cron` such that its corresponding background workers also connect to the appropriate database.
0 commit comments