1

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 !

1 Answer 1

0

Maybe something like this

"answer the following question with yes or no only: did you understand my question?"

I'm pretty sure that is not how you check the health of an API, asking the AI if he is alive would just cost you money without getting any value. try to find /health/ request or check here for downtime https://status.perplexity.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Hi ! Thanks for your reply. Indeed my question was just to check if an api key is valid or not so it's pretty much the same thing than mine here ;) I was just wondering if there was a same route than in openai (the route to list the models) that I would have missed that allows to check if an api key is valid, without having to ask for a completion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.