client.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; guile-openai --- An OpenAI API client for Guile
  2. ;;; Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
  3. ;;;
  4. ;;; This file is part of guile-openai.
  5. ;;;
  6. ;;; guile-openai is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU Affero General Public License as
  8. ;;; published by the Free Software Foundation, either version 3 of the
  9. ;;; License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-openai is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Affero General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Affero General Public
  17. ;;; License along with guile-openai. If not, see
  18. ;;; <https://www.gnu.org/licenses/>.
  19. (define-module (openai client)
  20. #:use-module (openai debug)
  21. #:use-module (openai utils event-stream)
  22. #:use-module (openai utils multipart)
  23. #:use-module (openai utils uri)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 textual-ports)
  26. #:use-module (json record)
  27. #:use-module (srfi srfi-8)
  28. #:use-module (srfi srfi-71)
  29. #:use-module (web client)
  30. #:use-module (web request)
  31. #:use-module (web response)
  32. #:export (openai-base-uri
  33. openai-api-key
  34. openai-organization
  35. openai-default-headers
  36. openai-default-user
  37. openai-request
  38. openai-get
  39. openai-post-json
  40. openai-post-multipart))
  41. (define-once openai-base-uri
  42. (make-parameter "https://api.openai.com/" ->uri))
  43. (define-once openai-api-key
  44. (make-parameter *unspecified*
  45. (lambda (value)
  46. (unless (or (unspecified? value) (string? value))
  47. (error "openai-api-key must be a string:" value))
  48. value)))
  49. (define-once openai-organization
  50. (make-parameter *unspecified*
  51. (lambda (value)
  52. (unless (or (unspecified? value) (string? value))
  53. (error "openai-organization must be a string:" value))
  54. value)))
  55. (define-once openai-default-headers
  56. (make-parameter '() (lambda (value)
  57. (unless (or (null? value) (pair? value))
  58. (error "openai-default-headers must be a list:" value))
  59. value)))
  60. (define-once openai-default-user
  61. (make-parameter *unspecified*
  62. (lambda (value)
  63. (unless (or (unspecified? value) (string? value))
  64. (error "openai-default-user must be a string:" value))
  65. value)))
  66. (define-json-type <api-response>
  67. (error "error" <api-error>))
  68. (define-json-type <api-error>
  69. (message)
  70. (type)
  71. (param)
  72. (code))
  73. (define (openai-request-uri path)
  74. (apply resolve-uri-refs
  75. (openai-base-uri)
  76. (if (pair? path) path (list path))))
  77. (define (openai-request-headers keep-alive? headers)
  78. (let ((api-key (openai-api-key))
  79. (organization (openai-organization)))
  80. (append (openai-default-headers)
  81. (if keep-alive?
  82. '()
  83. '((connection close)))
  84. (if (unspecified? api-key)
  85. '()
  86. `((Authorization . ,(string-append "Bearer " api-key))))
  87. (if (unspecified? organization)
  88. '()
  89. `((OpenAI-Organization . ,organization)))
  90. headers)))
  91. (define* (openai-request path #:key
  92. (body #f)
  93. (verify-certificate? #t)
  94. (port #f)
  95. (method 'GET)
  96. (version '(1 . 1))
  97. (keep-alive? #f)
  98. (headers '()))
  99. (openai-last-api-call #f)
  100. ;; XXX: The web module doesn't provide a nice way to capture a fully
  101. ;; inflated request object (which we want for debugging), so we end up
  102. ;; jumping through a few hoops here.
  103. (let* ((uri (openai-request-uri path))
  104. (port (or port (open-socket-for-uri uri
  105. #:verify-certificate?
  106. verify-certificate?)))
  107. (bytes (and body (multipart-tree->bytevector body)))
  108. (headers (openai-request-headers keep-alive? headers))
  109. (request (build-request uri
  110. #:method method
  111. #:version version
  112. #:headers headers
  113. #:port port))
  114. (request bytes ((@@ (web client) sanitize-request) request bytes)))
  115. (openai-last-api-call (list request body #f #f))
  116. (let ((response (http-request uri
  117. #:body bytes
  118. #:verify-certificate? verify-certificate?
  119. #:port port
  120. #:method method
  121. #:version version
  122. #:keep-alive? keep-alive?
  123. #:headers headers
  124. #:decode-body? #f
  125. #:streaming? #t
  126. #:request request)))
  127. (openai-last-api-call (list request body response #f))
  128. (receive (port->data data->result)
  129. (match (response-content-type response)
  130. ((or '(application/json)
  131. '(application/json (charset . "utf-8")))
  132. (values get-string-all identity))
  133. ('(text/event-stream)
  134. (values port->line-stream line-stream->event-stream)))
  135. (let ((data (port->data (response-port response))))
  136. (openai-last-api-call (list request body response data))
  137. (when (not (= (response-code response) 200))
  138. (error "Received API error:"
  139. (or (false-if-exception (api-error-message
  140. (api-response-error
  141. (json->api-response data))))
  142. data)))
  143. (data->result data))))))
  144. (define* (openai-get path #:rest args)
  145. (apply openai-request path `(,@args #:method GET)))
  146. (define* (openai-post-json path body #:rest args)
  147. (apply openai-request path `(,@args
  148. #:method POST
  149. #:headers ((content-type application/json))
  150. #:body ,body)))
  151. ;; TODO best practice boundary string
  152. (define %boundary "========")
  153. (define* (openai-post-multipart path params #:rest args)
  154. (apply openai-request path
  155. `(,@args
  156. #:method POST
  157. #:headers ((content-type multipart/form-data
  158. (boundary . ,%boundary)))
  159. #:body ,(multipart-params->tree params %boundary))))