ahmedash95/sentimento

一个laravel包,使用OpenAI的GPT提供情感分析功能,轻松分析基于文本的数据,并深入了解潜在的语义。

0.0.2 2023-03-10 19:57 UTC

This package is auto-updated.

Last update: 2024-09-10 23:30:34 UTC


README

一个laravel包,使用OpenAI的GPT提供情感分析功能,轻松分析基于文本的数据,并深入了解潜在的语义。

Laravel Version GitHub Workflow Status (master) Total Downloads Latest Version License

logo

use Ahmedash95\Sentimento\Facade\Sentimento;

$result = Sentimento::analyze("Food was great!");

$result->value; // Positive

安装

  • 首先,您需要使用composer安装此包
composer require ahmedash95/sentimento
  • (可选)发布配置文件
php artisan vendor:publish --tag="sentimento-config"
  • 将您的OpenAI API密钥添加到.env文件中
SENTIMENTO_OPENAI_TOKEN=sk-xxxxxxxxxxxxxxxxxxxx

现在您可以开始使用此包了

使用方法

OpenAI可以理解多种语言,使用它就像调用一个函数一样简单。

use Ahmedash95\Sentimento\Facade\Sentimento;

Sentimento::analyze("Food was great!"); // Positive

Sentimento::analyze("Food was bad!"); // Negative

Sentimento::analyze("لما اتصلت بالدليفري قالي انه اكل الاوردر"); // Negative

⚠️⚠️⚠️ 当调用OpenAI分析文本时,如果请求失败,可能会发生HTTP异常。这可能是由多种原因造成的,例如网络连接问题、服务器错误,甚至OpenAI施加的速率限制策略。如果请求失败,包可能无法正确分析给定文本的情感,并可能导致意外的错误或输出。为了处理这种情况,重要的是要捕获并处理可能发生的任何潜在的HTTP异常。

use Ahmedash95\Sentimento\Facade\Sentimento;
use Illuminate\Http\Client\RequestException;

try {
    $result = Sentimento::analyze("Food was great!");
} catch (RequestException $e) {
    // $e->getMessage()
}

或者您可以在配置文件中将report_failures选项设置为false以禁用错误报告

// instead of exception, the result will be Decision::Unknown

$result = Sentimento::analyze("Food was great!"); // Unknown

测试

composer test

贡献

请随意fork此包,并通过提交pull request来增强功能。

致谢

许可证

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