capevace/laravel-gpt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

capevace/laravel-gpt

Composer 安装命令:

composer require capevace/laravel-gpt

包简介

A Laravel package for interacting with OpenAI's GPT-3.

README 文档

README

laravel-gpt

This package provides a type-safe interface for making requests to the GPT-3 API.

Latest Version on Packagist GitHub Tests Action Status Total Downloads

use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Name a thing that is blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

echo $response->first(); // "The sky"

Installation

You can install the package via composer:

composer require capevace/laravel-gpt

Configuration

You will need an API key for the OpenAI GPT-3 API. Once you have obtained an API key, you can configure it in your .env file by adding the following line:

OPENAI_API_KEY=your-api-key-here

You could also publish the config file directly, but this really probably isn't necessary:

php artisan vendor:publish --tag="laravel-gpt-config"

Usage

The Capevace\GPT\GPTService class provides methods for making requests to the GPT-3 API. You can inject it into controllers or use the Facade to access the container.

# Access via injection

use Capevace\GPT\GPTService;

class MyController extends Controller {
    protected GPTService $gpt;

    public function __construct(GPTService $gpt) {
        $this->gpt = $gpt;
    }

    public function index() {
        $this->gpt->generate(
            // ..
        );
    }

}

# Access via Facade

use Capevace\GPT\Facades\GPT;

GPT::generate(/* .. */);

GPT::generate(<prompt>, [...options])

The generate method returns a GPTResponse object that contains the response from the GPT-3 API. If no text is returned (empty string), the method will throw an error.

generate takes the following arguments:

  • prompt (required): the prompt to send to the GPT-3 API
  • model: the GPT-3 model to use (defaults to text-davinci-003)
  • temperature: a value between 0 and 1 that determines how "creative" the response will be (defaults to 0.83)
  • maxTokens: the maximum number of tokens (i.e., words) to return in the response (defaults to 1200)
  • stop: a string that, when encountered in the response, will cause the response to end (defaults to null)
  • frequencyPenalty: a value between 0 and 1 that determines how much the model will penalize frequent words (defaults to 0.11)
  • presencePenalty: a value between 0 and 1 that determines how much the model will penalize words that don't appear in the prompt (defaults to 0.03)

Example

use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Generate a list of things that are blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

Handling responses

The generate method returns a GPTResponse object that contains the response from the GPT-3 API.

It has two methods:

  • $response->first() (string): returns the first text suggested by GPT-3
  • $response->all() (array): returns a list of all the text choices suggested by GPT-3

Example

use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Name a thing that is blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

$firstChoice = $response->first(); // "the sky"

$allChoices = $response->all(); // ["the sky", "the ocean" ...]

Error handling

If an error occurs while making a request to the GPT-3 API, the generate() method will throw a Capevace\GPT\Support\GPTException exception.

laravel-gpt will also throw an error, if a response does not contain any text (empty string).

Example

use Capevace\GPT\Facades\GPT;
use Capevace\GPT\Support\GPTException;
use Exception;

try {
    $response = GPT::generate('Do nothing.');
} catch (GPTException $e) {
    // Exception will be thrown, as the response text is ""
} catch (Exception $e) {
    // Catch connectivity errors etc.
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

capevace/laravel-gpt 适用场景与选型建议

capevace/laravel-gpt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131 次下载、GitHub Stars 达 12, 最近一次更新时间为 2022 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「ai」 「GPT-3」 「openai」 「capevace」 「gpt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 capevace/laravel-gpt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 capevace/laravel-gpt 我们能提供哪些服务?
定制开发 / 二次开发

基于 capevace/laravel-gpt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 131
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-13