|
| 1 | +--- |
| 2 | +title: "Getting Started with Pipelines" |
| 3 | +navTitle: "Getting Started" |
| 4 | +description: "How to get started with AI Accelerator Pipelines." |
| 5 | +redirects: |
| 6 | +- /purl/aidb/gettingstarted |
| 7 | +--- |
| 8 | + |
| 9 | +## Where to Start |
| 10 | + |
| 11 | +The best place to start is with the [Pipelines Overview](/edb-postgres-ai/ai-accelerator/overview) to get an understanding of what Pipelines is and how it works. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Pipelines is included with the EDB Postgres AI - AI Accelerator suite of tools. To install Pipelines, follow the instructions in the [AI Accelerator Installation Guide](/edb-postgres-ai/ai-accelerator/installing). |
| 16 | + |
| 17 | +## Using Pipelines |
| 18 | + |
| 19 | +Once you have Pipelines installed, you can start using it to work with your data. |
| 20 | + |
| 21 | +Log in to your Postgres server and ensure the Pipelines extension is installed: |
| 22 | + |
| 23 | +```sql |
| 24 | +CREATE EXTENSION aidb CASCADE; |
| 25 | +``` |
| 26 | + |
| 27 | +We'll be working solely with Postgres table data for this example, so we won't need to install the pgfs extension. |
| 28 | + |
| 29 | +Let's also create an example table to work with: |
| 30 | + |
| 31 | +```sql |
| 32 | +CREATE TABLE products ( |
| 33 | + id SERIAL PRIMARY KEY, |
| 34 | + product_name TEXT NOT NULL, |
| 35 | + description TEXT, |
| 36 | + last_updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP |
| 37 | +); |
| 38 | +__OUTPUT__ |
| 39 | +CREATE TABLE |
| 40 | +``` |
| 41 | + |
| 42 | +And let's insert some data: |
| 43 | + |
| 44 | +```sql |
| 45 | +INSERT INTO products (product_name, description) VALUES |
| 46 | + ('Hamburger', 'A delicious combination of bread and meat'), |
| 47 | + ('Cheesburger', 'Improving on a classic, the cheese brings favorite flavors'), |
| 48 | + ('Fish n Chips', 'The fish is a little greasy and the chips do not help'), |
| 49 | + ('Fries', 'Never sure about these on their own, needs seasoning'), |
| 50 | + ('Burrito', 'Always ready for this parcel of edible wonder'), |
| 51 | + ('Pizza', 'It is very much a staple, but the rolled dough with toppings does not inspire'), |
| 52 | + ('Sandwich', 'The blandest of offerings, the sandwich is predominantly boring bread'), |
| 53 | + ('Veggie Burger', 'The ultra-processed vegetable product in this is neither healthy nor delicious'), |
| 54 | + ('Kebab', 'Maybe one of the great edible treats, sliced lamb, salad and crisp pitta'); |
| 55 | +__OUTPUT__ |
| 56 | +INSERT 0 9 |
| 57 | +``` |
| 58 | + |
| 59 | +So now we have a table with some data in it, food products and some very personal opinions about them. |
| 60 | + |
| 61 | +## Registering a Retriever |
| 62 | + |
| 63 | +The first step to using Pipelines with this data is to register a retriever. A retriever is a way to access the data in the table and use it in AI workflows. |
| 64 | + |
| 65 | +```sql |
| 66 | +select aidb.register_retriever_for_table('products_retriever', 't5', 'products', 'description', 'Text'); |
| 67 | +__OUTPUT__ |
| 68 | + register_retriever_for_table |
| 69 | +------------------------------ |
| 70 | + products_retriever |
| 71 | +(1 row) |
| 72 | +``` |
| 73 | + |
| 74 | +## Querying the retriever |
| 75 | + |
| 76 | +Now that we have a retriever registered, we can query it to get similar results based on the data in the table. |
| 77 | + |
| 78 | +```sql |
| 79 | +select * from aidb.retrieve_key('products_retriever','I like it',5); |
| 80 | +__OUTPUT__ |
| 81 | +ERROR: Query returned no data. Hint: The "products_retriever_vector" table is likely empty. Make sure the embeddings have been computed. |
| 82 | +``` |
| 83 | + |
| 84 | +First, we haven't computed embeddings for our retriever yet. |
| 85 | +The `products_retriever_vector` table is where aidb keeps the computed embeddings for the retriever. |
| 86 | +Let's compute those embeddings now using `aidb.bulk_embedding`: |
| 87 | + |
| 88 | +```sql |
| 89 | +select aidb.bulk_embedding('products_retriever'); |
| 90 | +__OUTPUT__ |
| 91 | +INFO: bulk_embedding_text found 9 rows in retriever products_retriever |
| 92 | + bulk_embedding |
| 93 | +---------------- |
| 94 | + |
| 95 | +(1 row) |
| 96 | +``` |
| 97 | + |
| 98 | +Now we can query the retriever again: |
| 99 | + |
| 100 | +```sql |
| 101 | +select * from aidb.retrieve_key('products_retriever','I like it',4); |
| 102 | +__OUTPUT__ |
| 103 | + key | distance |
| 104 | +-----+-------------------- |
| 105 | + 4 | 1.0369428080621286 |
| 106 | + 3 | 1.03737124138149 |
| 107 | + 2 | 1.0839594107837638 |
| 108 | + 5 | 1.0869412071766262 |
| 109 | +(4 rows) |
| 110 | +``` |
| 111 | + |
| 112 | +Now we have some results. The `key` column is the primary key of the row in the `products` table, and the `distance` column is the distance between the query and the result. The lower the distance, the more similar the result is to the query. |
| 113 | + |
| 114 | +What we really want is the actual matching text, not just the key. We can use `aidb.retrieve_text` for that: |
| 115 | + |
| 116 | +```sql |
| 117 | +select * from aidb.retrieve_text('products_retriever','I like it',4); |
| 118 | +__OUTPUT__ |
| 119 | + key | value | distance |
| 120 | +-----+------------------------------------------------------------+-------------------- |
| 121 | + 4 | Never sure about these on their own, needs seasoning | 1.0369428080621286 |
| 122 | + 3 | The fish is a little greasy and the chips do not help | 1.03737124138149 |
| 123 | + 2 | Improving on a classic, the cheese brings favorite flavors | 1.0839594107837638 |
| 124 | + 5 | Always ready for this parcel of edible wonder | 1.0869412071766262 |
| 125 | +(4 rows) |
| 126 | +``` |
| 127 | + |
| 128 | +Now we have the actual data from the table that matches the query. |
| 129 | + |
| 130 | +You may want the row data from the `products` table instead of the `products_retriever_vector` table. You can do that by joining the two tables: |
| 131 | + |
| 132 | +```sql |
| 133 | + select * from aidb.retrieve_key('products_retriever','I like it',4) as a |
| 134 | + left join products as b |
| 135 | + on a.key=b.id; |
| 136 | +__OUTPUT__ |
| 137 | + key | distance | id | product_name | description | last_updated_at |
| 138 | +-----+--------------------+----+--------------+------------------------------------------------------------+---------------------------------- |
| 139 | + 2 | 1.0839594107837638 | 2 | Cheesburger | Improving on a classic, the cheese brings favorite flavors | 04-DEC-24 16:48:52.599806 +00:00 |
| 140 | + 3 | 1.03737124138149 | 3 | Fish n Chips | The fish is a little greasy and the chips do not help | 04-DEC-24 16:48:52.599806 +00:00 |
| 141 | + 4 | 1.0369428080621286 | 4 | Fries | Never sure about these on their own, needs seasoning | 04-DEC-24 16:48:52.599806 +00:00 |
| 142 | + 5 | 1.0869412071766262 | 5 | Burrito | Always ready for this parcel of edible wonder | 04-DEC-24 16:48:52.599806 +00:00 |
| 143 | + (4 rows) |
| 144 | +``` |
| 145 | + |
| 146 | +Now you have the actual data from the `products` table that matches the query and as you can see, the full power of Postgres is available to you to work with your AI workflows. |
| 147 | + |
| 148 | +## One more thing, auto-embedding |
| 149 | + |
| 150 | +As it stands vectors have been calculated for our data, but if we added data to the table it wouldn't be automatically embedded. The retriever would go out of sync. |
| 151 | + |
| 152 | +To keep the embeddings up to date, we can enable auto-embedding: |
| 153 | + |
| 154 | +```sql |
| 155 | +select aidb.enable_auto_embedding_for_table('products_retriever'); |
| 156 | +__OUTPUT__ |
| 157 | + enable_auto_embedding_for_table |
| 158 | +--------------------------------- |
| 159 | + |
| 160 | +(1 row) |
| 161 | +``` |
| 162 | + |
| 163 | +Now, if we add data to the table, the embeddings will be automatically calculated. We can quickly test this: |
| 164 | + |
| 165 | +```sql |
| 166 | +INSERT INTO products (product_name, description) VALUES |
| 167 | + ('Pasta', 'A carb-heavy delight that is always welcome, especially with a good sauce'), |
| 168 | + ('Salad', 'Meh, it is what it is and it is not much. Occasionally saved by a good dressing'); |
| 169 | +__OUTPUT__ |
| 170 | +NOTICE: Running auto embedding for retriever products. key: "10" content: "A carb-heavy delight that is always welcome, especially with a good sauce" |
| 171 | +NOTICE: Running auto embedding for retriever products. key: "11" content: "Meh, it is what it is and it is not much. Occasionally saved by a good dressing" |
| 172 | +INSERT 0 2 |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | +```sql |
| 177 | +select * from aidb.retrieve_key('products_retriever','I like it',4) as a |
| 178 | + left join products as b |
| 179 | + on a.key=b.id; |
| 180 | +__OUTPUT__ |
| 181 | + key | distance | id | product_name | description | last_updated_at |
| 182 | +-----+--------------------+----+--------------+---------------------------------------------------------------------------------+---------------------------------- |
| 183 | + 10 | 1.0351907976251493 | 10 | Pasta | A carb-heavy delight that is always welcome, especially with a good sauce | 04-DEC-24 17:09:44.97484 +00:00 |
| 184 | + 11 | 0.979874632270706 | 11 | Salad | Meh, it is what it is and it is not much. Occasionally saved by a good dressing | 04-DEC-24 17:09:44.97484 +00:00 |
| 185 | + 3 | 1.03737124138149 | 3 | Fish n Chips | The fish is a little greasy and the chips do not help | 04-DEC-24 16:48:52.599806 +00:00 |
| 186 | + 4 | 1.0369428080621286 | 4 | Fries | Never sure about these on their own, needs seasoning | 04-DEC-24 16:48:52.599806 +00:00 |
| 187 | +(4 rows) |
| 188 | +``` |
| 189 | + |
| 190 | +## Further reading |
| 191 | + |
| 192 | +In the [Models](../models) section, you can learn how to register more models with Pipelines, including external models from OpenAI API compatible services. |
| 193 | + |
| 194 | +In the [Retrievers](../retrievers) section, you can learn more about how to use retrievers with external data sources, local files or S3 storage, and how to use the retriever functions to get the data you need. |
0 commit comments