helgesverre / brain
The Adorably Simple OpenAI Wrapper
Requires
- php: ^8.2
- openai-php/laravel: ^v0.10.2
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.13
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
README
🧠 Brain - The Adorably Simple OpenAI Wrapper
Just a small and simple wrapper around the OpenAI SDK.
📦 Installation
composer require helgesverre/brain
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
OPENAI_API_KEY="your-key-here" OPENAI_REQUEST_TIMEOUT=60
🛠 Usage
🔧 Basic Setup
After installation, set up the Brain Facade in config/app.php
:
'aliases' => [ 'Brain' => HelgeSverre\Brain\Facades\Brain::class, ],
📝 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
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(<<<PROMPT Create an $style blog post with the title '$title'. Write over $minWords words. PROMPT ); echo "Title: " . $response['title'] . PHP_EOL; echo "Body: " . $response['body'] . PHP_EOL;
📄 Example: Text Classification with Arrays or Enums
Brain::classify
simplifies the categorization of text. You can classify text using either an array of options or an
Enum class.
Array Classification
Pass a list of categories as an array to classify your text.
use HelgeSverre\Brain\Facades\Brain; $input = 'banana'; $categories = ["bread", "animal", "car", "plane"]; $result = Brain::fast()->classify($input, $categories);
This method evaluates 'banana' and categorizes it as one of the provided options.
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
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
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
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
This package is licensed under the MIT License. For more details, refer to the License File.