diff options
| author | Arun Isaac | 2026-08-06 02:14:57 +0100 |
|---|---|---|
| committer | Arun Isaac | 2026-08-06 02:14:57 +0100 |
| commit | 54385b3100cbcf451be172f7faa13759f84d92e0 (patch) | |
| tree | 5729faa6e3cfcb282295126a3dde5d9a26a5b302 | |
| parent | 98b89fb73f22845a353fa88f09a7109ac6851477 (diff) | |
| download | kaagum-54385b3100cbcf451be172f7faa13759f84d92e0.tar.gz kaagum-54385b3100cbcf451be172f7faa13759f84d92e0.tar.lz kaagum-54385b3100cbcf451be172f7faa13759f84d92e0.zip | |
openai: Return the &http-error condition on completions API failure.
| -rw-r--r-- | kaagum/openai.scm | 39 |
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 |
