@genkit-ai/vertexai
Version:
Genkit AI framework plugin for Google Cloud Vertex AI APIs including Gemini APIs, Imagen, and more.
60 lines • 2.31 kB
JavaScript
import { extractErrMsg } from "../../common/utils.mjs";
const DEFAULT_LOCATION = "us-central1";
async function rerankerRank(model, request, clientOptions) {
const url = getVertexRerankUrl(clientOptions);
const token = await getToken(clientOptions.authClient);
const headers = {
Authorization: `Bearer ${token}`,
"x-goog-user-project": clientOptions.projectId,
"Content-Type": "application/json"
};
const fetchOptions = {
method: "POST",
headers,
body: JSON.stringify(request)
};
const response = await makeRequest(url, fetchOptions);
return response.json();
}
function getVertexRerankUrl(clientOptions) {
const loc = clientOptions.location || DEFAULT_LOCATION;
return `https://discoveryengine.googleapis.com/v1/projects/${clientOptions.projectId}/locations/${loc}/rankingConfigs/default_ranking_config:rank`;
}
async function getToken(authClient) {
const CREDENTIAL_ERROR_MESSAGE = "\nUnable to authenticate your request \nDepending on your run time environment, you can get authentication by \n- if in local instance or cloud shell: `!gcloud auth login` \n- if in Colab: \n -`from google.colab import auth` \n -`auth.authenticate_user()` \n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication";
const token = await authClient.getAccessToken().catch((e) => {
throw new Error(CREDENTIAL_ERROR_MESSAGE, e);
});
if (!token) {
throw new Error(CREDENTIAL_ERROR_MESSAGE);
}
return token;
}
async function makeRequest(url, fetchOptions) {
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
let errorText = await response.text();
let errorMessage = errorText;
try {
const json = JSON.parse(errorText);
if (json.error && json.error.message) {
errorMessage = json.error.message;
}
} catch (e) {
}
throw new Error(
`Error fetching from ${url}: [${response.status} ${response.statusText}] ${errorMessage}`
);
}
return response;
} catch (e) {
console.error(e);
throw new Error(`Failed to fetch from ${url}: ${extractErrMsg(e)}`);
}
}
export {
getVertexRerankUrl,
rerankerRank
};
//# sourceMappingURL=client.mjs.map