shithub: oai

Download patch

ref: b18c42cd8e69bf915eab3e83484aec0efae97063
parent: 6ff6f63950fa65e7d3ca0981f572bb31ac328796
author: sirjofri <sirjofri@sirjofri.de>
date: Tue Dec 30 14:01:41 EST 2025

optional arguments, fetch from env. document oai.h

--- a/README
+++ b/README
@@ -17,10 +17,12 @@
 
 USAGE:
 
-oai [-k apikey] [-m model] baseurl
-ocomplete [-k apikey] [-m model] baseurl
+oai [-k apikey] [-m model] [-u baseurl]
+ocomplete [-k apikey] [-m model] [-u baseurl]
 
 baseurl is the http url without the v1/... stuff, with llama-server this is usually just http://server:8080.
+
+The apikey and the baseurl are optional if you set them as environment variables ($oaikey and $oaiurl).
 
 After that, you get a user: prompt for your user messages.
 
--- a/oai.c
+++ b/oai.c
@@ -6,7 +6,7 @@
 static void
 usage(void)
 {
-	fprint(2, "usage: %s [-k apikey] [-m model] baseurl\n", argv0);
+	fprint(2, "usage: %s [-k apikey] [-m model] [-u baseurl]\n", argv0);
 	exits("usage");
 }
 
@@ -14,6 +14,7 @@
 main(int argc, char **argv)
 {
 	Biobuf *bin;
+	char *url = nil;
 	char *s;
 	ORequest req;
 	OResult res;
@@ -31,12 +32,12 @@
 	case 'm':
 		req.model = EARGF(usage());
 		break;
+	case 'u':
+		url = EARGF(usage());
+		break;
 	}ARGEND;
 	
-	if (argc != 1)
-		usage();
-	
-	if (!initoai(argv[0], key))
+	if (!initoai(url, key))
 		usage();
 	
 	bin = Bfdopen(0, OREAD);
--- a/oai.h
+++ b/oai.h
@@ -18,6 +18,19 @@
 	char *message;
 };
 
+/*
+ * initoai returns 1 on success. If baseurl or apikey is nil, it'll try to fetch the data
+ * from the environment variables $oaiurl and $oaikey. Key is optional.
+ */
 int initoai(char *baseurl, char *apikey);
+
+/*
+ * makerequest to make the request.
+ */
 OResult makerequest(ORequest);
-int addprompt(ORequest*, char *role, char *content, ...);
+
+/*
+ * append a prompt to the existing request. You can set the role and the content.
+ * The content is built from the fmt and the variadic arguments.
+ */
+int addprompt(ORequest*, char *role, char *fmt, ...);
--- a/oailib.c
+++ b/oailib.c
@@ -10,13 +10,17 @@
 int
 initoai(char *url, char *key)
 {
-	baseurl = url;
-	apikey = key;
-	
-	if (!baseurl) {
+	if (!url) 
+		url = getenv("oaiurl");
+	if (!url) {
 		werrstr("invalid baseurl");
 		return 0;
 	}
+	baseurl = url;
+	
+	if (!key)
+		key = getenv("oaikey");
+	apikey = key;
 	
 	JSONfmtinstall();
 	
--- a/ocomplete.c
+++ b/ocomplete.c
@@ -6,7 +6,7 @@
 static void
 usage(void)
 {
-	fprint(2, "usage: %s [-k apikey] [-m model] baseurl\n", argv0);
+	fprint(2, "usage: %s [-k apikey] [-m model] [-u baseurl]\n", argv0);
 	exits("usage");
 }
 
@@ -20,6 +20,7 @@
 	char *body, *rdsel;
 	Biobuf *bodyin, *selio;
 	char *winid;
+	char *url = nil;
 	
 	req.prompts = nil;
 	req.model = nil;
@@ -33,16 +34,16 @@
 	case 'm':
 		req.model = EARGF(usage());
 		break;
+	case 'u':
+		url = EARGF(usage());
+		break;
 	}ARGEND;
 	
-	if (argc != 1)
-		usage();
-	
 	winid = getenv("winid");
 	if (!winid || winid[0] == 0)
 		sysfatal("not in acme");
 	
-	if (!initoai(argv[0], key))
+	if (!initoai(url, key))
 		sysfatal("initoai: %r");
 	
 	snprint(buf, sizeof buf, "/mnt/acme/%s/body", winid);
--