Ship AI with Laravel: RAG with Embeddings and pgvector in Laravel 13
Published on by Harris Raftopoulos
▶️ Watch the video tutorial (15 minutes)
Our agent can look up orders, classify tickets, and remember conversations. But ask it "what's your return policy for damaged items?" and it makes something up. The agent has no access to our actual policies.
In this episode we give it a searchable knowledge base. Real documentation it can search by meaning.
I start with what embeddings actually are. Text converted into vectors that capture meaning. "How do I return a damaged item?" and "What if my package arrived broken?" use different words but mean the same thing, so their vectors end up close together. That's how semantic search works.
We build a KnowledgeArticle model with a vector column using PostgreSQL's pgvector extension, set up a 1,536-dimension embedding column, and use the Laravel AI SDK's Embeddings facade to generate vectors for each article. An Artisan command seeds eight FAQ articles and generates their embeddings.
For searching we use Laravel 13's whereVectorSimilarTo method. I test it in the browser with a few queries and the right article comes back even when the wording is completely different from what's stored.
Then we wire it into the support agent as a similarity search tool and update the instructions so the agent reaches for the knowledge base on policy questions.
Ask "what's your return policy?" and the answer comes from the actual return policy.
Next episode we use provider-hosted vector stores and the FileSearch tool so the agent can search entire PDFs without us writing the search logic.