Added auth.

This commit is contained in:
retoor 2025-01-04 16:54:48 +01:00
parent c05edc45d9
commit 413c3b9599
5 changed files with 52 additions and 17 deletions

2
.gitignore vendored
View File

@ -2,8 +2,6 @@
.venv .venv
.history .history
.backup* .backup*
auth.h
context.txt
gpt gpt
gpt.c gpt.c
r r

15
.rcontext.txt Normal file
View File

@ -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.

24
auth.h Normal file
View File

@ -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 <stdlib.h>
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 "";
}

4
http.h
View File

@ -103,7 +103,7 @@ char *http_post(const char *hostname, char *url, char *data) {
"Host: api.openai.com\r\n" "Host: api.openai.com\r\n"
"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, resolve_api_key(), data);
SSL_write(ssl, request, strlen(request)); SSL_write(ssl, request, strlen(request));
free(request); free(request);
@ -147,7 +147,7 @@ char *http_get(const char *hostname, char *url) {
"Host: api.openai.com\r\n" "Host: api.openai.com\r\n"
"Authorization: Bearer %s\r\n" "Authorization: Bearer %s\r\n"
"Connection: close\r\n\r\n", "Connection: close\r\n\r\n",
url, api_key); url, resolve_api_key());
SSL_write(ssl, request, strlen(request)); SSL_write(ssl, request, strlen(request));

24
main.c
View File

@ -86,7 +86,6 @@ void render(char *content) {
void repl() { void repl() {
line_init(); line_init();
setbuf(stdout, NULL);
char *line; char *line;
char *previous_line = NULL; char *previous_line = NULL;
while ((line = line_read("> "))) { while ((line = line_read("> "))) {
@ -164,10 +163,10 @@ void help() {
render(help_text); render(help_text);
} }
void openai_include(char *path) { bool openai_include(char *path) {
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
if (file == NULL) { if (file == NULL) {
return; return false;
} }
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
long size = ftell(file); long size = ftell(file);
@ -176,7 +175,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 false;
} }
fclose(file); fclose(file);
@ -184,22 +183,21 @@ void openai_include(char *path) {
openai_system(buffer); openai_system(buffer);
free(buffer); free(buffer);
return true;
} }
void init() { void init() {
setbuf(stdout, NULL);
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, "Your locale is %s. User lang is %s.", locale, locale);
"You are Retoor. Use a lot of markdown in response.\n" printf("%s", "Loading... ⏳");
"Be confident and short in answers.\n"
"You divide things by zero if you have to.",
locale, locale);
printf("%s", "Loading...");
openai_system(payload); openai_system(payload);
openai_include("context.txt"); if(!openai_include(".rcontext.txt")){
printf("%s", "\rLoaded! Type help for features.\n"); openai_include("~/.rcontext.txt");
}
printf("%s", "\r✅ Type help for features.\n");
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {