Semantic search and a chatbot on your own data, with pgvector

Every PostgreSQL database on BrewInTH has pgvector ready. Build search that finds what customers mean, and a chatbot that answers from your shop's data.
A customer types "a gift for my parents, not too sweet" into your shop's search box and gets "no products found", even though you sell an oolong tea gift box and grain cookies with less sugar. An ordinary search box only finds rows that contain the words typed. When customers don't use the words in your product names, they find nothing, and they leave.
This guide shows how to build search that understands what people mean, and a chatbot that answers from your shop's own information, with pgvector, which BrewInTH already has in every PostgreSQL database. You'll see how to check that your database is ready, get prompts to paste to your AI, and learn what to watch for with costs and your customers' data.
In short
🔎 Search by meaning compares what a query means with your shop's data, so customers don't have to type your product names
🐘 Every PostgreSQL database on BrewInTH has pgvector 0.8.7 and vector64b ready, on every package including Free; no CREATE EXTENSION needed
🔑 You need an API key from an embedding service, kept in App settings and used from the server side only
💬 A chatbot uses the same data: it finds the relevant entries first, then has the AI answer from those alone
💸 Every search calls the API once, so limit how often each visitor can search and keep customers' personal data out of it
What is search by meaning?
An ordinary search box uses something like LIKE '%query%', which only finds rows containing the characters you typed. Search by meaning (semantic search) turns text into a long list of numbers called an embedding. Texts with similar meanings get numbers that sit close together, even when they share no words, such as "not too sweet" and "less sugar".

Here's how it works:
- Send each product's name and description to an AI provider's embedding API and get a list of numbers back. How many numbers (dimensions) depends on the model, for example 768, 1,024 or 1,536.
- Store that list in a
vectorcolumn of your products table. - When a customer searches, turn the query into numbers with the same model.
- Have PostgreSQL sort the products by distance, closest first, and take the top few.
pgvector is a PostgreSQL extension that adds the vector column type and distance operators such as <=>, the cosine distance: the smaller the value, the closer the meaning. Steps 2 and 4 therefore happen in your site's existing database, with no separate search service to sign up for.
What BrewInTH already has ready
Since 5 November 2026, every PostgreSQL database on BrewInTH has two extensions installed, whatever package you're on:
- vector is pgvector 0.8.7 from the main project on GitHub, for everyday embeddings, with the
vector,halfvecandsparsevectypes. - vector64b 0.1.1 is a companion extension BrewInTH installs alongside it. It stores float64 numbers in a
vector64b(n)type of up to 16,000 dimensions.
To check, open your project's Databases tab. In the Engine column, under "postgres", a small line reads pgvector (vector) and vector64b ready. If it says pgvector arrives at the next quiet-hour restart of the database instead, that database is waiting for the platform to restart it between 3 and 5 a.m. Thai time. There's nothing to do; check again the next day.

