tnhnclskn/openai

OpenAI API Advanced for Chat Completions with Functions

0.0.2 2023-06-22 15:20 UTC

This package is auto-updated.

Last update: 2024-04-22 19:55:30 UTC


README

License Latest Version on Packagist Total Downloads

Tnhnclskn/OpenAI is a library that provides advanced chat completions with functions for OpenAI API.

Installation

You can install the library using Composer. Run the following command in your project's root directory:

$ composer require tnhnclskn/openai

Usage Chat

<?php

use Tnhnclskn\OpenAI\OpenAI;

require_once __DIR__ . '/vendor/autoload.php';

$client = new OpenAI($organizationId, $secretKey);
$chat = $client->chat('You are a helpful assistant.');

Usage with functions

// ...

use Tnhnclskn\OpenAI\Chat\BaseFunction;

class GetCurrentWeather extends BaseFunction
{
    public static function name(): string
    {
        return 'get_current_weather';
    }

    public static function description(): ?string
    {
        return 'Get the current weather in a given location';
    }

    public static function parameters(): array
    {
        return [
            static::parameter('location', 'string', 'The city and state, e.g. San Francisco, CA')->required(),
            static::parameter('unit', 'string')->enum(['celcius', 'fahrenheit']),
        ];
    }

    public function __construct(
        private string $location,
        private string $unit = 'farhenheit',
    ) {
    }

    public function handle(): string
    {
        return $this->json([
            "location" => $this->location,
            "temperature" => "72",
            "unit" => $this->unit,
            "forecast" => ["sunny", "windy"],
        ]);
    }
}

$chat->loadFunction(GetCurrentWeather::class);

$reply = $chat->prompt('What\'s the weather like in Boston?');
// Reply: The weather in Boston is currently 72°F and sunny with some winds.

Usage only prompt

// ...

$reply = $chat->prompt('What\'s the yellow flower?');
// Reply: The yellow flower is a common name that could refer to various types of flowers. Some examples include sunflowers, daffodils, marigolds, dandelions, or buttercups. Can you provide any additional information or description about the flower you are referring to?

Export messages for continue conversation

// Export messages
$messages = $chat->exportMessages();

// Import messages for continue conversation
$newChat = $openAI->chat('You are another helpful assistant.');
$newChat->importMessages($messages);

Configuration

// Configuration
$config = [
    'chat_model' => 'gpt-3.5-turbo-0613', // if you want to use function chat, you must use gpt-3.5-turbo-0613 model or higher
    'retries' => 3, // number of retries
    'retry_delay' => 1, // delay between retries in seconds
];

$client = new OpenAI($organizationId, $secretKey, $config);

License

This project is licensed under the MIT License. For more information, see the LICENSE file.

Contributing

If you want to contribute to this project, please read the CONTRIBUTING file.

Support

If you have any questions or need support, please use the GitHub Issues section.

Acknowledgements

This project was created with contributions from the following individuals:

Contact

Email: mail@tunahancaliskan.com