fork download
  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <string.h>
  4. include <curl/curl.h>
  5.  
  6. define MAX_INPUT_LENGTH 500
  7. define MAX_RESPONSE_LENGTH 4096
  8.  
  9. char endpoint[] = "https://a...content-available-to-author-only...i.com/v1/engines/davinci-codex/completions";
  10.  
  11. void call_api(char input[]) {
  12. CURL *curl;
  13. CURLcode res;
  14. char response[MAX_RESPONSE_LENGTH] = "";
  15.  
  16. curl = curl_easy_init();
  17. if (curl) {
  18. char *data = malloc(MAX_INPUT_LENGTH + strlen(input) + 100);
  19. if (data == NULL) {
  20. printf("Memory allocation failed.\n");
  21. return;
  22. }
  23.  
  24. sprintf(data, "{\"prompt\": \"%s\", \"max_tokens\": 150}", input);
  25.  
  26. curl_easy_setopt(curl, CURLOPT_URL, endpoint);
  27. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  28. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  29. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Content-Type: application/json");
  30. curl_easy_setopt(curl, CURLOPT_USERPWD, "sk-proj-O6ivYofdtFBpWeqTrl4GT3BlbkFJuxxxO0vW5MiMgAVAfIE9"); // Replace "YOUR_API_KEY" with your OpenAI API key
  31. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  32. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  33.  
  34. res = curl_easy_perform(curl);
  35. if (res == CURLE_OK) {
  36. printf("Generated Details:\n%s\n", response);
  37. } else {
  38. printf("Failed to call OpenAI API: %s\n", curl_easy_strerror(res));
  39. }
  40.  
  41. free(data);
  42. curl_easy_cleanup(curl);
  43. }
  44. }
  45.  
  46. int main() {
  47. char input[MAX_INPUT_LENGTH];
  48.  
  49. printf("Enter your input: ");
  50. fgets(input, MAX_INPUT_LENGTH, stdin);
  51.  
  52. // Remove newline character from the input
  53. input[strcspn(input, "\n")] = 0;
  54.  
  55. call_api(input);
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0.02s 25744KB
stdin
Abc
stdout
include <stdio.h>
include <stdlib.h>
include <string.h>
include <curl/curl.h>
 
define MAX_INPUT_LENGTH 500
define MAX_RESPONSE_LENGTH 4096
 
char endpoint[] = "https://a...content-available-to-author-only...i.com/v1/engines/davinci-codex/completions";
 
void call_api(char input[]) {
    CURL *curl;
    CURLcode res;
    char response[MAX_RESPONSE_LENGTH] = "";
 
    curl = curl_easy_init();
    if (curl) {
        char *data = malloc(MAX_INPUT_LENGTH + strlen(input) + 100);
        if (data == NULL) {
            printf("Memory allocation failed.\n");
            return;
        }
 
        sprintf(data, "{\"prompt\": \"%s\", \"max_tokens\": 150}", input);
 
        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "sk-proj-O6ivYofdtFBpWeqTrl4GT3BlbkFJuxxxO0vW5MiMgAVAfIE9"); // Replace "YOUR_API_KEY" with your OpenAI API key
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
 
        res = curl_easy_perform(curl);
        if (res == CURLE_OK) {
            printf("Generated Details:\n%s\n", response);
        } else {
            printf("Failed to call OpenAI API: %s\n", curl_easy_strerror(res));
        }
 
        free(data);
        curl_easy_cleanup(curl);
    }
}
 
int main() {
    char input[MAX_INPUT_LENGTH];
 
    printf("Enter your input: ");
    fgets(input, MAX_INPUT_LENGTH, stdin);
 
    // Remove newline character from the input
    input[strcspn(input, "\n")] = 0;
 
    call_api(input);
 
    return 0;
}