PHP code example of cblink / openai

1. Go to this page and download the library: Download cblink/openai library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cblink / openai example snippets



// 传入配置
$config = [
    // openai的apikey
    'api_key' => '',
    // 请求的初始配置
    'guzzle' => [
        // 代理配置,如果没有的话可以不传入,配置参考 https://docs.guzzlephp.org/en/stable/request-options.html#proxy
        'proxy' => [],
    ],
];

$openai = new Cblink\ChatGPT\OpenAI($config);

// 查询可用的模型
$openai->models->lists();

// 创建对话
$openai->chat->create([
    'model' => 'gpt-3.5-turbo',
    'messages' => [
        ['role' => 'user', 'content' => 'hello!'],
    ],
]);

// 创建图片
$openai->image->create([
    'prompt' => 'A cute baby sea otter',
]);


// 未封装的接口请求

// get 请求
$openai->httpGet('v1/files')

// post 请求 ,参数: 路径,请求参数,文件
$openai->httpPost('v1/files', [
    'purpose' => 'fine-tune',
], [
    // 此处传入路径即可
    'file' => '/path/file'
])

// delete请求
$file_id = 'file-XjGxS3KTG0uNmNOK362iJua3';
$openai->httpDelete(sprintf('v1/files/%s', $file_id))