承接 tuzlu07x/openai 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tuzlu07x/openai

最新稳定版本:v1.0.0

Composer 安装命令:

composer require tuzlu07x/openai

包简介

OpenAI Call Function API PHP

README 文档

README

OpenAI Callfunction API PHP PACKAGE

Installation

composer require tuzlu07x/openai

Usage With Function

<?php

use Ftuzlu\OpenAI\BaseFunction;
use Ftuzlu\OpenAI\Client;
use Ftuzlu\OpenAI\OpenAI;

class Example extends BaseFunction
{
    public function __construct(
        private $location = 'Londra',
        private $unit = 'farhenheit',
    ) {
    }

    public function properties(): array
    {
        return $this->parameters();
    }
    public static function required(): array
    {
        return ['location'];
    }

    public static function model(): string
    {
        return 'gpt-3.5-turbo-0613';
    }

    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 type(): string
    {
        return 'object';
    }

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

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

$yourApiKey = 'XXXXXXXXXXXX';
$yourOrganization = 'XXXXXXXXXX';
$example = new Example();

$messages = [
    ["role" => "system", "content" => "Hello I am helper"],
];
$functions = [
    $example->function()
];
$client = new Client($yourApiKey, $yourOrganization);
$openAI = new OpenAI($example, $client);
$chat = $openAI->chat($functions, $messages, 'handle'); //handle is functionName on example
var_dump($chat->say('How is weather in London?'));

First step, You call Client class

<?php
    use Ftuzlu\OpenAI\Client;

    $yourAPIKey=XXXXXX;
    $yourOrganization=XXXXX;
    $baseUrl = 'https://api.openai.com/';
    $client = new Client($yourApiKey, $yourOrganization, $baseUrl);

Second Step

<?php
    use Ftuzlu\OpenAI\OpenAI;

   $example = new Example(); //Example is your Class
   $openAI = new OpenAI($example, $client);
   $chat = $openAI->chat($functions, $messages, 'handle'); //handle is also your class's functionName.

统计信息

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

GitHub 信息

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

其他信息

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