OpenAI Chat Completions API: 如果流参数设置为false,为什么我没有得到响应?

您可以在下方看到我的应用程序中的$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;}

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注