ai-ping
Version:
ping AI provider to check if a api key is valid
59 lines (55 loc) • 1.98 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var _async_to_generator = require('@swc/helpers/_/_async_to_generator');
/**
* Predict the platform based on the API key prefix
*
* This function will check the prefix of the API key and return the corresponding platform.
*
* @param apiKey
*/ function predict(apiKey) {
if (apiKey.startsWith('sk-proj')) {
return 'openai';
} else if (apiKey.startsWith('sk-ant')) {
return 'anthropic';
} else if (apiKey.startsWith('AI')) {
if (apiKey.length === 39) {
return 'google';
}
}
return 'unknown';
}
/**
* Ping the API to check if the key is valid
*
* This function will make a request to the API and check if the response is 401 (Unauthorized).
*
* @param apiKey - The API key to validate
* @param platform - The platform to validate against. If not provided, it will be predicted from the API key.
*/ function ping(apiKey, platform) {
return _async_to_generator._(function*() {
if (!platform) {
platform = predict(apiKey);
}
switch(platform){
case 'openai':
return fetch('https://api.openai.com/v1/models', {
headers: {
Authorization: `Bearer ${apiKey}`
}
}).then((r)=>r.status !== 401);
case 'anthropic':
return fetch('https://api.anthropic.com/v1/models', {
headers: {
'x-api-key': apiKey,
'anthropic-version': '2023-06-01'
}
}).then((r)=>r.status !== 401);
case 'google':
return fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${apiKey}`).then((r)=>r.status !== 401);
default:
throw new Error('Unsupported API key');
}
})();
}
exports.ping = ping;
exports.predict = predict;