Introduction
Large Language Models (LLMs) like ChatGPT are revolutionizing the way we interact with software. But building an LLM-powered app doesn’t mean you need to be a data scientist. In fact, with tools like Laravel and Neo4j, you can build a powerful AI-driven Q&A system right from your PHP stack.
In this blog, I’ll show you how to use Laravel and Neo4j to build a lightweight LLM app — one that can answer questions from uploaded documents. If you know SQL but are new to graph databases, stick around — I’ve got something special for you at the end.
What We’ll Build
We’ll build a simple Document Q&A system:
- User uploads a PDF.
- Laravel extracts the text and breaks it into paragraphs.
- Each paragraph is embedded using OpenAI’s embedding API.
- These are stored in Neo4j with their vector representations.
- When a user asks a question, we:
- Embed the question,
- Run a vector similarity search in Neo4j,
- Fetch relevant paragraphs,
- Send them to GPT for the final answer.
Laravel + Neo4j: Why This Combo Works
Laravel is great for quick backends and UI, but it lacks native graph storage or vector similarity search. Neo4j solves this by allowing:
- Rich relationship modeling between data
- Native vector indexing (from Neo4j 5+)
- Powerful graph + semantic search
Together, they create an AI-ready backend stack that’s clean, fast, and scalable.
Technologies Used
- Laravel 11
- Neo4j (v5+, with vector indexing)
- OpenAI API (Embedding + GPT-3.5/4)
- Spatie PDF-to-Text
- Laudis Neo4j PHP Client
Key Steps (Simplified)
// 1. Upload PDF → Extract paragraphs
$text = Pdf::getText($pdfPath);
$paragraphs = explode("\n\n", $text);
// 2. Create embedding for each paragraph
$embedding = Http::post('https://api.openai.com/v1/embeddings', [...]);
// 3. Store each paragraph + embedding in Neo4j
$client->run('CREATE (:Paragraph {text: $text, embedding: $embedding})', [...]);
// 4. When a question comes in:
$questionEmbedding = getEmbedding($userQuestion);
// 5. Find similar paragraphs in Neo4j
$cypher = '
MATCH (p:Paragraph)
WITH p, gds.similarity.cosine(p.embedding, $embedding) AS score
RETURN p.text ORDER BY score DESC LIMIT 3
';
// 6. Send context + question to GPT
$prompt = "Answer based on:\n" . $context . "\n\nQuestion: " . $userQuestion;
That’s it! You now have an LLM-powered Q&A app using Laravel and Neo4j.
Don’t Know Neo4j Yet? But Know SQL?
You’re in luck. If you’ve been working with SQL (like MySQL/PostgreSQL), learning Cypher — Neo4j’s query language — will feel surprisingly familiar.
Here’s a quick comparison:
SQL | Cypher |
---|---|
SELECT * FROM users |
MATCH (u:User) RETURN u |
JOIN |
MATCH (a)-[:REL]->(b) |
WHERE age > 30 |
MATCH (u:User) WHERE u.age > 30 RETURN u |
See the similarity?
Learn More with My Book:
Relational to Graph: Rethink Data with Graph Databases
If you're a developer transitioning from relational databases (SQL) to graph databases like Neo4j — this book is for you.
Inside, you'll find:
- Real-world examples comparing SQL and Cypher
- Data modeling best practices for graphs
- How to use Neo4j with Laravel and LLMs
- Hands-on exercises and mini projects
👉 Get your copy now:
Relational to Graph: Rethink Data with Graph Databases
Learn how to break free from rigid table joins and think in relationships.
Final Thoughts
You don’t need a PhD in AI to start building LLM apps. With Laravel + Neo4j, you can leverage graph data + semantic search in just a few days of learning.
Whether you're building AI assistants, smart document search, or a SaaS — this stack is future-ready.
Questions? Feedback? Curious to explore more?
Let’s connect — I’d love to hear how you’re building with graphs and LLMs.
Share & Support
If you found this post useful, consider:
- Sharing with your dev community
- Grabbing a copy of Relational to Graph
Want the full codebase or guided walkthrough?
Comment below or message me — I’m working on a full open-source boilerplate for Laravel + Neo4j + LLM AI apps.
Top comments (0)