83 lines
3.4 KiB
C
Raw Normal View History

2025-01-04 07:40:31 +00:00
// Written by retoor@molodetz.nl
// This code provides functions to interact with OpenAI's APIs. It includes functionalities for fetching available models, system interactions, and engaging in chat-based conversations using the OpenAI API.
// Imports the "http" library for handling HTTP requests and the "chat" library for JSON handling related to chat content.
// MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, to deal in the Software without restriction, including the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2025-01-04 05:00:03 +00:00
#ifndef CALPACA_OPENAI_H
#define CALPACA_OPENAI_H
#include "http.h"
#include "chat.h"
#include <string.h>
#include <stdbool.h>
2025-01-04 07:40:31 +00:00
char *openai_get_models() {
2025-01-04 05:00:03 +00:00
const char *hostname = "api.openai.com";
char *url = "/v1/models";
2025-01-04 07:40:31 +00:00
return http_get(hostname, url);
2025-01-04 05:00:03 +00:00
}
2025-01-04 07:40:31 +00:00
bool openai_system(char *content) {
2025-01-04 05:00:03 +00:00
const char *hostname = "api.openai.com";
char *url = "/v1/chat/completions";
char *data = chat_json("system", content);
char *result = http_post(hostname, url, data);
2025-01-04 07:40:31 +00:00
bool is_done = result != NULL;
2025-01-04 05:00:03 +00:00
2025-01-04 07:40:31 +00:00
free(result);
2025-01-04 05:00:03 +00:00
return is_done;
}
2025-01-04 07:40:31 +00:00
char *openai_chat(char *role, char *content) {
2025-01-04 05:00:03 +00:00
const char *hostname = "api.openai.com";
char *url = "/v1/chat/completions";
char *data = chat_json(role, content);
char *result = http_post(hostname, url, data);
2025-01-04 07:40:31 +00:00
char *body = strstr(result, "\r\n\r\n") + 4;
body = strstr(body, "\r\n");
body = strstr(body, "\r\n");
2025-01-04 05:00:03 +00:00
*(body - 5) = 0;
struct json_object *parsed_json = json_tokener_parse(body);
2025-01-04 07:40:31 +00:00
if (!parsed_json) {
2025-01-04 05:00:03 +00:00
fprintf(stderr, "Failed to parse JSON.\n");
return NULL;
}
2025-01-04 07:40:31 +00:00
struct json_object *choices_array;
2025-01-04 05:00:03 +00:00
if (!json_object_object_get_ex(parsed_json, "choices", &choices_array)) {
fprintf(stderr, "Failed to get 'choices' array.\n");
json_object_put(parsed_json);
return NULL;
}
struct json_object *first_choice = json_object_array_get_idx(choices_array, 0);
if (!first_choice) {
fprintf(stderr, "Failed to get the first element of 'choices'.\n");
json_object_put(parsed_json);
return NULL;
}
struct json_object *message_object;
if (!json_object_object_get_ex(first_choice, "message", &message_object)) {
fprintf(stderr, "Failed to get 'message' object.\n");
json_object_put(parsed_json);
return NULL;
}
2025-01-04 07:40:31 +00:00
char *content_str = (char *)json_object_get_string(json_object_object_get(message_object, "content"));
message_add("assistant", content_str);
free(data);
free(result);
char *final_result = strdup(content_str);
2025-01-04 05:00:03 +00:00
json_object_put(parsed_json);
2025-01-04 07:40:31 +00:00
return final_result;
2025-01-04 05:00:03 +00:00
}
2025-01-04 07:35:39 +00:00
#endif