Compare commits
2 Commits
35b53a586a
...
1c45ebfd79
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c45ebfd79 | |||
| 1d0ce864e8 |
5
Makefile
5
Makefile
@ -1,11 +1,12 @@
|
|||||||
all: build run
|
all: build run
|
||||||
|
|
||||||
build:
|
build:
|
||||||
gcc main.c -lcurl -lssl -lcrypto -ljson-c -Ofast -o r -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses
|
# -lpython3.14 -I/usr/include/python3.14
|
||||||
|
gcc main.c -lcurl -lssl -lcrypto -ljson-c -Ofast -o r -Werror -Wall -lreadline -lncurses
|
||||||
publish r
|
publish r
|
||||||
|
|
||||||
build_free:
|
build_free:
|
||||||
gcc main.c -lcurl -DFREE_VERSION -lssl -lcrypto -ljson-c -Ofast -o rf -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses
|
gcc main.c -lcurl -DFREE_VERSION -lssl -lcrypto -ljson-c -Ofast -o rf -Werror -Wall -lreadline -lncurses
|
||||||
publish rf
|
publish rf
|
||||||
|
|
||||||
run:
|
run:
|
||||||
|
|||||||
14
chat.h
14
chat.h
@ -31,6 +31,7 @@
|
|||||||
#ifndef R_PROMPT_H
|
#ifndef R_PROMPT_H
|
||||||
#define R_PROMPT_H
|
#define R_PROMPT_H
|
||||||
|
|
||||||
|
#include "tools.h"
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include "messages.h"
|
#include "messages.h"
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
@ -56,10 +57,17 @@ void chat_free() {
|
|||||||
|
|
||||||
char *chat_json(char *role, char *message) {
|
char *chat_json(char *role, char *message) {
|
||||||
chat_free();
|
chat_free();
|
||||||
message_add(role, message);
|
|
||||||
|
|
||||||
struct json_object *root_object = json_object_new_object();
|
struct json_object *root_object = json_object_new_object();
|
||||||
json_object_object_add(root_object, "model", json_object_new_string(prompt_model));
|
json_object_object_add(root_object, "model", json_object_new_string(prompt_model));
|
||||||
|
|
||||||
|
if (role != NULL && message != NULL) {
|
||||||
|
message_add(role, message);
|
||||||
|
|
||||||
|
json_object_object_add(root_object, "tools", tools_descriptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
json_object_object_add(root_object, "messages", message_list());
|
json_object_object_add(root_object, "messages", message_list());
|
||||||
json_object_object_add(root_object, "max_tokens", json_object_new_int(prompt_max_tokens));
|
json_object_object_add(root_object, "max_tokens", json_object_new_int(prompt_max_tokens));
|
||||||
json_object_object_add(root_object, "temperature", json_object_new_double(prompt_temperature));
|
json_object_object_add(root_object, "temperature", json_object_new_double(prompt_temperature));
|
||||||
@ -67,4 +75,4 @@ char *chat_json(char *role, char *message) {
|
|||||||
return (char *)json_object_to_json_string_ext(root_object, JSON_C_TO_STRING_PRETTY);
|
return (char *)json_object_to_json_string_ext(root_object, JSON_C_TO_STRING_PRETTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
59
http.h
59
http.h
@ -127,6 +127,39 @@ size_t hex_to_int(const char *hex) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool http_has_header(char * http_headers, char * http_header_name){
|
||||||
|
char search_for[strlen(http_header_name)+10];
|
||||||
|
search_for[0] = '\0';
|
||||||
|
strcpy(search_for,http_header_name);
|
||||||
|
strcat(search_for,": ");
|
||||||
|
return strstr(http_headers, search_for) != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char * http_header_get_str(char * http_headers, char * http_header_name){
|
||||||
|
char search_for[strlen(http_header_name)+10];
|
||||||
|
search_for[0] = '\0';
|
||||||
|
strcpy(search_for,http_header_name);
|
||||||
|
strcat(search_for,": ");
|
||||||
|
char * header = strstr(http_headers, search_for);
|
||||||
|
if(header == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
header += strlen(search_for);
|
||||||
|
char * end = strstr(header, "\r\n");
|
||||||
|
*end = '\0';
|
||||||
|
char * result = (char *)malloc(end - header + 1);
|
||||||
|
strcpy(result, header);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
long http_header_get_long(char * http_headers, char * http_header_name){
|
||||||
|
char * str_value = http_header_get_str(http_headers, http_header_name);
|
||||||
|
if(str_value == NULL)
|
||||||
|
return 0;
|
||||||
|
long long_value = atol(str_value);
|
||||||
|
free(str_value);
|
||||||
|
return long_value;
|
||||||
|
}
|
||||||
|
|
||||||
char *https_post(char *url, char *data) {
|
char *https_post(char *url, char *data) {
|
||||||
url_t parsed_url;
|
url_t parsed_url;
|
||||||
parse_url(url, &parsed_url);
|
parse_url(url, &parsed_url);
|
||||||
@ -142,13 +175,13 @@ char *https_post(char *url, char *data) {
|
|||||||
SSL_set_fd(ssl, sock);
|
SSL_set_fd(ssl, sock);
|
||||||
|
|
||||||
int buffer_size = 1024 * 1024;
|
int buffer_size = 1024 * 1024;
|
||||||
char *buffer = malloc(buffer_size);
|
char *buffer = (char *)malloc(buffer_size);
|
||||||
size_t chunk_size_total = 0;
|
size_t chunk_size_total = 0;
|
||||||
if (SSL_connect(ssl) <= 0) {
|
if (SSL_connect(ssl) <= 0) {
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
} else {
|
} else {
|
||||||
size_t len = strlen(data);
|
size_t len = strlen(data);
|
||||||
char *request = malloc(len + 1024 * 1024);
|
char *request = (char *)malloc(len + 1024 * 1024);
|
||||||
sprintf(request,
|
sprintf(request,
|
||||||
"POST %s HTTP/1.1\r\n"
|
"POST %s HTTP/1.1\r\n"
|
||||||
"Content-Length: %ld\r\n"
|
"Content-Length: %ld\r\n"
|
||||||
@ -164,6 +197,24 @@ char *https_post(char *url, char *data) {
|
|||||||
char *headers = read_until_ssl(ssl, "\r\n\r\n");
|
char *headers = read_until_ssl(ssl, "\r\n\r\n");
|
||||||
(void)headers;
|
(void)headers;
|
||||||
|
|
||||||
|
long content_length = http_header_get_long(headers, "Content-Length");
|
||||||
|
if (content_length){
|
||||||
|
if( content_length > buffer_size) {
|
||||||
|
buffer_size = content_length;
|
||||||
|
buffer = (char *)realloc(buffer, buffer_size);
|
||||||
|
}
|
||||||
|
size_t bytes_read = 0;
|
||||||
|
while(bytes_read < content_length) {
|
||||||
|
int bytes_read_chunk = SSL_read(ssl, buffer + bytes_read, buffer_size - bytes_read);
|
||||||
|
if(bytes_read_chunk <= 0){
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
bytes_read += bytes_read_chunk;
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
size_t actual_buffer_size = buffer_size;
|
size_t actual_buffer_size = buffer_size;
|
||||||
while (true) {
|
while (true) {
|
||||||
char *header = read_until_ssl(ssl, "\r\n");
|
char *header = read_until_ssl(ssl, "\r\n");
|
||||||
@ -173,7 +224,7 @@ char *https_post(char *url, char *data) {
|
|||||||
size_t remaining = chunk_size;
|
size_t remaining = chunk_size;
|
||||||
while (remaining > 0) {
|
while (remaining > 0) {
|
||||||
size_t to_read = (remaining < buffer_size) ? remaining : buffer_size;
|
size_t to_read = (remaining < buffer_size) ? remaining : buffer_size;
|
||||||
buffer = realloc(buffer, actual_buffer_size + to_read + 1);
|
buffer = (char *)realloc(buffer, actual_buffer_size + to_read + 1);
|
||||||
actual_buffer_size += to_read;
|
actual_buffer_size += to_read;
|
||||||
size_t bytes_read = SSL_read(ssl, buffer + chunk_size_total, to_read);
|
size_t bytes_read = SSL_read(ssl, buffer + chunk_size_total, to_read);
|
||||||
chunk_size_total += bytes_read;
|
chunk_size_total += bytes_read;
|
||||||
@ -371,4 +422,4 @@ char *http_get(char *url) {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
38
http_curl.h
38
http_curl.h
@ -78,4 +78,40 @@ char *curl_post(const char *url, const char *data) {
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
char *curl_get(const char *url) {
|
||||||
|
CURL *curl;
|
||||||
|
CURLcode res;
|
||||||
|
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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
246
main.c
246
main.c
@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#include "openai.h"
|
#include "openai.h"
|
||||||
#include "markdown.h"
|
#include "markdown.h"
|
||||||
#include "plugin.h"
|
|
||||||
#include "line.h"
|
#include "line.h"
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -38,6 +37,13 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
bool SYNTAX_HIGHLIGHT_ENABLED = true;
|
||||||
|
bool API_MODE = false;
|
||||||
|
void help();
|
||||||
|
void render(char *);
|
||||||
|
bool openai_include(char *path);
|
||||||
|
char * strreplace(char * content, char * what, char * with);
|
||||||
|
|
||||||
char * get_prompt_from_stdin(char * prompt) {
|
char * get_prompt_from_stdin(char * prompt) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
prompt[index] = '\0';
|
prompt[index] = '\0';
|
||||||
@ -50,21 +56,49 @@ char * get_prompt_from_stdin(char * prompt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *get_prompt_from_args(int c, char **argv) {
|
char *get_prompt_from_args(int c, char **argv) {
|
||||||
char *prompt = malloc(1024 * 1024 * 10 + 1);
|
char *prompt = (char *)malloc(1024 * 1024 * 10 + 1);
|
||||||
|
char *system = (char *)malloc(1024*1024);
|
||||||
|
|
||||||
|
system[0] = 0;
|
||||||
prompt[0] = 0;
|
prompt[0] = 0;
|
||||||
|
bool get_from_std_in = false;
|
||||||
for (int i = 1; i < c; i++) {
|
for (int i = 1; i < c; i++) {
|
||||||
if (!strcmp(argv[i],"--stdin")){
|
if (!strcmp(argv[i],"--stdin")){
|
||||||
prompt = get_prompt_from_stdin(prompt);
|
fprintf(stderr, "%s\n", "Reading from stdin.");
|
||||||
break;
|
get_from_std_in = true;
|
||||||
}else{
|
}else if(!strcmp(argv[i],"--context")){
|
||||||
strcat(prompt, argv[i]);
|
if(i+1 <= c){
|
||||||
|
char * context_file_path = argv[i+1];
|
||||||
|
fprintf(stderr, "Including \"%s\".\n", context_file_path);
|
||||||
|
openai_include(context_file_path);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}else if(!strcmp(argv[i],"--api")){
|
||||||
|
API_MODE = true;
|
||||||
|
}else if(!strcmp(argv[i], "--nh")){
|
||||||
|
SYNTAX_HIGHLIGHT_ENABLED = false;
|
||||||
|
fprintf(stderr, "%s\n", "Syntax highlighting disabled.");
|
||||||
|
}else if (!get_from_std_in){
|
||||||
|
strcat(system, argv[i]);
|
||||||
if (i < c - 1) {
|
if (i < c - 1) {
|
||||||
strcat(prompt, " ");
|
strcat(system, " ");
|
||||||
} else {
|
} else {
|
||||||
strcat(prompt, ". ");
|
strcat(system, ".");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(get_from_std_in){
|
||||||
|
if(*system){
|
||||||
|
openai_system(system);
|
||||||
|
}
|
||||||
|
free(system);
|
||||||
|
prompt = get_prompt_from_stdin(prompt);
|
||||||
|
}else{
|
||||||
|
free(prompt);
|
||||||
|
prompt = system;
|
||||||
|
}
|
||||||
|
|
||||||
if (!*prompt) {
|
if (!*prompt) {
|
||||||
free(prompt);
|
free(prompt);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -81,8 +115,7 @@ bool try_prompt(int argc, char *argv[]) {
|
|||||||
free(prompt);
|
free(prompt);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
parse_markdown_to_ansi(response);
|
render(response);
|
||||||
printf("\n");
|
|
||||||
free(response);
|
free(response);
|
||||||
free(prompt);
|
free(prompt);
|
||||||
return true;
|
return true;
|
||||||
@ -90,17 +123,132 @@ bool try_prompt(int argc, char *argv[]) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void help();
|
|
||||||
void render(char *);
|
|
||||||
void serve() {
|
void serve() {
|
||||||
render("Starting server. *Put executables in a dir named cgi-bin and they will behave as web pages.*");
|
render("Starting server. *Put executables in a dir named cgi-bin and they will behave as web pages.*");
|
||||||
int res = system("python3 -m http.server --cgi");
|
int res = system("python3 -m http.server --cgi");
|
||||||
(void)res;
|
(void)res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(char *content) {
|
char ** get_parameters(char * content, char * delimiter){
|
||||||
parse_markdown_to_ansi(content);
|
char * start = NULL;
|
||||||
printf("\n\n");
|
char ** parameters = NULL; //(char **)malloc(sizeof(char *) * 2);
|
||||||
|
int count = 0;
|
||||||
|
while((start = strstr(content, delimiter)) != NULL){
|
||||||
|
start += 3;
|
||||||
|
char * end = strstr(start, delimiter);
|
||||||
|
char * parameter = (char *)malloc(end - start + 1);
|
||||||
|
|
||||||
|
memcpy(parameter, start, end - start);
|
||||||
|
parameter[end - start] = '\0';
|
||||||
|
|
||||||
|
// printf("%s\n", parameter);
|
||||||
|
content = end + 3;
|
||||||
|
count+=1;
|
||||||
|
parameters = (char **)realloc(parameters, sizeof(char *) * (1+count*2));
|
||||||
|
parameters[count-1] = parameter;
|
||||||
|
parameters[count] = NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
void render(char *content){
|
||||||
|
|
||||||
|
char ** parameters = get_parameters(content,"\"\"\"");
|
||||||
|
int parameter_index = 0;
|
||||||
|
bool print_result = true;
|
||||||
|
while(parameters){
|
||||||
|
char * parameter = parameters[parameter_index];
|
||||||
|
if(!parameter)
|
||||||
|
break;
|
||||||
|
print_result = false;
|
||||||
|
if(!strcmp(parameter, "!find_note")){
|
||||||
|
char * note_name = parameters[parameter_index + 1];
|
||||||
|
FILE * file = fopen("notes.txt", "r");
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long size = ftell(file);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
char *buffer = (char *)malloc(size + 1);
|
||||||
|
size = fread(buffer, 1, size, file);
|
||||||
|
buffer[size] = '\0';
|
||||||
|
fclose(file);
|
||||||
|
char * prompt = (char *)malloc(size + 1000);
|
||||||
|
prompt[0] = 0;
|
||||||
|
sprintf(prompt,"This are records seperated by newline: ```%s```. Respond to use with record that is about ```%s```. Rspond in plain text, do not execute command.",buffer, note_name);
|
||||||
|
char* result = openai_chat("user",prompt);
|
||||||
|
printf("%s\n",result);
|
||||||
|
free(prompt);
|
||||||
|
free(result);
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!strcmp(parameter, "!write_note")){
|
||||||
|
char * file_name = "notes.txt";
|
||||||
|
char * file_content = parameters[parameter_index + 1];
|
||||||
|
FILE * file = fopen(file_name, "a+");
|
||||||
|
fprintf(file, "%s\n\n", file_content);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
if(!strcmp(parameter, "!write_file")){
|
||||||
|
|
||||||
|
char * file_name = parameters[parameter_index + 1];
|
||||||
|
|
||||||
|
printf("Writing to file: %s\n",file_name);
|
||||||
|
char * file_content = parameters[parameter_index + 2];
|
||||||
|
FILE * file = fopen(file_name, "w");
|
||||||
|
fprintf(file, "%s", file_content);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
}
|
||||||
|
if(!strcmp(parameter, "!system")){
|
||||||
|
// printf("%s\n",parameters[parameter_index+1]);
|
||||||
|
char * command = parameters[parameter_index + 1];
|
||||||
|
FILE * f = popen(command,"r");;
|
||||||
|
if(!f){
|
||||||
|
printf("Execution failed: %s\n",command);
|
||||||
|
}
|
||||||
|
char buffer[4096];
|
||||||
|
char * full_buffer = (char *)malloc(1);
|
||||||
|
int bytes_read = 0;
|
||||||
|
while (fgets(buffer, sizeof(buffer), f) != NULL) {
|
||||||
|
full_buffer = realloc(full_buffer, bytes_read + strlen(buffer) + 1);
|
||||||
|
memcpy(full_buffer + bytes_read, buffer, strlen(buffer));
|
||||||
|
bytes_read += strlen(buffer);
|
||||||
|
printf("%s",buffer);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
pclose(f);
|
||||||
|
full_buffer[bytes_read] = '\0';
|
||||||
|
//printf("%s",full_buffer);
|
||||||
|
char * prompt = (char *)malloc(strlen(full_buffer) + 1000);
|
||||||
|
prompt[0] = 0;
|
||||||
|
printf(prompt,"Last execution result and thus current context is: ```%s```\n",full_buffer);
|
||||||
|
char * rmsg = openai_chat("system",full_buffer);
|
||||||
|
|
||||||
|
//printf("%s\n",rmsg);
|
||||||
|
free(prompt);
|
||||||
|
free(rmsg);
|
||||||
|
free(full_buffer);
|
||||||
|
|
||||||
|
}else if(parameter[0] == '!'){
|
||||||
|
printf("%s\n",parameter);
|
||||||
|
}
|
||||||
|
parameter_index+= 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
if(!print_result){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(SYNTAX_HIGHLIGHT_ENABLED)
|
||||||
|
{
|
||||||
|
parse_markdown_to_ansi(content);
|
||||||
|
printf("\n\n");
|
||||||
|
}else{
|
||||||
|
printf("%s", content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void repl() {
|
void repl() {
|
||||||
@ -114,10 +262,7 @@ void repl() {
|
|||||||
if (!line || !*line)
|
if (!line || !*line)
|
||||||
continue;
|
continue;
|
||||||
previous_line = line;
|
previous_line = line;
|
||||||
if (line[0] == '!') {
|
|
||||||
plugin_run(line + 1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!strncmp(line, "exit", 4)) {
|
if (!strncmp(line, "exit", 4)) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -126,8 +271,12 @@ void repl() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strncmp(line, "serve", 5)) {
|
if (!strncmp(line, "serve", 5)) {
|
||||||
|
continue ;
|
||||||
serve();
|
serve();
|
||||||
}
|
}
|
||||||
|
if(!strncmp(line,"retoor",6)){
|
||||||
|
openai_include("retoor");
|
||||||
|
}
|
||||||
if (!strncmp(line, "spar ", 5)) {
|
if (!strncmp(line, "spar ", 5)) {
|
||||||
char *response = line + 5;
|
char *response = line + 5;
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -136,7 +285,8 @@ void repl() {
|
|||||||
response = openai_chat("user", response);
|
response = openai_chat("user", response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!strncmp(line, "ls", 2) || !strncmp(line, "list", 4)) {
|
if (!strncmp(line, "/_", 2) || !strncmp(line, "____", 4)) {
|
||||||
|
continue;
|
||||||
int offset = 2;
|
int offset = 2;
|
||||||
if (!strncmp(line, "list", 4)) {
|
if (!strncmp(line, "list", 4)) {
|
||||||
offset = 4;
|
offset = 4;
|
||||||
@ -154,6 +304,7 @@ void repl() {
|
|||||||
line_add_history(line);
|
line_add_history(line);
|
||||||
char *response = openai_chat("user", line);
|
char *response = openai_chat("user", line);
|
||||||
render(response);
|
render(response);
|
||||||
|
printf("\n");
|
||||||
free(response);
|
free(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,7 +334,49 @@ void help() {
|
|||||||
render(help_text);
|
render(help_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char * strreplace(char * content, char * what, char * with){
|
||||||
|
char * pos = strstr(content, what);
|
||||||
|
if(!pos){
|
||||||
|
return strdup(content);
|
||||||
|
}
|
||||||
|
char * result = (char *)malloc(strlen(content) + strlen(with) + 5);
|
||||||
|
memset(result, 0, strlen(content) + strlen(with) + 3);
|
||||||
|
memcpy(result, content, pos - content);
|
||||||
|
memcpy(result + (pos - content), with, strlen(with));
|
||||||
|
memcpy(result + (pos - content) + strlen(with), pos + strlen(what), strlen(content) - (pos - content) - strlen(what) + 1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char * linux_instructions =
|
||||||
|
"You are a linux master and are able to transform the prompt of "
|
||||||
|
"user into shell commands that will be executed on a debian "
|
||||||
|
"based system. You can execute shell commands by responding with python code: "
|
||||||
|
"literally `\"\"\"!system\"\"\",\"\"\"`your shell command`\"\"\". Execute immediately. Read bash history file if you have to work with history.";
|
||||||
|
char * retoor_instructions = "If the user prompts with social talk, "
|
||||||
|
"respond like replica and emoji. Your name is retoor and made by molodetz. Be interested. Be creative.";
|
||||||
|
|
||||||
bool openai_include(char *path) {
|
bool openai_include(char *path) {
|
||||||
|
if(!strcmp(path,"retoor")){
|
||||||
|
render("**Loading retoor.**");
|
||||||
|
size_t new_size = strlen(linux_instructions) + strlen(retoor_instructions)+10;
|
||||||
|
char * all = (char *)malloc(new_size);
|
||||||
|
memset(all,0,new_size);
|
||||||
|
strcpy(all,linux_instructions);
|
||||||
|
strcat(all,retoor_instructions);
|
||||||
|
openai_chat("system",all);
|
||||||
|
free(all);
|
||||||
|
render("**Retoor is loaded. Retoor can do bash. Retoor can do all the bash.**");
|
||||||
|
render("Let's diagnose your computer and network together by asking things like: \n"
|
||||||
|
" 1. how many devices are there on my network? Check also MDNS devices. \n"
|
||||||
|
" 2 does my network does something suspicious? \n"
|
||||||
|
" 3. can you benchmark https://molodetz.nl?\n"
|
||||||
|
" 4. do i run strange processes?\n"
|
||||||
|
" 5. what is the performance of my pc?\n"
|
||||||
|
" 6. describe my hardware.\n"
|
||||||
|
" 7. please make a backup from my current directory.\n"
|
||||||
|
" 8. find ten largest folders on my pc using sudo.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
FILE *file = fopen(path, "r");
|
FILE *file = fopen(path, "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
return false;
|
return false;
|
||||||
@ -200,8 +393,11 @@ bool openai_include(char *path) {
|
|||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
buffer[read] = '\0';
|
buffer[read] = '\0';
|
||||||
openai_system(buffer);
|
char * replaced_linux = strreplace(buffer,"[linux]", linux_instructions);
|
||||||
|
char * replaced_retoor = strreplace(replaced_linux,"[retoor]", retoor_instructions);
|
||||||
|
openai_system(replaced_retoor);
|
||||||
|
free(replaced_retoor);
|
||||||
|
free(replaced_linux);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -212,15 +408,15 @@ void init() {
|
|||||||
const char *locale = setlocale(LC_ALL, NULL);
|
const char *locale = setlocale(LC_ALL, NULL);
|
||||||
char payload[4096] = {0};
|
char payload[4096] = {0};
|
||||||
sprintf(payload, "Your locale is %s. User lang is %s.", locale, locale);
|
sprintf(payload, "Your locale is %s. User lang is %s.", locale, locale);
|
||||||
printf("%s", "Loading... ⏳");
|
fprintf(stderr, "%s", "Loading... ⏳");
|
||||||
openai_system(payload);
|
openai_system(payload);
|
||||||
if(!openai_include(".rcontext.txt")){
|
if(!openai_include(".rcontext.txt")){
|
||||||
openai_include("~/.rcontext.txt");
|
openai_include("~/.rcontext.txt");
|
||||||
}
|
}
|
||||||
#ifndef FREE_VERSION
|
#ifndef FREE_VERSION
|
||||||
printf("%s", "\râś… Commercial version. Type help for features.\n");
|
fprintf(stderr, "%s", "\râś… Commercial version. Type help for features.\n");
|
||||||
#else
|
#else
|
||||||
printf("%s","\râś… Free version (GPT-3.5 Turbo), for you by retoor.\n");
|
fprintf(stderr, "%s","\râś… Free version (GPT-3.5 Turbo), for you by retoor.\n");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
messages.h
31
messages.h
@ -12,6 +12,8 @@
|
|||||||
#ifndef R_MESSAGES_H
|
#ifndef R_MESSAGES_H
|
||||||
#define R_MESSAGES_H
|
#define R_MESSAGES_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "tools.h"
|
||||||
#include "json-c/json.h"
|
#include "json-c/json.h"
|
||||||
|
|
||||||
struct json_object *message_array = NULL;
|
struct json_object *message_array = NULL;
|
||||||
@ -23,11 +25,38 @@ struct json_object *message_list() {
|
|||||||
return message_array;
|
return message_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct json_object *message_add_tool_call(struct json_object *message) {
|
||||||
|
struct json_object *messages = message_list();
|
||||||
|
json_object_array_add(messages, message);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
struct json_object *message_add_tool_result(char * tool_call_id, char * tool_result) {
|
||||||
|
struct json_object *messages = message_list();
|
||||||
|
struct json_object * message = json_object_new_object();
|
||||||
|
|
||||||
|
json_object_object_add(message, "tool_call_id", json_object_new_string(tool_call_id));
|
||||||
|
json_object_object_add(message, "tool_result", json_object_new_string(tool_result));
|
||||||
|
|
||||||
|
json_object_array_add(messages, message);
|
||||||
|
return message;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
struct json_object *message_add(const char *role, const char *content) {
|
struct json_object *message_add(const char *role, const char *content) {
|
||||||
struct json_object *messages = message_list();
|
struct json_object *messages = message_list();
|
||||||
struct json_object *message = json_object_new_object();
|
struct json_object *message = json_object_new_object();
|
||||||
json_object_object_add(message, "role", json_object_new_string(role));
|
json_object_object_add(message, "role", json_object_new_string(role));
|
||||||
|
|
||||||
|
|
||||||
json_object_object_add(message, "content", json_object_new_string(content));
|
json_object_object_add(message, "content", json_object_new_string(content));
|
||||||
|
|
||||||
|
if(!strcmp(role, "user")) {
|
||||||
|
json_object_object_add(message, "tools", tools_descriptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
json_object_array_add(messages, message);
|
json_object_array_add(messages, message);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
@ -43,4 +72,4 @@ void message_free() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
51
openai.h
51
openai.h
@ -31,10 +31,13 @@ bool openai_system(char* content) {
|
|||||||
return is_done;
|
return is_done;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* openai_chat(char* role, char* content) {
|
|
||||||
char* url = "https://api.openai.com/v1/chat/completions";
|
struct json_object * chat_message(char * url, char * data){
|
||||||
char* data = chat_json(role, content);
|
printf("QQQ %s\n",data);
|
||||||
|
|
||||||
char* result = curl_post(url, data);
|
char* result = curl_post(url, data);
|
||||||
|
printf("<<%s>>",result);
|
||||||
|
|
||||||
struct json_object* parsed_json = json_tokener_parse(result);
|
struct json_object* parsed_json = json_tokener_parse(result);
|
||||||
if (!parsed_json) {
|
if (!parsed_json) {
|
||||||
fprintf(stderr, "Failed to parse JSON.\n");
|
fprintf(stderr, "Failed to parse JSON.\n");
|
||||||
@ -56,20 +59,50 @@ char* openai_chat(char* role, char* content) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct json_object* message_object;
|
struct json_object* message_object;
|
||||||
if (!json_object_object_get_ex(first_choice, "message", &message_object)) {
|
if (!json_object_object_get_ex(first_choice, "message", &message_object)) {
|
||||||
fprintf(stderr, "Failed to get 'message' object.\n");
|
fprintf(stderr, "Failed to get 'message' object.\n");
|
||||||
json_object_put(parsed_json);
|
json_object_put(parsed_json);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
return message_object;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* openai_chat(char* role, char* content) {
|
||||||
|
char* url = "https://api.openai.com/v1/chat/completions";
|
||||||
|
char* data = chat_json(role, content);
|
||||||
|
|
||||||
|
json_object * message_object = chat_message(url, data);
|
||||||
|
|
||||||
|
struct json_object *tool_calls;
|
||||||
|
json_object_object_get_ex(message_object, "tool_calls", &tool_calls);
|
||||||
|
|
||||||
|
if (tool_calls) {
|
||||||
|
message_add_tool_call(message_object);
|
||||||
|
// char* tool_calls_str = (char *)json_object_to_json_string(tool_calls);
|
||||||
|
json_object * tool_call_results = tools_execute(tool_calls);
|
||||||
|
int array_len = json_object_array_length(tool_call_results);
|
||||||
|
// message_add_tool_call(tool_call_results);
|
||||||
|
for(int i = 0; i < array_len; i++){
|
||||||
|
|
||||||
|
json_object * tool_call_result = json_object_array_get_idx(tool_call_results, i);
|
||||||
|
message_add_tool_call(tool_call_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
char * tool_calls_result_str = chat_json(NULL,NULL);
|
||||||
|
|
||||||
|
message_object = chat_message(url, tool_calls_result_str);
|
||||||
|
message_add_tool_call(message_object);
|
||||||
|
|
||||||
|
}
|
||||||
char* content_str = (char*)json_object_get_string(json_object_object_get(message_object, "content"));
|
char* content_str = (char*)json_object_get_string(json_object_object_get(message_object, "content"));
|
||||||
message_add("assistant", content_str);
|
|
||||||
free(data);
|
|
||||||
free(result);
|
//message_add("assistant", content_str);
|
||||||
|
// free(data);
|
||||||
|
|
||||||
char* final_result = strdup(content_str);
|
char* final_result = strdup(content_str);
|
||||||
json_object_put(parsed_json);
|
|
||||||
return final_result;
|
return final_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
134
tools.h
Normal file
134
tools.h
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
#ifndef R_TOOLS_H
|
||||||
|
#define R_TOOLS_H
|
||||||
|
|
||||||
|
#include <json-c/json.h>
|
||||||
|
#include <json-c/json_object.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "http_curl.h"
|
||||||
|
|
||||||
|
struct json_object * tool_description_http_get();
|
||||||
|
|
||||||
|
struct json_object * tools_descriptions(){
|
||||||
|
struct json_object *root = json_object_new_array();
|
||||||
|
json_object_array_add(root, tool_description_http_get());
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char * tool_function_http_get(char * url) {
|
||||||
|
return curl_get(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct json_object * tool_description_http_get(){
|
||||||
|
// Create root object
|
||||||
|
struct json_object *root = json_object_new_object();
|
||||||
|
json_object_object_add(root, "type", json_object_new_string("function"));
|
||||||
|
|
||||||
|
// Create function object
|
||||||
|
struct json_object *function = json_object_new_object();
|
||||||
|
json_object_object_add(function, "name", json_object_new_string("http_fetch"));
|
||||||
|
json_object_object_add(function, "description", json_object_new_string("Get the contents of an url."));
|
||||||
|
|
||||||
|
// Create parameters object
|
||||||
|
struct json_object *parameters = json_object_new_object();
|
||||||
|
json_object_object_add(parameters, "type", json_object_new_string("object"));
|
||||||
|
|
||||||
|
// Create properties object
|
||||||
|
struct json_object *properties = json_object_new_object();
|
||||||
|
struct json_object *url = json_object_new_object();
|
||||||
|
json_object_object_add(url, "type", json_object_new_string("string"));
|
||||||
|
json_object_object_add(url, "description", json_object_new_string("fetch url contents."));
|
||||||
|
json_object_object_add(properties, "url", url);
|
||||||
|
|
||||||
|
json_object_object_add(parameters, "properties", properties);
|
||||||
|
|
||||||
|
// Add required array
|
||||||
|
struct json_object *required = json_object_new_array();
|
||||||
|
json_object_array_add(required, json_object_new_string("url"));
|
||||||
|
json_object_object_add(parameters, "required", required);
|
||||||
|
|
||||||
|
// Add additionalProperties as false
|
||||||
|
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
|
||||||
|
|
||||||
|
// Add parameters to function object
|
||||||
|
json_object_object_add(function, "parameters", parameters);
|
||||||
|
|
||||||
|
// Add strict mode
|
||||||
|
json_object_object_add(function, "strict", json_object_new_boolean(1));
|
||||||
|
|
||||||
|
// Add function object to root
|
||||||
|
json_object_object_add(root, "function", function);
|
||||||
|
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct json_object * tools_execute(struct json_object * tools_array){
|
||||||
|
//struct json_object * tools_result = json_object_new_object();
|
||||||
|
struct json_object * tools_result_messages = json_object_new_array();
|
||||||
|
int array_len = json_object_array_length(tools_array);
|
||||||
|
// Iterate over array
|
||||||
|
for (int i = 0; i < array_len; i++) {
|
||||||
|
struct json_object *obj = json_object_array_get_idx(tools_array, i);
|
||||||
|
struct json_object * tool_result = json_object_new_object();
|
||||||
|
|
||||||
|
|
||||||
|
json_object_object_add(tool_result, "tool_call_id", json_object_new_string(json_object_get_string(json_object_object_get(obj, "id"))));
|
||||||
|
|
||||||
|
json_object_object_add(tool_result, "role", json_object_new_string("tool"));
|
||||||
|
|
||||||
|
// Get "id"
|
||||||
|
struct json_object *id_obj;
|
||||||
|
if (json_object_object_get_ex(obj, "id", &id_obj)) {
|
||||||
|
|
||||||
|
|
||||||
|
printf("ID: %s\n", json_object_get_string(id_obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get "type"
|
||||||
|
struct json_object *type_obj;
|
||||||
|
if (json_object_object_get_ex(obj, "type", &type_obj)) {
|
||||||
|
printf("Type: %s\n", json_object_get_string(type_obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get "function" object
|
||||||
|
struct json_object *function_obj;
|
||||||
|
if (json_object_object_get_ex(obj, "function", &function_obj)) {
|
||||||
|
// Get function "name"
|
||||||
|
struct json_object *name_obj;
|
||||||
|
char * function_name = NULL;
|
||||||
|
if (json_object_object_get_ex(function_obj, "name", &name_obj)) {
|
||||||
|
function_name = (char *)json_object_get_string(name_obj);
|
||||||
|
printf("Function Name: %s\n", json_object_get_string(name_obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get function "arguments"
|
||||||
|
if(!strcmp(function_name,"http_fetch")){
|
||||||
|
|
||||||
|
|
||||||
|
struct json_object *arguments_obj;
|
||||||
|
if (json_object_object_get_ex(function_obj, "arguments", &arguments_obj)) {
|
||||||
|
struct json_object *arguments = json_tokener_parse(json_object_get_string(arguments_obj));;
|
||||||
|
struct json_object *url_obj;
|
||||||
|
if (json_object_object_get_ex(arguments, "url", &url_obj)) {
|
||||||
|
char * url = (char *)json_object_get_string(url_obj);
|
||||||
|
char * http_result = curl_get(url);
|
||||||
|
printf("URL: %s\n", json_object_get_string(url_obj));
|
||||||
|
printf("content: %s\n",http_result);
|
||||||
|
json_object_object_add(tool_result, "content", json_object_new_string(http_result));
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Function Arguments: %s\n", json_object_get_string(arguments_obj));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
json_object_array_add(tools_result_messages, tool_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n"); // Add spacing for readability
|
||||||
|
}
|
||||||
|
|
||||||
|
//json_object_object_add(tools_result, "messages", tools_result_messages);
|
||||||
|
return tools_result_messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user