How to call the chatgpt interface to verify whether the key value is valid?

Recently, I am developing a micro-program to implement, poll the key and proxy ip, and then execute the function of calling the openai api interface in a loop. But there is a problem. That is how to predict in advance whether the key is valid?

The solution I can think of at present is to actually perform a request to ask questions. If the feedback is normal, then the key must be usable. If an exception is returned, it means that the key is invalid.

I don’t know if you have any other good ideas. Welcome to leave me a message for interactive discussion.

The following is my question to chatgpt. It gave me some case codes, but there are various small and wonderful problems in it.

A case demo provided by chatgpt to check whether the key is valid:

import openai
import openai_secret_manager
# 获取API密钥
secrets = openai_secret_manager.get_secret("openai")

# 将API密钥设置为OpenAI API客户端的身份验证标头
openai.api_key = secrets["api_key"]

# 验证API密钥是否有效
try:
    openai.api_key_test()
    print("API密钥有效!")
except Exception as e:
    print("API密钥无效 - 错误消息:", str(e))

In fact, I asked chatgpt again, and it admitted that the method api_key_test() does not exist in openai, sdk.

As shown in the picture, this is the feedback written by itself. Since you don’t exist, are you confusing me by providing me with this idea?

I hope that there is an idea to verify whether the key is valid. Friends, you can provide some tips, and I would like to express my gratitude again.

Guess you like

Origin blog.csdn.net/yrldjsbk/article/details/129688110