fix provider FreeGpt (#44)

This commit is contained in:
Dmitry Afanasyev 2023-10-24 23:39:51 +03:00 committed by GitHub
parent d6d21b4292
commit ca0ad81ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 91 deletions

View File

@ -39,7 +39,7 @@ python main.py
```shell
cd bot_microservice
poetry run uvicorn --factory --host 0.0.0.0 --port 8080 --reload --workers 2 --log-level warning
poetry run uvicorn main:create_app --factory --host 0.0.0.0 --port 8080 --forwarded-allow-ips='*' --proxy-headers --reload --workers 2 --log-level warning
```
To start on polling mode set `START_WITH_WEBHOOK` to blank

View File

@ -1648,37 +1648,16 @@ boost::asio::awaitable<void> FreeGpt::freeGpt(std::shared_ptr<Channel> ch, nlohm
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 = "") {
Curl curl;
auto ret = curl.setUrl("https://r.aifree.site/api/generate")
.setProxy(m_cfg.http_proxy)
.setRecvBodyCallback([&](std::string str) mutable {
boost::asio::post(ch->get_executor(), [=, str = std::move(str)] { ch->try_send(err, str); });
return;
})
.setBody([&] {
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();
@ -1688,7 +1667,8 @@ boost::asio::awaitable<void> FreeGpt::freeGpt(std::shared_ptr<Channel> ch, nlohm
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]);
sha_stream << std::setfill('0') << std::setw(2) << std::hex
<< static_cast<int>(digest[i]);
}
return sha_stream.str();
};
@ -1714,31 +1694,13 @@ boost::asio::awaitable<void> FreeGpt::freeGpt(std::shared_ptr<Channel> ch, nlohm
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) {
return str;
}())
.perform();
if (ret.has_value()) {
SPDLOG_ERROR("https://r.aifree.site/api/generate: [{}]", ret.value());
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));
ch->try_send(err, ret.value());
co_return;
}
co_return;