2026-05-04 00:29:02 -04:00
|
|
|
// OpenAI-compatible embeddings adapter (most providers)
|
|
|
|
|
import { bearerAuth } from "./_base.js";
|
2026-06-14 02:15:48 -04:00
|
|
|
import { PROVIDER_MEDIA } from "../../providers/index.js";
|
2026-05-04 00:29:02 -04:00
|
|
|
|
2026-06-14 02:15:48 -04:00
|
|
|
// media-only providers without a registry file keep URL here; rest derive from registry media.embeddingConfig.baseUrl
|
2026-05-04 00:29:02 -04:00
|
|
|
const ENDPOINTS = {
|
|
|
|
|
"jina-ai": "https://api.jina.ai/v1/embeddings",
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-14 02:15:48 -04:00
|
|
|
const embedCfg = (id) => PROVIDER_MEDIA[id]?.embeddingConfig || {};
|
|
|
|
|
const embedUrl = (id) => embedCfg(id).baseUrl || ENDPOINTS[id];
|
|
|
|
|
|
2026-05-04 00:29:02 -04:00
|
|
|
export default function createOpenAIEmbeddingAdapter(providerId) {
|
2026-06-14 02:15:48 -04:00
|
|
|
const cfg = embedCfg(providerId);
|
2026-05-04 00:29:02 -04:00
|
|
|
return {
|
2026-06-14 02:15:48 -04:00
|
|
|
buildUrl: () => embedUrl(providerId),
|
2026-05-04 00:29:02 -04:00
|
|
|
buildHeaders: (creds) => {
|
2026-06-14 02:15:48 -04:00
|
|
|
return { "Content-Type": "application/json", ...bearerAuth(creds), ...(cfg.headers || {}) };
|
2026-05-04 00:29:02 -04:00
|
|
|
},
|
|
|
|
|
buildBody: (model, { input, encoding_format, dimensions }) => {
|
|
|
|
|
const body = { model, input };
|
|
|
|
|
if (encoding_format) body.encoding_format = encoding_format;
|
|
|
|
|
if (dimensions != null && dimensions !== "") {
|
|
|
|
|
const dim = Number(dimensions);
|
|
|
|
|
if (Number.isFinite(dim) && dim > 0) body.dimensions = dim;
|
|
|
|
|
}
|
|
|
|
|
return body;
|
|
|
|
|
},
|
|
|
|
|
normalize: (responseBody) => responseBody,
|
|
|
|
|
};
|
|
|
|
|
}
|