Everything safe.
This commit is contained in:
parent
de80b013c0
commit
016e335a23
3
chat.h
3
chat.h
@ -16,7 +16,6 @@ void chat_free(){
|
|||||||
_prompt = NULL;
|
_prompt = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char * chat_json(char * role, char * message){
|
char * chat_json(char * role, char * message){
|
||||||
chat_free();
|
chat_free();
|
||||||
message_add(role,message);
|
message_add(role,message);
|
||||||
@ -28,4 +27,4 @@ json_object_object_add(root_object, "temperature", json_object_new_double(prompt
|
|||||||
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
|
17
http.h
17
http.h
@ -96,22 +96,20 @@ char *http_post(const char *hostname, char *url, char *data)
|
|||||||
SSL_set_tlsext_host_name(ssl, hostname);
|
SSL_set_tlsext_host_name(ssl, hostname);
|
||||||
|
|
||||||
SSL_set_fd(ssl, sock);
|
SSL_set_fd(ssl, sock);
|
||||||
|
|
||||||
int buffer_size = 4096;
|
int buffer_size = 4096;
|
||||||
char *buffer = (char *)malloc(buffer_size);
|
char *buffer = (char *)malloc(buffer_size);
|
||||||
|
|
||||||
|
|
||||||
if (SSL_connect(ssl) <= 0)
|
if (SSL_connect(ssl) <= 0)
|
||||||
{
|
{
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
|
|
||||||
size_t len = strlen(data);
|
size_t len = strlen(data);
|
||||||
char *request = (char *)malloc(len + 4096);
|
char *request = (char *)malloc(len + 4096);
|
||||||
request[0] = 0;
|
request[0] = 0;
|
||||||
|
|
||||||
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"
|
||||||
@ -120,11 +118,11 @@ char *http_post(const char *hostname, char *url, char *data)
|
|||||||
"Authorization: Bearer %s\r\n"
|
"Authorization: Bearer %s\r\n"
|
||||||
"Connection: close\r\n\r\n%s",
|
"Connection: close\r\n\r\n%s",
|
||||||
url, len, api_key, data);
|
url, len, api_key, data);
|
||||||
|
|
||||||
SSL_write(ssl, request, strlen(request));
|
SSL_write(ssl, request, strlen(request));
|
||||||
free(request);
|
free(request);
|
||||||
|
|
||||||
|
|
||||||
int bytes;
|
int bytes;
|
||||||
int bytes_total = 0;
|
int bytes_total = 0;
|
||||||
while ((bytes = SSL_read(ssl, buffer + bytes_total, buffer_size - 1)) > 0)
|
while ((bytes = SSL_read(ssl, buffer + bytes_total, buffer_size - 1)) > 0)
|
||||||
@ -173,7 +171,6 @@ char *http_get(const char *hostname, char *url)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
|
|
||||||
|
|
||||||
char request[buffer_size];
|
char request[buffer_size];
|
||||||
request[0] = 0;
|
request[0] = 0;
|
||||||
@ -207,4 +204,4 @@ char *http_get(const char *hostname, char *url)
|
|||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
96
main.c
96
main.c
@ -5,29 +5,29 @@
|
|||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
char * get_prompt_from_args(int c, char **argv){
|
char * get_prompt_from_args(int c, char **argv) {
|
||||||
char * prompt = malloc(1024*1024 + 1);
|
char * prompt = malloc(1024*1024 + 1);
|
||||||
prompt[0] = 0;
|
prompt[0] = 0;
|
||||||
for(int i = 1; i < c; i++){
|
for(int i = 1; i < c; i++) {
|
||||||
if(argv[i][0] == '-')
|
if(argv[i][0] == '-')
|
||||||
break;
|
break;
|
||||||
strncat(prompt, argv[i], 1024*1024);
|
strncat(prompt, argv[i], 1024*1024);
|
||||||
if(i < c - 1){
|
if(i < c - 1) {
|
||||||
strncat(prompt, " ", 1024*1024);
|
strncat(prompt, " ", 1024*1024);
|
||||||
}else{
|
} else {
|
||||||
strncat(prompt, ".", 1024*1024);
|
strncat(prompt, ".", 1024*1024);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!*prompt){
|
if(!*prompt) {
|
||||||
free(prompt);
|
free(prompt);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return prompt;
|
return prompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool try_prompt(int argc,char*argv[]){
|
bool try_prompt(int argc,char*argv[]) {
|
||||||
char * prompt = get_prompt_from_args(argc, argv);
|
char * prompt = get_prompt_from_args(argc, argv);
|
||||||
if(prompt != NULL){
|
if(prompt != NULL) {
|
||||||
char * response = openai_chat("user",prompt);
|
char * response = openai_chat("user",prompt);
|
||||||
parse_markdown_to_ansi(response);
|
parse_markdown_to_ansi(response);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -40,59 +40,54 @@ bool try_prompt(int argc,char*argv[]){
|
|||||||
|
|
||||||
void help();
|
void help();
|
||||||
void render(char *);
|
void render(char *);
|
||||||
void serve(){
|
void serve() {
|
||||||
render("Starting server. *Put executables in a dir named cgi-bin and they will behave as webpages.*");
|
render("Starting server. *Put executables in a dir named cgi-bin and they will behave as webpages.*");
|
||||||
int res = system("python3 -m http.server --cgi");
|
int res = system("python3 -m http.server --cgi");
|
||||||
// Thanks tsoding!
|
|
||||||
(void)res;
|
(void)res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void render(char * content) {
|
||||||
void render(char * content){
|
|
||||||
parse_markdown_to_ansi(content);
|
parse_markdown_to_ansi(content);
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void repl(){
|
void repl() {
|
||||||
line_init();
|
line_init();
|
||||||
setbuf(stdout, NULL);
|
setbuf(stdout, NULL);
|
||||||
char *line;
|
char *line;
|
||||||
char *previous_line = NULL;
|
char *previous_line = NULL;
|
||||||
while((line = line_read("> "))){
|
while((line = line_read("> "))) {
|
||||||
if(!line || !*line){
|
if(!line || !*line) {
|
||||||
line = previous_line;
|
line = previous_line;
|
||||||
}
|
}
|
||||||
if(!line || !*line)
|
if(!line || !*line)
|
||||||
continue;
|
continue;
|
||||||
previous_line = line;
|
previous_line = line;
|
||||||
if(line[0] == '!'){
|
if(line[0] == '!') {
|
||||||
plugin_run(line + 1);
|
plugin_run(line + 1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(!strncmp(line,"exit", 4)){
|
if(!strncmp(line,"exit", 4)) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
if(!strncmp(line,"help",4)){
|
if(!strncmp(line,"help",4)) {
|
||||||
help();
|
help();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(!strncmp(line,"serve",5)){
|
if(!strncmp(line,"serve",5)) {
|
||||||
serve();
|
serve();
|
||||||
}
|
}
|
||||||
if(!strncmp(line,"spar ",5)){
|
if(!strncmp(line,"spar ",5)) {
|
||||||
char * response = line+5;
|
char * response = line+5;
|
||||||
while(true){
|
while(true) {
|
||||||
render(response);
|
render(response);
|
||||||
sleep(2);
|
sleep(2);
|
||||||
//line = line_read("> ");
|
response = openai_chat("user",response);
|
||||||
//if(!*line)
|
}
|
||||||
response = openai_chat("user",response);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(!strncmp(line,"ls",2) || !strncmp(line,"list",4)){
|
if(!strncmp(line,"ls",2) || !strncmp(line,"list",4)) {
|
||||||
int offset = 2;
|
int offset = 2;
|
||||||
if(!strncmp(line,"list",4)){
|
if(!strncmp(line,"list",4)) {
|
||||||
offset = 4;
|
offset = 4;
|
||||||
}
|
}
|
||||||
char * command = (char *)malloc(strlen(line) + 42);
|
char * command = (char *)malloc(strlen(line) + 42);
|
||||||
@ -112,8 +107,7 @@ void repl(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void help() {
|
||||||
void help(){
|
|
||||||
char help_text[1024*1024] = {0};
|
char help_text[1024*1024] = {0};
|
||||||
char * template = "# Help\n"
|
char * template = "# Help\n"
|
||||||
"Written by retoor@molodetz.nl.\n\n"
|
"Written by retoor@molodetz.nl.\n\n"
|
||||||
@ -133,13 +127,13 @@ void help(){
|
|||||||
" - **google search** and actions with those results.\n"
|
" - **google search** and actions with those results.\n"
|
||||||
" - **reminders**.\n"
|
" - **reminders**.\n"
|
||||||
" - predefined **templates** for **reviewing** / **refactoring** so you can personalize.\n";
|
" - predefined **templates** for **reviewing** / **refactoring** so you can personalize.\n";
|
||||||
sprintf(help_text,template,prompt_temperature,prompt_model,prompt_max_tokens);
|
sprintf(help_text,template,prompt_temperature,prompt_model,prompt_max_tokens);
|
||||||
render(help_text);
|
render(help_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void openai_include(char * path){
|
void openai_include(char * path) {
|
||||||
FILE * file = fopen(path,"r");
|
FILE * file = fopen(path,"r");
|
||||||
if(file == NULL){
|
if(file == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
@ -148,7 +142,7 @@ void openai_include(char * path){
|
|||||||
|
|
||||||
char * buffer = (char *)malloc(size);
|
char * buffer = (char *)malloc(size);
|
||||||
size_t read = fread(buffer,1,size,file);
|
size_t read = fread(buffer,1,size,file);
|
||||||
if(read == 0){
|
if(read == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,27 +153,27 @@ void openai_include(char * path){
|
|||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(){
|
void init() {
|
||||||
line_init();
|
line_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, "User locale is %s. User lang is %s.\n"
|
sprintf(payload, "User locale is %s. User lang is %s.\n"
|
||||||
"You are Retoor. Use a lot of markdown in response.\n"
|
"You are Retoor. Use a lot of markdown in response.\n"
|
||||||
"Be confident and short in answers.\n"
|
"Be confident and short in answers.\n"
|
||||||
"You divide things by zero if you have to."
|
"You divide things by zero if you have to."
|
||||||
, locale, locale);
|
, locale, locale);
|
||||||
|
|
||||||
printf("%s","Loading...");
|
printf("%s","Loading...");
|
||||||
openai_system(payload);
|
openai_system(payload);
|
||||||
openai_include("context.txt");
|
openai_include("context.txt");
|
||||||
printf("%s", "\rLoaded! Type help for feautures.\n");
|
printf("%s", "\rLoaded! Type help for feautures.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]) {
|
||||||
init();
|
init();
|
||||||
if(try_prompt(argc,argv))
|
if(try_prompt(argc,argv))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
repl();
|
repl();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -29,4 +29,4 @@ void message_free(){
|
|||||||
_message_array = NULL;
|
_message_array = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
10
openai.h
10
openai.h
@ -27,7 +27,6 @@ bool openai_system(char * content){
|
|||||||
return is_done;
|
return is_done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *openai_chat(char * role, char * content){
|
char *openai_chat(char * role, char * content){
|
||||||
const char *hostname = "api.openai.com";
|
const char *hostname = "api.openai.com";
|
||||||
char *url = "/v1/chat/completions";
|
char *url = "/v1/chat/completions";
|
||||||
@ -50,7 +49,6 @@ char *openai_chat(char * role, char * content){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the first element of the "choices" array
|
|
||||||
struct json_object *first_choice = json_object_array_get_idx(choices_array, 0);
|
struct json_object *first_choice = json_object_array_get_idx(choices_array, 0);
|
||||||
if (!first_choice) {
|
if (!first_choice) {
|
||||||
fprintf(stderr, "Failed to get the first element of 'choices'.\n");
|
fprintf(stderr, "Failed to get the first element of 'choices'.\n");
|
||||||
@ -58,7 +56,6 @@ char *openai_chat(char * role, char * content){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the "message" object
|
|
||||||
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");
|
||||||
@ -66,19 +63,14 @@ char *openai_chat(char * role, char * content){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the "message" object
|
|
||||||
// printf("Message object:\n%s\n", json_object_to_json_string_ext(message_object, JSON_C_TO_STRING_PRETTY));
|
|
||||||
message_add("assistant",(char *)json_object_get_string(json_object_object_get(message_object, "content")));
|
message_add("assistant",(char *)json_object_get_string(json_object_object_get(message_object, "content")));
|
||||||
// Clean up
|
|
||||||
free(data);
|
free(data);
|
||||||
free(result);
|
free(result);
|
||||||
result = strdup((char *)json_object_get_string(json_object_object_get(message_object, "content")));
|
result = strdup((char *)json_object_get_string(json_object_object_get(message_object, "content")));
|
||||||
|
|
||||||
json_object_put(parsed_json);
|
json_object_put(parsed_json);
|
||||||
|
|
||||||
//printf("Parsed JSON:\n%s\n", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
3
plugin.h
3
plugin.h
@ -8,9 +8,8 @@ bool plugin_construct(){
|
|||||||
if(plugin_initialized)
|
if(plugin_initialized)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
Py_Initialize();
|
Py_Initialize();
|
||||||
|
|
||||||
// Check if Python initialized successfully
|
|
||||||
if (!Py_IsInitialized()) {
|
if (!Py_IsInitialized()) {
|
||||||
fprintf(stderr, "Failed to initialize Python interpreter\n");
|
fprintf(stderr, "Failed to initialize Python interpreter\n");
|
||||||
return plugin_initialized;
|
return plugin_initialized;
|
||||||
|
Loading…
Reference in New Issue
Block a user