#include "kbs_dp_insert.h"
#include "servagt/router.h"
#include "http_session.h"
//#include "user_session.h"
#include "khttp/khttp.h"
#include <json/json.h>
using namespace vi;
SERVAGT_BIND(kbs_dp_insert);
void handle_server_error(const std::shared_ptr<session>& sp_session, const http::status code, const std::string& msg) {
res_string_t res{http::status::ok, sp_session->get_req().version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json");
res.keep_alive(sp_session->get_req().keep_alive());
res.result(code);
res.body() = msg;
res.prepare_payload();
sp_session->set_res(res);
sp_session->set_succeed();
sp_session->set_respond();
}
void handle_milvus_response(const std::string& uri, const std::string& json_data, const std::function<void(sp_res_string_t)>& callback) {
std::shared_ptr<khttp> sp_khttp = std::make_shared<khttp>(sp_session);
sp_khttp->send_post(uri, json_data, callback);
}
void vi::kbs_dp_insert::handle(std::shared_ptr<session> sp_session) {
using namespace boost::beast;
auto& req = sp_session->get_req();
vlogi << "servagt try to handle the request" << std::endl;
auto const server_msg = [&req]() {
std::shared_ptr<vi::json_manager> _sp_json_mgr = std::make_shared<vi::json_manager>();
auto sp_root = _sp_json_mgr->get_json_root();
auto& root = *sp_root;
root["result"] = "success";
root["server_time"] = vi::vtime::now();
res_string_t res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json");
res.keep_alive(req.keep_alive());
res.body() = _sp_json_mgr->get_unformat();
res.prepare_payload();
return res;
};
auto const server_fail = [&req](std::string what, http::status code) {
res_string_t res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json");
res.keep_alive(req.keep_alive());
res.result(code);
res.body() = what;
res.prepare_payload();
return res;
};
// Create an instance of khttp for communicating with Milvus
std::shared_ptr<khttp> sp_khttp = std::make_shared<khttp>(sp_session);
// Sending a POST request to Milvus to create a collection
std::string milvus_create_uri = "http://localhost:19530/v2/vectordb/collections/create";
std::string milvus_list_uri = "http://localhost:19530/v2/vectordb/collections/list";
handle_milvus_response(milvus_list_uri, R"({"dbName": "default"})", [](sp_res_string_t sp_res) {
vlogi << "Milvus list response:\n" << *sp_res << std::endl;
});
std::string milvus_has_uri = "http://localhost:19530/v2/vectordb/collections/has";
handle_milvus_response(milvus_has_uri, R"({"dbName": "default", "collectionName": "kbsCollection"})", [sp_session](sp_res_string_t sp_res_has) {
vlogi << "Milvus has response:\n" << *sp_res_has << std::endl;
json_manager jm_has;
jm_has.read_from_string(sp_res_has->body());
auto& root_res = *jm_has.get_json_root();
int int_value = root_res["code"].asInt();
bool bool_value = root_res["data"]["has"].asBool();
if (int_value == 200 && !bool_value) {
vlogi << "Milvus create response:\n" << std::endl;
//if don't has, milvus return "code" : 200, create
// handle_milvus_response(milvus_create_uri, "", [](sp_res_string_t sp_res) {
// vlogi << "Milvus create response:\n" << *sp_res << std::endl;
// });
}
});
auto sp_my_req = sp_khttp->get_sp_request();
auto& client_request = *sp_my_req;
client_request.set(http::field::content_type, "application/json");
// JSON data for creating a collection
Json::Value create_json_data;
create_json_data["collectionName"] = "kbsCollection";
create_json_data["schema"]["autoId"] = false;
create_json_data["schema"]["enabledDynamicField"] = false;
Json::Value fields;
Json::Value uuid_field;
uuid_field["fieldName"] = "uuid";
uuid_field["dataType"] = "VarChar";
uuid_field["isPrimary"] = true;
Json::Value elementTypeParams;
elementTypeParams["max_length"] = "512";
elementTypeParams["dim"] = "512";
elementTypeParams["max_capacity"] = "512";
uuid_field["elementTypeParams"] = elementTypeParams;
fields.append(uuid_field);
Json::Value embedding_field;
embedding_field["fieldName"] = "embedding";
embedding_field["dataType"] = "FloatVector";
Json::Value embedding_params;
embedding_params["dim"] = "2048";
embedding_field["elementTypeParams"] = embedding_params;
fields.append(embedding_field);
Json::Value fileBlock_base64_field;
fileBlock_base64_field["fieldName"] = "fileBlock_base64";
fileBlock_base64_field["dataType"] = "VarChar";
Json::Value fileBlock_base64_params;
fileBlock_base64_params["max_length"] = "2048";
fileBlock_base64_params["dim"] = "2048";
fileBlock_base64_params["max_capacity"] = "2048";
fileBlock_base64_field["elementTypeParams"] = fileBlock_base64_params;
fields.append(fileBlock_base64_field);
Json::Value order_field;
order_field["fieldName"] = "order";
order_field["dataType"] = "Int64";
fields.append(order_field);
create_json_data["schema"]["fields"] = fields;
Json::Value indexParams;
Json::Value embedding_index;
embedding_index["fieldName"] = "embedding";
embedding_index["indexName"] = "embedding_index";
Json::Value embedding_index_params;
embedding_index_params["index_type"] = "IVF_FLAT";
embedding_index_params["nlist"] = "128";
embedding_index["params"] = embedding_index_params;
embedding_index["metricType"] = "IP";
indexParams.append(embedding_index);
Json::Value order_index;
order_index["fieldName"] = "order";
order_index["indexName"] = "order_index";
Json::Value order_index_params;
order_index_params["index_type"] = "STL_SORT";
order_index_params["partitionsNum"] = "512";
order_index["params"] = order_index_params;
indexParams.append(order_index);
create_json_data["indexParams"] = indexParams;
// Convert JSON data to string
Json::StreamWriterBuilder builder;
std::string create_json_str = Json::writeString(builder, create_json_data);
client_request.body() = create_json_str;
// sending the POST create request to Milvus
vlogi << "is_first_create" << is_first_create << std::endl;
if (is_first_create) {
handle_milvus_response(milvus_create_uri, "", [](sp_res_string_t sp_res) {
vlogi << "Milvus create response:\n" << *sp_res << std::endl;
});
is_first_create = false;
vlogi << "is_first_create" << is_first_create << std::endl;
}
// Prepare JSON data for inserting into Milvus
json_manager json_mgr;
json_mgr.read_from_string(req.body());
auto& root_received = *json_mgr.get_json_root();
vlogi << "newly received data to be stored:" << json_mgr.get_format() << std::endl;
if (!root_received.isMember("results") || !root_received["results"]["embedding"].isArray()) {
std::string msg = "received data error,";
if (req.body().empty())
msg += "body data is empty,";
if (root_received.isMember("results"))
msg += "it does not contain results,";
if (root_received["results"]["embedding"].isArray())
msg += "embedding is not array,";
static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION;
beast::error_code ec = beast::errc::make_error_code(beast::errc::bad_message, &loc);
msg = msg + "what():" + ec.what();
handle_server_error(sp_session, http::status::bad_request, msg);
return;
}
std::string md5_value = root_received["fileMD5"].asString();
std::string insert_data = root_received["results"].asString();
// Construct JSON data using jsoncpp
Json::Value root_insert;
root_insert["data"] = insert_data;
root_insert["partitionName"] = md5_value;
root_insert["collectionName"] = "kbsCollection";
FastWriter writer;
std::string insert_json_data = writer.write(root_insert);
// sending the POST request to Milvus
handle_milvus_response(milvusinsertUri, insert_json_data, [sp_session, server_msg](sp_res_string_t sp_res) {
vlogi << "Milvus insert response:\n" << *sp_res << std::endl;
sp_session->send_response(server_msg());
});
sp_session->set_succeed();
sp_session->set_respond();
}
【无标题】
最新推荐文章于 2025-05-21 10:09:58 发布