My site’s users are allowed to (in a private settings page) enter/store their OpenAI key to generate unique-to-them content.
My problem is that I’d like to default to GPT-4 and if the key isn’t compatible (access not yet granted), fallback to gpt-3.5-turbo.
Keys without access appear to not get a response. I tried to trigger 3.5-turbo using an === false argument but only ever end up getting the default error message of my own making.
What’s a good, well-oiled approach to trigger a fallback based on compatibility and access?
In the few examples I’ve seen, they simply do a test completion call when the user saves the API key with gpt-4, and if it responds with the expected response then gpt-4 access is toggled active, if not then greys out the gpt-4 option in the UI.
This function checks if a model is accessible, and if it doesn’t, it tries another model.
const API_URL = 'https://api.openai.com/v1/models/';
const AUTH_TOKEN = 'Bearer XXXX..'; // replace with your actual token
async function checkModel(model: string): Promise<void> {
try {
const response = await fetch(`${API_URL}${model}`, {
method: 'GET',
headers: {
'Authorization': AUTH_TOKEN,
},
});
const data = await response.json();
if (data.error) {
// Try another model here
const anotherModel = 'gpt-3.5-turbo-0301'; // switch to the gpt-3.5 model
console.log(`${model} is not accessible. Switching to ${anotherModel}`);
await checkModel(anotherModel);
} else {
console.log(`${model} is accessible`);
// Fetch chat model here and assume that GPT-4 is available
}
} catch (error) {
console.error(`An error occurred: ${error}`);
}
}
// Usage
checkModel('gpt-4');
This function uses the fetch API to make a GET request to the OpenAI API. If the response contains an error, it assumes that the model does not exist or is not accessible to the person. If there is no error in the response, it so the person have access to GPT-4.