MCPcopy Create free account
hub / github.com/LagPixelLOL/ChatGPTCLIBot / check_key

Function check_key

main/interface/network/APIKey.cpp:84–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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.

Callers 1

get_key_from_consoleFunction · 0.85

Calls 10

println_infoFunction · 0.85
http_postFunction · 0.85
println_errFunction · 0.85
check_err_objFunction · 0.85
emptyMethod · 0.45
push_backMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
whatMethod · 0.45
pop_backMethod · 0.45

Tested by

no test coverage detected