chat.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 api chat)
  20. #:use-module (openai client)
  21. #:use-module (json record)
  22. #:use-module (srfi srfi-41)
  23. #:export (make-chat-request
  24. json->chat-request
  25. chat-request->json
  26. chat-request?
  27. chat-request-model
  28. chat-request-messages
  29. chat-request-functions
  30. chat-request-function-call
  31. chat-request-temperature
  32. chat-request-top-p
  33. chat-request-n
  34. chat-request-stream?
  35. chat-request-stop
  36. chat-request-max-tokens
  37. chat-request-presence-penalty
  38. chat-request-frequency-penalty
  39. chat-request-logit-bias
  40. chat-request-user
  41. make-chat-response
  42. json->chat-response
  43. chat-response->json
  44. chat-response?
  45. chat-response-id
  46. chat-response-object
  47. chat-response-created
  48. chat-response-model
  49. chat-response-usage
  50. chat-response-choices
  51. make-chat-usage
  52. json->chat-usage
  53. chat-usage->json
  54. chat-usage?
  55. chat-usage-prompt-tokens
  56. chat-usage-completion-tokens
  57. chat-usage-total-tokens
  58. make-chat-choice
  59. json->chat-choice
  60. chat-choice->json
  61. chat-choice?
  62. chat-choice-delta
  63. chat-choice-message
  64. chat-choice-finish-reason
  65. chat-choice-index
  66. make-chat-message
  67. json->chat-message
  68. chat-message->json
  69. chat-message?
  70. chat-message-role
  71. chat-message-content
  72. chat-message-name
  73. chat-message-function-call
  74. make-chat-function
  75. json->chat-function
  76. chat-function->json
  77. chat-function?
  78. chat-function-name
  79. chat-function-description
  80. chat-function-parameters
  81. make-chat-function-call
  82. json->chat-function-call
  83. chat-function-call->json
  84. chat-function-call?
  85. chat-function-call-name
  86. chat-function-call-arguments
  87. send-chat-request))
  88. ;; See https://platform.openai.com/docs/api-reference/chat
  89. (define-json-type <chat-request>
  90. (model)
  91. (messages "messages" #(<chat-message>))
  92. (functions "functions") ;; TODO #(<chat-function>)
  93. (function-call "function_call") ;; none, auto, (("name" . "my_function"))
  94. (temperature)
  95. (top-p "top_p")
  96. (n)
  97. (stream? "stream")
  98. (stop)
  99. (max-tokens "max_tokens")
  100. (presence-penalty "presence_penalty")
  101. (frequency-penalty "frequency_penalty")
  102. (logit-bias "logit_bias")
  103. (user))
  104. (define-json-type <chat-response>
  105. (id)
  106. (object)
  107. (created)
  108. (model)
  109. (usage "usage" <chat-usage>)
  110. (choices "choices" #(<chat-choice>)))
  111. (define-json-type <chat-usage>
  112. (prompt-tokens "prompt_tokens")
  113. (completion-tokens "completion_tokens")
  114. (total-tokens "total_tokens"))
  115. (define-json-type <chat-choice>
  116. (delta "delta" <chat-message>)
  117. (message "message" <chat-message>)
  118. (finish-reason "finish_reason")
  119. (index))
  120. (define-json-type <chat-message>
  121. (role) ;; system, user, assistant, function
  122. (content)
  123. (name)
  124. (function-call "function_call" <chat-function-call>))
  125. (define-json-type <chat-function>
  126. (name)
  127. (description)
  128. (parameters)) ;; TODO JSON Schema
  129. (define-json-type <chat-function-call>
  130. (name)
  131. (arguments)) ;; string containing JSON
  132. (define %make-chat-message make-chat-message)
  133. (define* (make-chat-message role content #:optional
  134. (name *unspecified*)
  135. (function-call *unspecified*))
  136. (%make-chat-message role content name function-call))
  137. (define (send-chat-request request)
  138. (let ((result (openai-post-json "/v1/chat/completions"
  139. (chat-request->json request))))
  140. (if (stream? result)
  141. (stream-map json->chat-response result)
  142. (json->chat-response result))))