无法使用 Ajax 将内容变量从 chatGBT 打印到屏幕

问题描述 投票:0回答:0

我有一个叫get.php的文件,里面用的是chatGBT小弟。代码运行顺利。但是,我在下面编写的 ajax 代码不会将 content 值打印到屏幕上。

我的 Ajax 代码显示此错误:

TypeError: undefined is not an object (evaluating '响应[0].选择[0]')

$(document).on("click", ".getAnswer", function() {
        var aiQuest = $(".askMe").val();
        var data = 'q=' + aiQuest;
        $.ajax({
            type: "POST",
            url: siteurl + 'req/get.php',
            data: data,
            data: "JSON",
            cache: false,
            beforeSend: function() {

            },
            success: function(response) { 
                alert(response[0].choices[0].delta.content);
            }
        });
    });

get.php 文件给出以下输出。

data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Hello"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"!"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" How"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" can"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" I"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" assist"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" you"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" today"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"?"},"index":0,"finish_reason":null}]}


data: {"id":"chatcmpl-70afKp2DGigVMKUqVrmaxxdMyyKUG","object":"chat.completion.chunk","created":1680375298,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}

data: [DONE]

这里还有 PHP get.php 回显行:

$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{
            echo $data;
            ob_flush();
            flush();
            return strlen($data);
        }
    },
];
javascript ajax
© www.soinside.com 2019 - 2024. All rights reserved.