PHPackages                             christopherarter/dream - 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. christopherarter/dream

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

christopherarter/dream
======================

The AI/ML toolbox for Laravel

0.3.0(3y ago)182265MITPHPPHP ^8.1

Since Oct 31Pushed 3y ago3 watchersCompare

[ Source](https://github.com/christopherarter/dream)[ Packagist](https://packagist.org/packages/christopherarter/dream)[ Docs](https://github.com/christopherarter/dream)[ RSS](/packages/christopherarter-dream/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (7)Used By (0)

[![title](.github/images/dream-logo.png)](.github/images/dream-logo.png)

The AI/ML Toolbox for Laravel
=============================

[](#the-aiml-toolbox-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0372afb4e776967ff3badbb6dcd263a479d27fcc88725c052b871fdae0208458/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368726973746f7068657261727465722f647265616d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/christopherarter/dream)[![Total Downloads](https://camo.githubusercontent.com/f38e75051c1c8722485a5d9aed40d44ef8249c9e8e1647a539589951c7ec6750/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368726973746f7068657261727465722f647265616d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/christopherarter/dream)[![GitHub Actions](https://github.com/christopherarter/dream/actions/workflows/main.yml/badge.svg)](https://github.com/christopherarter/dream/actions/workflows/main.yml/badge.svg)

Dream is a package for Laravel that brings common AI/ML tools into your Laravel application without all the boilerplate.

It currently supports:

- **OpenAI**
- **AWS Comprehend**

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

[](#requirements)

- PHP 8.1
- Laravel 9

Getting Started
---------------

[](#getting-started)

### Installation

[](#installation)

You can install the package via composer:

```
composer require christopherarter/dream
```

Next, publish the vendor config file:

```
php artisan vendor:publish --provider="Dream\DreamServiceProvider"
```

### Authentication

[](#authentication)

#### OpenAI

[](#openai)

```
DREAM_OPENAI_API_KEY=your-api-key
```

#### AWS

[](#aws)

To use AWS Comprehend, you can add your AWS credentials in your `.env` file. **Note: the user must have access to the AWS Comprehend service.**

```
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
```

---

Usage
-----

[](#usage)

To use this package, you can rely on the `Dream` facade. This facade will automatically use the default driver you have set in your `config/dream.php` file.

```
use Dream\Facades\Dream;
```

### Sentiment Analysis

[](#sentiment-analysis)

Sentiment analysis is the process of determining whether a piece of writing is positive, negative, or neutral. It's also known as opinion mining, deriving the opinion or attitude of a speaker. Dream includes several helper methods to make it easy to use sentiment analysis.

Example:

```
use Dream\Facades\Dream;

$sentiment = Dream::text('I love Laravel!')->sentiment();
$sentiment->disposition(); // 'positive';
$sentiment->positive(); // true;
```

Available Methods:

```
use Dream\Facades\Dream;

$sentiment->disposition(); // 'positive' | 'negative' | 'neutral';
$sentiment->positive(); // true | false;
$sentiment->negative(); // true | false;
$sentiment->neutral(); // true | false;
```

### Entity Extraction

[](#entity-extraction)

Entity extraction is the process of detecting and classifying key information from text and other unstructured data sources. It's also known as named entity recognition (NER).

```
use Dream\Facades\Dream;

$entities = Dream::text('I need a reservation for Mr. Foo and Mr. Bar at
the Foo Bar Restaurant on October 31st.')
->entities();

$entities->people()->toArray(); // ['Mr. Foo', 'Mr. Bar'];
$entities->places()->toArray(); // ['Foo Bar Restaurant'];
$entities->dates()->toArray(); // ['October 31st'];
```

Available Methods:

```
$entities->people(); // Collection of people
$entities->places(); // Collection of places
$entities->dates(); // Collection of dates
$entities->organizations(); // Collection of organizations
$entities->events(); // Collection of events
$entities->products(); // Collection of products
$entities->quantities(); // Collection of quantities
$enteties->other(); // Collection of other entities
```

### Key Phrase Extraction

[](#key-phrase-extraction)

Key phrase extraction is the process of identifying the most important phrases in a block of text. Dream includes the ability to extract key phrases from a string.

```
use Dream\Facades\Dream;

Dream::text('Laravel is a web application framework with expressive,
elegant syntax. We’ve already laid the foundation — freeing you to create
without sweating the small things.')
  ->phrases()
  ->pluck('text')
  ->toArray();

// [
//   "Laravel",
//   "a web application framework",
//   "expressive, elegant syntax",
//   "the foundation —",
//   "the small things",
// ]
```

### Language Detection

[](#language-detection)

Language detection is the process of identifying the language of a given text. Dream includes the ability to detect the language of a string.

```
use Dream\Facades\Dream;

Dream::text('¿Cuál es tu película favorita?')->language(); // 'es'
```

The language code will be the [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code for the language. The return type is a value backed enum `Dream\Enums\Language` to ensure consistency across clients.

### Image Text Detection

[](#image-text-detection)

Dream can detect the text inside of an image. To do this, we'll use the `imageText()` method.

```
use Dream\Facades\Dream;

$file = Storage::get('image.jpg');
Dream::image($file)
    ->text()
    ->pluck('text')
    ->toArray();

// ["This was text in an image"]
```

*Note: Currently only available using AWS*

### Image Label Detection

[](#image-label-detection)

Dream can determine labels for an image using the `imageLabels()` method.

```
use Dream\Facades\Dream;

$file = Storage::get('image.jpg');
Dream::image($file)
    ->labels()
    ->pluck('name')
    ->toArray();

// ["man", "fish", "boat", "water", "ocean", "sea"];
```

*Note: Currently only available using AWS*

---

Clients
-------

[](#clients)

### AWS Comprehend

[](#aws-comprehend)

#### IAM Configuration

[](#iam-configuration)

The AWS user you provide will need to have access to the Comprehend service.

Here's a policy example:

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "comprehend:*",
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}
```

If you'd like to strictly adhere to [least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege), you can limit the Actions in the role to just the ones you need. For example, if you only need to use the `entities` method, you can limit the Actions to just `comprehend:DetectEntities`.

#### AWS Rekognition

[](#aws-rekognition)

To use the image detection methods, you'll need to enable the [AWS Rekognition](https://aws.amazon.com/rekognition/) service for your user as well. You can add this to the example policy above:

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "comprehend:*",
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "rekognition:*"
            ],
            "Resource": "*"
        }
    ]
}
```

#### Environment Variables

[](#environment-variables)

By default, Dream will use the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables to authenticate with AWS Comprehend. If you would like to specify a different AWS key &amp; secret to use with Dream, you can do so by setting the keys with the `DREAM_` prefix.

For example,

```
DREAM_AWS_ACCESS_KEY_ID=your-key
DREAM_AWS_SECRET_ACCESS_KEY=your-secret
```

### Azure Cognitive Services

[](#azure-cognitive-services)

Coming soon :)

---

### Building Custom Clients

[](#building-custom-clients)

If you would like to build your own client, or contribute to this project by adding another client from a different provider, you can do so by extending the base classes:

- `Dream\Clients\Client` - The base client class
- `Dream\Clients\TextClient` - The base text client class
- `Dream\Clients\ImageClient` - The base image client class

A client should have both image and text capabilities. These capabilities are handled in their own respective client classes.

```
