<?php

namespace app\admin\controller\ChatGpt;

use app\Request;
use app\common\model\Flow\Flow as ModelFlow;
use think\db\Where;
use think\facade\Validate;
use app\BaseController;
use app\exception\ErrorMsg;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use Goutte\Client;
use Orhanerday\OpenAi\OpenAi;

class ChatGpt extends BaseController
{

    /**
     * chatGpt测试
     */
    public function Test(Request $request)
    {
        $api_key = '';
        $message = "问题内容";
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://api.openai.com/v1/completions',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Authorization: Bearer ' . $api_key
            ],
            CURLOPT_POSTFIELDS => json_encode([
                'prompt' => $message, //你的内容
                'model' => 'text-davinci-003', //机器人3代
                'max_tokens' => 4000, //最大字符串
                'temperature' => 0.5 //回答精准度
            ])
        ]);

        // 请求结束
        $domain = curl_exec($curl);
        curl_close($curl);
        //处理请求的数据
        $domain_array = json_decode($domain, true);
        $data = $domain_array['choices'][0]['text']; //该数组输出最后回答的内容F

        return [
            'code' => 0,
            'data' => $data
        ];
    }
}
