定制 tnhnclskn/openai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tnhnclskn/openai

最新稳定版本:0.0.2

Composer 安装命令:

composer require tnhnclskn/openai

包简介

OpenAI API Advanced for Chat Completions with Functions

README 文档

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

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固