mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
// Google Gemini embeddings — embedContent / batchEmbedContents
|
|
const BASE = "https://generativelanguage.googleapis.com/v1beta";
|
|
|
|
function modelPath(model) {
|
|
return model.startsWith("models/") ? model : `models/${model}`;
|
|
}
|
|
|
|
export default {
|
|
buildUrl: (model, creds, { input } = {}) => {
|
|
const apiKey = creds.apiKey || creds.accessToken;
|
|
const path = modelPath(model);
|
|
const op = Array.isArray(input) ? "batchEmbedContents" : "embedContent";
|
|
return `${BASE}/${path}:${op}?key=${encodeURIComponent(apiKey)}`;
|
|
},
|
|
buildHeaders: () => ({ "Content-Type": "application/json" }),
|
|
buildBody: (model, { input }) => {
|
|
const m = modelPath(model);
|
|
if (Array.isArray(input)) {
|
|
return { requests: input.map((text) => ({ model: m, content: { parts: [{ text: String(text) }] } })) };
|
|
}
|
|
return { model: m, content: { parts: [{ text: String(input) }] } };
|
|
},
|
|
normalize: (responseBody, model) => {
|
|
if (responseBody.object === "list" && Array.isArray(responseBody.data)) return responseBody;
|
|
let items = [];
|
|
if (Array.isArray(responseBody.embeddings)) {
|
|
items = responseBody.embeddings.map((emb, idx) => ({
|
|
object: "embedding",
|
|
index: idx,
|
|
embedding: emb.values || [],
|
|
}));
|
|
} else if (responseBody.embedding?.values) {
|
|
items = [{ object: "embedding", index: 0, embedding: responseBody.embedding.values }];
|
|
}
|
|
return {
|
|
object: "list",
|
|
data: items,
|
|
model,
|
|
usage: { prompt_tokens: 0, total_tokens: 0 },
|
|
};
|
|
},
|
|
};
|