@softvisio/core
Version:
Softisio core
98 lines (76 loc) • 2.02 kB
JavaScript
import fetch from "#lib/fetch";
const VERSION = "v1",
ID = "open-ai",
MODELS = {
"text-embedding-3-small": {
"embeddingSize": 1536,
},
"text-embedding-3-large": {
"embeddingSize": 3072,
},
};
export default class OperAiApi {
constructor ( apiKey ) {
this.
}
// static
static get id () {
return ID;
}
static get models () {
return MODELS;
}
// properties
get id () {
return this.constructor.id;
}
get models () {
return this.constructor.models;
}
// public
// XXX simple
// https://platform.openai.com/docs/api-reference/embeddings/create
async getEmbedding ( model, data, { simple } = {} ) {
var body;
if ( typeof data === "string" ) {
body = {
model,
"input": data,
};
}
else {
body = {
...data,
model,
};
}
return this.
}
// private
async
const url = new URL( `https://api.openai.com/${ VERSION }/${ path }` );
if ( params ) {
const urlSearchParams = new URLSearchParams( params );
url.search = urlSearchParams;
}
const headers = {
"Authorization": `Bearer ${ this.
};
if ( body ) {
method ||= "post";
headers[ "Content-Type" ] = "application/json";
body = JSON.stringify( body );
}
const res = await fetch( url, {
method,
headers,
body,
} );
const data = await res.json().catch( e => null );
if ( !res.ok ) {
return result( [ res.status, data?.error?.message ] );
}
return result( 200, data );
}
}