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

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

devdojo/ai
==========

This is a package that simplifies communicating with AI services using PrismPHP

v0.0.4(10mo ago)62.1kMITPHPPHP ^7.4|^8.0

Since Aug 21Pushed 10mo agoCompare

[ Source](https://github.com/thedevdojo/ai)[ Packagist](https://packagist.org/packages/devdojo/ai)[ Docs](https://github.com/devdojo/ai)[ RSS](/packages/devdojo-ai/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (4)Versions (5)Used By (0)

AI with PrismPHP
================

[](#ai-with-prismphp)

[![Latest Version on Packagist](https://camo.githubusercontent.com/55857c35548b1724167b419413b1414e630cccc4bff2a117d2b74f7473cba726/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646576646f6a6f2f61692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/devdojo/ai)[![Total Downloads](https://camo.githubusercontent.com/6f7cf7b9b32721778650c6cc27aa44dce704c9f78ae4e2cf4cdc04418b95ece4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646576646f6a6f2f61692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/devdojo/ai)

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
-------------

[](#video-example)

    ai-functionality.mp4    Installation
------------

[](#installation)

You can install the package via composer:

```
composer require devdojo/ai
```

Usage
-----

[](#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
--------

[](#examples)

You can publish a few examples by running:

```
php artisan ai:install-examples

```

This will publish two component examples to your project.

1. **basic-example.blade.php** (a basic example of sending a message and getting a streamed response back)
2. **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.

```

```

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>

        AI Examples
        @vite(['resources/css/app.css', 'resources/js/app.js'])

                Basic Example

                Chat Example

```

**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
-------------

[](#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`:

```
