PHPackages                             datlechin/flarum-ai - 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. datlechin/flarum-ai

ActiveFlarum-extension[Utility &amp; Helpers](/categories/utility)

datlechin/flarum-ai
===================

Drop-in AI integration for Flarum. Text generation, vector search, content filtering. OpenAI, Anthropic, Gemini, and custom provider support.

v0.1.1(8mo ago)5369↓37.5%[1 PRs](https://github.com/datlechin/flarum-ai/pulls)1MITPHPPHP ^8.1

Since Oct 11Pushed 1mo agoCompare

[ Source](https://github.com/datlechin/flarum-ai)[ Packagist](https://packagist.org/packages/datlechin/flarum-ai)[ Docs](https://nqd.vn)[ GitHub Sponsors](https://github.com/datlechin)[ RSS](/packages/datlechin-flarum-ai/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (3)Versions (10)Used By (1)

Flarum AI
=========

[](#flarum-ai)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667) [![Latest Stable Version](https://camo.githubusercontent.com/8002e0ba8f1d61c5a66bb2f9a35ca9a212bfc955f10b41b2eee6195a90a6ee80/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461746c656368696e2f666c6172756d2d61692e737667)](https://packagist.org/packages/datlechin/flarum-ai) [![Total Downloads](https://camo.githubusercontent.com/9e42045c8371138af0c2471661b9b1305212edb9ca2517ac4d1d063d9f414a36/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461746c656368696e2f666c6172756d2d61692e737667)](https://packagist.org/packages/datlechin/flarum-ai)

AI integration framework for Flarum with text generation, embeddings, and moderation. Multi-provider support (OpenAI, Gemini, Anthropic) with extensible architecture.

Features
--------

[](#features)

- 🤖 Text generation with streaming support
- 🔍 Vector embeddings for semantic search
- 🛡️ AI-powered content moderation
- 🔌 Multi-provider architecture (OpenAI, Gemini, Anthropic)
- ⚡ SSE streaming for real-time responses
- 🔧 Extensible provider system

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

[](#installation)

```
composer require datlechin/flarum-ai
```

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

[](#configuration)

1. Navigate to **Admin Panel → Extensions → AI**
2. Select your LLM provider (OpenAI, Gemini, or Anthropic)
3. Enter your API key
4. Configure model settings

Developer Usage
---------------

[](#developer-usage)

### Text Generation

[](#text-generation)

```
use Datlechin\Ai\Providers\HttpProviderFactory;

// Get the provider instance
$provider = app(HttpProviderFactory::class)->createLlmProvider();

// Generate text
$messages = [
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user', 'content' => 'Hello!']
];

$result = $provider->complete($messages);
echo $result['content'];
```

### Streaming Text Generation

[](#streaming-text-generation)

```
// Stream responses in real-time
foreach ($provider->stream($messages) as $chunk) {
    echo $chunk; // Output each chunk as it arrives
}
```

### Embeddings

[](#embeddings)

```
use Datlechin\Ai\Providers\HttpProviderFactory;

// Get embeddings provider
$provider = app(HttpProviderFactory::class)->createEmbeddingsProvider();

// Generate embeddings
$text = "This is some text to embed";
$embedding = $provider->embed($text);

// Returns array of floats (vector representation)
print_r($embedding);
```

### Content Moderation

[](#content-moderation)

```
use Datlechin\Ai\Providers\HttpProviderFactory;

// Get moderation provider
$provider = app(HttpProviderFactory::class)->createModerationProvider();

// Check content
$result = $provider->moderate("Content to check");

if ($result['flagged']) {
    // Handle flagged content
    print_r($result['categories']);
}
```

Creating Custom Providers
-------------------------

[](#creating-custom-providers)

Implement the provider interfaces:

```
namespace MyExtension\Providers;

use Datlechin\Ai\Providers\Contracts\LlmProviderInterface;

class CustomLlmProvider implements LlmProviderInterface
{
    public function complete(array $messages, array $options = []): array
    {
        // Your implementation
        return [
            'content' => 'Generated text',
            'usage' => ['tokens' => 100]
        ];
    }

    public function stream(array $messages, array $options = []): \Generator
    {
        // Yield chunks
        yield "chunk1";
        yield "chunk2";
    }

    public function getName(): string
    {
        return 'custom';
    }

    public function getModel(): string
    {
        return 'custom-model';
    }
}
```

Register in `extend.php`:

```
use Datlechin\Ai\Providers\ProviderCatalog;

return [
    (new Extend\ServiceProvider())
        ->register(function ($container) {
            $catalog = $container->make(ProviderCatalog::class);
            $catalog->register('custom', MyCustomProvider::class);
        }),
];
```

Available Providers
-------------------

[](#available-providers)

### OpenAI

[](#openai)

- Models: GPT-4, GPT-4 Turbo, GPT-3.5 Turbo
- Supports: Text generation, embeddings, moderation
- Streaming: ✅

### Google Gemini

[](#google-gemini)

- Models: Gemini Pro, Gemini Flash
- Supports: Text generation, embeddings
- Streaming: ✅

### Anthropic

[](#anthropic)

- Models: Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus
- Supports: Text generation
- Streaming: ✅

Events
------

[](#events)

Listen to AI events in your extensions:

```
use Datlechin\Ai\Events\TextGenerated;
use Illuminate\Contracts\Events\Dispatcher;

return [
    (new Extend\Event())
        ->listen(TextGenerated::class, function (TextGenerated $event) {
            // $event->content
            // $event->provider
            // $event->model
        }),
];
```

Available events:

- `TextGenerationStarted`
- `TextGenerated`
- `EmbeddingsStarted`
- `EmbeddingsGenerated`
- `ModerationStarted`
- `ModerationCompleted`
- `ProviderInitialized`
- `ProviderFailed`

API Endpoints
-------------

[](#api-endpoints)

### Generate Text

[](#generate-text)

```
POST /api/ai/generate
Content-Type: application/json

{
  "messages": [
    {"role": "system", "content": "You are helpful"},
    {"role": "user", "content": "Hello"}
  ],
  "stream": true
}
```

### Generate Embeddings

[](#generate-embeddings)

```
POST /api/ai/embeddings
Content-Type: application/json

{
  "text": "Text to embed"
}
```

### Moderate Content

[](#moderate-content)

```
POST /api/ai/moderate
Content-Type: application/json

{
  "content": "Content to check"
}
```

Extension Examples
------------------

[](#extension-examples)

### Text Summarization

[](#text-summarization)

```
$provider = app(HttpProviderFactory::class)->createLlmProvider();

$messages = [
    ['role' => 'system', 'content' => 'Summarize the following text concisely.'],
    ['role' => 'user', 'content' => $longText]
];

$summary = $provider->complete($messages);
```

### Semantic Search

[](#semantic-search)

```
$embeddingsProvider = app(HttpProviderFactory::class)->createEmbeddingsProvider();

// Embed query
$queryVector = $embeddingsProvider->embed($searchQuery);

// Search in database (using vector similarity)
$results = DB::table('ai_embeddings')
    ->selectRaw('*, vector_distance(embedding, ?) as distance', [$queryVector])
    ->orderBy('distance')
    ->limit(10)
    ->get();
```

### Content Filtering

[](#content-filtering)

```
$moderationProvider = app(HttpProviderFactory::class)->createModerationProvider();

$result = $moderationProvider->moderate($userContent);

if ($result['flagged']) {
    // Auto-hide or flag for review
    $post->hide();
}
```

Configuration Options
---------------------

[](#configuration-options)

Settings available in admin panel:

- `datlechin-ai.provider` - Selected provider (openai, gemini)
- `datlechin-ai.api_key` - API key for provider
- `datlechin-ai.models.selected.text` - Text generation model
- `datlechin-ai.models.selected.embeddings` - Embeddings model
- `datlechin-ai.models.selected.moderation` - Moderation model

Access in code:

```
$provider = $settings->get('datlechin-ai.provider');
$model = $settings->get('datlechin-ai.models.selected.text');
```

Requirements
------------

[](#requirements)

- Flarum 1.2+
- PHP 8.1+
- Composer 2.0+
- Valid API key for chosen provider

Links
-----

[](#links)

- [Packagist](https://packagist.org/packages/datlechin/flarum-ai)
- [GitHub](https://github.com/datlechin/flarum-ai)

Sponsor
-------

[](#sponsor)

If you find this extension helpful, you can support ongoing development through [GitHub Sponsors](https://github.com/sponsors/datlechin).

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance76

Regular maintenance activity

Popularity20

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 63.3% 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

2

Last Release

264d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b5dca3124d040fb5f1e59100485f3a23e42e4e4b1c6d89c5d4cd3e79d95f574e?d=identicon)[Ngô Quốc Đạt](/maintainers/Ng%C3%B4%20Qu%E1%BB%91c%20%C4%90%E1%BA%A1t)

---

Top Contributors

[![datlechin](https://avatars.githubusercontent.com/u/56961917?v=4)](https://github.com/datlechin "datlechin (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![flarum-bot](https://avatars.githubusercontent.com/u/39334649?v=4)](https://github.com/flarum-bot "flarum-bot (4 commits)")

---

Tags

aiopenaiGeminiflarumllmmoderationembeddings

### Embed Badge

![Health badge](/badges/datlechin-flarum-ai/health.svg)

```
[![Health](https://phpackages.com/badges/datlechin-flarum-ai/health.svg)](https://phpackages.com/packages/datlechin-flarum-ai)
```

###  Alternatives

[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

318123.0k1](/packages/cognesy-instructor-php)[flarum-lang/russian

Russian language pack for Flarum.

12128.3k](/packages/flarum-lang-russian)

PHPackages © 2026

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