Laravel 的 AI/ML 工具箱

0.3.0 2023-02-19 21:45 UTC

This package is auto-updated.

Last update: 2024-09-20 01:44:58 UTC


README

title

Laravel 的 AI/ML 工具箱

Latest Version on Packagist Total Downloads GitHub Actions

Dream 是一个为 Laravel 提供常用 AI/ML 工具的包,无需所有样板代码。

它目前支持

  • OpenAI
  • AWS Comprehend

需求

  • PHP 8.1
  • Laravel 9

入门

安装

您可以通过 composer 安装此包

composer require christopherarter/dream

接下来,发布供应商配置文件

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

身份验证

OpenAI

DREAM_OPENAI_API_KEY=your-api-key

AWS

要使用 AWS Comprehend,您可以在 .env 文件中添加您的 AWS 凭据。 注意:用户必须有权访问 AWS Comprehend 服务。

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

用法

要使用此包,您可以使用 Dream 门面。此门面将自动使用您在 config/dream.php 文件中设置的默认驱动程序。

use Dream\Facades\Dream;

情感分析

情感分析是确定一段文字是积极、消极还是中性的过程。它也被称为意见挖掘,提取说话者的意见或态度。Dream 包含多个辅助方法,使情感分析变得简单。

示例

use Dream\Facades\Dream;

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

可用方法

use Dream\Facades\Dream;

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

实体提取

实体提取是从文本和其他非结构化数据源中检测和分类关键信息的过程。它也称为命名实体识别 (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'];

可用方法

$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

关键词提取

关键词提取是识别文本块中最重要短语的过程。Dream 包含从字符串中提取关键词的能力。

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",
// ]

语言检测

语言检测是识别给定文本的语言的过程。Dream 包含检测字符串语言的能力。

use Dream\Facades\Dream;

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

语言代码将是该语言的 ISO 639-1 代码。返回类型是受支持的枚举 Dream\Enums\Language,以确保客户端之间的连贯性。

图像文本检测

Dream 可以检测图像中的文本。为此,我们将使用 imageText() 方法。

use Dream\Facades\Dream;

$file = Storage::get('image.jpg');
Dream::image($file)
    ->text()
    ->pluck('text')
    ->toArray();
    
// ["This was text in an image"]

注意:目前仅通过 AWS 提供

图像标签检测

Dream 可以使用 imageLabels() 方法确定图像的标签。

use Dream\Facades\Dream;

$file = Storage::get('image.jpg');
Dream::image($file)
    ->labels()
    ->pluck('name')
    ->toArray();
    
// ["man", "fish", "boat", "water", "ocean", "sea"];

注意:目前仅通过 AWS 提供

客户端

AWS Comprehend

IAM 配置

您提供的 AWS 用户需要有权访问 Comprehend 服务。

以下是一个策略示例

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

如果您想严格遵循 最小权限原则,您可以限制角色的操作仅限于您需要的操作。例如,如果您只需要使用 entities 方法,则可以将操作限制为仅 comprehend:DetectEntities

AWS Rekognition

要使用图像检测方法,您还需要为您的用户启用 AWS Rekognition 服务。您可以将此添加到上面的示例策略中

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

环境变量

默认情况下,Dream 将使用 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 环境变量通过 AWS Comprehend 进行身份验证。如果您想为 Dream 指定不同的 AWS 密钥和密钥,可以通过设置以 DREAM_ 前缀的键来完成。

例如,

DREAM_AWS_ACCESS_KEY_ID=your-key
DREAM_AWS_SECRET_ACCESS_KEY=your-secret

Azure 认知服务

即将推出 :)

构建自定义客户端

如果您想构建自己的客户端,或者通过添加来自不同提供商的另一个客户端来为此项目做出贡献,您可以通过扩展基类来实现。

  • Dream\Clients\Client - 基础客户端类
  • Dream\Clients\TextClient - 基础文本客户端类
  • Dream\Clients\ImageClient - 基础图像客户端类

客户端应具备图像和文本功能。这些功能在其各自的客户端类中处理。

<?php

use Dream\Clients\Client;
use Dream\Collections\TextEntityCollection;
use Illuminate\Support\Collection

class MyCustomClient extends Client
{
    public function text(string $text): MyCustomTextClient
    {
        return new MyCustomTextClient($text);
    }
    
    public function image(string $image): MyCustomImageClient
    {
        return new MyCustomImageClient($image);
    }
}

接下来,您需要将您的客户端添加到 config/dream.php 文件中。

<?php

return [
    'connections' => [
        // ...
        'my-custom-client' => [
            'driver' => MyCustomClient::class,
        ],   
    ],
];

最后,您需要在您的环境中将客户端设置为默认驱动程序。以下是在您的 .env 文件中的示例:

DREAM_DRIVER=my-custom-client

测试

composer test

路线图

  • 添加 OCR 和图像识别
  • 添加对 OpenAI 的支持
  • 添加对 Google 自然语言的支持
  • 添加对使用 Rubix ML 的自定义本地模型的支持

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 chris@arter.dev 联系,而不是使用问题跟踪器。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件