Project update.
This commit is contained in:
parent
ab254abb4b
commit
48a543b56e
4
auth.h
4
auth.h
@ -1,8 +1,8 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This source code declares a constant character pointer variable that retrieves an API key from environment variables or falls back to a hardcoded key if not found.
|
||||
// This source code retrieves an API key from environment variables or defaults to a hardcoded key if none are found.
|
||||
|
||||
// Uses standard library functions from stdlib.h and stdio.h to manage environment variables and output error messages.
|
||||
// Uses the C standard library functions from stdlib.h for environment management and stdio.h for error handling.
|
||||
|
||||
// MIT License
|
||||
|
||||
|
12
chat.h
12
chat.h
@ -35,14 +35,14 @@
|
||||
#include "messages.h"
|
||||
|
||||
#ifdef FREE_VERSION
|
||||
char *prompt_model = "gpt-3.5-turbo";
|
||||
static char *prompt_model = "gpt-3.5-turbo";
|
||||
#else
|
||||
char *prompt_model = "gpt-4o-mini";
|
||||
static char *prompt_model = "gpt-4o-mini";
|
||||
#endif
|
||||
int prompt_max_tokens = 2048;
|
||||
double prompt_temperature = 0.1;
|
||||
static int prompt_max_tokens = 2048;
|
||||
static double prompt_temperature = 0.1;
|
||||
|
||||
json_object *_prompt = NULL;
|
||||
static json_object *_prompt = NULL;
|
||||
|
||||
void chat_free() {
|
||||
if (_prompt == NULL) return;
|
||||
@ -52,7 +52,7 @@ void chat_free() {
|
||||
|
||||
char *chat_json(const char *role, const char *message) {
|
||||
chat_free();
|
||||
struct json_object *root_object = json_object_new_object();
|
||||
json_object *root_object = json_object_new_object();
|
||||
json_object_object_add(root_object, "model", json_object_new_string(prompt_model));
|
||||
|
||||
if (role != NULL && message != NULL) {
|
||||
|
21
http_curl.h
21
http_curl.h
@ -1,6 +1,6 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This code defines a simple HTTP client using libcurl in C, providing functions `curl_post` and `curl_get`. These functions enable POST and GET requests with JSON data and include authorization via a bearer token.
|
||||
// This code defines a simple HTTP client using libcurl in C. It provides functions for executing POST and GET HTTP requests with JSON data, including authorization via a bearer token. The functions `curl_post` and `curl_get` handle these operations and return the server's response as a string.
|
||||
|
||||
// Uses libcurl for HTTP requests and includes a custom "auth.h" for API key resolution.
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
// from, out of or in connection with the software or the use or other dealings in
|
||||
// the Software.
|
||||
|
||||
|
||||
#ifndef HTTP_CURL
|
||||
#define HTTP_CURL
|
||||
|
||||
@ -52,18 +53,18 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use
|
||||
char *curl_post(const char *url, const char *data) {
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
struct ResponseBuffer response;
|
||||
response.data = malloc(1);
|
||||
response.size = 0;
|
||||
struct ResponseBuffer response = {malloc(1), 0};
|
||||
|
||||
if (!response.data) return NULL;
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
struct curl_slist *headers = NULL;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
char *bearer_header = malloc(1337);
|
||||
char bearer_header[1337];
|
||||
sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key());
|
||||
headers = curl_slist_append(headers, bearer_header);
|
||||
free(bearer_header);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
@ -91,25 +92,19 @@ char *curl_get(const char *url) {
|
||||
struct curl_slist *headers = NULL;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
|
||||
char *bearer_header = malloc(1337);
|
||||
char bearer_header[1337];
|
||||
sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key());
|
||||
headers = curl_slist_append(headers, bearer_header);
|
||||
free(bearer_header);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res));
|
||||
}
|
||||
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
|
4
line.h
4
line.h
@ -1,6 +1,6 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This source code provides command-line input functionalities with autocomplete and history features using readline library functionalities. It allows users to complete commands and manage input history.
|
||||
// This source code provides command-line input functionalities with autocomplete and history features using the readline library. It allows users to complete commands and manage input history.
|
||||
|
||||
// External includes:
|
||||
// - <readline/readline.h>
|
||||
@ -52,9 +52,7 @@ void line_init() {
|
||||
char* line_read(char* prefix) {
|
||||
char* data = readline(prefix);
|
||||
if (!(data && *data)) {
|
||||
if (data) {
|
||||
free(data);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
return data;
|
||||
|
@ -25,7 +25,6 @@ struct json_object *message_list() {
|
||||
return message_array;
|
||||
}
|
||||
|
||||
|
||||
void messages_remove_last() {
|
||||
struct json_object *messages = message_list();
|
||||
int size = json_object_array_length(messages);
|
||||
@ -40,7 +39,7 @@ struct json_object *message_add_tool_call(struct json_object *message) {
|
||||
return message;
|
||||
}
|
||||
|
||||
struct json_object *message_add_tool_result(char *tool_call_id, char *tool_result) {
|
||||
struct json_object *message_add_tool_result(const char *tool_call_id, const char *tool_result) {
|
||||
struct json_object *messages = message_list();
|
||||
struct json_object *message = json_object_new_object();
|
||||
|
||||
|
1
openai.h
1
openai.h
@ -4,7 +4,6 @@
|
||||
|
||||
// Uncommon imports include "http.h", "chat.h", and "http_curl.h". These may be internal or external libraries providing HTTP and JSON communication capabilities required to interact with APIs.
|
||||
|
||||
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2023
|
||||
|
17
tools.h
17
tools.h
@ -58,7 +58,7 @@ char *tool_function_linux_terminal(char *command) {
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
|
||||
size_t chunk_size = strlen(buffer);
|
||||
char *new_output = (char *)realloc(output, total_size + chunk_size + 1);
|
||||
char* new_output = realloc(output, total_size + chunk_size + 1);
|
||||
if (new_output == NULL) {
|
||||
perror("realloc failed");
|
||||
free(output);
|
||||
@ -79,7 +79,7 @@ char *tool_function_linux_terminal_interactive(char *command) {
|
||||
|
||||
int result_code = system(command);
|
||||
|
||||
char * result = (char *)malloc(100);
|
||||
char* result = malloc(100);
|
||||
result[0] = 0;
|
||||
sprintf(result, "Command exited with status code %d.", result_code);
|
||||
|
||||
@ -100,7 +100,7 @@ struct json_object *tool_description_linux_terminal_interactive() {
|
||||
struct json_object* properties = json_object_new_object();
|
||||
struct json_object* path = json_object_new_object();
|
||||
json_object_object_add(path, "type", json_object_new_string("string"));
|
||||
json_object_object_add(path, "description", json_object_new_string("Executable with parameters to execute interactive."));;
|
||||
json_object_object_add(path, "description", json_object_new_string("Executable with parameters to execute interactively."));
|
||||
json_object_object_add(properties, "command", path);
|
||||
|
||||
json_object_object_add(parameters, "properties", properties);
|
||||
@ -119,9 +119,6 @@ struct json_object *tool_description_linux_terminal_interactive() {
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct json_object* tool_description_directory_rglob() {
|
||||
struct json_object* root = json_object_new_object();
|
||||
json_object_object_add(root, "type", json_object_new_string("function"));
|
||||
@ -129,7 +126,7 @@ struct json_object *tool_description_directory_rglob() {
|
||||
struct json_object* function = json_object_new_object();
|
||||
json_object_object_add(function, "name", json_object_new_string("directory_rglob"));
|
||||
json_object_object_add(function, "description", json_object_new_string("Recursively list the contents of a specified directory in glob format. "
|
||||
"Result is a json array containing objects with keys: name, modification_date(iso), creation_date(iso), type and size_bytes."));
|
||||
"Result is a JSON array containing objects with keys: name, modification_date(iso), creation_date(iso), type, and size_bytes."));
|
||||
|
||||
struct json_object* parameters = json_object_new_object();
|
||||
json_object_object_add(parameters, "type", json_object_new_string("object"));
|
||||
@ -241,7 +238,7 @@ char *tool_function_read_file(char *path) {
|
||||
long size = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
char *content = (char *)malloc(size + 1);
|
||||
char* content = malloc(size + 1);
|
||||
if (content == NULL) {
|
||||
fclose(fp);
|
||||
return strdup("Memory allocation failed!");
|
||||
@ -491,7 +488,7 @@ struct json_object *tool_description_directory_glob() {
|
||||
struct json_object* function = json_object_new_object();
|
||||
json_object_object_add(function, "name", json_object_new_string("directory_glob"));
|
||||
json_object_object_add(function, "description", json_object_new_string("List the contents of a specified directory in glob format. "
|
||||
"Result is a json array containing objects with keys: name, modification_date(iso), creation_date(iso), type and size_bytes."));
|
||||
"Result is a JSON array containing objects with keys: name, modification_date(iso), creation_date(iso), type, and size_bytes."));
|
||||
|
||||
struct json_object* parameters = json_object_new_object();
|
||||
json_object_object_add(parameters, "type", json_object_new_string("object"));
|
||||
@ -670,7 +667,7 @@ struct json_object *tools_execute(struct json_object *tools_array) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Unkown function: %s\n", function_name);
|
||||
fprintf(stderr, "Unknown function: %s\n", function_name);
|
||||
json_object_object_add(tool_result, "content", json_object_new_string("Error: function not found."));
|
||||
}
|
||||
|
||||
|
47
utils.h
Normal file
47
utils.h
Normal file
@ -0,0 +1,47 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This header file contains utility functions for manipulating file system paths, focusing primarily on expanding paths starting with '~' to the home directory.
|
||||
|
||||
// This code uses standard libraries: stdio.h, stdlib.h, and string.h, and conditionally includes the posix libraries pwd.h and unistd.h when expanding the home directory manually.
|
||||
|
||||
// MIT License
|
||||
//
|
||||
// Permission is granted to use, copy, modify, merge, distribute, sublicense, and/or sell copies of the Software.
|
||||
// The license includes conditions about providing a copy of the license and the limitation of liability and warranty.
|
||||
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* expand_home_directory(const char* path) {
|
||||
if (path == NULL) return NULL;
|
||||
|
||||
if (path[0] == '~' && path[1] == '/') {
|
||||
const char* home_dir = getenv("HOME");
|
||||
if (home_dir == NULL) {
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
if (pw == NULL) return NULL;
|
||||
home_dir = pw->pw_dir;
|
||||
}
|
||||
|
||||
size_t home_len = strlen(home_dir);
|
||||
size_t path_len = strlen(path) - 1;
|
||||
char* expanded_path = malloc(home_len + path_len + 1);
|
||||
|
||||
if (expanded_path == NULL) return NULL;
|
||||
|
||||
strcpy(expanded_path, home_dir);
|
||||
strcat(expanded_path, path + 1);
|
||||
|
||||
return expanded_path;
|
||||
} else {
|
||||
return strdup(path);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user