| 82 | } |
| 83 | |
| 84 | APIKeyStatus check_key(const std::string& api_key, const std::string& api_base_url) { |
| 85 | util::println_info("Checking API key..."); |
| 86 | if (api_key.empty()) { |
| 87 | return APIKeyStatus::EMPTY; |
| 88 | } |
| 89 | std::vector<std::string> headers = {"Content-Type: application/json"}; |
| 90 | std::string auth = "Authorization: Bearer "; |
| 91 | headers.push_back(auth.append(api_key)); |
| 92 | nlohmann::json payload = nlohmann::json::object(); |
| 93 | payload.emplace("model", "text-embedding-ada-002"); |
| 94 | payload.emplace("input", nlohmann::json{"Test"}); |
| 95 | APIKeyStatus status = APIKeyStatus::API_REQUEST_FAILED; |
| 96 | std::string response; |
| 97 | try { |
| 98 | curl::http_post(api_base_url + "/v1/embeddings", [&](const std::vector<char>& vec, CURL*){ |
| 99 | response.append(vec.begin(), vec.end()); |
| 100 | }, payload.dump(), headers); |
| 101 | } catch (const std::exception& e) { |
| 102 | util::println_err("API request failed: " + std::string(e.what())); |
| 103 | return status; |
| 104 | } |
| 105 | if (boost::ends_with(response, "\n")) { |
| 106 | response.pop_back(); |
| 107 | } |
| 108 | try { |
| 109 | check_err_obj(nlohmann::json::parse(response), status, false); |
| 110 | } catch (const nlohmann::json::parse_error& e) { |
| 111 | util::println_err("API returned string is not a valid json. String: " + response); |
| 112 | } |
| 113 | return status; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check if the json object has an error object, if so, print the error message and set the status. |
no test coverage detected