joacir/cake-open-a-i

OpenAI plugin for CakePHP

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0

Type:cakephp-plugin

1.0.1 2023-04-13 19:09 UTC

This package is not auto-updated.

Last update: 2024-05-10 22:25:43 UTC


README

CakeOpenAI plugin integrates CakePHP with OpenAI API using OpenAI API Client in PHP.

Install

Install it as require dependency:

composer require joacir/cake-open-a-i

Setup

Enable the plugin in your Application.php or call

bin/cake plugin load CakeOpenAI

Set the OpenAI credentials in your app_local.php:

'OpenAI' => [
    'apiKey' => '**************',
    'organizationID' => '***************',
];

Load a OpenAI Behavior in your Table initialize() method:

$this->addBehavior('CakeOpenAI.OpenAI');

Usage

Chat

To send messages to ChatGPT:

$messages = [
    [
        'role' => 'system',
        'content' => 'You are a helpful assistant.',
    ],
    [
        'role' => 'user',
        'content' => 'Who won the world series in 2020?',
    ],
    [
        'role' => 'assistant',
        'content' => 'The Los Angeles Dodgers won the World Series in 2020.',
    ],
    [
        'role' => 'user',
        'content' => 'Where was it played?',
    ],
];

$responses = $this->chat($messages);

echo $responses['choices'][0]['message']['content'];

Image

To create image with DALL-e:

$prompt = 'Darth Vader riding on a bike.';

$responses = $this->image($prompt);

echo $responses['data'][0]['url'];

Configurations

You can change the defaults configurations of chat and image creation, according to the OpenAI API Reference:

$this->setChatConfig([
    'model' => 'gpt-3.5-turbo',
    'temperature' => 1.0,
    'max_tokens' => 4000,
    'frequency_penalty' => 0,
    'presence_penalty' => 0,
]);

$this->setImageConfig([
    'n' => 1,
    'size' => '1024x1024',
    'response_format' => 'url',
]);