前提:要有chatgpt账号,不会注册的关注抖音:21402780125,有免费教程!!
在 C 语言中调用 ChatGPT API,您需要使用 C 的网络编程库(例如 libcurl)发送 HTTP 请求。
以下是使用 libcurl 调用 ChatGPT API 的示例代码:
#include <stdio.h> #include <string.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; char *url = "https://api.openai.com/v1/engines/davinci-codex/completions"; char *apikey = "YOUR_API_SECRET_KEY"; char *prompt = "Hello, how are you?"; double temperature = 0.7; int max_tokens = 100; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); char authorization[128]; sprintf(authorization, "Authorization: Bearer %s", apikey); headers = curl_slist_append(headers, authorization); char data[256]; sprintf(data, "{\"prompt\":\"%s\",\"temperature\":%lf,\"max_tokens\":%d}", prompt, temperature, max_tokens); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }
标签:调用,res,char,headers,api,easy,chatgpt,include,curl From: https://www.cnblogs.com/ow-dwh/p/17153611.html