Chatgpt,ThinkPHP6使用openai api示例

TP6自行安装,本文只介绍openai api接口

1.composer安装open-ai

composer require orhanerday/open-ai

2.命名空间引入

use Orhanerday\OpenAi\OpenAi;

3.请求接口

OPENAI_API_KEY申请地址

$question = $this->request->param('question','');
$open_ai_key = "xxx";//你的key
$open_ai = new OpenAi($open_ai_key);
$open_ai->setProxy("http://127.0.0.1:2023");//本地调试代理
// 返回文本
$complete = $open_ai->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => [
        [
            "role" => "user",
            "content" => $question
        ],
    ],
    'temperature' => 1.0,
    'max_tokens' => 4000,
    'frequency_penalty' => 0,
    'presence_penalty' => 0,
]);

$returnData = json_decode($complete,true);
$this->success('返回成功', $returnData);

 使用示例

Chatgpt,ThinkPHP6使用openai api示例_第1张图片

 4.其它类型接口使用详见GitHub源码

// 返回图片
$complete = $open_ai->image([
    "prompt" => $question,
    "n" => 1,
    "size" => "256x256",
    "response_format" => "url",
]);

// 将音频转录成输入语言
$c_file = curl_file_create('./assets/img/CKwRIRwE9j-9ABqphgDaXVV7.m4a');
$result = $open_ai->transcribe([
    "model" => "whisper-1",
    "file" => $c_file,
]);

// 上传文件
$c_file = curl_file_create('./assets/img/openai.docx');
$result = $open_ai->uploadFile([
        "purpose" => "answers",
        "file" => $c_file,
]);

你可能感兴趣的:(chatgpt,人工智能)