embedding.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 embedding)
  20. #:use-module (openai client)
  21. #:use-module (json record)
  22. #:export (make-embedding-request
  23. json->embedding-request
  24. embedding-request->json
  25. embedding-request?
  26. embedding-request-model
  27. embedding-request-input
  28. embedding-request-user
  29. make-embedding-response
  30. json->embedding-response
  31. embedding-response->json
  32. embedding-response?
  33. embedding-response-object
  34. embedding-response-data
  35. embedding-response-model
  36. embedding-response-usage
  37. make-embedding-data
  38. json->embedding-data
  39. embedding-data->json
  40. embedding-data?
  41. embedding-data-object
  42. embedding-data-index
  43. embedding-data-embedding
  44. send-embedding-request))
  45. ;; See https://platform.openai.com/docs/api-reference/embeddings
  46. (define-json-type <embedding-request>
  47. (model)
  48. (input)
  49. (user))
  50. (define-json-type <embedding-response>
  51. (object)
  52. (data "data" #(<embedding-data>))
  53. (model)
  54. (usage "usage" <embedding-usage>))
  55. (define-json-type <embedding-data>
  56. (object)
  57. (index)
  58. (embedding))
  59. (define-json-type <embedding-usage>
  60. (prompt-tokens "prompt_tokens")
  61. (total-tokens "total_tokens"))
  62. (define (send-embedding-request request)
  63. (json->embedding-response
  64. (openai-post-json "/v1/embeddings"
  65. (embedding-request->json request))))