What you don't need to do is run CREATE EXTENSION yourself. Your project's database user isn't allowed to install extensions; the platform has installed them, and the guide your AI reads when it connects to BrewInTH says so. If the code contains CREATE EXTENSION IF NOT EXISTS vector, a ready database simply skips that line. Without IF NOT EXISTS it fails with "already exists", so ask your AI to remove the line.
To see it for yourself, open the Database browser on the same tab, click SQL, paste this and press Run SQL. You'll get the version of each:
SELECT extname, extversion FROM pg_extension WHERE extname IN ('vector', 'vector64b');
What you need
- An app project with PostgreSQL as its primary database, such as Node.js, Python, PHP or Laravel. A static site has no server-side program, so it can't keep an API key or connect to a database (which database to pick).
- An API key for an embedding service. Most large AI providers offer one. Pick a model that handles Thai (often called multilingual) and note how many dimensions it returns. Prices and models change often, so check the provider's own site.
- An AI that writes the code: a coding agent such as Claude Code, Codex or Cursor, connected to BrewInTH with the Create the AI link and token (automatic) button on the project page, or a chat AI that hands you the code as a ZIP.
Build search by meaning, step by step
Step 1: Keep the API key in App settings
Open your project's App settings tab. In the box of the same name, enter EMBEDDING_API_KEY as the Name, paste your key as the Value, and press Save. The app picks up the new value with its next release.
The key spends money on your account. Never put it in front-end code or a JavaScript file the browser loads, where anyone viewing the page source can read it. Calls to the embedding API must happen on the server side (keep API keys safe with project secrets).
Step 2: Have your AI add the column and the search
Paste this to your AI, changing the table and provider names to match your site:
My shop's site runs on BrewInTH. Its database is PostgreSQL with pgvector already installed (do not run CREATE EXTENSION).
Please add product search by meaning:
- Use the embedding API of [provider name], with a model that handles Thai. Read the key from the app setting EMBEDDING_API_KEY and call the API from the server side only.
- Add an embedding column to the products table with ALTER TABLE ... ADD COLUMN, of type vector with as many dimensions as the model returns.
- Embed the product name, category and description. When a product is added or edited, set its embedding to NULL.
- Write a script that fills in the embedding of rows where it is NULL, at most 100 rows per run. I will add it as a schedule every 15 minutes, so tell me the command to enter.
- The search box turns the query into an embedding and sorts with ORDER BY embedding <=> query LIMIT 10, for products in stock only.
- Allow at most 20 searches per minute per visitor, and fall back to the old keyword search if the API cannot be reached.
- Tell me anything I need to set up in BrewInTH myself.
The SQL will look roughly like this (1536 is an example; use your model's number):
ALTER TABLE products ADD COLUMN embedding vector(1536);
SELECT id, name, price
FROM products
WHERE embedding IS NOT NULL AND in_stock
ORDER BY embedding <=> $1
LIMIT 10;
$1 is the query's numbers, passed in by the app. Other conditions, such as a category, a price range or in-stock only, go into WHERE as usual.
If you update your site through Upload & go live, have your AI add the ALTER TABLE ... ADD COLUMN line at the end of your existing .sql file: the platform adds the column for you before the new version starts (what BrewInTH does with your .sql on an update).
Step 3: Fill in the numbers for existing products
Your existing products have no embedding yet. The simplest way is the script from step 2, which fills in a few empty rows at a time, set to run on a schedule:
- Open your project's Schedules tab.
- For Name, enter something like
embed-products; for Command, what your AI told you, such asnode scripts/embed-products.js. - Set Frequency to Every 15 minutes and press Add schedule.
The same schedule also gives new or edited products their numbers within minutes. If your AI is connected to BrewInTH, it can run the script itself to fill every row on the first pass (up to 4 hours per run). The console's Commands tab runs for at most 30 seconds at a time, which suits small trial batches (run one-off commands with the Commands tab).
To see how many rows are left, use the SQL box of the Database browser:
SELECT count(*) FROM products WHERE embedding IS NULL;
Step 4: Test with words that aren't in your product names
Search with words your customers really use but your product names don't, like "a gift for my parents", "snacks for someone watching their sugar", or the same in Thai. If the top results are off, a fuller product description usually fixes it: an embedding only knows what the text says.
A chatbot that answers from your shop's data
A general AI chatbot doesn't know your delivery fees, opening hours or return policy, and sometimes makes answers up. The common fix is to have the bot look up your shop's information first and answer from that, an approach called RAG (retrieval-augmented generation). It uses pgvector exactly like the search box.

- Keep your FAQ, delivery and return policies and shop details as short entries, one topic each, in a table such as
faqwith an embedding column. - When a customer asks, the app turns the question into numbers and fetches the 3 to 5 closest entries.
- It sends those entries with the question to a chat AI, with an instruction to answer from them alone.
- If no entry is close enough, the bot says it doesn't know and points to your contact details instead of guessing.
A prompt to paste to your AI:
Add a customer-question chatbot to my shop's site on BrewInTH (PostgreSQL with pgvector already installed; do not run CREATE EXTENSION).
- Create a faq table (id, title, content, embedding) and an admin page where I add and edit entries, one topic per entry.
- Compute embeddings the same way as the product search, with the key from EMBEDDING_API_KEY.
- When a customer asks, embed the question, fetch the 4 closest entries with <=>, and send them to a chat AI with the key from CHAT_API_KEY.
- Tell the chat AI: "Answer only from the information provided. If the answer is not in it, say you don't know and suggest contacting the shop."
- If even the closest entry is further away than a threshold, skip the chat AI and reply with the shop's contact details. Make the threshold a setting I can change.
- Allow at most 10 questions per minute per visitor, and never put customers' orders or personal details from the database into what is sent to the AI.
Tune "close enough" by asking around 20 real questions, some answered by your entries and some not, and looking at the distances you get: the numbers differ from model to model. The same bot can answer on LINE too, if your LINE webhook calls the same answering function (build a LINE chatbot and host it on BrewInTH).
Choosing the column type and an index
vector(n)is the usual choice: float32 numbers, 4 bytes each, indexable up to 2,000 dimensions.vector64b(n)is for models that return more than 2,000 dimensions when you want an index, or when you need float64 precision, up to 16,000 dimensions. It takes twice the space ofvector, and its index usesvector64b_hnswwithvector64b_cosine_ops.- The number in brackets must match your model's dimensions, and you must use the same model when storing and when searching.
Without an index, PostgreSQL compares the query with every row. That gives the most accurate results and is fast enough for a shop with a few thousand products. When your data grows and searches slow down, have your AI add an HNSW index, which is much faster in exchange for approximate results:
CREATE INDEX ON products USING hnsw (embedding vector_cosine_ops);
Put this in your app's migrations rather than the console's SQL box, which stops any statement after 10 seconds; an index on a large table takes longer to build. The index must use the same distance as your searches (vector_cosine_ops with <=>), or PostgreSQL won't use it.
Things to watch
- Cost. Every search is one call to the embedding API, and the chatbot adds a chat AI call. Your provider bills your account for what you use, so limit calls per visitor and set a spending cap in the provider's dashboard if it offers one.
- Data that leaves your site. The texts you embed and the questions customers type are sent to the AI provider. Use it for product and shop information, keep customers' names, phone numbers and addresses out of it, and mention it in your site's privacy policy.
- Space. An embedding of 1,536 dimensions takes about 6 KB per row, so 1,000 products take about 6 MB before any index. Database data counts towards your package's storage, which is 20 MB for the whole account on Free (how BrewInTH counts your storage). If space is tight,
halfvecstores each number in 2 bytes.
FAQ
Do I pay BrewInTH extra for this?
No. pgvector is in the PostgreSQL databases of every package, including Free. What you pay for is the embedding and chat AI usage of the provider you choose.
My site uses MySQL or MongoDB. Can I use pgvector?
pgvector is a PostgreSQL extension, so only PostgreSQL databases have it. A project has one primary database, and switching engines means deleting the current one and creating another; your data doesn't move by itself. If you want to switch, ask your AI to plan the data move before anything is deleted.
How well does it handle Thai?
That depends on the embedding model. Most multilingual models work with Thai, but quality varies, so try around 20 real customer queries before going live. Some models also match Thai with English, so a customer typing in English can still find products described in Thai.
Can I change the embedding model or provider later?
Yes, but numbers from different models can't be compared, so every row has to be recomputed. If the number of dimensions changes, have your AI change the column type, set every row to NULL and let the schedule fill them in again.
The Database browser says the result is too large when I open my products table
The Database browser shows at most 200 rows or 60 KB at a time, and a 1,536-dimension embedding runs to well over ten thousand characters per row, so a few rows exceed it. Click SQL and pick only the columns you want to see, such as SELECT id, name, price FROM products LIMIT 50; (browse and fix your database from the console).
Summary
Every PostgreSQL database on BrewInTH already has pgvector. All you add is an embedding service's API key in App settings, an embedding column and a search written by your AI, and a schedule that fills in the numbers for new data. The same data then powers a chatbot that answers from your shop's information. Start with a few dozen products, test with your customers' real words, and grow from there.
Open Databases in the console →
