about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-04-14 00:20:50 +0100
committerArun Isaac2026-04-14 00:20:50 +0100
commit4002fc3d832d722a09401013b4a6d45151fee155 (patch)
treea4167903f207815503e3664c92851ab7aa6c0f1e
parent1741c47933912435ed6b401540bf32f72fc5f2b4 (diff)
downloadkaagum-4002fc3d832d722a09401013b4a6d45151fee155.tar.gz
kaagum-4002fc3d832d722a09401013b4a6d45151fee155.tar.lz
kaagum-4002fc3d832d722a09401013b4a6d45151fee155.zip
Add openai-models.
We need to fetch the list of available models so we can advertise to
the client.
-rw-r--r--kaagum/openai.scm22
1 files changed, 21 insertions, 1 deletions
diff --git a/kaagum/openai.scm b/kaagum/openai.scm
index 646d15a..5e4cab5 100644
--- a/kaagum/openai.scm
+++ b/kaagum/openai.scm
@@ -24,8 +24,15 @@
   #:use-module (web http)
   #:use-module (web response)
   #:use-module (json)
+  #:use-module (lens)
+  #:use-module (kaagum records)
   #:export (get-api-key
-            openai-query))
+            openai-query
+            openai-models))
+
+(define-public-record-type* (<model> model model?)
+  (fields (name model-name)
+          (description model-description)))
 
 ;; TODO: URIs must not be operated on using string operations. Replace with a
 ;; more principled implementation involving (web uri).
@@ -90,3 +97,16 @@ compatible with the OpenAI API specification."
              #:json `(("model" . ,model)
                       ("messages" . ,(list->vector messages))
                       ("tools" . ,(list->vector tools)))))
+
+(define (openai-models base-uri api-key)
+  "Return available models. The return value is an association list mapping model
+IDs to @code{<model>} objects. @var{base-uri} and @var{api-key} are the same as
+in @code{openai-query}."
+  (map (lambda (tree)
+         (cons (focus (key-ref "id") tree)
+               (model (focus (key-ref "name") tree)
+                      (focus (key-ref "description") tree))))
+       (vector->list (focus (key-ref "data")
+                            (json-get (uri-join base-uri "/v1/models")
+                                      #:headers `((authorization
+                                                   . ,(string-append "Bearer " api-key))))))))