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
Create virtual functions to generate error JSONs from responses
ozanarmagan29830e1d5882
diff --git a/include/text_embedder_remote.h b/include/text_embedder_remote.hindex 652c0b61..46c32778 100644--- a/include/text_embedder_remote.h+++ b/include/text_embedder_remote.h@@ -27,6 +27,7 @@ class RemoteEmbedder {static Option<bool> validate_string_properties(const nlohmann::json& model_config, const std::vector<std::string>& properties);static long call_remote_api(const std::string& method, const std::string& url, const std::string& body, std::string& res_body, std::map<std::string, std::string>& headers, const std::unordered_map<std::string, std::string>& req_headers);static inline ReplicationState* raft_server = nullptr;+ virtual nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) = 0;public:virtual embedding_res_t Embed(const std::string& text) = 0;virtual std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs) = 0;@@ -44,6 +45,7 @@ class OpenAIEmbedder : public RemoteEmbedder {std::string openai_model_path;static constexpr char* OPENAI_LIST_MODELS = "https://api.openai.com/v1/models";static constexpr char* OPENAI_CREATE_EMBEDDING = "https://api.openai.com/v1/embeddings";+ nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;public:OpenAIEmbedder(const std::string& openai_model_path, const std::string& api_key);static Option<bool> is_model_valid(const nlohmann::json& model_config, unsigned int& num_dims);@@ -59,6 +61,7 @@ class GoogleEmbedder : public RemoteEmbedder {inline static constexpr short GOOGLE_EMBEDDING_DIM = 768;inline static constexpr char* GOOGLE_CREATE_EMBEDDING = "https://generativelanguage.googleapis.com/v1beta2/models/embedding-gecko-001:embedText?key=";std::string google_api_key;+ nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;public:GoogleEmbedder(const std::string& google_api_key);static Option<bool> is_model_valid(const nlohmann::json& model_config, unsigned int& num_dims);@@ -84,6 +87,7 @@ class GCPEmbedder : public RemoteEmbedder {static std::string get_gcp_embedding_url(const std::string& project_id, const std::string& model_name) {return GCP_EMBEDDING_BASE_URL + project_id + GCP_EMBEDDING_PATH + model_name + GCP_EMBEDDING_PREDICT;}+ nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;public:GCPEmbedder(const std::string& project_id, const std::string& model_name, const std::string& access_token,const std::string& refresh_token, const std::string& client_id, const std::string& client_secret);diff --git a/src/http_proxy.cpp b/src/http_proxy.cppindex 4ed4db5a..518cd45b 100644--- a/src/http_proxy.cpp+++ b/src/http_proxy.cpp@@ -44,7 +44,7 @@ http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& methauto res = call(url, method, body, headers);- if(res.status_code == 408){+ if(res.status_code >= 500 || res.status_code == 408){// retryres = call(url, method, body, headers);}@@ -57,7 +57,7 @@ http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& meth// add to cache- if(res.status_code != 408){+ if(res.status_code == 200){cache.insert(key, res);}diff --git a/src/text_embedder_remote.cpp b/src/text_embedder_remote.cppindex 4e844871..a54315e1 100644--- a/src/text_embedder_remote.cpp+++ b/src/text_embedder_remote.cpp@@ -144,41 +144,13 @@ 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;- try {- json_res = nlohmann::json::parse(res);- } catch (const std::exception& e) {- json_res = nlohmann::json::object();- json_res["error"] = "Malformed response from OpenAI API.";-- }- nlohmann::json embedding_res = nlohmann::json::object();- embedding_res["response"] = json_res;- embedding_res["request"] = nlohmann::json::object();- embedding_res["request"]["url"] = OPENAI_CREATE_EMBEDDING;- embedding_res["request"]["method"] = "POST";- embedding_res["request"]["body"] = req_body;-- if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {- embedding_res["error"] = "OpenAI API error: " + json_res["error"]["message"].get<std::string>();- }- if(res_code == 408) {- embedding_res["error"] = "OpenAI API timeout.";- }- return embedding_res_t(res_code, embedding_res);+ return embedding_res_t(res_code, get_error_json(req_body, res_code, 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(500, embedding_res);+ return embedding_res_t(500, get_error_json(req_body, res_code, res));}}@@ -196,46 +168,19 @@ 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;- try {- json_res = nlohmann::json::parse(res);- } catch (const std::exception& e) {- json_res = nlohmann::json::object();- json_res["error"] = "Malformed response from OpenAI API.";- }- LOG(INFO) << "OpenAI API error: " << json_res.dump();- nlohmann::json embedding_res = nlohmann::json::object();- embedding_res["response"] = json_res;- 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]};- if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {- embedding_res["error"] = "OpenAI API error: " + json_res["error"]["message"].get<std::string>();- }- if(res_code == 408) {- embedding_res["error"] = "OpenAI API timeout.";- }-+ nlohmann::json embedding_res = get_error_json(req_body, res_code, res);for(size_t i = 0; i < inputs.size(); i++) {embedding_res["request"]["body"]["input"][0] = inputs[i];outputs.push_back(embedding_res_t(res_code, embedding_res));}return outputs;}+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";+ nlohmann::json embedding_res = get_error_json(req_body, res_code, res);std::vector<embedding_res_t> outputs;for(size_t i = 0; i < inputs.size(); i++) {embedding_res["request"]["body"]["input"][0] = inputs[i];@@ -252,6 +197,36 @@ std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::}+nlohmann::json OpenAIEmbedder::get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) {+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res_body);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "Malformed response from OpenAI API.";+ }+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["response"] = json_res;+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = OPENAI_CREATE_EMBEDDING;+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ if(embedding_res["request"]["body"].count("input") > 0 && embedding_res["request"]["body"]["input"].get<std::vector<std::string>>().size() > 1) {+ auto vec = embedding_res["request"]["body"]["input"].get<std::vector<std::string>>();+ vec.resize(1);+ embedding_res["request"]["body"]["input"] = vec;+ }+ if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {+ embedding_res["error"] = "OpenAI API error: " + json_res["error"]["message"].get<std::string>();+ }+ if(res_code == 408) {+ embedding_res["error"] = "OpenAI API timeout.";+ }++ return embedding_res;+}++GoogleEmbedder::GoogleEmbedder(const std::string& google_api_key) : google_api_key(google_api_key) {}@@ -321,37 +296,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;- try {- nlohmann::json json_res = nlohmann::json::parse(res);- } catch (const std::exception& e) {- json_res = nlohmann::json::object();- json_res["error"] = "Malformed response from Google API."- }- nlohmann::json embedding_res = nlohmann::json::object();- embedding_res["response"] = json_res;- embedding_res["request"] = nlohmann::json::object();- embedding_res["request"]["url"] = GOOGLE_CREATE_EMBEDDING;- embedding_res["request"]["method"] = "POST";- embedding_res["request"]["body"] = req_body;- if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {- embedding_res["error"] = "Google API error: " + json_res["error"]["message"].get<std::string>();- }- if(res_code == 408) {- embedding_res["error"] = "Google API timeout.";- }- return embedding_res_t(res_code, embedding_res);+ return embedding_res_t(res_code, get_error_json(req_body, res_code, res));}+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);+ return embedding_res_t(500, get_error_json(req_body, res_code, res));}}@@ -366,6 +317,30 @@ std::vector<embedding_res_t> GoogleEmbedder::batch_embed(const std::vector<std::return outputs;}+nlohmann::json GoogleEmbedder::get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) {+ nlohmann::json json_res;+ try {+ nlohmann::json json_res = nlohmann::json::parse(res_body);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "Malformed response from Google API.";+ }+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["response"] = json_res;+ embedding_res["request"] = nlohmann::json::object();+ embedding_res["request"]["url"] = GOOGLE_CREATE_EMBEDDING;+ embedding_res["request"]["method"] = "POST";+ embedding_res["request"]["body"] = req_body;+ if(json_res.count("error") != 0 && json_res["error"].count("message") != 0) {+ embedding_res["error"] = "Google API error: " + json_res["error"]["message"].get<std::string>();+ }+ if(res_code == 408) {+ embedding_res["error"] = "Google API timeout.";+ }++ return embedding_res;+}+GCPEmbedder::GCPEmbedder(const std::string& project_id, const std::string& model_name, const std::string& access_token,const std::string& refresh_token, const std::string& client_id, const std::string& client_secret) :@@ -473,46 +448,17 @@ embedding_res_t GCPEmbedder::Embed(const std::string& text) {}if(res_code != 200) {- nlohmann::json json_res;- try {- json_res = nlohmann::json::parse(res);- } catch (const std::exception& e) {- json_res = nlohmann::json::object();- json_res["error"] = "Malformed response from GCP API.";- }- nlohmann::json embedding_res = nlohmann::json::object();- embedding_res["response"] = json_res;- 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;- 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.";- }-- if(res_code == 408) {- embedding_res["error"] = "GCP API timeout.";- }- return embedding_res_t(res_code, embedding_res);+ return embedding_res_t(res_code, get_error_json(req_body, res_code, 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(500, get_error_json(req_body, res_code, res));}return embedding_res_t(res_json["predictions"][0]["embeddings"]["values"].get<std::vector<float>>());}-std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::string>& inputs) {// GCP API has a limit of 5 instances per requestif(inputs.size() > 5) {@@ -556,28 +502,7 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::str}if(res_code != 200) {- nlohmann::json json_res;- try {- json_res = nlohmann::json::parse(res);- } catch (const std::exception& e) {- json_res = nlohmann::json::object();- json_res["error"] = "Malformed response from GCP API.";- }- nlohmann::json embedding_res = nlohmann::json::object();- embedding_res["response"] = json_res;- 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;- 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.";- }-- if(res_code == 408) {- embedding_res["error"] = "GCP API timeout.";- }+ auto embedding_res = get_error_json(req_body, res_code, res);std::vector<embedding_res_t> outputs;for(size_t i = 0; i < inputs.size(); i++) {outputs.push_back(embedding_res_t(res_code, embedding_res));@@ -588,12 +513,7 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::strtry {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;+ nlohmann::json embedding_res = get_error_json(req_body, res_code, res);std::vector<embedding_res_t> outputs;for(size_t i = 0; i < inputs.size(); i++) {outputs.push_back(embedding_res_t(400, embedding_res));@@ -608,6 +528,34 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::strreturn outputs;}++nlohmann::json GCPEmbedder::get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) {+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res_body);+ } catch (const std::exception& e) {+ json_res = nlohmann::json::object();+ json_res["error"] = "Malformed response from GCP API.";+ }+ nlohmann::json embedding_res = nlohmann::json::object();+ embedding_res["response"] = json_res;+ 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;+ 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.";+ }++ if(res_code == 408) {+ embedding_res["error"] = "GCP API timeout.";+ }++ return embedding_res;+}+Option<std::string> GCPEmbedder::generate_access_token(const std::string& refresh_token, const std::string& client_id, const std::string& client_secret) {std::unordered_map<std::string, std::string> headers;headers["Content-Type"] = "application/x-www-form-urlencoded";@@ -643,5 +591,3 @@ Option<std::string> GCPEmbedder::generate_access_token(const std::string& refresreturn Option<std::string>(access_token);}--