php - OpenAI ChatGPT (GPT-3.5) API : Why am I not getting a response if the stream parameter is set to false?

标签 php openai-api chatgpt-api

您可以在下面的我的应用程序中看到 $prompt 值。当我输入此提示值时,chatGPT 不会给出结果。但这是因为参数中的 "stream"=> false 。如果"stream"=> true,chatGPT 会给出结果。

我的问题是为什么当 "stream"=> false 时 chatGPT 不给出结果。以及如何做才能产生结果。

$API_KEY = "API_KEY_HERE";

$model = 'gpt-3.5-turbo';
$header = [
    "Authorization: Bearer " . $API_KEY,
    "Content-type: application/json",
];

$temperature = 0.6;
$frequency_penalty = 0;
$presence_penalty= 0;
$prompt = 'What can you help me with? For example: What do you suggest to keep me motivated?';
 

$messages = array(
    array(
        "role" => "system",
        "content" => "Your name is 'JOHN DOE'. I want you to act as a motivational coach. I will provide you with some information about someone's goals and challenges, and it will be your job to come up with strategies that can help this person achieve their goals. This could involve providing positive affirmations, giving helpful advice or suggesting activities they can do to reach their end goal. if you don't understand the question, don't think too much, tell the user to be more specific with more details"
        ),
    array(
        "role" => "assistant",
        "content" => "Hello, I'm JOHN DOE, and I'm a motivational coach who loves helping people find their drive and achieve their goals. With years of experience in coaching and personal development, I've developed a unique approach to motivation that combines mindset, energy, and action."
    ),
    array(
        "role" => "user",
        "content" => $prompt
    )
);
//Turbo model
$isTurbo = true;
$url = "https://api.openai.com/v1/chat/completions";
$params = json_encode([
    "messages" => $messages,
    "model" => $model,
    "temperature" => $temperature,
    "max_tokens" => 1024,
    "frequency_penalty" => $frequency_penalty,
    "presence_penalty" => $presence_penalty,
    "stream" => false
]);

$curl = curl_init($url);
$options = [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_POSTFIELDS => $params,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_WRITEFUNCTION => function($curl, $data) {
        //echo $curl;
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ($httpCode != 200) {
            $r = json_decode($data);
            echo 'data: {"error": "[ERROR]","message":"'.$r->error->message.'"}' . PHP_EOL;
        } else {
            $trimmed_data = trim($data); 
            if ($trimmed_data != '') {
                $response_array = json_decode($trimmed_data, true);
                $content = $response_array['choices'][0]['message']['content'];
                echo $content;
                ob_flush();
                flush();
            }
        }
        return strlen($data);
    },
];

curl_setopt_array($curl, $options);
$response = curl_exec($curl);

if ($response === false) {
    echo 'data: {"error": "[ERROR]","message":"'.curl_error($curl).'"}' . PHP_EOL;
}else{

}

最佳答案

如果设置 "stream"=> false 则得不到响应的原因是,整个代码设计为当 Stream 参数设置为 true

通过以下修改,响应将作为一个整体进行处理,无论 stream 参数的值如何。

试试这个:

$API_KEY = "API_KEY_HERE";

$model = 'gpt-3.5-turbo';
$header = [
    "Authorization: Bearer " . $API_KEY,
    "Content-type: application/json",
];

$temperature = 0.6;
$frequency_penalty = 0;
$presence_penalty= 0;
$prompt = 'What can you help me with? For example: What do you suggest to keep me motivated?';

$messages = array(
    array(
        "role" => "system",
        "content" => "Your name is 'JOHN DOE'. I want you to act as a motivational coach. I will provide you with some information about someone's goals and challenges, and it will be your job to come up with strategies that can help this person achieve their goals. This could involve providing positive affirmations, giving helpful advice or suggesting activities they can do to reach their end goal. if you don't understand the question, don't think too much, tell the user to be more specific with more details"
        ),
    array(
        "role" => "assistant",
        "content" => "Hello, I'm JOHN DOE, and I'm a motivational coach who loves helping people find their drive and achieve their goals. With years of experience in coaching and personal development, I've developed a unique approach to motivation that combines mindset, energy, and action."
    ),
    array(
        "role" => "user",
        "content" => $prompt
    )
);

$url = "https://api.openai.com/v1/chat/completions";

$params = json_encode([
    "messages" => $messages,
    "model" => $model,
    "temperature" => $temperature,
    "max_tokens" => 1024,
    "frequency_penalty" => $frequency_penalty,
    "presence_penalty" => $presence_penalty,
    "stream" => false
]);

$curl = curl_init($url);
$options = [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_POSTFIELDS => $params,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 0,
];

curl_setopt_array($curl, $options);
$response = curl_exec($curl);

if ($response === false) {
    echo 'data: {"error": "[ERROR]","message":"'.curl_error($curl).'"}' . PHP_EOL;
}else{
    $response_array = json_decode($response, true);
    $content = $response_array['choices'][0]['message']['content'];
    echo $content;
}

关于php - OpenAI ChatGPT (GPT-3.5) API : Why am I not getting a response if the stream parameter is set to false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76100086/

相关文章:

php - 用 Vim 高亮代码块背景

openai-api - 无法在 vscode 中导入 ChatOpenAI,因为 .chat_models 不可用

openai-api - 如何在 LlamaIndex 中支持 OpenAI 的 Chat Completions API 格式?

c# - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Bad Request" when adding multiple questions

python - 如何处理KeyError : 'choices' with chatGPT?

PHP 会跳过检查并插入数据,即使数据已经存在

php - 将参数中的空格传递给php文件

openai-api - 如何记住来自不同 ChatCompletion 实例的消息

python - OpenAI ChatGPT (GPT-3.5) API 错误 429 : "You exceeded your current quota, please check your plan and billing details"

php - 为什么 in_array 函数在查找空字符串时返回 TRUE?