* Get the API key from console. * @return The API key. */
| 12 | * @return The API key. |
| 13 | */ |
| 14 | std::string get_key_from_console(const std::string& api_base_url) { |
| 15 | std::string api_key; |
| 16 | while (true) { |
| 17 | std::cout << "Please enter your OpenAI API key: "; |
| 18 | getline(std::cin, api_key); |
| 19 | APIKeyStatus status = check_key(api_key, api_base_url); |
| 20 | if (status == APIKeyStatus::VALID) { |
| 21 | util::println_info("API key is valid."); |
| 22 | break; |
| 23 | } else if (status == APIKeyStatus::EMPTY) { |
| 24 | util::println_warn("API key cannot be empty, please try again."); |
| 25 | } else if (status == APIKeyStatus::QUOTA_EXCEEDED) { |
| 26 | util::println_warn("This API key has exceeded its quota, please try again."); |
| 27 | } else if (status == APIKeyStatus::INVALID_KEY) { |
| 28 | util::println_warn("API key is invalid, please try again."); |
| 29 | } else if (status == APIKeyStatus::API_REQUEST_FAILED) { |
| 30 | util::println_warn("API request failed, please try again."); |
| 31 | } |
| 32 | } |
| 33 | return api_key; |
| 34 | } |
| 35 | |
| 36 | size_t get_key_count() { |
| 37 | return api_keys_.size(); |
no test coverage detected