tnhnclskn/openai

支持函数的 OpenAI API 高级聊天完成库

0.0.2 2023-06-22 15:20 UTC

This package is auto-updated.

Last update: 2024-09-22 20:51:17 UTC


README

License Latest Version on Packagist Total Downloads

Tnhnclskn/OpenAI 是一个库,为 OpenAI API 提供高级的带有函数的聊天完成功能。

安装

您可以使用 Composer 安装此库。在项目根目录中运行以下命令

$ composer require tnhnclskn/openai

聊天使用

<?php

use Tnhnclskn\OpenAI\OpenAI;

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

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

使用函数

// ...

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.

仅提示使用

// ...

$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
$messages = $chat->exportMessages();

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

配置

// 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);

许可证

本项目采用 MIT 许可证。更多信息请参阅LICENSE 文件。

贡献

如果您想为此项目做出贡献,请阅读CONTRIBUTING 文件。

支持

如果您有任何问题或需要支持,请使用GitHub Issues 部分。

致谢

以下个人为此项目的创建做出了贡献

联系方式

电子邮件: mail@tunahancaliskan.com