@xsai/embed
Version:
extra-small AI SDK.
32 lines (28 loc) • 1.05 kB
JavaScript
import { requestURL, requestHeaders, requestBody, responseCatch, responseJSON } from '@xsai/shared';
const embed = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("embeddings", options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
"Content-Type": "application/json",
...options.headers
}, options.apiKey),
method: "POST",
signal: options.abortSignal
}).then(responseCatch).then(responseJSON).then(({ data, usage }) => ({
embedding: data[0].embedding,
input: options.input,
usage
}));
const embedMany = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("embeddings", options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
"Content-Type": "application/json",
...options.headers
}, options.apiKey),
method: "POST",
signal: options.abortSignal
}).then(responseCatch).then(responseJSON).then(({ data, usage }) => ({
embeddings: data.map((data2) => data2.embedding),
input: options.input,
usage
}));
export { embed, embedMany };