completion.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 completion)
  20. #:use-module (openai api completion)
  21. #:use-module (openai client)
  22. #:use-module (openai utils colorized)
  23. #:use-module (ice-9 match)
  24. #:use-module (srfi srfi-9)
  25. #:use-module (srfi srfi-9 gnu)
  26. #:use-module (srfi srfi-41)
  27. #:export (openai-default-completion-model
  28. openai-default-completion-temperature
  29. openai-default-completion-top-p
  30. completion?
  31. completion-content
  32. completion-stream
  33. openai-completion))
  34. (define-once openai-default-completion-model
  35. (make-parameter 'ada))
  36. (define-once openai-default-completion-temperature
  37. (make-parameter *unspecified*))
  38. (define-once openai-default-completion-top-p
  39. (make-parameter *unspecified*))
  40. (define-once openai-default-completion-stream?
  41. (make-parameter #t))
  42. (define-record-type <Completion>
  43. (%make-completion content stream)
  44. completion?
  45. (content %completion-content)
  46. (stream completion-stream))
  47. (define (completion-content completion)
  48. (force (%completion-content completion)))
  49. (define (make-completions result n)
  50. (define make-one-completion
  51. (if (stream? result)
  52. ;; streamed response will be lazily parsed & merged
  53. (lambda (ix)
  54. (let* ((strm (responses->content-stream result ix))
  55. (cont (delay (string-concatenate (stream->list strm)))))
  56. (%make-completion cont strm)))
  57. ;; regular response will be lazily parsed
  58. (lambda (ix)
  59. (let* ((cont (response->content result ix))
  60. (strm (stream (force cont))))
  61. (%make-completion cont strm)))))
  62. (apply values (map make-one-completion (iota n))))
  63. (define (response->content response n)
  64. (completion-choice-text
  65. (list-ref (completion-response-choices response) n)))
  66. (define (responses->content-stream responses n)
  67. (stream-let loop ((responses responses))
  68. (if (stream-null? responses)
  69. stream-null
  70. (let* ((response (stream-car responses))
  71. (choice (car (completion-response-choices response)))
  72. (index (completion-choice-index choice))
  73. (content (completion-choice-text choice)))
  74. (cond ((not (eqv? index n)) ;; ignore wrong index
  75. (loop (stream-cdr responses)))
  76. ((unspecified? content) ;; ignore unspecified content
  77. (loop (stream-cdr responses)))
  78. (else
  79. (stream-cons content (loop (stream-cdr responses)))))))))
  80. (define (print-completion completion port)
  81. (newline port)
  82. (stream-for-each (lambda (content)
  83. (display content port))
  84. (completion-stream completion)))
  85. (set-record-type-printer! <Completion> print-completion)
  86. (add-color-scheme! `((,completion? completion ,color-stream (GREEN BOLD))))
  87. (define* (openai-completion prompt #:key
  88. (model (openai-default-completion-model))
  89. (suffix *unspecified*)
  90. (max-tokens *unspecified*)
  91. (temperature (openai-default-completion-temperature))
  92. (top-p (openai-default-completion-top-p))
  93. (n *unspecified*)
  94. (stream? (openai-default-completion-stream?))
  95. (logprobs *unspecified*)
  96. (echo *unspecified*)
  97. (stop *unspecified*)
  98. (presence-penalty *unspecified*)
  99. (frequency-penalty *unspecified*)
  100. (best-of *unspecified*)
  101. (logit-bias *unspecified*)
  102. (user (openai-default-user)))
  103. (let* ((model (if (symbol? model) (symbol->string model) model))
  104. (stream? (or stream? *unspecified*))
  105. (request (make-completion-request model prompt suffix max-tokens
  106. temperature top-p n stream? logprobs echo
  107. stop presence-penalty frequency-penalty
  108. best-of logit-bias user))
  109. (response (send-completion-request request)))
  110. (make-completions response (if (unspecified? n) 1 n))))