embeddings.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.Embeddings = void 0;
  5. const resource_1 = require("../core/resource.js");
  6. const utils_1 = require("../internal/utils.js");
  7. class Embeddings extends resource_1.APIResource {
  8. /**
  9. * Creates an embedding vector representing the input text.
  10. *
  11. * @example
  12. * ```ts
  13. * const createEmbeddingResponse =
  14. * await client.embeddings.create({
  15. * input: 'The quick brown fox jumped over the lazy dog',
  16. * model: 'text-embedding-3-small',
  17. * });
  18. * ```
  19. */
  20. create(body, options) {
  21. const hasUserProvidedEncodingFormat = !!body.encoding_format;
  22. // No encoding_format specified, defaulting to base64 for performance reasons
  23. // See https://github.com/openai/openai-node/pull/1312
  24. let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';
  25. if (hasUserProvidedEncodingFormat) {
  26. (0, utils_1.loggerFor)(this._client).debug('embeddings/user defined encoding_format:', body.encoding_format);
  27. }
  28. const response = this._client.post('/embeddings', {
  29. body: {
  30. ...body,
  31. encoding_format: encoding_format,
  32. },
  33. ...options,
  34. });
  35. // if the user specified an encoding_format, return the response as-is
  36. if (hasUserProvidedEncodingFormat) {
  37. return response;
  38. }
  39. // in this stage, we are sure the user did not specify an encoding_format
  40. // and we defaulted to base64 for performance reasons
  41. // we are sure then that the response is base64 encoded, let's decode it
  42. // the returned result will be a float32 array since this is OpenAI API's default encoding
  43. (0, utils_1.loggerFor)(this._client).debug('embeddings/decoding base64 embeddings from base64');
  44. return response._thenUnwrap((response) => {
  45. if (response && response.data) {
  46. response.data.forEach((embeddingBase64Obj) => {
  47. const embeddingBase64Str = embeddingBase64Obj.embedding;
  48. embeddingBase64Obj.embedding = (0, utils_1.toFloat32Array)(embeddingBase64Str);
  49. });
  50. }
  51. return response;
  52. });
  53. }
  54. }
  55. exports.Embeddings = Embeddings;
  56. //# sourceMappingURL=embeddings.js.map