PHPackages                             helgesverre/brain - 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. helgesverre/brain

ActiveLibrary[API Development](/categories/api)

helgesverre/brain
=================

The Adorably Simple OpenAI Wrapper

v0.2.5(8mo ago)264361[1 issues](https://github.com/HelgeSverre/brain/issues)MITPHPPHP ^8.2

Since Dec 10Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/HelgeSverre/brain)[ Packagist](https://packagist.org/packages/helgesverre/brain)[ Docs](https://github.com/helgesverre/brain)[ RSS](/packages/helgesverre-brain/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (8)Versions (15)Used By (0)

[![](art/brain.webp)](art/brain.webp)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a0341229df7d19bfbd6b62a4d11f9856284dfe3169dbba136ff1f2182f35680c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656c67657376657272652f627261696e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a0341229df7d19bfbd6b62a4d11f9856284dfe3169dbba136ff1f2182f35680c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656c67657376657272652f627261696e2e7376673f7374796c653d666c61742d737175617265) [![Total Downloads](https://camo.githubusercontent.com/6c106b15350f0f69d670faff5bb8f03061b9b4a520c657793de41a64b23a9f76/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656c67657376657272652f627261696e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/6c106b15350f0f69d670faff5bb8f03061b9b4a520c657793de41a64b23a9f76/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656c67657376657272652f627261696e2e7376673f7374796c653d666c61742d737175617265)

🧠 Brain - The Adorably Simple OpenAI Wrapper
============================================

[](#-brain---the-adorably-simple-openai-wrapper)

Just a small and simple wrapper around the OpenAI SDK.

---

📦 Installation
--------------

[](#-installation)

```
composer require helgesverre/brain
```

```
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
```

```
OPENAI_API_KEY="your-key-here"
OPENAI_REQUEST_TIMEOUT=60
```

🛠 Usage
-------

[](#-usage)

### 🔧 Basic Setup

[](#-basic-setup)

After installation, set up the Brain Facade in `config/app.php`:

```
'aliases' => [
    'Brain' => HelgeSverre\Brain\Facades\Brain::class,
],
```

### 📝 Example: Generating Blog Titles

[](#-example-generating-blog-titles)

Generate a list of blog titles about a given subject:

```
use HelgeSverre\Brain\Facades\Brain;

$subject = 'Technology';
$count = 5;

$titles = Brain::list("Suggest $count blog titles about '$subject'", 200);

foreach ($titles as $title) {
    echo $title . PHP_EOL;
}
```

### 📄 Example: Generating a Structured Blog Post

[](#-example-generating-a-structured-blog-post)

Generate a structured blog post in JSON format:

```
use HelgeSverre\Brain\Facades\Brain;

$title = 'The Future of Technology';
$style = 'informative';
$minWords = 500;

$response = Brain::slow()->json(classify($input, $categories);
```

This method evaluates 'banana' and categorizes it as one of the provided options.

#### Enum Classification

[](#enum-classification)

For structured categorization, use an Enum class.

```
use HelgeSverre\Brain\Facades\Brain;

enum Category: string {
    case Fruit = 'fruit';
    case Animal = 'animal';
    case Car = 'car';
}

$input = 'banana';

$result = Brain::fast()->classify($input, Category::class);
```

Here, 'banana' is classified into the most fitting Enum category.

### 📄 Example: Generating a Vector Embedding

[](#-example-generating-a-vector-embedding)

This method returns a vector embedding of the input, or a list of vector embeddings if you pass an array or a collection.

```
use HelgeSverre\Brain\Facades\Brain;

// Single embedding
$result = Brain::embedding('banana');
// Returns ['0.123', '0.456', '0.789' ....]

// Or, for multiple inputs:
$result = Brain::embedding(['banana', 'apple', 'orange']);
// Returns [['0.123', '0.456', '0.789' ....], ['0.123', '0.456', '0.789' ....], ['0.123', '0.456', '0.789' ....]]

// Or, for a collection of inputs:
$result = Brain::embedding(collect(['banana', 'apple', 'orange']));
// Returns [['0.123', '0.456', '0.789' ....], ['0.123', '0.456', '0.789' ....], ['0.123', '0.456', '0.789' ....]]
```

Changing the Base URL to use Together.AI, Mistral.AI, Perplexity.AI or other compatible API
-------------------------------------------------------------------------------------------

[](#changing-the-base-url-to-use-togetherai-mistralai-perplexityai-or-other-compatible-api)

You can change the base URL to use other compatible APIs by using the `usingTogetherAI`, `usingMistralAI`or `usingPerplexity` methods.

```
use HelgeSverre\Brain\Facades\Brain;

Brain::usingTogetherAI()->text('Hello, world!');
Brain::usingMistralAI()->text('Hello, world!');
Brain::usingPerplexity()->text('Hello, world!');
```

Or you can set the base URL directly, note that the API must be compatible with OpenAI's API, specifically the Chat completion endpoints.

Also note that JSON Mode (as used by `Brain::json`, `Brain::classify`, `Brain::embedding` and `Brain::list`) is not supported by all APIs.

```
use HelgeSverre\Brain\Facades\Brain;

Brain::baseUrl('api.example.com');
```

📖 Available Methods
-------------------

[](#-available-methods)

MethodParametersDescription`Brain::maxTokens()``int $maxTokens`Sets the maximum number of tokens (words) the AI response can contain.`Brain::temperature()``float $temperature`Sets the 'temperature' for the AI responses, influencing the randomness of the output.`Brain::apiKey()``string $apiKey`Sets the API Key to use for subsequent requests.`Brain::timeout()``int $timeout`Sets the request timeout in seconds.`Brain::fast()`*None*Sets the AI model to 'gpt-3.5-turbo-1106' for faster responses.`Brain::slow()`*None*Sets the AI model to 'gpt-4-1106-preview' for more detailed responses.`Brain::text()``$prompt, ?int $max = null`Sends a text prompt to the AI and returns a text response. Optionally set a custom maximum token limit for this request.`Brain::json()``$prompt, ?int $max = null`Sends a prompt to the AI and returns a response in JSON format. Optionally set a custom maximum token limit for this request.`Brain::list()``$prompt, ?int $max = null`Sends a prompt to the AI and returns a list of items in an array, useful for generating multiple suggestions or ideas. Optionally set a custom maximum token limit for this request.`Brain::classify()``$input, array|StringBackedEnum $categories`Classifies the given input text into one of the provided categories. Categories can be an array of strings or an Enum class.`Brain::embedding()``$input`Uses the `text-embedding-ada-002` model to generate an embedding vector, returns a single vector for string input, an array of vectors when passed an array or collection.`Brain::toText()``CreateResponse $response, $fallback = null`Converts an OpenAI `CreateResponse` object to a text string. Includes an optional fallback value.`Brain::toJson()``CreateResponse $response, $fallback = null`Converts an OpenAI `CreateResponse` object to a JSON object. Includes an optional fallback value.`Brain::usingTogetherAI()`*None*Uses the [Together.AI](https://www.together.ai/) API instead of OpenAI`Brain::usingMistralAI()`*None*Uses the [Mistral.AI](https://mistral.ai/) API instead of OpenAI`Brain::usingPerplexity()`*None*Uses the [Perplexity.AI](https://www.perplexity.ai/) API instead of OpenAI`Brain::usingGroq()`*None*Uses the [Groq](https://console.groq.com/playground) API instead of OpenAIUsing other providers (Mistral, Together, Perplexity, Groq etc)
---------------------------------------------------------------

[](#using-other-providers-mistral-together-perplexity-groq-etc)

```
use HelgeSverre\Brain\Facades\Brain;

Brain::apiKey("api-key-from-together")->usingTogetherAI()->text('Hello, world!');

Brain::apiKey("api-key-from-mistral")->usingMistralAI()->text('Hello, world!');

Brain::apiKey("api-key-from-perplexity")->usingPerplexity()->text('Hello, world!');

Brain::apiKey("api-key-from-groq")->usingGroq()->text('Hello, world!');
```

📜 License
---------

[](#-license)

This package is licensed under the MIT License. For more details, refer to the [License File](LICENSE.md).

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance58

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community8

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 ~51 days

Recently: every ~149 days

Total

14

Last Release

259d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/68f3958f40262d577ddc0596e4ba78b42c0409ebc7de948bab47edee392d5f68?d=identicon)[HelgeSverre](/maintainers/HelgeSverre)

---

Top Contributors

[![HelgeSverre](https://avatars.githubusercontent.com/u/1089652?v=4)](https://github.com/HelgeSverre "HelgeSverre (30 commits)")

---

Tags

aiwrapperopenaibrain

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/helgesverre-brain/health.svg)

```
[![Health](https://phpackages.com/badges/helgesverre-brain/health.svg)](https://phpackages.com/packages/helgesverre-brain)
```

###  Alternatives

[halilcosdu/laravel-slower

Laravel Slower: Optimize Your DB Queries with AI

40924.4k](/packages/halilcosdu-laravel-slower)[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

46784.5k5](/packages/deepseek-php-deepseek-php-client)[helgesverre/extractor

AI-Powered Data Extraction for your Laravel application.

22233.1k](/packages/helgesverre-extractor)[helgesverre/mistral

Laravel Client for the Mistral.ai API

5516.5k1](/packages/helgesverre-mistral)

PHPackages © 2026

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