I don't find an efficient way to check if an api key of perplexity is valid, either on python or anything.
Indeed, for openai I do :
def check_openai_api_key(api_key):
openai.api_key = api_key
try:
openai.Model.list()
except openai.error.AuthenticationError as e:
return False
else:
return True
But perplexity does not seem to have a model index. Does anyone know how I could do ?
Currently I do a simple completion request with the API like so :
def check_perplexity_api_key(api_key):
url = "https://api.perplexity.ai/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "llama-3.1-sonar-small-128k-online",
"messages": [{"role": "user", "content": "SOME PROMPT TO ASK AN EASY AND QUICK THING"}]
}
try:
response = requests.post(url, json=data, headers=headers)
print(response.content)
if response.status_code == 200:
return True
else:
return False
except requests.exceptions.RequestException:
return False
but it's ineficient, takes time and consumes tokens.
What would you suggest ?
Thanks in advance !