chat.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-temperature
  30. chat-request-top-p
  31. chat-request-n
  32. chat-request-stream?
  33. chat-request-stop
  34. chat-request-max-tokens
  35. chat-request-presence-penalty
  36. chat-request-frequency-penalty
  37. chat-request-logit-bias
  38. chat-request-user
  39. make-chat-response
  40. json->chat-response
  41. chat-response->json
  42. chat-response?
  43. chat-response-id
  44. chat-response-object
  45. chat-response-created
  46. chat-response-model
  47. chat-response-usage
  48. chat-response-choices
  49. make-chat-usage
  50. json->chat-usage
  51. chat-usage->json
  52. chat-usage?
  53. chat-usage-prompt-tokens
  54. chat-usage-completion-tokens
  55. chat-usage-total-tokens
  56. make-chat-choice
  57. json->chat-choice
  58. chat-choice->json
  59. chat-choice?
  60. chat-choice-delta
  61. chat-choice-message
  62. chat-choice-finish-reason
  63. chat-choice-index
  64. make-chat-message
  65. json->chat-message
  66. chat-message->json
  67. chat-message?
  68. chat-message-role
  69. chat-message-content
  70. send-chat-request))
  71. ;; See https://platform.openai.com/docs/api-reference/chat
  72. (define-json-type <chat-request>
  73. (model)
  74. (messages "messages" #(<chat-message>))
  75. (temperature)
  76. (top-p "top_p")
  77. (n)
  78. (stream? "stream")
  79. (stop)
  80. (max-tokens "max_tokens")
  81. (presence-penalty "presence_penalty")
  82. (frequency-penalty "frequency_penalty")
  83. (logit-bias "logit_bias")
  84. (user))
  85. (define-json-type <chat-response>
  86. (id)
  87. (object)
  88. (created)
  89. (model)
  90. (usage "usage" <chat-usage>)
  91. (choices "choices" #(<chat-choice>)))
  92. (define-json-type <chat-usage>
  93. (prompt-tokens "prompt_tokens")
  94. (completion-tokens "completion_tokens")
  95. (total-tokens "total_tokens"))
  96. (define-json-type <chat-choice>
  97. (delta "delta" <chat-message>)
  98. (message "message" <chat-message>)
  99. (finish-reason "finish_reason")
  100. (index))
  101. (define-json-type <chat-message>
  102. (role) ;; system, user, assistant
  103. (content))
  104. (define (send-chat-request request)
  105. (let ((result (openai-post-json "/v1/chat/completions"
  106. (chat-request->json request))))
  107. (if (stream? result)
  108. (stream-map json->chat-response result)
  109. (json->chat-response result))))