Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Fix JSON parsing errors
ozanarmagan01a17a697271
diff --git a/src/text_embedder_remote.cpp b/src/text_embedder_remote.cppindex bb984e86..931da129 100644--- a/src/text_embedder_remote.cpp+++ b/src/text_embedder_remote.cpp@@ -60,14 +60,24 @@ Option<bool> OpenAIEmbedder::is_model_valid(const nlohmann::json& model_config,std::string res;auto res_code = call_remote_api("GET", OPENAI_LIST_MODELS, "", res, res_headers, headers);if (res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "OpenAI API error: " + res);+ }if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {return Option<bool>(400, "OpenAI API error: " + res);}return Option<bool>(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());}- auto models_json = nlohmann::json::parse(res);+ nlohmann::json models_json;+ try {+ models_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "Got malformed response from OpenAI API.");+ }bool found = false;// extract model name by removing "openai/" prefixauto model_name_without_namespace = TextEmbedderManager::get_model_name_without_namespace(model_name);@@ -92,14 +102,23 @@ Option<bool> OpenAIEmbedder::is_model_valid(const nlohmann::json& model_config,if (res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(embedding_res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(embedding_res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "OpenAI API error: " + embedding_res);+ }if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {return Option<bool>(400, "OpenAI API error: " + embedding_res);}return Option<bool>(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());}-- auto embedding = nlohmann::json::parse(embedding_res)["data"][0]["embedding"].get<std::vector<float>>();+ std::vector<float> embedding;+ try {+ embedding = nlohmann::json::parse(embedding_res)["data"][0]["embedding"].get<std::vector<float>>();+ } catch (const std::exception& e) {+ return Option<bool>(400, "Got malformed response from OpenAI API.");+ }num_dims = embedding.size();return Option<bool>(true);}@@ -116,7 +135,12 @@ embedding_res_t OpenAIEmbedder::Embed(const std::string& text) {req_body["model"] = TextEmbedderManager::get_model_name_without_namespace(openai_model_path);auto res_code = call_remote_api("POST", OPENAI_CREATE_EMBEDDING, req_body.dump(), res, res_headers, headers);if (res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return embedding_res_t(400, "OpenAI API error: " + res);+ }nlohmann::json embedding_res = nlohmann::json::object();embedding_res["response"] = json_res;embedding_res["request"] = nlohmann::json::object();@@ -129,8 +153,19 @@ embedding_res_t OpenAIEmbedder::Embed(const std::string& text) {}return embedding_res_t(res_code, embedding_res);}+ try {+ embedding_res_t embedding_res = embedding_res_t(nlohmann::json::parse(res)["data"][0]["embedding"].get<std::vector<float>>());+ return embedding_res;+ } catch (const std::exception& e) {+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = OPENAI_CREATE_EMBEDDING;+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ embedding_res["error"] = "Malformed response from OpenAI API.";- return embedding_res_t(nlohmann::json::parse(res)["data"][0]["embedding"].get<std::vector<float>>());+ return embedding_res_t(500, embedding_res);+ }}std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::string>& inputs) {@@ -148,7 +183,13 @@ std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::if(res_code != 200) {std::vector<embedding_res_t> outputs;- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "OpenAI API error: " + res;+ }LOG(INFO) << "OpenAI API error: " << json_res.dump();nlohmann::json embedding_res = nlohmann::json::object();embedding_res["response"] = json_res;@@ -167,8 +208,24 @@ std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::}return outputs;}-- nlohmann::json res_json = nlohmann::json::parse(res);+ nlohmann::json res_json;+ try {+ res_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = OPENAI_CREATE_EMBEDDING;+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ embedding_res["request"]["body"]["input"] = std::vector<std::string>{inputs[0]};+ embedding_res["error"] = "Malformed response from OpenAI API";+ std::vector<embedding_res_t> outputs;+ for(size_t i = 0; i < inputs.size(); i++) {+ embedding_res["request"]["body"]["input"][0] = inputs[i];+ outputs.push_back(embedding_res_t(500, embedding_res));+ }+ return outputs;+ }std::vector<embedding_res_t> outputs;for(auto& data : res_json["data"]) {outputs.push_back(embedding_res_t(data["embedding"].get<std::vector<float>>()));@@ -210,14 +267,23 @@ Option<bool> GoogleEmbedder::is_model_valid(const nlohmann::json& model_config,auto res_code = call_remote_api("POST", std::string(GOOGLE_CREATE_EMBEDDING) + api_key, req_body.dump(), res, res_headers, headers);if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "Google API error: " + res);+ }if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {return Option<bool>(400, "Google API error: " + res);}return Option<bool>(400, "Google API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());}- num_dims = nlohmann::json::parse(res)["embedding"]["value"].get<std::vector<float>>().size();+ try {+ num_dims = nlohmann::json::parse(res)["embedding"]["value"].get<std::vector<float>>().size();+ } catch (const std::exception& e) {+ return Option<bool>(500, "Got malformed response from Google API.");+ }return Option<bool>(true);}@@ -233,7 +299,13 @@ embedding_res_t GoogleEmbedder::Embed(const std::string& text) {auto res_code = call_remote_api("POST", std::string(GOOGLE_CREATE_EMBEDDING) + google_api_key, req_body.dump(), res, res_headers, headers);if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ nlohmann::json json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = res;+ }nlohmann::json embedding_res = nlohmann::json::object();embedding_res["response"] = json_res;embedding_res["request"] = nlohmann::json::object();@@ -245,8 +317,17 @@ embedding_res_t GoogleEmbedder::Embed(const std::string& text) {}return embedding_res_t(res_code, embedding_res);}-- return embedding_res_t(nlohmann::json::parse(res)["embedding"]["value"].get<std::vector<float>>());+ try {+ return embedding_res_t(nlohmann::json::parse(res)["embedding"]["value"].get<std::vector<float>>());+ } catch (const std::exception& e) {+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = GOOGLE_CREATE_EMBEDDING;+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ embedding_res["error"] = "Malformed response from Google API.";+ return embedding_res_t(500, embedding_res);+ }}@@ -302,14 +383,23 @@ Option<bool> GCPEmbedder::is_model_valid(const nlohmann::json& model_config, unsauto res_code = call_remote_api("POST", get_gcp_embedding_url(project_id, model_name_without_namespace), req_body.dump(), res, res_headers, headers);if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "Got malformed response from GCP API.");+ }if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {return Option<bool>(400, "GCP API error: " + res);}return Option<bool>(400, "GCP API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());}-- auto res_json = nlohmann::json::parse(res);+ nlohmann::json res_json;+ try {+ res_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<bool>(400, "Got malformed response from GCP API.");+ }if(res_json.count("predictions") == 0 || res_json["predictions"].size() == 0 || res_json["predictions"][0].count("embeddings") == 0) {LOG(INFO) << "Invalid response from GCP API: " << res_json.dump();return Option<bool>(400, "GCP API error: Invalid response");@@ -355,7 +445,13 @@ embedding_res_t GCPEmbedder::Embed(const std::string& text) {}if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "Got malformed response from GCP API.";+ }nlohmann::json embedding_res = nlohmann::json::object();embedding_res["response"] = json_res;embedding_res["request"] = nlohmann::json::object();@@ -364,11 +460,23 @@ embedding_res_t GCPEmbedder::Embed(const std::string& text) {embedding_res["request"]["body"] = req_body;if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {embedding_res["error"] = "GCP API error: " + json_res["error"]["message"].get<std::string>();+ } else {+ embedding_res["error"] = "Malformed response from GCP API.";}return embedding_res_t(res_code, embedding_res);}-- nlohmann::json res_json = nlohmann::json::parse(res);+ nlohmann::json res_json;+ try {+ res_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = get_gcp_embedding_url(project_id, model_name);+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ embedding_res["error"] = "Malformed response from GCP API.";+ return embedding_res_t(500, embedding_res);+ }return embedding_res_t(res_json["predictions"][0]["embeddings"]["values"].get<std::vector<float>>());}@@ -416,7 +524,13 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::str}if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "Got malformed response from GCP API.";+ }nlohmann::json embedding_res = nlohmann::json::object();embedding_res["response"] = json_res;embedding_res["request"] = nlohmann::json::object();@@ -425,6 +539,8 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::strembedding_res["request"]["body"] = req_body;if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {embedding_res["error"] = "GCP API error: " + json_res["error"]["message"].get<std::string>();+ } else {+ embedding_res["error"] = "Malformed response from GCP API.";}std::vector<embedding_res_t> outputs;for(size_t i = 0; i < inputs.size(); i++) {@@ -432,8 +548,22 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::str}return outputs;}-- nlohmann::json res_json = nlohmann::json::parse(res);+ nlohmann::json res_json;+ try {+ res_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["error"] = "Nalformed response from GCP API.";+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = get_gcp_embedding_url(project_id, model_name);+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ std::vector<embedding_res_t> outputs;+ for(size_t i = 0; i < inputs.size(); i++) {+ outputs.push_back(embedding_res_t(400, embedding_res));+ }+ return outputs;+ }std::vector<embedding_res_t> outputs;for(const auto& prediction : res_json["predictions"]) {outputs.push_back(embedding_res_t(prediction["embeddings"]["values"].get<std::vector<float>>()));@@ -453,14 +583,23 @@ Option<std::string> GCPEmbedder::generate_access_token(const std::string& refresauto res_code = call_remote_api("POST", GCP_AUTH_TOKEN_URL, req_body, res, res_headers, headers);if(res_code != 200) {- nlohmann::json json_res = nlohmann::json::parse(res);+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<std::string>(400, "Got malformed response from GCP API.");+ }if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {return Option<std::string>(400, "GCP API error: " + res);}return Option<std::string>(400, "GCP API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());}-- nlohmann::json res_json = nlohmann::json::parse(res);+ nlohmann::json res_json;+ try {+ res_json = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<std::string>(400, "Got malformed response from GCP API.");+ }std::string access_token = res_json["access_token"].get<std::string>();return Option<std::string>(access_token);