summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c54
1 files changed, 13 insertions, 41 deletions
diff --git a/main.c b/main.c
index e89ed7a..e46c515 100644
--- a/main.c
+++ b/main.c
@@ -1,13 +1,9 @@
#include <stdio.h>
-#include <stdbool.h>
-#include <ctype.h>
#include <err.h>
#include <errno.h>
-#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/un.h>
-#include <sys/stat.h>
#include <sys/socket.h>
#include "llm.h"
@@ -15,9 +11,9 @@
#define SOCK_PATH "/var/run/lex.sock"
#define MODEL_PATH "qwen2.5-3b-instruct-q4_k_m.gguf"
-static void usage(const char *bin)
+static void usage(const char *prog)
{
- errx(1, "usage: %s [-m model_path] [-d] [prompt]", bin);
+ errx(1, "usage: %s [-m model_path]", prog);
}
static void loop(struct llama_model *model, int lsock)
@@ -87,26 +83,19 @@ int sock_init(void)
int main(int argc , char *argv[])
{
- int daemon_mode, sock, opt;
+ int sock, opt;
const char *prog;
- const char *prompt;
const char *model_path;
struct llama_model *model;
- if (pledge("stdio rpath wpath cpath unix", NULL) == -1)
+ if (pledge("stdio rpath wpath cpath unix unveil", NULL) == -1)
err(1, "pledge failed");
- sock = -1;
- daemon_mode = 0;
- prompt = NULL;
prog = argv[0];
model_path = MODEL_PATH;
- while ((opt = getopt(argc, argv, "dm:")) != -1) {
+ while ((opt = getopt(argc, argv, "m:")) != -1) {
switch (opt) {
- case 'd':
- daemon_mode = 1;
- break;
case 'm':
model_path = optarg;
break;
@@ -117,42 +106,25 @@ int main(int argc , char *argv[])
argc -= optind;
argv += optind;
-
- if (daemon_mode) {
- if (argc != 0) // -d takes no positional args
- usage(prog);
- } else {
- if (argc != 1) // normal mode requires one prompt
- usage(prog);
-
- prompt = argv[0];
- }
+ if (argc != 0)
+ usage(prog);
if (unveil(model_path, "r") == -1)
err(1, "unveil %s failed", model_path);
- if (daemon_mode && unveil(SOCK_PATH, "rwc") == -1)
+ if (unveil(SOCK_PATH, "rwc") == -1)
err(1, "unveil %s failed", SOCK_PATH);
if (unveil(NULL, NULL) == -1)
err(1, "unveil lock failed");
- if (daemon_mode) {
- sock = sock_init();
- if (pledge("stdio rpath unix", NULL) == -1)
- err(1, "Failed to narrow pledge after sock_init()");
- }
+ sock = sock_init();
- model = llm_init(model_path);
- if (!model)
+ if (!(model = llm_init(model_path)))
errx(1, "Failed to load model");
- if (daemon_mode)
- loop(model, sock); /* doesn't return */
-
- if (pledge("stdio", NULL) == -1)
- err(1, "Failed to narrow pledge for normal mode");
+ if (pledge("stdio unix", NULL) == -1)
+ err(1, "secondary pledge failed");
- llm_process_request(model, prompt, stdout);
- llm_free(model);
+ loop(model, sock); /* doesn't return */
return 0;
}