UNPKG

ai-ping

Version:

ping AI provider to check if a api key is valid

56 lines (53 loc) 1.86 kB
import { _ } from '@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 _(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'); } })(); } export { ping, predict };