OpenAI davinci 在给出任何提示时返回随机代码废话

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

我正在尝试使用 C++ 和 openAI davinci 在控制台中创建一个聊天机器人。问题是,无论何时给出任何提示,openAI 都会返回如下内容:

d flags with conflicting types: \\n\\t{}\\n\\t{}\\n\\t{}'\n                        print(sep.join([line, prevLine, nextLine]))\n                    prevLine = line\n                    continue\n            else:\n                if inFlag == 2:\n                    if isParameter:\n                        params = set(returnValues)\n                        if (not gSSA.isActionRetTypeValid(code, params)):\n                           

显然,这不是我从提示“Hello openAI”中得到的任何期望......

我的密码是:

size_t write_string_callback(void *ptr, size_t size, size_t nmemb, std::string *stream)
{
    size_t bytes = size * nmemb;
    stream->append((char*)ptr, bytes);
    return bytes;
}

int main() {
    // Construct the HTTP request
    std::string url = "https://api.openai.com/v1/engines/davinci-codex/completions";
    std::string api_key = "API_KEY";
    std::string prompt = "No code";
    nlohmann::json payload = {
            {"prompt", prompt},
            {"max_tokens", 100},
            {"temperature", 1}
    };
    std::string payload_str = payload.dump();
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, ("Content-Type: application/json"));
    headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key).c_str());

    // Initialize the curl session
    CURL *curl = curl_easy_init();

    // Set the curl options
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload_str.c_str());
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    std::cout << payload_str.c_str() << std::endl;

    // Execute the request and get the response
    std::string response_string;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_string_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
    CURLcode res = curl_easy_perform(curl);

    // Cleanup the curl session
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    // Parse the JSON response and extract the response text
    if (res == CURLE_OK) {
        nlohmann::json response_json = nlohmann::json::parse(response_string);
        std::string response_text = response_json.dump(2);
        std::cout << "ChatGPT: " << response_text << std::endl;
    } else {
        std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
    }

    return 0;
}

有人知道这怎么会发生吗?

c++ curl libcurl openai-api chatgpt-api
© www.soinside.com 2019 - 2024. All rights reserved.