mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-16 21:20:39 +03:00
add database and migration logic (#27)
* update chat_microservice * reformat logger_conf * add database * add service and repository logic * fix constants gpt base url * add models endpoints
This commit is contained in:
2
chat_gpt_microservice/.gitignore
vendored
2
chat_gpt_microservice/.gitignore
vendored
@@ -154,4 +154,4 @@ cython_debug/
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
.idea/
|
||||
|
||||
@@ -23,7 +23,6 @@ public:
|
||||
boost::asio::awaitable<void> deepAi(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> aiChat(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> chatGptAi(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> weWordle(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> acytoo(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> openAi(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> h2o(std::shared_ptr<Channel>, nlohmann::json);
|
||||
@@ -31,7 +30,6 @@ public:
|
||||
boost::asio::awaitable<void> huggingChat(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> you(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> binjie(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> codeLinkAva(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> chatBase(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> aivvm(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> ylokh(std::shared_ptr<Channel>, nlohmann::json);
|
||||
@@ -39,6 +37,9 @@ public:
|
||||
boost::asio::awaitable<void> gptGo(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> aibn(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> chatGptDuo(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> chatForAi(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> freeGpt(std::shared_ptr<Channel>, nlohmann::json);
|
||||
boost::asio::awaitable<void> cromicle(std::shared_ptr<Channel>, nlohmann::json);
|
||||
|
||||
private:
|
||||
boost::asio::awaitable<std::expected<boost::beast::ssl_stream<boost::beast::tcp_stream>, std::string>>
|
||||
|
||||
@@ -641,11 +641,15 @@ FreeGpt::createHttpClient(boost::asio::ssl::context& ctx, std::string_view host,
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::deepAi(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
ScopeExit auto_exit{[&] { ch->close(); }};
|
||||
co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
|
||||
|
||||
boost::system::error_code err{};
|
||||
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }};
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
std::string user_agent{
|
||||
R"(Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36)"};
|
||||
R"(Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36)"};
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 mt(rd());
|
||||
std::uniform_int_distribution<uint64_t> dist(0, 100000000);
|
||||
@@ -654,19 +658,39 @@ boost::asio::awaitable<void> FreeGpt::deepAi(std::shared_ptr<Channel> ch, nlohma
|
||||
auto api_key = std::format("tryit-{}-{}", part1, part2);
|
||||
|
||||
constexpr char CRLF[] = "\r\n";
|
||||
constexpr char MULTI_PART_BOUNDARY[] = "9bc627aea4f77e150e6057f78036e73f";
|
||||
constexpr std::string_view host{"api.deepai.org"};
|
||||
constexpr std::string_view port{"443"};
|
||||
static std::string MULTI_PART_BOUNDARY = "9bc627aea4f77e150e6057f78036e73f";
|
||||
|
||||
boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::post,
|
||||
"/make_me_a_sandwich", 11};
|
||||
req.set(boost::beast::http::field::host, host);
|
||||
req.set(boost::beast::http::field::user_agent, user_agent);
|
||||
req.set("Api-Key", api_key);
|
||||
req.set(boost::beast::http::field::content_type,
|
||||
std::format("multipart/form-data; boundary={}", MULTI_PART_BOUNDARY));
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
auto error_info = std::format("curl_easy_init() failed:{}", curl_easy_strerror(res));
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepai.org/hacking_is_a_crime");
|
||||
|
||||
if (!m_cfg.http_proxy.empty())
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, m_cfg.http_proxy.c_str());
|
||||
|
||||
struct Input {
|
||||
std::shared_ptr<Channel> ch;
|
||||
std::string recv;
|
||||
};
|
||||
Input input{ch};
|
||||
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t {
|
||||
boost::system::error_code err{};
|
||||
auto input_ptr = static_cast<Input*>(userp);
|
||||
std::string data{(char*)contents, size * nmemb};
|
||||
auto& [ch, recv] = *input_ptr;
|
||||
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, data); });
|
||||
return size * nmemb;
|
||||
};
|
||||
size_t (*action_fn)(void* contents, size_t size, size_t nmemb, void* userp) = action_cb;
|
||||
curlEasySetopt(curl);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, action_fn);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
|
||||
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
nlohmann::json request_json{{{"role", "user"}, {"content", std::move(prompt)}}};
|
||||
|
||||
std::ostringstream payload;
|
||||
@@ -674,30 +698,37 @@ boost::asio::awaitable<void> FreeGpt::deepAi(std::shared_ptr<Channel> ch, nlohma
|
||||
<< CRLF << "chat" << CRLF << "--" << MULTI_PART_BOUNDARY << CRLF
|
||||
<< R"(Content-Disposition: form-data; name="chatHistory")" << CRLF << CRLF << request_json.dump() << CRLF
|
||||
<< "--" << MULTI_PART_BOUNDARY << "--" << CRLF;
|
||||
|
||||
SPDLOG_INFO("{}", payload.str());
|
||||
req.body() = payload.str();
|
||||
req.prepare_payload();
|
||||
auto str = payload.str();
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str());
|
||||
|
||||
int recreate_num{0};
|
||||
create_client:
|
||||
boost::asio::ssl::context ctx(boost::asio::ssl::context::tls);
|
||||
ctx.set_verify_mode(boost::asio::ssl::verify_none);
|
||||
auto client = co_await createHttpClient(ctx, host, port);
|
||||
if (!client.has_value()) {
|
||||
SPDLOG_ERROR("createHttpClient: {}", client.error());
|
||||
co_await ch->async_send(err, client.error(), use_nothrow_awaitable);
|
||||
struct curl_slist* headers = nullptr;
|
||||
auto content_type_str = std::format("Content-Type: multipart/form-data; boundary={}", MULTI_PART_BOUNDARY);
|
||||
SPDLOG_INFO("content_type_str: {}", content_type_str);
|
||||
headers = curl_slist_append(headers, content_type_str.c_str());
|
||||
auto api_key_str = std::format("api-key: {}", api_key);
|
||||
headers = curl_slist_append(headers, api_key_str.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
ScopeExit auto_exit{[=] {
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
}};
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
auto error_info = std::format("curl_easy_perform() failed:{}", curl_easy_strerror(res));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
auto& stream_ = client.value();
|
||||
|
||||
auto ret = co_await sendRequestRecvChunk(ch, stream_, req, 200, [&ch](std::string recv_str) {
|
||||
boost::system::error_code ec{};
|
||||
ch->try_send(ec, recv_str);
|
||||
});
|
||||
if (ret == Status::Close && recreate_num == 0) {
|
||||
recreate_num++;
|
||||
goto create_client;
|
||||
int32_t response_code;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||
if (response_code != 200) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, std::format("deepai http code:{}", response_code));
|
||||
co_return;
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
@@ -1007,116 +1038,6 @@ create_client:
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::weWordle(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
ScopeExit auto_exit{[&] { ch->close(); }};
|
||||
boost::system::error_code err{};
|
||||
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
auto random = [](int len) {
|
||||
static std::string chars{"abcdefghijklmnopqrstuvwxyz0123456789"};
|
||||
static std::string letter{"abcdefghijklmnopqrstuvwxyz"};
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(0, 1000000);
|
||||
std::string random_string;
|
||||
random_string += chars[dis(gen) % letter.length()];
|
||||
len = len - 1;
|
||||
for (int i = 0; i < len; i++)
|
||||
random_string += chars[dis(gen) % chars.length()];
|
||||
return random_string;
|
||||
};
|
||||
auto user_id = random(16);
|
||||
auto app_id = random(31);
|
||||
auto now = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());
|
||||
auto request_date = std::format("{:%Y-%m-%dT%H:%M:%S.000Z}", now);
|
||||
|
||||
constexpr std::string_view host = "wewordle.org";
|
||||
constexpr std::string_view port = "443";
|
||||
|
||||
boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::post,
|
||||
"/gptapi/v1/android/turbo", 11};
|
||||
req.set(boost::beast::http::field::host, host);
|
||||
req.set("pragma", "no-cache");
|
||||
req.set("accept", "*/*");
|
||||
req.set(
|
||||
boost::beast::http::field::user_agent,
|
||||
R"(Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36)");
|
||||
req.set(boost::beast::http::field::content_type, "application/json");
|
||||
req.set("Accept-Encoding", "gzip, deflate");
|
||||
|
||||
constexpr std::string_view json_str = R"({
|
||||
"user":"j1b892x978flimoa",
|
||||
"messages":[
|
||||
{
|
||||
"role":"user",
|
||||
"content":"user: hello\nassistant:"
|
||||
}
|
||||
],
|
||||
"subscriber":{
|
||||
"originalPurchaseDate":null,
|
||||
"originalApplicationVersion":null,
|
||||
"allPurchaseDatesMillis":{},
|
||||
"entitlements":{
|
||||
"active":{},
|
||||
"all":{}
|
||||
},
|
||||
"allPurchaseDates":{},
|
||||
"allExpirationDatesMillis":{},
|
||||
"allExpirationDates":{},
|
||||
"originalAppUserId":"$RCAnonymousID:z6xyxaasvt841d5zttw7q2iisb023tf",
|
||||
"latestExpirationDate":null,
|
||||
"requestDate":"2023-08-03T00:29:53.000Z",
|
||||
"latestExpirationDateMillis":null,
|
||||
"nonSubscriptionTransactions":[],
|
||||
"originalPurchaseDateMillis":null,
|
||||
"managementURL":null,
|
||||
"allPurchasedProductIdentifiers":[],
|
||||
"firstSeen":"2023-08-03T00:29:53.000Z",
|
||||
"activeSubscriptions":[]
|
||||
}
|
||||
})";
|
||||
|
||||
nlohmann::json request = nlohmann::json::parse(json_str, nullptr, false);
|
||||
|
||||
request["user"] = user_id;
|
||||
request["subscriber"]["originalAppUserId"] = std::format("$RCAnonymousID:{}", app_id);
|
||||
request["subscriber"]["firstSeen"] = request_date;
|
||||
request["subscriber"]["requestDate"] = request_date;
|
||||
request["messages"][0]["content"] = std::format("user: {}\nassistant:", prompt);
|
||||
|
||||
SPDLOG_INFO("{}", request.dump(2));
|
||||
|
||||
req.body() = request.dump();
|
||||
req.prepare_payload();
|
||||
|
||||
auto ret = co_await sendRequestRecvResponse(req, host, port, std::bind_front(&FreeGpt::createHttpClient, *this));
|
||||
if (!ret.has_value()) {
|
||||
co_await ch->async_send(err, ret.error(), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
auto& [res, ctx, stream_] = ret.value();
|
||||
if (boost::beast::http::status::ok != res.result()) {
|
||||
SPDLOG_ERROR("http status code: {}", res.result_int());
|
||||
co_await ch->async_send(err, res.reason(), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
|
||||
nlohmann::json rsp = nlohmann::json::parse(res.body(), nullptr, false);
|
||||
if (rsp.is_discarded()) {
|
||||
SPDLOG_ERROR("json parse error");
|
||||
co_await ch->async_send(err, std::format("json parse error: {}", res.body()), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
if (!rsp.contains("message")) {
|
||||
SPDLOG_ERROR("not contains message: {}", rsp.dump());
|
||||
co_await ch->async_send(err, std::format("not contains message : {}", rsp.dump()), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
co_await ch->async_send(err, rsp["message"].value("content", rsp.dump()), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::acytoo(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
boost::system::error_code err{};
|
||||
ScopeExit auto_exit{[&] { ch->close(); }};
|
||||
@@ -1606,7 +1527,7 @@ boost::asio::awaitable<void> FreeGpt::huggingChat(std::shared_ptr<Channel> ch, n
|
||||
req_init_conversation.set(boost::beast::http::field::user_agent, user_agent);
|
||||
req_init_conversation.set("Accept", "*/*");
|
||||
req_init_conversation.set("Content-Type", "application/json");
|
||||
req_init_conversation.body() = R"({"model": "OpenAssistant/oasst-sft-6-llama-30b-xor"})";
|
||||
req_init_conversation.body() = R"({"model": "meta-llama/Llama-2-70b-chat-hf"})";
|
||||
req_init_conversation.prepare_payload();
|
||||
|
||||
auto [ec, count] = co_await boost::beast::http::async_write(stream_, req_init_conversation, use_nothrow_awaitable);
|
||||
@@ -1902,87 +1823,6 @@ boost::asio::awaitable<void> FreeGpt::binjie(std::shared_ptr<Channel> ch, nlohma
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::codeLinkAva(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
boost::system::error_code err{};
|
||||
ScopeExit auto_exit{[&] { ch->close(); }};
|
||||
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
constexpr std::string_view host = "ava-alpha-api.codelink.io";
|
||||
constexpr std::string_view port = "443";
|
||||
|
||||
constexpr std::string_view user_agent{
|
||||
R"(Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36)"};
|
||||
|
||||
boost::asio::ssl::context ctx(boost::asio::ssl::context::tls);
|
||||
ctx.set_verify_mode(boost::asio::ssl::verify_none);
|
||||
|
||||
auto client = co_await createHttpClient(ctx, host, port);
|
||||
if (!client.has_value()) {
|
||||
SPDLOG_ERROR("createHttpClient: {}", client.error());
|
||||
co_await ch->async_send(err, client.error(), use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
auto& stream_ = client.value();
|
||||
|
||||
boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::post, "/api/chat", 11};
|
||||
req.set(boost::beast::http::field::host, host);
|
||||
req.set(boost::beast::http::field::user_agent, user_agent);
|
||||
req.set("Accept", "*/*");
|
||||
req.set("accept-language", "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3");
|
||||
req.set("origin", "https://ava-ai-ef611.web.app");
|
||||
req.set("referer", "https://ava-ai-ef611.web.app/");
|
||||
req.set(boost::beast::http::field::content_type, "application/json");
|
||||
req.set("sec-fetch-dest", "empty");
|
||||
req.set("sec-fetch-mode", "cors");
|
||||
req.set("sec-fetch-site", "same-origin");
|
||||
|
||||
constexpr std::string_view json_str = R"({
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "hello"
|
||||
}
|
||||
],
|
||||
"stream": true,
|
||||
"temperature": 0.6
|
||||
})";
|
||||
nlohmann::json request = nlohmann::json::parse(json_str, nullptr, false);
|
||||
|
||||
request["messages"][0]["content"] = prompt;
|
||||
SPDLOG_INFO("{}", request.dump(2));
|
||||
|
||||
req.body() = request.dump();
|
||||
req.prepare_payload();
|
||||
|
||||
std::string recv;
|
||||
auto result = co_await sendRequestRecvChunk(ch, stream_, req, 200, [&ch, &recv](std::string chunk_str) {
|
||||
recv.append(chunk_str);
|
||||
while (true) {
|
||||
auto position = recv.find("\n");
|
||||
if (position == std::string::npos)
|
||||
break;
|
||||
auto msg = recv.substr(0, position + 1);
|
||||
recv.erase(0, position + 1);
|
||||
msg.pop_back();
|
||||
if (msg.empty() || !msg.contains("content"))
|
||||
continue;
|
||||
auto fields = splitString(msg, "data:");
|
||||
boost::system::error_code err{};
|
||||
nlohmann::json line_json = nlohmann::json::parse(fields.back(), nullptr, false);
|
||||
if (line_json.is_discarded()) {
|
||||
SPDLOG_ERROR("json parse error: [{}]", fields.back());
|
||||
ch->try_send(err, std::format("json parse error: [{}]", fields.back()));
|
||||
continue;
|
||||
}
|
||||
auto str = line_json["choices"][0]["delta"]["content"].get<std::string>();
|
||||
if (!str.empty())
|
||||
ch->try_send(err, str);
|
||||
}
|
||||
});
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::chatBase(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
boost::system::error_code err{};
|
||||
ScopeExit auto_exit{[&] { ch->close(); }};
|
||||
@@ -2703,3 +2543,318 @@ boost::asio::awaitable<void> FreeGpt::chatGptDuo(std::shared_ptr<Channel> ch, nl
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::chatForAi(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
|
||||
|
||||
boost::system::error_code err{};
|
||||
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }};
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
auto error_info = std::format("curl_easy_init() failed:{}", curl_easy_strerror(res));
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://chatforai.com/api/handle/provider-openai");
|
||||
if (!m_cfg.http_proxy.empty())
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, m_cfg.http_proxy.c_str());
|
||||
|
||||
struct Input {
|
||||
std::shared_ptr<Channel> ch;
|
||||
std::string recv;
|
||||
};
|
||||
Input input{ch};
|
||||
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t {
|
||||
boost::system::error_code err{};
|
||||
auto input_ptr = static_cast<Input*>(userp);
|
||||
std::string data{(char*)contents, size * nmemb};
|
||||
auto& [ch, recv] = *input_ptr;
|
||||
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, data); });
|
||||
return size * nmemb;
|
||||
};
|
||||
size_t (*action_fn)(void* contents, size_t size, size_t nmemb, void* userp) = action_cb;
|
||||
curlEasySetopt(curl);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, action_fn);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
|
||||
|
||||
auto generate_signature = [](int timestamp, const std::string& conversation_id, const std::string& message) {
|
||||
std::stringstream ss;
|
||||
ss << timestamp << ":" << conversation_id << ":" << message << ":6B46K4pt";
|
||||
std::string data = ss.str();
|
||||
|
||||
unsigned char digest[SHA256_DIGEST_LENGTH];
|
||||
SHA256(reinterpret_cast<const unsigned char*>(data.c_str()), data.length(), digest);
|
||||
|
||||
std::stringstream sha_stream;
|
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
sha_stream << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(digest[i]);
|
||||
}
|
||||
return sha_stream.str();
|
||||
};
|
||||
uint64_t timestamp =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
auto onversation_id = std::format("id_{}", timestamp);
|
||||
std::string signature = generate_signature(timestamp, onversation_id, prompt);
|
||||
|
||||
constexpr std::string_view request_str{R"({
|
||||
"conversationId": "id_1696338587",
|
||||
"conversationType": "chat_continuous",
|
||||
"botId": "chat_continuous",
|
||||
"globalSettings": {
|
||||
"baseUrl": "https://api.openai.com",
|
||||
"model": "gpt-3.5-turbo",
|
||||
"messageHistorySize": 5,
|
||||
"temperature": 0.7,
|
||||
"top_p": 1,
|
||||
"stream": true
|
||||
},
|
||||
"botSettings": {},
|
||||
"prompt": "hello",
|
||||
"messages": [{
|
||||
"role": "user",
|
||||
"content": "hello"
|
||||
}],
|
||||
"sign": "1505ec882d72d5f3175a74ac84d665b1b904e6671b2e7334268f540975929a26",
|
||||
"timestamp": 1696338587
|
||||
})"};
|
||||
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
|
||||
|
||||
request["sign"] = signature;
|
||||
request["conversationId"] = onversation_id;
|
||||
request["messages"] = getConversationJson(json);
|
||||
request["timestamp"] = timestamp;
|
||||
|
||||
auto str = request.dump();
|
||||
SPDLOG_INFO("request : [{}]", str);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str());
|
||||
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
ScopeExit auto_exit{[=] {
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
}};
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
auto error_info = std::format("curl_easy_perform() failed:{}", curl_easy_strerror(res));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
int32_t response_code;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||
if (response_code != 200) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, std::format("you http code:{}", response_code));
|
||||
co_return;
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::freeGpt(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
|
||||
|
||||
boost::system::error_code err{};
|
||||
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }};
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
auto error_info = std::format("curl_easy_init() failed:{}", curl_easy_strerror(res));
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://k.aifree.site/api/generate");
|
||||
if (!m_cfg.http_proxy.empty())
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, m_cfg.http_proxy.c_str());
|
||||
|
||||
struct Input {
|
||||
std::shared_ptr<Channel> ch;
|
||||
std::string recv;
|
||||
};
|
||||
Input input{ch};
|
||||
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t {
|
||||
boost::system::error_code err{};
|
||||
auto input_ptr = static_cast<Input*>(userp);
|
||||
std::string data{(char*)contents, size * nmemb};
|
||||
auto& [ch, recv] = *input_ptr;
|
||||
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, data); });
|
||||
return size * nmemb;
|
||||
};
|
||||
size_t (*action_fn)(void* contents, size_t size, size_t nmemb, void* userp) = action_cb;
|
||||
curlEasySetopt(curl);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, action_fn);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
|
||||
|
||||
auto generate_signature = [](int timestamp, const std::string& message, const std::string& secret = "") {
|
||||
std::stringstream ss;
|
||||
ss << timestamp << ":" << message << ":" << secret;
|
||||
std::string data = ss.str();
|
||||
|
||||
unsigned char digest[SHA256_DIGEST_LENGTH];
|
||||
SHA256(reinterpret_cast<const unsigned char*>(data.c_str()), data.length(), digest);
|
||||
|
||||
std::stringstream sha_stream;
|
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
sha_stream << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(digest[i]);
|
||||
}
|
||||
return sha_stream.str();
|
||||
};
|
||||
uint64_t timestamp =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
std::string signature = generate_signature(timestamp, prompt);
|
||||
|
||||
constexpr std::string_view request_str{R"({
|
||||
"messages":[
|
||||
{
|
||||
"role":"user",
|
||||
"content":"hello"
|
||||
}
|
||||
],
|
||||
"pass":null,
|
||||
"sign":"7c2700b5813053ff8000cb9fb1ebdadbfcf62882829da59e4474bee466de7c89",
|
||||
"time":1695716667
|
||||
})"};
|
||||
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
|
||||
|
||||
request["sign"] = signature;
|
||||
request["time"] = timestamp;
|
||||
request["messages"] = getConversationJson(json);
|
||||
|
||||
auto str = request.dump();
|
||||
SPDLOG_INFO("request : [{}]", str);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str());
|
||||
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
ScopeExit auto_exit{[=] {
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
}};
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
auto error_info = std::format("curl_easy_perform() failed:{}", curl_easy_strerror(res));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
int32_t response_code;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||
if (response_code != 200) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, std::format("you http code:{}", response_code));
|
||||
co_return;
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::cromicle(std::shared_ptr<Channel> ch, nlohmann::json json) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
|
||||
|
||||
boost::system::error_code err{};
|
||||
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }};
|
||||
auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
|
||||
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
auto error_info = std::format("curl_easy_init() failed:{}", curl_easy_strerror(res));
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://cromicle.top/chat");
|
||||
if (!m_cfg.http_proxy.empty())
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, m_cfg.http_proxy.c_str());
|
||||
|
||||
struct Input {
|
||||
std::shared_ptr<Channel> ch;
|
||||
std::string recv;
|
||||
};
|
||||
Input input{ch};
|
||||
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t {
|
||||
boost::system::error_code err{};
|
||||
auto input_ptr = static_cast<Input*>(userp);
|
||||
std::string data{(char*)contents, size * nmemb};
|
||||
auto& [ch, recv] = *input_ptr;
|
||||
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, data); });
|
||||
return size * nmemb;
|
||||
};
|
||||
size_t (*action_fn)(void* contents, size_t size, size_t nmemb, void* userp) = action_cb;
|
||||
curlEasySetopt(curl);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, action_fn);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
|
||||
|
||||
auto generate_signature = [](const std::string& message) {
|
||||
std::stringstream ss;
|
||||
ss << "asdap" << message;
|
||||
std::string data = ss.str();
|
||||
|
||||
unsigned char digest[SHA256_DIGEST_LENGTH];
|
||||
SHA256(reinterpret_cast<const unsigned char*>(data.c_str()), data.length(), digest);
|
||||
|
||||
std::stringstream sha_stream;
|
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
sha_stream << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(digest[i]);
|
||||
}
|
||||
return sha_stream.str();
|
||||
};
|
||||
std::string signature = generate_signature(prompt);
|
||||
|
||||
constexpr std::string_view request_str{R"({
|
||||
"message": "hello",
|
||||
"hash": "dda6ea4e1dc215f198084018b1df20cfeafe9fbdfe31d8a350d6917509158d8a",
|
||||
"token": "asdap"
|
||||
})"};
|
||||
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
|
||||
|
||||
request["hash"] = signature;
|
||||
request["message"] = prompt;
|
||||
|
||||
auto str = request.dump();
|
||||
SPDLOG_INFO("request : [{}]", str);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str());
|
||||
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
ScopeExit auto_exit{[=] {
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
}};
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
auto error_info = std::format("curl_easy_perform() failed:{}", curl_easy_strerror(res));
|
||||
ch->try_send(err, error_info);
|
||||
co_return;
|
||||
}
|
||||
int32_t response_code;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||
if (response_code != 200) {
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, std::format("you http code:{}", response_code));
|
||||
co_return;
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
@@ -92,6 +92,18 @@ boost::asio::awaitable<void> sendHttpResponse(auto& stream, auto& request, auto
|
||||
co_return;
|
||||
}
|
||||
|
||||
void setContentType(auto& res, const std::string& file) {
|
||||
SPDLOG_INFO("file: {}", file);
|
||||
if (file.ends_with("js")) {
|
||||
res.set(boost::beast::http::field::content_type, "text/javascript");
|
||||
} else if (file.ends_with("css")) {
|
||||
res.set(boost::beast::http::field::content_type, "text/css");
|
||||
} else if (file.ends_with("png")) {
|
||||
res.set(boost::beast::http::field::content_type, "image/png");
|
||||
} else
|
||||
SPDLOG_ERROR("invalid file type: {}", file);
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> startSession(boost::asio::ip::tcp::socket sock, Config& cfg,
|
||||
boost::asio::io_context& context) {
|
||||
boost::beast::tcp_stream stream{std::move(sock)};
|
||||
@@ -172,7 +184,7 @@ boost::asio::awaitable<void> startSession(boost::asio::ip::tcp::socket sock, Con
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::ok,
|
||||
request.version()};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, "text/javascript");
|
||||
setContentType(res, file);
|
||||
res.keep_alive(request.keep_alive());
|
||||
res.body() = std::move(chat_js_content);
|
||||
res.prepare_payload();
|
||||
@@ -191,8 +203,7 @@ boost::asio::awaitable<void> startSession(boost::asio::ip::tcp::socket sock, Con
|
||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||
std::make_tuple(boost::beast::http::status::ok, request.version())};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type,
|
||||
req_path.contains("css") ? "text/css" : "text/javascript");
|
||||
setContentType(res, file);
|
||||
res.content_length(size);
|
||||
res.keep_alive(request.keep_alive());
|
||||
boost::beast::http::message_generator rsp = std::move(res);
|
||||
@@ -324,24 +335,25 @@ int main(int argc, char** argv) {
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-openai", FreeGpt::openAi);
|
||||
ADD_METHOD("gpt-3.5-turbo-Aichat", FreeGpt::aiChat);
|
||||
ADD_METHOD("gpt-4-ChatgptAi", FreeGpt::chatGptAi);
|
||||
// ADD_METHOD("gpt-3.5-turbo-weWordle", FreeGpt::weWordle);
|
||||
ADD_METHOD("gpt-3.5-turbo-acytoo", FreeGpt::acytoo);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-DeepAi", FreeGpt::deepAi);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-H2o", FreeGpt::h2o);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-yqcloud", FreeGpt::yqcloud);
|
||||
ADD_METHOD("gpt-OpenAssistant-stream-HuggingChat", FreeGpt::huggingChat)
|
||||
ADD_METHOD("gpt-4-turbo-stream-you", FreeGpt::you);
|
||||
ADD_METHOD("gpt-3.5-turbo-AItianhu", FreeGpt::aiTianhu);
|
||||
// ADD_METHOD("gpt-3.5-turbo-AItianhu", FreeGpt::aiTianhu);
|
||||
ADD_METHOD("gpt-3-stream-binjie", FreeGpt::binjie);
|
||||
// ADD_METHOD("gpt-3.5-turbo-stream-CodeLinkAva", FreeGpt::codeLinkAva);
|
||||
ADD_METHOD("gpt-4-stream-ChatBase", FreeGpt::chatBase);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-aivvm", FreeGpt::aivvm);
|
||||
ADD_METHOD("gpt-3.5-turbo-16k-stream-Ylokh", FreeGpt::ylokh);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-Vitalentum", FreeGpt::vitalentum);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-GptGo", FreeGpt::gptGo);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-AItianhuSpace", FreeGpt::aiTianhuSpace);
|
||||
// ADD_METHOD("gpt-3.5-turbo-stream-AItianhuSpace", FreeGpt::aiTianhuSpace);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-Aibn", FreeGpt::aibn);
|
||||
ADD_METHOD("gpt-3.5-turbo-ChatgptDuo", FreeGpt::chatGptDuo);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-ChatForAi", FreeGpt::chatForAi);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-FreeGpt", FreeGpt::freeGpt);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-Cromicle", FreeGpt::cromicle);
|
||||
|
||||
IoContextPool pool{cfg.work_thread_num};
|
||||
pool.start();
|
||||
|
||||
Reference in New Issue
Block a user