edit.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 edit)
  20. #:use-module (openai api edit)
  21. #:use-module (openai client)
  22. #:use-module (ice-9 match)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-9 gnu)
  25. #:export (openai-default-text-edit-model
  26. openai-default-text-edit-temperature
  27. openai-default-text-edit-top-p
  28. openai-default-code-edit-model
  29. openai-default-code-edit-temperature
  30. openai-default-code-edit-top-p
  31. edit?
  32. edit-content
  33. openai-text-edit
  34. openai-code-edit))
  35. (define-once openai-default-text-edit-model
  36. (make-parameter 'text-davinci-edit-001))
  37. (define-once openai-default-text-edit-temperature
  38. (make-parameter *unspecified*))
  39. (define-once openai-default-text-edit-top-p
  40. (make-parameter *unspecified*))
  41. (define-once openai-default-code-edit-model
  42. (make-parameter 'code-davinci-edit-001))
  43. (define-once openai-default-code-edit-temperature
  44. (make-parameter *unspecified*))
  45. (define-once openai-default-code-edit-top-p
  46. (make-parameter *unspecified*))
  47. (define-record-type <Edit>
  48. (%make-edit content)
  49. edit?
  50. (content edit-content))
  51. (define (print-edit edit port)
  52. (newline port)
  53. (display (edit-content edit)))
  54. (set-record-type-printer! <Edit> print-edit)
  55. (define* (openai-text-edit input instruction #:key
  56. (model (openai-default-text-edit-model))
  57. (n *unspecified*)
  58. (temperature (openai-default-text-edit-temperature))
  59. (top-p (openai-default-text-edit-top-p)))
  60. "Send an edit request. Returns an edit record.
  61. The INPUT must be a string, which is the text to be edited. The
  62. INSTRUCTION is a string describing the changes to be made.
  63. The keyword arguments correspond to the request parameters described
  64. in the edit request documentation:
  65. #:model - A symbol or string identifying the model to use. Defaults
  66. to `text-davinci-edit-001.
  67. #:n - The number of responses to generate, returned as multiple
  68. values.
  69. #:temperature - The sampling temperature to use, a number between 0
  70. and 2.
  71. #:top-p - An alternative sampling parameter, a number between 0 and
  72. 1."
  73. (%openai-edit input instruction model n temperature top-p))
  74. (define* (openai-code-edit input instruction #:key
  75. (model (openai-default-code-edit-model))
  76. (n *unspecified*)
  77. (temperature (openai-default-code-edit-temperature))
  78. (top-p (openai-default-code-edit-top-p)))
  79. "Send an edit request. Returns an edit record.
  80. The INPUT must be a string, which is the code to be edited. The
  81. INSTRUCTION is a string describing the changes to be made.
  82. The keyword arguments correspond to the request parameters described
  83. in the edit request documentation:
  84. #:model - A symbol or string identifying the model to use. Defaults
  85. to `code-davinci-edit-001.
  86. #:n - The number of responses to generate, returned as multiple
  87. values.
  88. #:temperature - The sampling temperature to use, a number between 0
  89. and 2.
  90. #:top-p - An alternative sampling parameter, a number between 0 and
  91. 1."
  92. (%openai-edit input instruction model n temperature top-p))
  93. (define (%openai-edit input instruction model n temperature top-p)
  94. (let* ((model (if (symbol? model) (symbol->string model) model))
  95. (request (make-edit-request model input instruction
  96. n temperature top-p))
  97. (response (send-edit-request request)))
  98. (apply values (map (lambda (choice)
  99. (%make-edit (edit-choice-text choice)))
  100. (edit-response-choices
  101. (json->edit-response response))))))