Most AI tutorials start with "get an API key." This one doesn't. By the end, you'll have a Laravel app that answers questions using an AI model running on your own computer. No API key, no monthly bill, and no data leaving your machine.
You need internet once, to download the tools and the model. After that, you can switch off your Wi-Fi and it keeps working.
The trade-off is speed and quality. A small local model is slower and less clever than the big hosted ones. For learning, internal tools, and simple assistants, it's plenty.
A single page with a text box. You type a question, click a button, and the answer appears below. Here's the flow:
Browser -> Laravel -> Ollama -> AI model Browser <- Laravel <- Ollama <- AI model LLM (large language model) - the AI brain that reads your text and writes a reply. Ollama - a free program that downloads LLMs and runs them on your computer. It also gives you a small web API on localhost, which is how Laravel talks to it. localhost - your own computer. Nothing here goes out to the internet. What you need PHP 8.2 or newer Composer About 4 GB of free disk space and 8 GB of RAM (more RAM makes it faster) Ollama (we'll install it next) Step 1: Install Ollama and get a model Download Ollama from ollama.com (macOS and Windows), or on Linux run:
curl -fsSL https://ollama.com/install.sh | sh Now download a small model. We'll use Llama 3.2, which is around 2 GB:
ollama pull llama3.2 Quick test in the terminal:
ollama run llama3.2 Type "Hello" and you should get a reply. Type /bye to exit.
Ollama normally runs in the background after installation. If you get a "connection refused" error later, start it manually with ollama serve.
Let's also check the API that Laravel will use. On macOS or Linux:
curl http://localhost:11434/api/chat -d '{ "model": "llama3.2", "stream": false, "messages": [{"role": "user", "content": "Say hello in one short sentence."}] }' You'll get JSON back, and the reply lives inside message.content. (On Windows PowerShell, quoting JSON is annoying, so skip this check. The Laravel app will test it for you anyway.)
composer create-project laravel/laravel offline-ai cd offline-ai We don't need a database for this app, and the default setup works fine as is.
OLLAMA_URL=http://localhost:11434 OLLAMA_MODEL=llama3.2 OLLAMA_TIMEOUT=120 The timeout is in seconds. Local models can take a while on the first request because the model has to load into memory.
Now open config/services.php and add this entry inside the returned array, next to the other services:
'ollama' => [ 'url' => env('OLLAMA_URL', 'http://localhost:11434'), 'model' => env('OLLAMA_MODEL', 'llama3.2'), 'timeout' => env('OLLAMA_TIMEOUT', 120), ], Why go through the config file instead of reading .env directly? Laravel can cache config for speed, and env() stops working outside config files when that happens. Reading from config() is the safe habit.
A service class is just a normal PHP class that does one job. Ours will talk to Ollama, which keeps that code out of the controller.
Create app/Services/OllamaService.php (make the Services folder if it doesn't exist):
post($url, [ 'model' => config('services.ollama.model'), 'stream' => false, 'messages' => [ [ 'role' => 'system', 'content' => 'You are a helpful assistant. Keep answers short and clear.', ], [ 'role' => 'user', 'content' => $question, ], ], ]); } catch (ConnectionException $e) { throw new RuntimeException('Could not reach Ollama. Is it running?'); } if ($response->failed()) { throw new RuntimeException('Ollama returned an error: ' . $response->body()); } return $response->json('message.content', ''); } } What's going on here:
php artisan make:controller ChatController Replace the contents of app/Http/Controllers/ChatController.php with:
validate([ 'question' => ['required', 'string', 'max:1000'], ]); try { $answer = $ollama->ask($data['question']); } catch (RuntimeException $e) { return back()->withInput()->withErrors(['question' => $e->getMessage()]); } return back()->withInput()->with('answer', $answer); } } Laravel builds OllamaService for you automatically because it's listed as a parameter. That's called dependency injection, and you don't have to set anything up for it.
<?php use App\Http\Controllers\ChatController; use Illuminate\Support\Facades\Route; Route::get('/', [ChatController::class, 'show']); Route::post('/ask', [ChatController::class, 'ask']); Step 7: Build the page Create resources/views/chat.blade.php:
Offline AI Assistant Offline AI Assistant
@csrf {{ old('question') }} @error('question') {{ $message }}
@enderror Ask @if (session('answer')) {{ session('answer') }}
@endif @csrf adds a hidden security token that Laravel requires on every form. Forget it and you'll get a "419 Page Expired" error. The little onsubmit script just disables the button and changes its label, so you know something is happening while the model thinks.
Make sure Ollama is running, then start Laravel:
php artisan serve Open http://127.0.0.1:8000 and try this question:
The first answer may take 10 to 30 seconds while the model loads. After that it's usually much quicker. You should see a short answer appear in the grey box under the form. The exact wording changes every time you ask, so yours won't match anyone else's word for word. That's normal.
This is the fun part. Turn off your Wi-Fi or unplug your network cable, then ask another question. It still works, because the model lives on your machine and Laravel is only talking to localhost.
Changes to .env are ignored. Run php artisan config:clear.
Now, You built a working AI feature with a handful of files and no outside accounts. The same pattern (Laravel talks to a local Ollama server over HTTP) scales from this small demo to real internal tools. Start small, get it running, and then improve it one piece at a time.




