PHPackages                             strtob/yii2-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. [API Development](/categories/api)
4. /
5. strtob/yii2-ollama

ActiveLibrary[API Development](/categories/api)

strtob/yii2-ollama
==================

Yii2 component for Ollama API with optional vector DB support (Qdrant)

v1.0(9mo ago)23MITPHPPHP &gt;=8.0

Since Sep 9Pushed 9mo agoCompare

[ Source](https://github.com/strtob/yii2-ollama)[ Packagist](https://packagist.org/packages/strtob/yii2-ollama)[ RSS](/packages/strtob-yii2-ollama/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (2)Used By (0)

Yii2 Ollama Component
=====================

[](#yii2-ollama-component)

Yii2 component for **Ollama API** with optional **vector database support** (e.g., Qdrant) for Retrieval-Augmented Generation (RAG).

Features
--------

[](#features)

- Connect to Ollama API (`llama2`, `mistral`, `gemma`)
- Optional **vector DB integration** for context injection
- Supports **Yii2 HTTP Client**
- Easy configuration via Yii2 components
- Multilingual exception messages (`Yii::t()`)
- **Events support**: `beforeGenerate`, `afterGenerate`, `generateError`
- **Request and User** automatically included in events for easy logging and auditing

---

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

[](#installation)

Install via Composer:

```
composer require strtob/yii2-ollama
```

Ensure you have yiisoft/yii2-httpclient and a Qdrant PHP client installed if you want vector DB support.

Migration:

```
'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'strtob\yii2Ollama\migrations',
        ],
    ],
],

yii migrate
```

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

[](#configuration)

Example `config/web.php` using a Qdrant adapter:

```
use strtob\yii2Ollama\QdrantAdapter;

$qdrantAdapter = new QdrantAdapter($qdrantClient, 'my_collection');

'components' => [
    'ollama' => [
        'class' => 'strtob\yii2Ollama\OllamaComponent',
        'apiUrl' => 'http://localhost:11434/v1/generate',
        'apiKey' => 'MY_SECRET_TOKEN',
        'model' => \strtob\yii2Ollama\OllamaComponent::MODEL_MISTRAL,
        'temperature' => 0.7,
        'maxTokens' => 512,
        'topP' => 0.9,
        'vectorDb' => $qdrantAdapter, // must implement VectorDbInterface
        'vectorDbTopK' => 5,
    ],
];
```

Usage in Controller
-------------------

[](#usage-in-controller)

```
try {
    $prompt = "Explain RAG with vector DB.";

    $text = \Yii::$app->ollama->generateText($prompt);

    echo $text;

    //or

    print_r(generateTextWithTokens(string $prompt, $options = []))

} catch (\yii\base\InvalidConfigException $e) {
    echo Yii::t('yii2-ollama', 'Configuration error: {message}', ['message' => $e->getMessage()]);
} catch (\strtob\yii2Ollama\OllamaApiException $e) {
    echo Yii::t('yii2-ollama', 'API request failed: {message}', ['message' => $e->getMessage()]);
}
```

---

### Stream Modus

[](#stream-modus)

See example:

```
 public function actionIndex()
    {
        $response = Yii::$app->response;
        $response->format = \yii\web\Response::FORMAT_RAW;
        Yii::$app->response->isSent = true;

        // Output-Puffer leeren
        while (ob_get_level())
            ob_end_clean();
        ob_implicit_flush(true);

        Yii::$app->ollama->stream = true;

        try {
            Yii::$app->ollama->generate("Whats up?", [], function ($chunk) {
                echo $chunk;
                flush();
            });
        } catch (\Throwable $e) {
            echo "\n[Error]: " . $e->getMessage();
        }

    }
```

### Yii2 Ollama Component – Events

[](#yii2-ollama-component--events)

`OllamaComponent` supports **three main events** during generation:

EventWhen TriggeredData Included`beforeGenerate`Before sending a request to the Ollama API`prompt`, `options`, `request`, `user``afterGenerate`After receiving a successful response`prompt`, `options`, `request`, `user`, `response``generateError`When an exception occurs during generation`prompt`, `options`, `request`, `user`, `exception`### Event Data Details

[](#event-data-details)

- Connect to Ollama API (`llama2`, `mistral`, `gemma`)
- Optional **vector DB integration** for context injection
- Supports **Yii2 HTTP Client**
- **Embedding generation** (`embedText`)
- Easy configuration via Yii2 components
- Multilingual exception messages (`Yii::t()`)
- **Events support**: `beforeGenerate`, `afterGenerate`, `generateError`
- **Request and User** automatically included in events for easy logging and auditing

---

### Example Usage

[](#example-usage)

```
// Log after generation
\Yii::$app->ollama->on(\strtob\yii2Ollama\OllamaComponent::EVENT_AFTER_GENERATE, function($event) {
    Yii::info("Prompt generated: {$event->data['prompt']}", 'ollama');
    Yii::info("User: " . ($event->data['user']->username ?? 'guest'), 'ollama');
});

// Handle errors
\Yii::$app->ollama->on(\strtob\yii2Ollama\OllamaComponent::EVENT_GENERATE_ERROR, function($event) {
    Yii::error("Generation failed for prompt: {$event->data['prompt']}", 'ollama');
    Yii::error("Exception: " . $event->data['exception']->getMessage(), 'ollama');
});
```

---

Embedding Generation Usage
--------------------------

[](#embedding-generation-usage)

```
try {
    $text = "Llamas are members of the camelid family.";

    $embeddingResult = \Yii::$app->ollama->embedText($text);

    echo "Embedding vector:\n";
    print_r($embeddingResult['embedding']); // Nur die Embeddings anzeigen

} catch (\yii\base\InvalidConfigException $e) {
    echo "Configuration error: " . $e->getMessage();
} catch (\strtob\yii2Ollama\OllamaApiException $e) {
    echo "Embedding request failed: " . $e->getMessage();
}
```

---

Document Model (ActiveRecord with Vector DB)
--------------------------------------------

[](#document-model-activerecord-with-vector-db)

You can use an ActiveRecord model to handle documents and automatically generate embeddings for them in your vector database. Supports PDF, TXT, DOCX uploads.

```
use app\models\DocumentModel;
use yii\web\UploadedFile;

// 1) Create a new document
$doc = new DocumentModel();
$doc->title = 'Sample PDF';
$doc->user_id = 1;
$doc->uploadedFile = UploadedFile::getInstance($model, 'uploadedFile');
$doc->save(); // extracts text and stores embeddings automatically

// 2) Update document content and embeddings
$doc = DocumentModel::findOne($id);
$doc->title = 'Updated Title';
$doc->uploadedFile = UploadedFile::getInstance($model, 'uploadedFile'); // optional
$doc->save(); // embeddings updated automatically

// 3) Delete document and corresponding vectors
$doc = DocumentModel::findOne($id);
$doc->delete(); // deletes vectors in vector DB automatically
```

### How It Works

[](#how-it-works)

- beforeSave() – Converts PDFs to text or reads uploaded TXT/DOCX.
- afterSave() – Generates embeddings using VectorizerHelper and stores them in the vector database.
- afterDelete() – Removes corresponding vectors from the vector database.

This makes your document storage fully RAG-ready, automatically connecting your database records with vector embeddings.

---

Vector DB Support
-----------------

[](#vector-db-support)

Implement the `VectorDbInterface` to use any vector database. Example Qdrant adapter:

```
use strtob\yii2Ollama\VectorDbInterface;
use strtob\yii2Ollama\QdrantAdapter;

$qdrantAdapter = new QdrantAdapter($qdrantClient, 'my_collection');
```

OllamaComponent will automatically prepend top-K context from the vector DB to the prompt.

---

### Supported LLM Models so far

[](#supported-llm-models-so-far)

- `llama2`
- `mistral`
- `gemma`

---

License
-------

[](#license)

MIT License – see [LICENSE](LICENSE)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance56

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

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

Unknown

Total

1

Last Release

298d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9219549?v=4)[Tobi](/maintainers/strtob)[@strtob](https://github.com/strtob)

---

Top Contributors

[![strtob](https://avatars.githubusercontent.com/u/9219549?v=4)](https://github.com/strtob "strtob (12 commits)")

### Embed Badge

![Health badge](/badges/strtob-yii2-ollama/health.svg)

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)

PHPackages © 2026

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