CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
embeddings.mjs52 linesDownload Raw Back to resources
1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.2import { APIResource } from "../core/resource.mjs";3import { loggerFor, toFloat32Array } from "../internal/utils.mjs";4export class Embeddings extends APIResource {5    /**6     * Creates an embedding vector representing the input text.7     *8     * @example9     * ```ts10     * const createEmbeddingResponse =11     *   await client.embeddings.create({12     *     input: 'The quick brown fox jumped over the lazy dog',13     *     model: 'text-embedding-3-small',14     *   });15     * ```16     */17    create(body, options) {18        const hasUserProvidedEncodingFormat = !!body.encoding_format;19        // No encoding_format specified, defaulting to base64 for performance reasons20        // See https://github.com/openai/openai-node/pull/131221        let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';22        if (hasUserProvidedEncodingFormat) {23            loggerFor(this._client).debug('embeddings/user defined encoding_format:', body.encoding_format);24        }25        const response = this._client.post('/embeddings', {26            body: {27                ...body,28                encoding_format: encoding_format,29            },30            ...options,31        });32        // if the user specified an encoding_format, return the response as-is33        if (hasUserProvidedEncodingFormat) {34            return response;35        }36        // in this stage, we are sure the user did not specify an encoding_format37        // and we defaulted to base64 for performance reasons38        // we are sure then that the response is base64 encoded, let's decode it39        // the returned result will be a float32 array since this is OpenAI API's default encoding40        loggerFor(this._client).debug('embeddings/decoding base64 embeddings from base64');41        return response._thenUnwrap((response) => {42            if (response && response.data) {43                response.data.forEach((embeddingBase64Obj) => {44                    const embeddingBase64Str = embeddingBase64Obj.embedding;45                    embeddingBase64Obj.embedding = toFloat32Array(embeddingBase64Str);46                });47            }48            return response;49        });50    }51}52//# sourceMappingURL=embeddings.mjs.map
basant307/AI_Governance_Project · CoolFace