要在 C++ 中调用 ChatGPT API,您可以使用以下步骤:
以下是 C++ 调用 ChatGPT API 的示例代码(使用 libcurl 库):
#include
#include
int main() {
CURL* curl = curl_easy_init();
if (curl) {
const char* url = "https://api.openai.com/v1/engines/davinci-codex/completions";
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YOUR_API_SECRET_KEY");
const char* json_data = "{"prompt": "Hello, how are you?", "temperature": 0.7, "max_tokens": 100}";
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK) {
long status_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
if (status_code == 200) {
std::string response;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
std::cout << response << std::endl;
} else {
std::cerr << "HTTP Error: " << status_code << std::endl;
}
} else {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
return 0;
}
页面更新:2024-04-23
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号