moderation.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 moderation)
  20. #:use-module (openai api moderation)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:export (openai-default-moderation-model
  25. moderation?
  26. moderation-flagged?
  27. moderation-categories
  28. moderation-scores
  29. moderation-score
  30. openai-moderation))
  31. (define-once openai-default-moderation-model
  32. (make-parameter *unspecified*))
  33. (define-record-type <Moderation>
  34. (%make-moderation flagged? categories scores)
  35. moderation?
  36. (flagged? moderation-flagged?)
  37. (categories moderation-categories)
  38. (scores moderation-scores))
  39. (define (make-moderation response)
  40. (let ((result (car (moderation-response-results response))))
  41. (%make-moderation
  42. (moderation-result-flagged? result)
  43. (filter-map (match-lambda
  44. ((category . true?)
  45. (and true? (string->symbol category))))
  46. (moderation-result-categories result))
  47. (map (match-lambda
  48. ((category . score)
  49. (cons (string->symbol category) score)))
  50. (moderation-result-category-scores result)))))
  51. (define (moderation-score moderation category)
  52. (assq-ref (moderation-scores moderation) category))
  53. (define* (openai-moderation input #:optional
  54. (model (openai-default-moderation-model)))
  55. "Send a moderation request. Returns a moderation record.
  56. The INPUT must be a string to be classified by the moderation model.
  57. The keyword arguments correspond to the request parameters described
  58. in the moderation request documentation:
  59. #:model - A symbol or string identifying the model to use. The
  60. default is unspecified, which is equivalent to
  61. `text-moderation-latest'."
  62. (let* ((model (if (symbol? model) (symbol->string model) model))
  63. (request (make-moderation-request input model))
  64. (response (send-moderation-request request)))
  65. (make-moderation response)))