LaravelPackages.net
Acme Inc.
Toggle sidebar
webboy/laravel-open-ai-api-client

Laravel package adapting PHP OpenAI API Client

10
5
1.0.5
About webboy/laravel-open-ai-api-client

webboy/laravel-open-ai-api-client is a Laravel package for laravel package adapting php openai api client. It currently has 5 GitHub stars and 10 downloads on Packagist (latest version 1.0.5). Install it with composer require webboy/laravel-open-ai-api-client. Discover more Laravel packages by webboy or browse all Laravel packages to compare alternatives.

Last updated

Laravel OpenAi API Client

Community-maintained Laravel package adapting PHP OpenAI API Client

Note: This is not an official Laravel Package

Installation

Intsall the package using composer

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

Then publish the configuration file

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

Add OPENAI_API_KEY={your OpenAI api key} to the .env file

That's it, you are all set up

Usage examples

An example Laravel Artisan command that uses OpenAIChat library:

<?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']);
    }
}

Use the facade OpenAIClient static methods to access all endpoints of thh 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

Refer to the official API reference for more details.

Comments