Skip to content

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

Support `remote_embedding_timeout_ms` and `remote_embedding_num_tries` for indexing

ozanarmagan34ade97636a7
diff --git a/include/collection.h b/include/collection.h
index a1f5fa87..01b6405d 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -391,7 +391,8 @@ public:
const std::vector<sort_by>& sort_by_fields);
void batch_index(std::vector<index_record>& index_records, std::vector<std::string>& json_out, size_t &num_indexed,
- const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size = 200);
+ const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2);
bool is_exceeding_memory_threshold() const;
@@ -405,7 +406,7 @@ public:
nlohmann::json get_summary_json() const;
size_t batch_index_in_memory(std::vector<index_record>& index_records, const size_t remote_embedding_batch_size,
- const bool generate_embeddings);
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries, const bool generate_embeddings);
Option<nlohmann::json> add(const std::string & json_str,
const index_operation_t& operation=CREATE, const std::string& id="",
@@ -415,7 +416,9 @@ public:
const index_operation_t& operation=CREATE, const std::string& id="",
const DIRTY_VALUES& dirty_values=DIRTY_VALUES::COERCE_OR_REJECT,
const bool& return_doc=false, const bool& return_id=false,
- const size_t remote_embedding_batch_size=200);
+ const size_t remote_embedding_batch_size=200,
+ const size_t remote_embedding_timeout_ms=60000,
+ const size_t remote_embedding_num_tries=2);
Option<nlohmann::json> update_matching_filter(const std::string& filter_query,
const std::string & json_str,
diff --git a/include/index.h b/include/index.h
index faa0d745..90515aa2 100644
--- a/include/index.h
+++ b/include/index.h
@@ -556,8 +556,9 @@ private:
void initialize_facet_indexes(const field& facet_field);
static void batch_embed_fields(std::vector<index_record*>& documents,
- const tsl::htrie_map<char, field>& embedding_fields,
- const tsl::htrie_map<char, field> & search_schema, const size_t remote_embedding_batch_size = 200);
+ const tsl::htrie_map<char, field>& embedding_fields,
+ const tsl::htrie_map<char, field> & search_schema, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2);
public:
// for limiting number of results on multiple candidates / query rewrites
@@ -696,7 +697,8 @@ public:
const std::string& fallback_field_type,
const std::vector<char>& token_separators,
const std::vector<char>& symbols_to_index,
- const bool do_validation, const size_t remote_embedding_batch_size = 200, const bool generate_embeddings = true);
+ const bool do_validation, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2, const bool generate_embeddings = true);
static size_t batch_memory_index(Index *index,
std::vector<index_record>& iter_batch,
@@ -706,7 +708,8 @@ public:
const std::string& fallback_field_type,
const std::vector<char>& token_separators,
const std::vector<char>& symbols_to_index,
- const bool do_validation, const size_t remote_embedding_batch_size = 200, const bool generate_embeddings = true,
+ const bool do_validation, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2, const bool generate_embeddings = true,
const bool use_addition_fields = false, const tsl::htrie_map<char, field>& addition_fields = {});
void index_field_in_memory(const field& afield, std::vector<index_record>& iter_batch);
diff --git a/include/text_embedder.h b/include/text_embedder.h
index ca64aa52..370d0670 100644
--- a/include/text_embedder.h
+++ b/include/text_embedder.h
@@ -17,7 +17,8 @@ class TextEmbedder {
TextEmbedder(const nlohmann::json& model_config, size_t num_dims);
~TextEmbedder();
embedding_res_t Embed(const std::string& text, const size_t remote_embedder_timeout_ms = 30000, const size_t remote_embedding_num_tries = 2);
- std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200);
+ std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2);
const std::string& get_vocab_file_name() const;
const size_t get_num_dim() const;
bool is_remote() {
diff --git a/include/text_embedder_remote.h b/include/text_embedder_remote.h
index b5f219d5..1c39489a 100644
--- a/include/text_embedder_remote.h
+++ b/include/text_embedder_remote.h
@@ -30,7 +30,8 @@ class RemoteEmbedder {
public:
virtual nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) = 0;
virtual embedding_res_t Embed(const std::string& text, const size_t remote_embedder_timeout_ms = 30000, const size_t remote_embedding_num_tries = 2) = 0;
- virtual std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200) = 0;
+ virtual std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2) = 0;
static const std::string get_model_key(const nlohmann::json& model_config);
static void init(ReplicationState* rs) {
raft_server = rs;
@@ -50,7 +51,8 @@ class OpenAIEmbedder : public RemoteEmbedder {
OpenAIEmbedder(const std::string& openai_model_path, const std::string& api_key);
static Option<bool> is_model_valid(const nlohmann::json& model_config, size_t& num_dims);
embedding_res_t Embed(const std::string& text, const size_t remote_embedder_timeout_ms = 30000, const size_t remote_embedding_num_tries = 2) override;
- std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200) override;
+ std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2) override;
nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;
static std::string get_model_key(const nlohmann::json& model_config);
};
@@ -68,7 +70,8 @@ class GoogleEmbedder : public RemoteEmbedder {
GoogleEmbedder(const std::string& google_api_key);
static Option<bool> is_model_valid(const nlohmann::json& model_config, size_t& num_dims);
embedding_res_t Embed(const std::string& text, const size_t remote_embedder_timeout_ms = 30000, const size_t remote_embedding_num_tries = 2) override;
- std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200) override;
+ std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2) override;
nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;
static std::string get_model_key(const nlohmann::json& model_config);
};
@@ -96,7 +99,8 @@ class GCPEmbedder : public RemoteEmbedder {
const std::string& refresh_token, const std::string& client_id, const std::string& client_secret);
static Option<bool> is_model_valid(const nlohmann::json& model_config, size_t& num_dims);
embedding_res_t Embed(const std::string& text, const size_t remote_embedder_timeout_ms = 30000, const size_t remote_embedding_num_tries = 2) override;
- std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200) override;
+ std::vector<embedding_res_t> batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size = 200,
+ const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2) override;
nlohmann::json get_error_json(const nlohmann::json& req_body, long res_code, const std::string& res_body) override;
static std::string get_model_key(const nlohmann::json& model_config);
};
diff --git a/src/collection.cpp b/src/collection.cpp
index 1afef53a..10056994 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -290,7 +290,9 @@ Option<nlohmann::json> Collection::add(const std::string & json_str,
nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohmann::json& document,
const index_operation_t& operation, const std::string& id,
const DIRTY_VALUES& dirty_values, const bool& return_doc, const bool& return_id,
- const size_t remote_embedding_batch_size) {
+ const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms,
+ const size_t remote_embedding_num_tries) {
//LOG(INFO) << "Memory ratio. Max = " << max_memory_ratio << ", Used = " << SystemMetrics::used_memory_ratio();
std::vector<index_record> index_records;
@@ -380,7 +382,7 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohma
if((i+1) % index_batch_size == 0 || i == json_lines.size()-1 || repeated_doc) {
- batch_index(index_records, json_lines, num_indexed, return_doc, return_id, remote_embedding_batch_size);
+ batch_index(index_records, json_lines, num_indexed, return_doc, return_id, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries);
// to return the document for the single doc add cases
if(index_records.size() == 1) {
@@ -497,9 +499,10 @@ bool Collection::is_exceeding_memory_threshold() const {
}
void Collection::batch_index(std::vector<index_record>& index_records, std::vector<std::string>& json_out,
- size_t &num_indexed, const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size) {
+ size_t &num_indexed, const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
- batch_index_in_memory(index_records, remote_embedding_batch_size, true);
+ batch_index_in_memory(index_records, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries, true);
// store only documents that were indexed in-memory successfully
for(auto& index_record: index_records) {
@@ -603,11 +606,12 @@ Option<uint32_t> Collection::index_in_memory(nlohmann::json &document, uint32_t
return Option<>(200);
}
-size_t Collection::batch_index_in_memory(std::vector<index_record>& index_records, const size_t remote_embedding_batch_size, const bool generate_embeddings) {
+size_t Collection::batch_index_in_memory(std::vector<index_record>& index_records, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries, const bool generate_embeddings) {
std::unique_lock lock(mutex);
size_t num_indexed = Index::batch_memory_index(index, index_records, default_sorting_field,
search_schema, embedding_fields, fallback_field_type,
- token_separators, symbols_to_index, true, remote_embedding_batch_size, generate_embeddings);
+ token_separators, symbols_to_index, true, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries, generate_embeddings);
num_documents += num_indexed;
return num_indexed;
}
@@ -3948,7 +3952,7 @@ Option<bool> Collection::batch_alter_data(const std::vector<field>& alter_fields
}
Index::batch_memory_index(index, iter_batch, default_sorting_field, search_schema, embedding_fields,
- fallback_field_type, token_separators, symbols_to_index, true, 200, found_embedding_field, true, schema_additions);
+ fallback_field_type, token_separators, symbols_to_index, true, 200, 60000, 2, found_embedding_field, true, schema_additions);
if(found_embedding_field) {
for(auto& index_record : iter_batch) {
if(index_record.indexed.ok()) {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 98cbc50b..29270d55 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1421,7 +1421,7 @@ Option<bool> CollectionManager::load_collection(const nlohmann::json &collection
// batch must match atleast the number of shards
if(exceeds_batch_mem_threshold || (num_valid_docs % batch_size == 0) || last_record) {
size_t num_records = index_records.size();
- size_t num_indexed = collection->batch_index_in_memory(index_records, 200, false);
+ size_t num_indexed = collection->batch_index_in_memory(index_records, 200, 60000, 2, false);
batch_doc_str_size = 0;
if(num_indexed != num_records) {
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 716eabf1..62b151fe 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -742,6 +742,8 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
const char *RETURN_DOC = "return_doc";
const char *RETURN_ID = "return_id";
const char *REMOTE_EMBEDDING_BATCH_SIZE = "remote_embedding_batch_size";
+ const char *REMOTE_EMBEDDING_TIMEOUT_MS = "remote_embedding_timeout_ms";
+ const char *REMOTE_EMBEDDING_NUM_TRIES = "remote_embedding_num_tries";
if(req->params.count(BATCH_SIZE) == 0) {
req->params[BATCH_SIZE] = "40";
@@ -796,8 +798,18 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
return false;
}
+ if(req->params.count(REMOTE_EMBEDDING_TIMEOUT_MS) == 0) {
+ req->params[REMOTE_EMBEDDING_TIMEOUT_MS] = "60000";
+ }
+
+ if(req->params.count(REMOTE_EMBEDDING_NUM_TRIES) == 0) {
+ req->params[REMOTE_EMBEDDING_NUM_TRIES] = "2";
+ }
+
const size_t IMPORT_BATCH_SIZE = std::stoi(req->params[BATCH_SIZE]);
const size_t REMOTE_EMBEDDING_BATCH_SIZE_VAL = std::stoi(req->params[REMOTE_EMBEDDING_BATCH_SIZE]);
+ const size_t REMOTE_EMBEDDING_TIMEOUT_MS_VAL = std::stoi(req->params[REMOTE_EMBEDDING_TIMEOUT_MS]);
+ const size_t REMOTE_EMBEDDING_NUM_TRIES_VAL = std::stoi(req->params[REMOTE_EMBEDDING_NUM_TRIES]);
if(IMPORT_BATCH_SIZE == 0) {
res->final = true;
@@ -813,6 +825,20 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
return false;
}
+ if(REMOTE_EMBEDDING_TIMEOUT_MS_VAL == 0) {
+ res->final = true;
+ res->set_400("Parameter `" + std::string(REMOTE_EMBEDDING_TIMEOUT_MS) + "` must be a positive integer.");
+ stream_response(req, res);
+ return false;
+ }
+
+ if(REMOTE_EMBEDDING_NUM_TRIES_VAL == 0) {
+ res->final = true;
+ res->set_400("Parameter `" + std::string(REMOTE_EMBEDDING_NUM_TRIES) + "` must be a positive integer.");
+ stream_response(req, res);
+ return false;
+ }
+
if(req->body_index == 0) {
// will log for every major chunk of request body
//LOG(INFO) << "Import, req->body.size=" << req->body.size() << ", batch_size=" << IMPORT_BATCH_SIZE;
@@ -882,7 +908,7 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
const bool& return_doc = req->params[RETURN_DOC] == "true";
const bool& return_id = req->params[RETURN_ID] == "true";
nlohmann::json json_res = collection->add_many(json_lines, document, operation, "",
- dirty_values, return_doc, return_id, REMOTE_EMBEDDING_BATCH_SIZE_VAL);
+ dirty_values, return_doc, return_id, REMOTE_EMBEDDING_BATCH_SIZE_VAL, REMOTE_EMBEDDING_TIMEOUT_MS_VAL, REMOTE_EMBEDDING_NUM_TRIES_VAL);
//const std::string& import_summary_json = json_res->dump();
//response_stream << import_summary_json << "\n";
@@ -926,6 +952,7 @@ bool post_add_document(const std::shared_ptr<http_req>& req, const std::shared_p
req->params[DIRTY_VALUES_PARAM] = ""; // set it empty as default will depend on whether schema is enabled
}
+
CollectionManager & collectionManager = CollectionManager::get_instance();
auto collection = collectionManager.get_collection(req->params["collection"]);
@@ -937,10 +964,22 @@ bool post_add_document(const std::shared_ptr<http_req>& req, const std::shared_p
const index_operation_t operation = get_index_operation(req->params[ACTION]);
const auto& dirty_values = collection->parse_dirty_values_option(req->params[DIRTY_VALUES_PARAM]);
+ size_t remote_embedding_timeout_ms = 60000;
+ size_t remote_embedding_num_tries = 2;
+
+ if(req->params.count("remote_embedding_timeout_ms") != 0) {
+ remote_embedding_timeout_ms = std::stoul(req->params["remote_embedding_timeout_ms"]);
+ }
+
+ if(req->params.count("remote_embedding_num_tries") != 0) {
+ remote_embedding_num_tries = std::stoul(req->params["remote_embedding_num_tries"]);
+ }
+
nlohmann::json document;
std::vector<std::string> json_lines = {req->body};
const nlohmann::json& inserted_doc_op = collection->add_many(json_lines, document, operation, "", dirty_values,
- false, false);
+ false, false, 200, remote_embedding_timeout_ms,
+ remote_embedding_num_tries);
if(!inserted_doc_op["success"].get<bool>()) {
nlohmann::json res_doc;
diff --git a/src/index.cpp b/src/index.cpp
index 65ae04ee..f027c09e 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -417,7 +417,8 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
const std::string& fallback_field_type,
const std::vector<char>& token_separators,
const std::vector<char>& symbols_to_index,
- const bool do_validation, const size_t remote_embedding_batch_size, const bool generate_embeddings) {
+ const bool do_validation, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries, const bool generate_embeddings) {
// runs in a partitioned thread
std::vector<index_record*> records_to_embed;
@@ -508,7 +509,7 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
}
if(generate_embeddings) {
- batch_embed_fields(records_to_embed, embedding_fields, search_schema, remote_embedding_batch_size);
+ batch_embed_fields(records_to_embed, embedding_fields, search_schema, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries);
}
}
@@ -519,7 +520,8 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
const std::string& fallback_field_type,
const std::vector<char>& token_separators,
const std::vector<char>& symbols_to_index,
- const bool do_validation, const size_t remote_embedding_batch_size, const bool generate_embeddings,
+ const bool do_validation, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries, const bool generate_embeddings,
const bool use_addition_fields, const tsl::htrie_map<char, field>& addition_fields) {
const size_t concurrency = 4;
@@ -552,7 +554,7 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
index->thread_pool->enqueue([&, batch_index, batch_len]() {
write_log_index = local_write_log_index;
validate_and_preprocess(index, iter_batch, batch_index, batch_len, default_sorting_field, actual_search_schema,
- embedding_fields, fallback_field_type, token_separators, symbols_to_index, do_validation, remote_embedding_batch_size, generate_embeddings);
+ embedding_fields, fallback_field_type, token_separators, symbols_to_index, do_validation, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries, generate_embeddings);
std::unique_lock<std::mutex> lock(m_process);
num_processed++;
@@ -6637,8 +6639,9 @@ bool Index::common_results_exist(std::vector<art_leaf*>& leaves, bool must_match
void Index::batch_embed_fields(std::vector<index_record*>& records,
- const tsl::htrie_map<char, field>& embedding_fields,
- const tsl::htrie_map<char, field> & search_schema, const size_t remote_embedding_batch_size) {
+ const tsl::htrie_map<char, field>& embedding_fields,
+ const tsl::htrie_map<char, field> & search_schema, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
for(const auto& field : embedding_fields) {
std::vector<std::pair<index_record*, std::string>> texts_to_embed;
auto indexing_prefix = TextEmbedderManager::get_instance().get_indexing_prefix(field.embed[fields::model_config]);
@@ -6709,7 +6712,8 @@ void Index::batch_embed_fields(std::vector<index_record*>& records,
texts.push_back(text_to_embed.second);
}
- auto embeddings = embedder_op.get()->batch_embed(texts, remote_embedding_batch_size);
+ auto embeddings = embedder_op.get()->batch_embed(texts, remote_embedding_batch_size, remote_embedding_timeout_ms,
+ remote_embedding_num_tries);
for(size_t i = 0; i < embeddings.size(); i++) {
auto& embedding_res = embeddings[i];
diff --git a/src/text_embedder.cpp b/src/text_embedder.cpp
index 126655f9..897b1c0b 100644
--- a/src/text_embedder.cpp
+++ b/src/text_embedder.cpp
@@ -152,7 +152,8 @@ embedding_res_t TextEmbedder::Embed(const std::string& text, const size_t remote
}
}
-std::vector<embedding_res_t> TextEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size) {
+std::vector<embedding_res_t> TextEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
std::vector<embedding_res_t> outputs;
if(!is_remote()) {
std::lock_guard<std::mutex> lock(mutex_);
@@ -235,7 +236,7 @@ std::vector<embedding_res_t> TextEmbedder::batch_embed(const std::vector<std::st
}
}
} else {
- outputs = std::move(remote_embedder_->batch_embed(inputs, remote_embedding_batch_size));
+ outputs = std::move(remote_embedder_->batch_embed(inputs, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries));
}
return outputs;
diff --git a/src/text_embedder_remote.cpp b/src/text_embedder_remote.cpp
index b8908baf..27409c2d 100644
--- a/src/text_embedder_remote.cpp
+++ b/src/text_embedder_remote.cpp
@@ -188,13 +188,14 @@ embedding_res_t OpenAIEmbedder::Embed(const std::string& text, const size_t remo
}
}
-std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size) {
+std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
// call recursively if inputs larger than remote_embedding_batch_size
if(inputs.size() > remote_embedding_batch_size) {
std::vector<embedding_res_t> outputs;
for(size_t i = 0; i < inputs.size(); i += remote_embedding_batch_size) {
auto batch = std::vector<std::string>(inputs.begin() + i, inputs.begin() + std::min(i + remote_embedding_batch_size, inputs.size()));
- auto batch_outputs = batch_embed(batch, remote_embedding_batch_size);
+ auto batch_outputs = batch_embed(batch, remote_embedding_batch_size, remote_embedding_timeout_ms, remote_embedding_num_tries);
outputs.insert(outputs.end(), batch_outputs.begin(), batch_outputs.end());
}
return outputs;
@@ -206,6 +207,8 @@ std::vector<embedding_res_t> OpenAIEmbedder::batch_embed(const std::vector<std::
std::unordered_map<std::string, std::string> headers;
headers["Authorization"] = "Bearer " + api_key;
headers["Content-Type"] = "application/json";
+ headers["timeout_ms"] = std::to_string(remote_embedding_timeout_ms);
+ headers["num_try"] = std::to_string(remote_embedding_num_tries);
std::map<std::string, std::string> res_headers;
std::string res;
auto res_code = call_remote_api("POST", OPENAI_CREATE_EMBEDDING, req_body.dump(), res, res_headers, headers);
@@ -370,11 +373,12 @@ embedding_res_t GoogleEmbedder::Embed(const std::string& text, const size_t remo
}
-std::vector<embedding_res_t> GoogleEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size) {
+std::vector<embedding_res_t> GoogleEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
std::vector<embedding_res_t> outputs;
bool timeout_prev = false;
for(auto& input : inputs) {
- auto res = Embed(input);
+ auto res = Embed(input, remote_embedding_timeout_ms, remote_embedding_num_tries);
if(res.status_code == 408) {
if(timeout_prev) {
// fail whole batch if two consecutive timeouts,
@@ -540,7 +544,8 @@ embedding_res_t GCPEmbedder::Embed(const std::string& text, const size_t remote_
}
-std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size) {
+std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::string>& inputs, const size_t remote_embedding_batch_size,
+ const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {
// GCP API has a limit of 5 instances per request
if(inputs.size() > 5) {
std::vector<embedding_res_t> res;
@@ -560,6 +565,8 @@ std::vector<embedding_res_t> GCPEmbedder::batch_embed(const std::vector<std::str
std::unordered_map<std::string, std::string> headers;
headers["Authorization"] = "Bearer " + access_token;
headers["Content-Type"] = "application/json";
+ headers["timeout_ms"] = std::to_string(remote_embedding_timeout_ms);
+ headers["num_try"] = std::to_string(remote_embedding_num_tries);
std::map<std::string, std::string> res_headers;
std::string res;
auto res_code = call_remote_api("POST", get_gcp_embedding_url(project_id, model_name), req_body.dump(), res, res_headers, headers);