From 413c3b959979b39f523897375abbf4bc1b0298f0 Mon Sep 17 00:00:00 2001 From: retoor Date: Sat, 4 Jan 2025 16:54:48 +0100 Subject: [PATCH] Added auth. --- .gitignore | 2 -- .rcontext.txt | 15 +++++++++++++++ auth.h | 24 ++++++++++++++++++++++++ http.h | 4 ++-- main.c | 24 +++++++++++------------- 5 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 .rcontext.txt create mode 100644 auth.h diff --git a/.gitignore b/.gitignore index 6a9a1fe..d48cb43 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,6 @@ .venv .history .backup* -auth.h -context.txt gpt gpt.c r diff --git a/.rcontext.txt b/.rcontext.txt new file mode 100644 index 0000000..203eb53 --- /dev/null +++ b/.rcontext.txt @@ -0,0 +1,15 @@ +Alle vragen die je krijgt en beantwoordt, worden behandeld in de context van de programmeertalen C of Python. +Alle voorbeelden die je geeft, moeten in een van deze programmeertalen zijn. + +Geef in elk antwoord aan of de gekozen taal de juiste keuze is voor het beschreven probleem. Noem één alternatief als dat niet het geval is. + +Als je wordt gevraagd om een review te doen: + - Geef de broncode een cijfer tussen 0 en 10. + - Geef een korte samenvatting van je review. + - Noem drie verbeterpunten voor de broncode. + - Noem drie sterke punten van de broncode. + +Als je wordt gevraagd om te controleren op AI-gebruik: + - Verdeel de broncode in categorieën. + - Wijs elke categorie een percentage van AI-gebruik toe. + - Geef een conclusie over de authenticiteit van de code. \ No newline at end of file diff --git a/auth.h b/auth.h new file mode 100644 index 0000000..56ef286 --- /dev/null +++ b/auth.h @@ -0,0 +1,24 @@ +// Written by retoor@molodetz.nl + +// This source code declares a constant character pointer variable with a value representing an API key. + + + +// MIT License + +#include + +const char * resolve_api_key(){ + char * api_key = getenv("R_KEY"); + if(api_key) + { + return api_key; + } + api_key = getenv("OPENAI_API_KEY"); + if(api_key) + { + return api_key; + } + fprintf(stderr, "There is no API key configured in environment."); + return ""; +} \ No newline at end of file diff --git a/http.h b/http.h index 7506248..1aff961 100644 --- a/http.h +++ b/http.h @@ -103,7 +103,7 @@ char *http_post(const char *hostname, char *url, char *data) { "Host: api.openai.com\r\n" "Authorization: Bearer %s\r\n" "Connection: close\r\n\r\n%s", - url, len, api_key, data); + url, len, resolve_api_key(), data); SSL_write(ssl, request, strlen(request)); free(request); @@ -147,7 +147,7 @@ char *http_get(const char *hostname, char *url) { "Host: api.openai.com\r\n" "Authorization: Bearer %s\r\n" "Connection: close\r\n\r\n", - url, api_key); + url, resolve_api_key()); SSL_write(ssl, request, strlen(request)); diff --git a/main.c b/main.c index 20f0305..4f523c5 100644 --- a/main.c +++ b/main.c @@ -86,7 +86,6 @@ void render(char *content) { void repl() { line_init(); - setbuf(stdout, NULL); char *line; char *previous_line = NULL; while ((line = line_read("> "))) { @@ -164,10 +163,10 @@ void help() { render(help_text); } -void openai_include(char *path) { +bool openai_include(char *path) { FILE *file = fopen(path, "r"); if (file == NULL) { - return; + return false; } fseek(file, 0, SEEK_END); long size = ftell(file); @@ -176,7 +175,7 @@ void openai_include(char *path) { char *buffer = (char *)malloc(size); size_t read = fread(buffer, 1, size, file); if (read == 0) { - return; + return false; } fclose(file); @@ -184,22 +183,21 @@ void openai_include(char *path) { openai_system(buffer); free(buffer); + return true; } void init() { + setbuf(stdout, NULL); line_init(); const char *locale = setlocale(LC_ALL, NULL); char payload[4096] = {0}; - sprintf(payload, "User locale is %s. User lang is %s.\n" - "You are Retoor. Use a lot of markdown in response.\n" - "Be confident and short in answers.\n" - "You divide things by zero if you have to.", - locale, locale); - - printf("%s", "Loading..."); + sprintf(payload, "Your locale is %s. User lang is %s.", locale, locale); + printf("%s", "Loading... ⏳"); openai_system(payload); - openai_include("context.txt"); - printf("%s", "\rLoaded! Type help for features.\n"); + if(!openai_include(".rcontext.txt")){ + openai_include("~/.rcontext.txt"); + } + printf("%s", "\r✅ Type help for features.\n"); } int main(int argc, char *argv[]) {