cl-openai/audio.lisp
2024-09-09 20:58:41 -04:00

31 lines
1.2 KiB
Common Lisp

(in-package #:cl-openai)
(defun make-audio/speech-json-data (model input voice &optional response-format speed)
(cl-json:encode-json-to-string
(remove nil
`(("model" . ,model)
("input" . ,input)
("voice" . ,voice)
,(when response-format `("response_format" . ,response-format))
,(when speed `("speed" . ,speed))))))
(defun audio/speech (model input voice &optional response-format speed)
(let ((json-data (make-audio/speech-json-data model input voice response-format speed))
(uri (server-path "v1/audio/speech")))
(apply #'drakma:http-request uri
(additional-api-args
:method :post
:content json-data))))
(defun save-audio (path response)
(with-open-file (f path :direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede
:if-does-not-exist :create)
(loop :for byte :across response
:do (write-byte byte f))))
(defun audio/speech-and-save (path model input voice &optional response-format speed)
(let ((response (audio/speech model input voice response-format speed)))
(save-audio path response)))