Building Intelligent Document Chatbots with Laravel AI SDK
Overview of Document-Based Chatbots
Integrating artificial intelligence into
Prerequisites
Before beginning, ensure you have a solid grasp of the
Key Libraries & Tools
- Laravel AI SDK: The core package providing high-level abstractions for AI interactions.
- Livewire: Used for creating the dynamic, real-time chatbot interface.
- OpenAI Vector Store: A managed service for storing and searching document embeddings.
- PostgreSQL (Optional): An alternative to OpenAIfor local vector storage via
pgvector.
Code Walkthrough: Document Processing
When a user uploads a document, the system must first prepare it for the AI. This involves creating a local record and then syncing it with a vector store.
// DocumentController.php
public function store(Request $request)
{
$document = Document::create([
'file_path' => $request->file('doc')->store('documents'),
]);
ProcessDocument::dispatch($document);
return redirect()->back();
}
The background job then handles the
// ProcessDocument Job
$store = Stores::create('openai');
$aiDocument = AIDocument::fromStorage($this->document->file_path);
$store->add($aiDocument);
// Save the remote IDs for later retrieval
$this->document->update([
'store_id' => $store->id,
'file_id' => $aiDocument->id,
]);
Implementing the QA Agent
Agents are the "brains" of the operation. You generate them using php artisan make:agent DocumentQA. Within the agent, you define the tools and models required to perform the search.
namespace App\AI\Agents;
use Laravel\AI\Agent;
use Laravel\AI\Tools\FileSearch;
class DocumentQA extends Agent
{
public function tools(): array
{
return [new FileSearch()];
}
public function instructions(): string
{
return "You are a helpful assistant. Use the provided document store to answer questions accurately.";
}
}
Syntax Notes & Best Practices
The #[Provider('openai')] attribute on your Agent classes, you can swap between different AI backends without changing your implementation logic. This follows the standard
Practical Examples
This technique is ideal for building internal support bots. For instance, uploading a 1,000-line markdown file of technical documentation allows a developer to ask, "How do I configure the codec support?" and receive a human-readable answer derived specifically from that text, rather than the AI's general training data.
Tips & Gotchas
- Cost Management: Using high-end models like GPT-4o for document retrieval can be expensive. Each question can cost several cents depending on document size and token usage.
- Performance: AI responses over document stores aren't instant; they often take 10-15 seconds. Always use Livewire's loading states orLaravel Queuesto manage user expectations.
- Local Storage: For production environments with high volume, consider PostgreSQLwith vector extensions to avoid the recurring costs of third-party vector storage.

Fancy watching it?
Watch the full video and context