embeddings.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  4. if (k2 === undefined) k2 = k;
  5. var desc = Object.getOwnPropertyDescriptor(m, k);
  6. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  7. desc = { enumerable: true, get: function() { return m[k]; } };
  8. }
  9. Object.defineProperty(o, k2, desc);
  10. }) : (function(o, m, k, k2) {
  11. if (k2 === undefined) k2 = k;
  12. o[k2] = m[k];
  13. }));
  14. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  15. Object.defineProperty(o, "default", { enumerable: true, value: v });
  16. }) : function(o, v) {
  17. o["default"] = v;
  18. });
  19. var __importStar = (this && this.__importStar) || function (mod) {
  20. if (mod && mod.__esModule) return mod;
  21. var result = {};
  22. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  23. __setModuleDefault(result, mod);
  24. return result;
  25. };
  26. Object.defineProperty(exports, "__esModule", { value: true });
  27. exports.Embeddings = void 0;
  28. const resource_1 = require("../resource.js");
  29. const Core = __importStar(require("../core.js"));
  30. class Embeddings extends resource_1.APIResource {
  31. /**
  32. * Creates an embedding vector representing the input text.
  33. *
  34. * @example
  35. * ```ts
  36. * const createEmbeddingResponse =
  37. * await client.embeddings.create({
  38. * input: 'The quick brown fox jumped over the lazy dog',
  39. * model: 'text-embedding-3-small',
  40. * });
  41. * ```
  42. */
  43. create(body, options) {
  44. const hasUserProvidedEncodingFormat = !!body.encoding_format;
  45. // No encoding_format specified, defaulting to base64 for performance reasons
  46. // See https://github.com/openai/openai-node/pull/1312
  47. let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';
  48. if (hasUserProvidedEncodingFormat) {
  49. Core.debug('Request', 'User defined encoding_format:', body.encoding_format);
  50. }
  51. const response = this._client.post('/embeddings', {
  52. body: {
  53. ...body,
  54. encoding_format: encoding_format,
  55. },
  56. ...options,
  57. });
  58. // if the user specified an encoding_format, return the response as-is
  59. if (hasUserProvidedEncodingFormat) {
  60. return response;
  61. }
  62. // in this stage, we are sure the user did not specify an encoding_format
  63. // and we defaulted to base64 for performance reasons
  64. // we are sure then that the response is base64 encoded, let's decode it
  65. // the returned result will be a float32 array since this is OpenAI API's default encoding
  66. Core.debug('response', 'Decoding base64 embeddings to float32 array');
  67. return response._thenUnwrap((response) => {
  68. if (response && response.data) {
  69. response.data.forEach((embeddingBase64Obj) => {
  70. const embeddingBase64Str = embeddingBase64Obj.embedding;
  71. embeddingBase64Obj.embedding = Core.toFloat32Array(embeddingBase64Str);
  72. });
  73. }
  74. return response;
  75. });
  76. }
  77. }
  78. exports.Embeddings = Embeddings;
  79. //# sourceMappingURL=embeddings.js.map