about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--kaagum/openai.scm39
1 files changed, 21 insertions, 18 deletions
diff --git a/kaagum/openai.scm b/kaagum/openai.scm
index 436a755..a18d108 100644
--- a/kaagum/openai.scm
+++ b/kaagum/openai.scm
@@ -31,28 +31,31 @@
           (context-length model-context-length lensed)))
 
 (define (openai-query base-uri api-key model messages tools)
-  "Send a request to the OpenAI completions API and return the JSON response.
+  "Send a request to the OpenAI completions API and return the JSON response or the
+@code{&http-error} condition on failure.
+
 @var{base-uri} is the base URI of the OpenAI-compatible service. @var{api-key}
 is the API key for authentication. @var{model} is a supported LLM model.
 @var{messages} and @var{tools} are respectively lists of JSON messages and tools
 compatible with the OpenAI API specification."
-  (json-post (uri-join base-uri "/v1/chat/completions")
-             #:headers `((authorization
-                          . ,(string-append "Bearer " api-key)))
-             #:json `(("model" . ,model)
-                      ;; Some providers (like Anthropic) require you to
-                      ;; explicitly enable automatic prompt caching. See
-                      ;; https://openrouter.ai/docs/guides/best-practices/prompt-caching
-                      ("cache_control"
-                       ("type" . "ephemeral")
-                       ;; Anthropic offers cheaper cache writes for a ttl of 5
-                       ;; minutes. But, it doesn't make a difference for an
-                       ;; extended session and is not worth it for our use case.
-                       ;; If we change our mind about this, we can make this a
-                       ;; configurable parameter.
-                       ("ttl" . "1h"))
-                      ("messages" . ,(list->vector messages))
-                      ("tools" . ,(list->vector tools)))))
+  (guard (c ((http-error? c) c))
+    (json-post (uri-join base-uri "/v1/chat/completions")
+               #:headers `((authorization
+                            . ,(string-append "Bearer " api-key)))
+               #:json `(("model" . ,model)
+                        ;; Some providers (like Anthropic) require you to
+                        ;; explicitly enable automatic prompt caching. See
+                        ;; https://openrouter.ai/docs/guides/best-practices/prompt-caching
+                        ("cache_control"
+                         ("type" . "ephemeral")
+                         ;; Anthropic offers cheaper cache writes for a ttl of 5
+                         ;; minutes. But, it doesn't make a difference for an
+                         ;; extended session and is not worth it for our use
+                         ;; case. If we change our mind about this, we can make
+                         ;; this a configurable parameter.
+                         ("ttl" . "1h"))
+                        ("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