webboy/laravel-open-ai-api-client

Laravel 包,适配 PHP OpenAI API 客户端

1.0.5 2023-03-21 15:56 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:31:21 UTC


README

社区维护的 Laravel 包,适配 PHP OpenAI API 客户端

注意:这不是官方的 Laravel 包

安装

使用 composer 安装此包

composer require webboy/laravel-open-ai-api-client

然后发布配置文件

php artisan vendor:publish --tag=openai-config

OPENAI_API_KEY={your OpenAI api key} 添加到 .env 文件中

就绪,设置完成

用法示例

一个使用 OpenAIChat 库的 Laravel Artisan 命令示例

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use OpenAIClient;

class OpenAIChatCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open-ai:chat-answer {question}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Answers a question using ChatGPT Chat API endpoint';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $question = $this->argument('question');

        $data['model']      = 'gpt-3.5-turbo';
        $data['messages'] = [
            [
                'role'      => 'system',
                'content'   => 'You are a good and all knowing assistant'
            ],
            [
                'role'      => 'user',
                'content'   => $question
            ]
        ];

        $response = OpenAIClient::chat()->create($data);

        $this->info($response['choices'][0]['message']['content']);
    }
}

使用外观 OpenAIClient 静态方法来访问 OpenAI 的所有端点

OpenAIClient::audio(): OpenAIAudio
OpenAIClient::chat(): OpenAIChat
OpenAIClient::completions(): OpenAICompletions
OpenAIClient::edits(): OpenAIEdits
OpenAIClient::embeddings(): OpenAIEmbeddings
OpenAIClient::files(): OpenAIFiles
OpenAIClient::fineTunes(): OpenAIFineTunes
OpenAIClient::images(): OpenAIImages
OpenAIClient::models(): OpenAIModels
OpenAIClient::moderations(): OpenAIModerations

有关详细信息,请参阅 官方 API 参考