PHPackages                             camh/laravel-ollama - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. camh/laravel-ollama

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

camh/laravel-ollama
===================

Laravel client for Ollama (generate, chat, embeddings) with PII masking &amp; JSON helpers.

v1.1.0(7mo ago)118MITPHPPHP &gt;=8.2

Since Sep 24Pushed 7mo agoCompare

[ Source](https://github.com/cam-hm/laravel-ollama)[ Packagist](https://packagist.org/packages/camh/laravel-ollama)[ RSS](/packages/camh-laravel-ollama/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (9)Used By (0)

Laravel Ollama
==============

[](#laravel-ollama)

[![Status](https://camo.githubusercontent.com/d2656a901783e71666d7809f3464464fa4464d567a223f27ce4a2b747864f169/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d6163746976652d627269676874677265656e)](https://camo.githubusercontent.com/d2656a901783e71666d7809f3464464fa4464d567a223f27ce4a2b747864f169/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d6163746976652d627269676874677265656e)

A Laravel client for [Ollama](https://ollama.com/) (local LLM): text generation, chat, embeddings, PII masking, and JSON guardrails.

---

Features
--------

[](#features)

- **Text Generation**: Use local LLMs for text generation.
- **Chat**: Multi-turn conversations with context.
- **Embeddings**: Generate vector embeddings for text.
- **PII Masking**: Automatically mask sensitive info (emails, tokens, etc.).
- **JSON Guardrails**: Repair/validate JSON output from models.
- **Artisan Commands**: Manage models, chat, and more from CLI.

Installation
------------

[](#installation)

```
composer require camh/laravel-ollama
php artisan vendor:publish --tag=config --provider="Camh\\Ollama\\OllamaServiceProvider"
```

Configuration
-------------

[](#configuration)

Add to your `.env`:

```
OLLAMA_BASE=http://127.0.0.1:11434
OLLAMA_GEN_MODEL=llama3.1
OLLAMA_EMBED_MODEL=nomic-embed-text
OLLAMA_TIMEOUT=60
OLLAMA_RETRIES=1
```

Edit `config/ollama.php` for advanced options:

- `base`: Ollama server URL
- `default_model`: Default model for generation
- `embed_model`: Model for embeddings
- `timeout`: Request timeout (seconds)
- `retries`: Number of retries
- `mask_pii`: Enable/disable PII masking
- `pii_patterns`: Custom regex patterns for PII

Usage
-----

[](#usage)

You can interact with the package through the `Ollama` facade.

### Generate Text

[](#generate-text)

To generate a simple text completion:

```
use Camh\Ollama\Facades\Ollama;

$response = Ollama::generate('Why is the sky blue?');
echo $response;
```

You can also pass additional options, such as the model or temperature:

```
$response = Ollama::generate('Why is the sky blue?', [
    'model' => 'llama3.1',
    'options' => [
        'temperature' => 0.7,
    ],
]);
```

### Chat Completions

[](#chat-completions)

For chat-based interactions, provide a history of messages:

```
$messages = [
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user', 'content' => 'Why is the sky blue?'],
];

$response = Ollama::chat($messages);
echo $response; // Outputs the assistant's message content
```

### Embeddings

[](#embeddings)

To create embeddings for a given input:

```
$embedding = Ollama::embed('Hello world');
// Returns an array of floats

$embeddings = Ollama::embed(['Hello', 'world']);
// Returns an array of embedding arrays
```

### Streaming Responses

[](#streaming-responses)

For real-time responses, use the stream method with a callback:

```
Ollama::stream('Tell me a long story.', function ($chunk) {
    echo $chunk;
});
```

### JSON Mode

[](#json-mode)

To receive a response in JSON format (auto-repairs invalid JSON):

```
$response = Ollama::json('Create a user profile for John Doe.');
// Returns an array
```

You can also provide a JSON schema for structured output:

```
$schema = [
    'type' => 'object',
    'properties' => [
        'name' => ['type' => 'string'],
        'email' => ['type' => 'string', 'format' => 'email'],
    ],
    'required' => ['name', 'email'],
];

$response = Ollama::json('Create a user profile for John Doe.', [], $schema);
```

### Persistent, Rich Conversations

[](#persistent-rich-conversations)

You can manage multi-turn, persistent conversations using the `Conversation` class:

```
use Camh\Ollama\Support\Conversation;
use Camh\Ollama\Facades\Ollama;

// Start or load a conversation for a user/session
$conversation = Conversation::load('user123') ?? new Conversation('user123', 'You are a helpful assistant.');

// Add a user message and get the assistant's reply
$reply = Ollama::conversation($conversation, 'Why is the sky blue?');
echo $reply;

// Continue the conversation
$reply2 = Ollama::conversation($conversation, 'What about sunsets?');
echo $reply2;

// Save the conversation (automatically done after each reply)
$conversation->save();

// Retrieve full history
$history = $conversation->getMessages();

// Clear the conversation
$conversation->clear();
$conversation->save();
```

- Each conversation is identified by a unique ID (e.g., user/session ID).
- Conversations are automatically persisted in cache and can be loaded later.
- You can add metadata, limit history, or extend the Conversation class for more features.

Artisan Commands
----------------

[](#artisan-commands)

```
php artisan ollama:tags                # List available models/tags
php artisan ollama:pull llama3.1       # Download a model
php artisan ollama:chat "Say hello"    # Chat with a model
php artisan ollama:show llama3.1       # Show model details
php artisan ollama:delete llama3.1     # Delete a model
php artisan ollama:copy src dest       # Copy a model
php artisan ollama:create name         # Create a new model
```

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Pull requests and issues are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) if available.

License
-------

[](#license)

MIT

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance63

Regular maintenance activity

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

5

Last Release

225d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d754ddb86d7837f141774e9f817ae7298a882ed9e4de5c1309f42e9e7a105dc?d=identicon)[hoangmanhcam](/maintainers/hoangmanhcam)

---

Top Contributors

[![cam-hm](https://avatars.githubusercontent.com/u/234133352?v=4)](https://github.com/cam-hm "cam-hm (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/camh-laravel-ollama/health.svg)

```
[![Health](https://phpackages.com/badges/camh-laravel-ollama/health.svg)](https://phpackages.com/packages/camh-laravel-ollama)
```

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[nativephp/mobile

NativePHP for Mobile

82724.0k43](/packages/nativephp-mobile)[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)[glhd/conveyor-belt

14797.0k](/packages/glhd-conveyor-belt)[pulkitjalan/ip-geolocation

IP Geolocation Wrapper with Laravel Support

89164.9k1](/packages/pulkitjalan-ip-geolocation)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
