Spaces:
Runtime error
Runtime error
File size: 4,521 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | // Embedding / rerank / clarifai provider key validators. Extracted from validation.ts (god-file
// decomposition) — top-level functions with no dispatcher-state captures; behavior is byte-identical
// to the original inline defs.
import { normalizeBaseUrl, addModelsSuffix, resolveChatUrl } from "./urlHelpers";
import { buildBearerHeaders, buildClarifaiHeaders } from "./headers";
import { toValidationErrorResult, validationRead, validationWrite } from "./transport";
export async function validateClarifaiProvider({ apiKey, providerSpecificData = {} }: any) {
const baseUrl =
normalizeBaseUrl(providerSpecificData.baseUrl) || "https://api.clarifai.com/v2/ext/openai/v1";
const modelsUrl = addModelsSuffix(baseUrl);
try {
const modelsRes = await validationRead(modelsUrl, {
method: "GET",
headers: buildClarifaiHeaders(apiKey, providerSpecificData),
});
if (modelsRes.ok) {
return { valid: true, error: null, method: "clarifai_models" };
}
if (modelsRes.status === 401 || modelsRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
const chatUrl = resolveChatUrl("clarifai", baseUrl, providerSpecificData);
const chatRes = await validationWrite(chatUrl, {
method: "POST",
headers: buildClarifaiHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model:
providerSpecificData?.validationModelId || "openai/chat-completion/models/gpt-oss-120b",
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
}),
});
if (chatRes.ok || chatRes.status === 400 || chatRes.status === 422 || chatRes.status === 429) {
return { valid: true, error: null, method: "clarifai_chat_probe" };
}
if (chatRes.status === 401 || chatRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (chatRes.status === 404 || chatRes.status === 405) {
return { valid: false, error: "Provider validation endpoint not supported" };
}
if (chatRes.status >= 500) {
return { valid: false, error: `Provider unavailable (${chatRes.status})` };
}
return { valid: true, error: null, method: "clarifai_chat_probe" };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
export async function validateEmbeddingApiProvider({
apiKey,
providerSpecificData = {},
url,
modelId,
}: any) {
if (!url) {
return { valid: false, error: "Missing embedding endpoint" };
}
try {
const response = await validationWrite(url, {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: providerSpecificData?.validationModelId || modelId,
input: ["test"],
}),
});
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (
response.ok ||
response.status === 400 ||
response.status === 422 ||
response.status === 429
) {
return { valid: true, error: null };
}
if (response.status >= 500) {
return { valid: false, error: `Provider unavailable (${response.status})` };
}
return { valid: false, error: `Validation failed: ${response.status}` };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
export async function validateRerankApiProvider({ apiKey, providerSpecificData = {}, url, modelId }: any) {
if (!url) {
return { valid: false, error: "Missing rerank endpoint" };
}
try {
const response = await validationWrite(url, {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: providerSpecificData?.validationModelId || modelId,
query: "test",
documents: ["test"],
top_n: 1,
return_documents: false,
}),
});
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (
response.ok ||
response.status === 400 ||
response.status === 422 ||
response.status === 429
) {
return { valid: true, error: null };
}
if (response.status >= 500) {
return { valid: false, error: `Provider unavailable (${response.status})` };
}
return { valid: false, error: `Validation failed: ${response.status}` };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
|