Fix Common Lisp to handle both vector and list responses from /models API

This commit is contained in:
Russell Ballestrini 2025-10-24 14:27:38 -04:00
parent 9aa834852c
commit 390e028769

View file

@ -46,12 +46,14 @@
(format nil "Bearer ~A" (api-key client))))))
(response (dex:get url :headers headers))
(parsed (jonathan:parse response))
(data (getf parsed :|data|)))
(loop for model across data
for model-id = (getf model :|id|)
for max-tokens = (or (getf model :|max_model_len|) 8192)
do (push (make-model-info model-id endpoint max-tokens)
(models client))))
(data (getf parsed :|data|))
;; Coerce to list to handle both vector (vLLM) and list (Ollama) responses
(models-list (coerce data 'list)))
(dolist (model models-list)
(let ((model-id (getf model :|id|))
(max-tokens (or (getf model :|max_model_len|) 8192)))
(push (make-model-info model-id endpoint max-tokens)
(models client)))))
(error (e)
(format t "Warning: Failed to discover models from ~A: ~A~%" endpoint e))))
@ -141,9 +143,10 @@
(unless (string= data "[DONE]")
(handler-case
(let* ((parsed (jonathan:parse data))
(choices (getf parsed :|choices|))
(delta (when (> (length choices) 0)
(getf (aref choices 0) :|delta|)))
;; Coerce choices to list to handle both vector and list responses
(choices (coerce (getf parsed :|choices|) 'list))
(delta (when choices
(getf (first choices) :|delta|)))
(content (when delta (getf delta :|content|))))
content)
(error (e) nil))))))
@ -167,19 +170,19 @@
:|max_tokens| max-tokens
:|temperature| temperature
:|stream| t)))
(headers (make-headers client)))
(dex:request url
:method :post
:headers headers
:content payload
:want-stream t
:stream-callback
(lambda (stream)
(loop for line = (read-line stream nil nil)
while line
do (let ((content (process-sse-line line)))
(when content
(funcall callback content))))))))
(headers (make-headers client))
(stream (dex:request url
:method :post
:headers headers
:content payload
:want-stream t)))
(unwind-protect
(loop for line = (read-line stream nil nil)
while line
do (let ((content (process-sse-line line)))
(when content
(funcall callback content))))
(close stream))))
;;; Text-to-Speech
@ -224,7 +227,9 @@
:max-tokens 100
:temperature 0.7))
(model-used (getf response :|model|))
(choice (aref (getf response :|choices|) 0))
;; Coerce choices to list to handle both vector and list responses
(choices (coerce (getf response :|choices|) 'list))
(choice (first choices))
(message (getf choice :|message|))
(content (getf message :|content|)))
(format t "Model: ~A~%" model-used)