embedding.scm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 embedding)
  20. #:use-module (openai api embedding)
  21. #:use-module (openai client)
  22. #:use-module (ice-9 match)
  23. #:use-module (srfi srfi-9)
  24. #:export (openai-default-embedding-model
  25. embedding?
  26. embedding-vector
  27. openai-embedding))
  28. (define-once openai-default-embedding-model
  29. (make-parameter 'text-embedding-ada-002))
  30. (define-record-type <Embedding>
  31. (%make-embedding vector)
  32. embedding?
  33. (vector embedding-vector))
  34. (define* (openai-embedding input #:key
  35. (model (openai-default-embedding-model))
  36. (user (openai-default-user)))
  37. "Send an embedding request. Returns an embedding record.
  38. The INPUT must be a string to be embedded.
  39. The keyword arguments correspond to the request parameters described
  40. in the embedding request documentation:
  41. #:model - A symbol or string identifying the model to use. Defaults
  42. to `text-embedding-ada-002'.
  43. #:user - An optional username to associate with this request."
  44. (let* ((model (if (symbol? model) (symbol->string model) model))
  45. (request (make-embedding-request model input user))
  46. (response (send-embedding-request request)))
  47. (%make-embedding
  48. (embedding-data-embedding
  49. (car (embedding-response-data response))))))