2025-01-05 22:59:51 +01:00
|
|
|
#ifndef R_OPENAI_H
|
|
|
|
#define R_OPENAI_H
|
2025-01-04 06:00:03 +01:00
|
|
|
#include "http.h"
|
|
|
|
#include "chat.h"
|
2025-01-27 18:57:21 +01:00
|
|
|
#include "http_curl.h"
|
2025-01-04 06:00:03 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2025-03-03 13:51:57 +01:00
|
|
|
char* openai_fetch_models() {
|
|
|
|
char* api_url = "https://api.openai.com/v1/models";
|
|
|
|
return https_get(api_url);
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
|
|
|
|
2025-03-03 13:51:57 +01:00
|
|
|
bool openai_system(char* message_content) {
|
|
|
|
(void)chat_json("system", message_content);
|
2025-03-03 13:53:24 +01:00
|
|
|
return true;
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
|
|
|
|
2025-03-03 13:51:57 +01:00
|
|
|
struct json_object* openai_process_chat_message(char* api_url, char* json_data) {
|
|
|
|
char* response = curl_post(api_url, json_data);
|
|
|
|
struct json_object* parsed_json = json_tokener_parse(response);
|
2025-01-04 08:40:31 +01:00
|
|
|
if (!parsed_json) {
|
2025-01-04 06:00:03 +01:00
|
|
|
fprintf(stderr, "Failed to parse JSON.\n");
|
2025-03-03 13:51:57 +01:00
|
|
|
fprintf(stderr, "%s\n", response);
|
|
|
|
return json_object_new_null();
|
|
|
|
}
|
|
|
|
struct json_object *error_object;
|
|
|
|
if(json_object_object_get_ex(parsed_json, "error", &error_object)) {
|
|
|
|
fprintf(stderr, "Failed to get 'error' object.\n");
|
|
|
|
fprintf(stderr, "%s\n", response);
|
|
|
|
json_object_put(parsed_json);
|
|
|
|
return json_object_new_null();
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
2025-03-03 13:51:57 +01:00
|
|
|
|
2025-01-27 19:06:59 +01:00
|
|
|
struct json_object* choices_array;
|
2025-01-04 06:00:03 +01:00
|
|
|
if (!json_object_object_get_ex(parsed_json, "choices", &choices_array)) {
|
|
|
|
fprintf(stderr, "Failed to get 'choices' array.\n");
|
2025-03-03 13:51:57 +01:00
|
|
|
fprintf(stderr, "%s\n", response);
|
2025-01-04 06:00:03 +01:00
|
|
|
json_object_put(parsed_json);
|
2025-03-03 13:51:57 +01:00
|
|
|
return json_object_new_null();
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
2025-01-27 19:06:59 +01:00
|
|
|
struct json_object* first_choice = json_object_array_get_idx(choices_array, 0);
|
2025-01-04 06:00:03 +01:00
|
|
|
if (!first_choice) {
|
|
|
|
fprintf(stderr, "Failed to get the first element of 'choices'.\n");
|
|
|
|
json_object_put(parsed_json);
|
2025-03-03 13:51:57 +01:00
|
|
|
return json_object_new_null();
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
2025-03-03 08:32:41 +01:00
|
|
|
struct json_object* message_object;
|
2025-01-04 06:00:03 +01:00
|
|
|
if (!json_object_object_get_ex(first_choice, "message", &message_object)) {
|
|
|
|
fprintf(stderr, "Failed to get 'message' object.\n");
|
|
|
|
json_object_put(parsed_json);
|
2025-03-03 13:51:57 +01:00
|
|
|
return json_object_new_null();
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
2025-03-03 08:07:17 +01:00
|
|
|
return message_object;
|
|
|
|
}
|
|
|
|
|
2025-03-03 13:51:57 +01:00
|
|
|
char* openai_chat(char* user_role, char* message_content) {
|
|
|
|
char* api_url = "https://api.openai.com/v1/chat/completions";
|
|
|
|
char* json_data = chat_json(user_role, message_content);
|
|
|
|
json_object* message_object = openai_process_chat_message(api_url, json_data);
|
2025-03-03 08:32:41 +01:00
|
|
|
struct json_object* tool_calls;
|
2025-03-03 08:07:17 +01:00
|
|
|
json_object_object_get_ex(message_object, "tool_calls", &tool_calls);
|
|
|
|
if (tool_calls) {
|
|
|
|
message_add_tool_call(message_object);
|
2025-03-03 08:32:41 +01:00
|
|
|
json_object* tool_call_results = tools_execute(tool_calls);
|
2025-03-03 13:51:57 +01:00
|
|
|
int results_count = json_object_array_length(tool_call_results);
|
|
|
|
for (int i = 0; i < results_count; i++) {
|
2025-03-03 08:32:41 +01:00
|
|
|
json_object* tool_call_result = json_object_array_get_idx(tool_call_results, i);
|
|
|
|
message_add_tool_call(tool_call_result);
|
2025-03-03 08:07:17 +01:00
|
|
|
}
|
2025-03-03 08:32:41 +01:00
|
|
|
char* tool_calls_result_str = chat_json(NULL, NULL);
|
2025-03-03 13:51:57 +01:00
|
|
|
message_object = openai_process_chat_message(api_url, tool_calls_result_str);
|
2025-03-03 08:07:17 +01:00
|
|
|
message_add_tool_call(message_object);
|
|
|
|
}
|
2025-01-27 19:06:59 +01:00
|
|
|
char* content_str = (char*)json_object_get_string(json_object_object_get(message_object, "content"));
|
2025-03-03 08:32:41 +01:00
|
|
|
return strdup(content_str);
|
2025-01-04 06:00:03 +01:00
|
|
|
}
|
|
|
|
|
2025-03-03 14:09:28 +01:00
|
|
|
#endif
|