ChatGPT API 问题或 jQuery - 未检索完整响应

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

我有一些正在分析内容的javascript,它最初生成内容的内容分析,然后添加一个按钮来“编写内容”关于内容分析。

我的问题是分析作品的第一部分,但它只生成前 50 个单词左右并被切断。当我点击写入内容按钮时,它似乎生成了接下来的 50 个单词,但实际上并没有按照提示执行。

我做错了什么?我的 API 调用正确吗?

<script>
function fetchChatGPTResponse(content, postNo) {
        $.ajax({
            url: 'chatgpt_api.php',
            method: 'POST',
            data: {
                prompt: 'write a content analysis based on this: ' + content // Use the post content as the prompt
            },
            success: function(response) {
                // Display the content analysis in the response div
                document.getElementById('response' + postNo).innerHTML = response + `<br><button onclick="fetchChatGPTArticle('${encodeURIComponent(response)}', '${postNo}');">Write Content</button>`;
    
                    
            },
            error: function() {
                document.getElementById('response').innerText = "Error";
            }
        });
    }
    
    function fetchChatGPTArticle(article, postNo2) {
        $.ajax({
            url: 'chatgpt_api.php',
            method: 'POST',
            data: {
                prompt: 'Based on this content analysis pasted after this statement, could you write me an article to reinforce this statement in 1000 words, ensure that it is engaging. Don\'t include any stats about upvotes and comments: ' + decodeURIComponent(article)
            },
            success: function(article) {
                // Display the generated article in the article div
                document.getElementById('article' + postNo2).innerHTML = article;
            },
            error: function() {
                document.getElementById('article' + postNo2).innerText = "Error";
            }
        });
    }
        </script>

chatgpt_api.php:

<?php
$api_key = 'XXXX';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['prompt'])) {
    $prompt = $_POST['prompt'];

    $data = [
        'model' => 'gpt-3.5-turbo',
        'messages' => [['role' => 'user', 'content' => $prompt]],
        'max_tokens' => 100
    ];

    $headers = [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key,
    ];

    $ch = curl_init('https://api.openai.com/v1/chat/completions');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $response = curl_exec($ch);
    curl_close($ch);

    $responseData = json_decode($response, true);
    $message = $responseData['choices'][0]['message']['content'] ?? 'No response from ChatGPT.';

    echo $message; // Return the response to the AJAX call
}
?>
javascript openai-api chatgpt-api
1个回答
0
投票

您记下的大约 50 个单词是有意义的,删除 API 调用正文中的“max_tokens”限制器,您应该会很好

$data = [
        'model' => 'gpt-3.5-turbo',
        'messages' => [['role' => 'user', 'content' => $prompt]],
];
© www.soinside.com 2019 - 2024. All rights reserved.