bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc119.c

luc119.c (2428B)


      1 /* Write a program to analyse a given sentence to detect the mood of the sentence using OpenAI Chat Completion API.
      2 */
      3 /* Let Us C, Chap- 24 (Interaction with ChatGPT through C), Qn No.: B(c) */
      4 
      5 /* This file is auto-generated by a bot. */
      6 /* This code is not compiled; it is for reference only. */
      7 
      8 /* NOTE: These programs require the 'libcurl' library to compile.
      9    Command: gcc luc119.c -o output -lcurl */
     10 
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <string.h>
     15 #include <curl/curl.h>
     16 
     17 /* Pre-requisites:
     18    1. Install libcurl.
     19    2. Get OpenAI API Key.
     20 */
     21 
     22 #define API_KEY "YOUR_OPENAI_API_KEY_HERE"
     23 
     24 int main(void) {
     25     CURL *curl;
     26     CURLcode res;
     27     
     28     const char *url = "https://api.openai.com/v1/chat/completions";
     29     
     30     /* We construct the JSON payload manually.
     31        System prompt instructs the model to detect mood.
     32        User prompt is the sentence to analyze.
     33     */
     34     const char *data = "{"
     35                        "\"model\": \"gpt-3.5-turbo\","
     36                        "\"messages\": ["
     37                        "  {\"role\": \"system\", \"content\": \"You are a helpful assistant. Analyze the mood of the user input sentence. Return only the mood keywords (e.g., admiration, appreciation, anger, joy).\"},"
     38                        "  {\"role\": \"user\", \"content\": \"I am so impressed by your performance\"}"
     39                        "]"
     40                        "}";
     41 
     42     struct curl_slist *headers = NULL;
     43     char auth_header[100];
     44     sprintf(auth_header, "Authorization: Bearer %s", API_KEY);
     45 
     46     curl = curl_easy_init();
     47     if(curl) {
     48         headers = curl_slist_append(headers, "Content-Type: application/json");
     49         headers = curl_slist_append(headers, auth_header);
     50         
     51         curl_easy_setopt(curl, CURLOPT_URL, url);
     52         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
     53         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
     54 
     55         printf("Analyzing sentence: 'I am so impressed by your performance'\n");
     56         printf("Waiting for OpenAI response...\n\n");
     57 
     58         // The response will be printed to standard output
     59         res = curl_easy_perform(curl);
     60 
     61         if(res != CURLE_OK)
     62             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
     63         
     64         printf("\n\n(Parse the JSON above to extract the 'content' field)\n");
     65 
     66         curl_slist_free_all(headers);
     67         curl_easy_cleanup(curl);
     68     }
     69     return 0;
     70 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror