devdojo / ai
This is a package that simplifies communicating with AI services using PrismPHP
Requires
- php: ^7.4|^8.0
- illuminate/support: ^10.0|^11.0|^12.0
- prism-php/prism: ^0.85
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
README
This package provides you with a global ai() method to easily get responses from your favorite AI provider. Here are a few examples:
$response = ai('What is the meaning of life?');
Or capture the streamed response in a callback
$response = ai('What color is the sky?', function($chunk){ // $chunk will contain the stream text response });
Video Example
ai-functionality.mp4
Installation
You can install the package via composer:
composer require devdojo/ai
Usage
After you've installed the package, you'll want to add your AI Provider API Key. Out of the box DevDojo AI uses OpenAI, so you'll want to grab your OpenAI key and add it to your .env
OPENAI_API_KEY=sk-proj-aReAlLyLoNgKey...
After that, you are ready to use the ai method anywher in your app:
Route::get('question', function(){ echo ai('What is the meaning of life?'); });
You can also capture the streamed response, by passing a callback as the second argument.
public function submit($input) { $this->output = ai($input, function($chunk){ logger($chunk); }); }
This makes it super easy to return streamed responses in your Livewire components. In fact, this package offers two Single-File Volt component examples.
Examples
You can publish a few examples by running:
php artisan ai:install-examples
This will publish two component examples to your project.
- basic-example.blade.php (a basic example of sending a message and getting a streamed response back)
- chat-example.blade.php (a chat example to show you how to pass an array of messages to create a conversation)
This will allow you to include those example components anywhere in your application.
<livewire:basic-example /> <livewire:chat-example />
You can easily test this out by utilizing the Livewire Starter Kit, Installing this package, Adding your API Key to .env, and then pasting the following into the welcome.blade.php file:
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>AI Examples</title> @vite(['resources/css/app.css', 'resources/js/app.js']) </head> <body class="w-screen h-screen flex items-center bg-stone-100 justify-center"> <div class="w-full max-w-7xl gap-5 mx-auto flex items-stretch"> <div class="h-full flex-1 p-10 space-y-5 rounded-xl bg-white shadow-sm"> <h2 class="text-xl font-semibold">Basic Example</h2> <livewire:basic-example /> </div> <div class="h-full flex-1 mx-auto p-10 space-y-5 rounded-xl bg-white shadow-sm"> <h2 class="text-xl font-semibold">Chat Example</h2> <livewire:chat-example /> </div> </div> </body> </html>
Make sure your asset running is watching via composer dev
or npm run dev
.
Now you will be able to see the basic and chat example fully functioning and communicating with your favorite AI service.
ai-functionality.mp4
Configuration
If you would like to change the AI provider and the AI model, you can specify the following .env
variables:
AI_DEFAULT_PROVIDER=openai
AI_DEFAULT_MODEL=gpt-4
You can also publish the AI config file that will live at config/ai.php
:
<?php return [ /* |-------------------------------------------------------------------------- | AI Configuration |-------------------------------------------------------------------------- | | Configuration options for the DevDojo AI package. | */ 'default_provider' => env('AI_DEFAULT_PROVIDER', 'openai'), 'default_model' => env('AI_DEFAULT_MODEL', 'gpt-4'), ];
PrismPHP
This package doesn't really do much else besides providing you with an easy to use global ai() method. Most of the magic comes from PrismPHP, you may wish to abstract this into your own global ai() method. All providers that are available via Prism are also supported in this package.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email tony@devdojo.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.