remove gpt-4-stream-Chatgpt4Online (#51)

This commit is contained in:
Dmitry Afanasyev 2023-11-03 18:59:01 +03:00 committed by GitHub
parent 6262059bc7
commit 18ea0a556a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 256 additions and 357 deletions

View File

@ -51,7 +51,6 @@ class ChatGptModelsEnum(StrEnum):
gpt_3_5_turbo_stream_GptGo = "gpt-3.5-turbo-stream-GptGo" gpt_3_5_turbo_stream_GptGo = "gpt-3.5-turbo-stream-GptGo"
gpt_3_5_turbo_stream_FreeGpt = "gpt-3.5-turbo-stream-FreeGpt" gpt_3_5_turbo_stream_FreeGpt = "gpt-3.5-turbo-stream-FreeGpt"
gpt_3_5_turbo_stream_Cromicle = "gpt-3.5-turbo-stream-Cromicle" gpt_3_5_turbo_stream_Cromicle = "gpt-3.5-turbo-stream-Cromicle"
gpt_4_stream_Chatgpt4Online = "gpt-4-stream-Chatgpt4Online"
gpt_3_5_turbo_stream_gptalk = "gpt-3.5-turbo-stream-gptalk" gpt_3_5_turbo_stream_gptalk = "gpt-3.5-turbo-stream-gptalk"
gpt_3_5_turbo_stream_ChatgptDemo = "gpt-3.5-turbo-stream-ChatgptDemo" gpt_3_5_turbo_stream_ChatgptDemo = "gpt-3.5-turbo-stream-ChatgptDemo"
llama2 = "llama2" llama2 = "llama2"

View File

@ -1398,3 +1398,115 @@ boost::asio::awaitable<void> FreeGpt::aibn(std::shared_ptr<Channel> ch, nlohmann
} }
co_return; co_return;
} }
boost::asio::awaitable<void> FreeGpt::chatGpt4Online(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://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit");
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 {
auto input_ptr = static_cast<Input*>(userp);
std::string data{(char*)contents, size * nmemb};
auto& [ch, recv] = *input_ptr;
recv.append(data);
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())
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());
boost::asio::post(ch->get_executor(),
[=] { ch->try_send(err, std::format("json parse error: [{}]", fields.back())); });
continue;
}
auto type = line_json["type"].get<std::string>();
if (type == "live") {
auto str = line_json["data"].get<std::string>();
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, str); });
}
}
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);
constexpr std::string_view request_str{R"({
"botId":"default",
"customId":null,
"session":"N/A",
"chatId":"",
"contextId":58,
"messages":[
{
"role":"user",
"content":"hello"
}
],
"newMessage":"hello",
"stream":true
})"};
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
request["messages"] = getConversationJson(json);
request["newMessage"] = 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;
}

View File

@ -28,7 +28,6 @@ public:
boost::asio::awaitable<void> gptGo(std::shared_ptr<Channel>, nlohmann::json); boost::asio::awaitable<void> gptGo(std::shared_ptr<Channel>, nlohmann::json);
boost::asio::awaitable<void> chatForAi(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> freeGpt(std::shared_ptr<Channel>, nlohmann::json);
boost::asio::awaitable<void> chatGpt4Online(std::shared_ptr<Channel>, nlohmann::json);
boost::asio::awaitable<void> gptalk(std::shared_ptr<Channel>, nlohmann::json); boost::asio::awaitable<void> gptalk(std::shared_ptr<Channel>, nlohmann::json);
boost::asio::awaitable<void> gptForLove(std::shared_ptr<Channel>, nlohmann::json); boost::asio::awaitable<void> gptForLove(std::shared_ptr<Channel>, nlohmann::json);
boost::asio::awaitable<void> chatGptDemo(std::shared_ptr<Channel>, nlohmann::json); boost::asio::awaitable<void> chatGptDemo(std::shared_ptr<Channel>, nlohmann::json);

View File

@ -445,6 +445,20 @@ public:
m_http_status_code = code; m_http_status_code = code;
return *this; return *this;
} }
static auto parseHttpHeaders(const std::string& buffer) {
std::regex pattern(R"(([^:\r\n]+):([^\r\n]+))");
std::smatch matches;
auto start = buffer.cbegin();
auto end = buffer.cend();
std::multimap<std::string, std::string> response_header;
while (std::regex_search(start, end, matches, pattern)) {
std::string field_name = matches[1].str();
std::string field_value = matches[2].str();
response_header.insert(std::pair{field_name, field_value});
start = matches[0].second;
}
return response_header;
}
private: private:
CURL* m_curl{nullptr}; CURL* m_curl{nullptr};
@ -459,40 +473,23 @@ private:
}; };
std::expected<nlohmann::json, std::string> callZeus(const std::string& host, const std::string& request_body) { std::expected<nlohmann::json, std::string> callZeus(const std::string& host, const std::string& request_body) {
CURLcode res; static std::unordered_multimap<std::string, std::string> headers{
CURL* curl = curl_easy_init(); {"Accept", "*/*"},
if (!curl) {
auto error_info = std::format("callZeus curl_easy_init() failed:{}", curl_easy_strerror(res));
return std::unexpected(error_info);
}
ScopeExit auto_exit{[=] { curl_easy_cleanup(curl); }};
std::string http_proxy;
std::string recv;
auto ret = sendHttpRequest(CurlHttpRequest{
.curl = curl,
.url = host,
.http_proxy = http_proxy,
.cb = [](void* contents, size_t size, size_t nmemb, void* userp) mutable -> size_t {
auto recv_ptr = static_cast<std::string*>(userp);
std::string data{(char*)contents, size * nmemb};
recv_ptr->append(data);
return size * nmemb;
},
.input = &recv,
.headers = [&] -> auto& {
static std::unordered_map<std::string, std::string> headers{
{"Content-Type", "application/json"}, {"Content-Type", "application/json"},
}; };
return headers; std::string recv;
}(), auto ret = Curl()
.body = request_body, .setUrl(host)
.response_header_ptr = nullptr, .setRecvHeadersCallback([](std::string) { return; })
.expect_response_code = 200, .setRecvBodyCallback([&](std::string str) { recv.append(str); })
.ssl_verify = false, .setBody(request_body)
}); .clearHeaders()
if (ret) .setHttpHeaders(headers)
.perform();
if (ret) {
SPDLOG_ERROR("call zeus error: {}", ret.value());
return std::unexpected(ret.value()); return std::unexpected(ret.value());
}
nlohmann::json rsp = nlohmann::json::parse(recv, nullptr, false); nlohmann::json rsp = nlohmann::json::parse(recv, nullptr, false);
if (rsp.is_discarded()) { if (rsp.is_discarded()) {
SPDLOG_ERROR("json parse error"); SPDLOG_ERROR("json parse error");
@ -1048,9 +1045,7 @@ boost::asio::awaitable<void> FreeGpt::you(std::shared_ptr<Channel> ch, nlohmann:
.curl = curl, .curl = curl,
.url = "https://you.com", .url = "https://you.com",
.http_proxy = m_cfg.http_proxy, .http_proxy = m_cfg.http_proxy,
.cb = [](void* contents, size_t size, size_t nmemb, void* userp) mutable -> size_t { .cb = [](void*, size_t size, size_t nmemb, void*) mutable -> size_t { return size * nmemb; },
return size * nmemb;
},
.headers = headers, .headers = headers,
.response_header_ptr = &response_header, .response_header_ptr = &response_header,
}); });
@ -1315,7 +1310,7 @@ boost::asio::awaitable<void> FreeGpt::gptGo(std::shared_ptr<Channel> ch, nlohman
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) { if (response_code != 200) {
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable)); 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, std::format("gptGo http code:{}", response_code));
co_return; co_return;
} }
SPDLOG_INFO("recv_str: [{}]", recv_str); SPDLOG_INFO("recv_str: [{}]", recv_str);
@ -1347,7 +1342,6 @@ boost::asio::awaitable<void> FreeGpt::gptGo(std::shared_ptr<Channel> ch, nlohman
}; };
Input input{ch}; Input input{ch};
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t { 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); auto input_ptr = static_cast<Input*>(userp);
std::string data{(char*)contents, size * nmemb}; std::string data{(char*)contents, size * nmemb};
auto& [ch, recv] = *input_ptr; auto& [ch, recv] = *input_ptr;
@ -1405,36 +1399,6 @@ boost::asio::awaitable<void> FreeGpt::chatForAi(std::shared_ptr<Channel> ch, nlo
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }}; 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>(); 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.store/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 = [](uint64_t timestamp, const std::string& message, const std::string& id) { auto generate_signature = [](uint64_t timestamp, const std::string& message, const std::string& id) {
std::string s = std::to_string(timestamp) + ":" + id + ":" + message + ":7YN8z6d6"; std::string s = std::to_string(timestamp) + ":" + id + ":" + message + ":7YN8z6d6";
unsigned char hash[SHA256_DIGEST_LENGTH]; unsigned char hash[SHA256_DIGEST_LENGTH];
@ -1450,6 +1414,20 @@ boost::asio::awaitable<void> FreeGpt::chatForAi(std::shared_ptr<Channel> ch, nlo
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]); ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
return ss.str(); return ss.str();
}; };
static std::unordered_multimap<std::string, std::string> headers{
{"Content-Type", "application/json"},
{"Origin", "https://chatforai.store"},
{"Referer", "https://chatforai.store/"},
};
auto ret = Curl()
.setUrl("https://chatforai.store/api/handle/provider-openai")
.setProxy(m_cfg.http_proxy)
.setRecvHeadersCallback([](std::string) { return; })
.setRecvBodyCallback([&](std::string str) {
boost::asio::post(ch->get_executor(), [=, str = std::move(str)] { ch->try_send(err, str); });
return;
})
.setBody([&] {
uint64_t timestamp = getTimestamp(); uint64_t timestamp = getTimestamp();
constexpr std::string_view request_str{R"({ constexpr std::string_view request_str{R"({
"conversationId": "id_1696984301982", "conversationId": "id_1696984301982",
@ -1478,37 +1456,15 @@ boost::asio::awaitable<void> FreeGpt::chatForAi(std::shared_ptr<Channel> ch, nlo
request["sign"] = generate_signature(timestamp, prompt, conversation_id); request["sign"] = generate_signature(timestamp, prompt, conversation_id);
request["messages"] = getConversationJson(json); request["messages"] = getConversationJson(json);
request["prompt"] = prompt; request["prompt"] = prompt;
auto str = request.dump(); auto str = request.dump();
SPDLOG_INFO("request : [{}]", str); SPDLOG_INFO("request : [{}]", str);
return str;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str()); }())
.setHttpHeaders(headers)
struct curl_slist* headers = nullptr; .perform();
headers = curl_slist_append(headers, "Content-Type: application/json"); if (ret.has_value()) {
headers = curl_slist_append(headers, "Origin: https://chatforai.store");
headers = curl_slist_append(headers, "Referer: https://chatforai.store/");
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)); 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, ret.value());
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; co_return;
} }
@ -1578,119 +1534,6 @@ boost::asio::awaitable<void> FreeGpt::freeGpt(std::shared_ptr<Channel> ch, nlohm
co_return; co_return;
} }
boost::asio::awaitable<void> FreeGpt::chatGpt4Online(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://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit");
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;
recv.append(data);
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())
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());
boost::asio::post(ch->get_executor(),
[=] { ch->try_send(err, std::format("json parse error: [{}]", fields.back())); });
continue;
}
auto type = line_json["type"].get<std::string>();
if (type == "live") {
auto str = line_json["data"].get<std::string>();
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, str); });
}
}
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);
constexpr std::string_view request_str{R"({
"botId":"default",
"customId":null,
"session":"N/A",
"chatId":"",
"contextId":58,
"messages":[
{
"role":"user",
"content":"hello"
}
],
"newMessage":"hello",
"stream":true
})"};
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
request["messages"] = getConversationJson(json);
request["newMessage"] = 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;
}
boost::asio::awaitable<void> FreeGpt::gptalk(std::shared_ptr<Channel> ch, nlohmann::json json) { boost::asio::awaitable<void> FreeGpt::gptalk(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)); co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }}; ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }};
@ -1923,33 +1766,29 @@ boost::asio::awaitable<void> FreeGpt::gptalk(std::shared_ptr<Channel> ch, nlohma
boost::asio::awaitable<void> FreeGpt::gptForLove(std::shared_ptr<Channel> ch, nlohmann::json json) { boost::asio::awaitable<void> FreeGpt::gptForLove(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)); co_await boost::asio::post(boost::asio::bind_executor(*m_thread_pool_ptr, boost::asio::use_awaitable));
boost::system::error_code err{}; boost::system::error_code err{};
ScopeExit _exit{[=] { boost::asio::post(ch->get_executor(), [=] { ch->close(); }); }}; 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>(); auto prompt = json.at("meta").at("content").at("parts").at(0).at("content").get<std::string>();
auto secret_rsp = callZeus(std::format("{}/gptforlove", m_cfg.zeus), "{}");
CURLcode res; if (!secret_rsp.has_value()) {
CURL* curl = curl_easy_init(); SPDLOG_ERROR("callZeus error: {}", secret_rsp.error());
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)); co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
ch->try_send(err, error_info); ch->try_send(err, secret_rsp.error());
co_return; co_return;
} }
curl_easy_setopt(curl, CURLOPT_URL, "https://api.gptplus.one/chat-process"); SPDLOG_INFO("zeus: [{}]", secret_rsp.value().dump());
if (!m_cfg.http_proxy.empty()) static std::unordered_multimap<std::string, std::string> headers{
curl_easy_setopt(curl, CURLOPT_PROXY, m_cfg.http_proxy.c_str()); {"Content-Type", "application/json"},
struct Input { {"referer", "https://ai18.gptforlove.com/"},
std::shared_ptr<Channel> ch; {"origin", "https://ai18.gptforlove.com"},
std::string recv; {"authority", "api.gptplus.one"},
}; };
Input input{ch}; std::string recv;
auto action_cb = [](void* contents, size_t size, size_t nmemb, void* userp) -> size_t { auto ret = Curl()
boost::system::error_code err{}; .setUrl("https://api.gptplus.one/chat-process")
auto input_ptr = static_cast<Input*>(userp); .setProxy(m_cfg.http_proxy)
std::string data{(char*)contents, size * nmemb}; .setRecvBodyCallback([&](std::string str) {
auto& [ch, recv] = *input_ptr; recv.append(str);
recv.append(data);
while (true) { while (true) {
auto position = recv.find("\n"); auto position = recv.find("\n");
if (position == std::string::npos) if (position == std::string::npos)
@ -1959,7 +1798,7 @@ boost::asio::awaitable<void> FreeGpt::gptForLove(std::shared_ptr<Channel> ch, nl
msg.pop_back(); msg.pop_back();
if (msg.contains("10分钟内提问超过了5次")) { if (msg.contains("10分钟内提问超过了5次")) {
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, msg); }); boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, msg); });
return size * nmemb; return;
} }
if (msg.empty() || !msg.contains("content")) if (msg.empty() || !msg.contains("content"))
continue; continue;
@ -1967,21 +1806,17 @@ boost::asio::awaitable<void> FreeGpt::gptForLove(std::shared_ptr<Channel> ch, nl
nlohmann::json line_json = nlohmann::json::parse(msg, nullptr, false); nlohmann::json line_json = nlohmann::json::parse(msg, nullptr, false);
if (line_json.is_discarded()) { if (line_json.is_discarded()) {
SPDLOG_ERROR("json parse error: [{}]", msg); SPDLOG_ERROR("json parse error: [{}]", msg);
boost::asio::post(ch->get_executor(), boost::asio::post(ch->get_executor(), [=] {
[=] { ch->try_send(err, std::format("json parse error: [{}]", msg)); }); ch->try_send(err, std::format("json parse error: [{}]", msg));
});
continue; continue;
} }
auto str = line_json["detail"]["choices"][0]["delta"]["content"].get<std::string>(); auto str = line_json["detail"]["choices"][0]["delta"]["content"].get<std::string>();
if (!str.empty()) if (!str.empty())
boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, str); }); boost::asio::post(ch->get_executor(), [=] { ch->try_send(err, str); });
} }
return size * nmemb; })
}; .setBody([&] {
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);
constexpr std::string_view request_str{R"({ constexpr std::string_view request_str{R"({
"prompt": "hello", "prompt": "hello",
"options": {}, "options": {},
@ -1992,49 +1827,18 @@ boost::asio::awaitable<void> FreeGpt::gptForLove(std::shared_ptr<Channel> ch, nl
"stream": false "stream": false
})"}; })"};
nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false); nlohmann::json request = nlohmann::json::parse(request_str, nullptr, false);
auto secret_rsp = callZeus("http://127.0.0.1:8860/gptforlove", "{}");
if (!secret_rsp.has_value()) {
SPDLOG_ERROR("callZeus error: {}", secret_rsp.error());
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
ch->try_send(err, secret_rsp.error());
co_return;
}
SPDLOG_INFO("zeus: [{}]", secret_rsp.value().dump());
request["secret"] = secret_rsp.value()["secret"]; request["secret"] = secret_rsp.value()["secret"];
request["prompt"] = prompt; request["prompt"] = prompt;
auto str = request.dump(); auto str = request.dump();
SPDLOG_INFO("request : [{}]", str); SPDLOG_INFO("request : [{}]", str);
return str;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str()); }())
.setHttpHeaders(headers)
struct curl_slist* headers = nullptr; .perform();
headers = curl_slist_append(headers, "Content-Type: application/json"); if (ret.has_value()) {
headers = curl_slist_append(headers, "referer: https://ai18.gptforlove.com/"); SPDLOG_ERROR("call api.gptplus.one error: [{}]", ret.value());
headers = curl_slist_append(headers, "origin: https://ai18.gptforlove.com");
headers = curl_slist_append(headers, "authority: api.gptplus.one");
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)); 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, ret.value());
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; co_return;
} }
@ -2712,21 +2516,7 @@ boost::asio::awaitable<void> FreeGpt::fakeGpt(std::shared_ptr<Channel> ch, nlohm
ch->try_send(err, ret.value()); ch->try_send(err, ret.value());
co_return; co_return;
} }
auto parse = [](auto& buffer) { auto response_header = Curl::parseHttpHeaders(header_str);
std::regex pattern(R"(([^:\r\n]+):([^\r\n]+))");
std::smatch matches;
auto start = buffer.cbegin();
auto end = buffer.cend();
std::multimap<std::string, std::string> response_header;
while (std::regex_search(start, end, matches, pattern)) {
std::string field_name = matches[1].str();
std::string field_value = matches[2].str();
response_header.insert(std::pair{field_name, field_value});
start = matches[0].second;
}
return response_header;
};
auto response_header = parse(header_str);
auto range = response_header.equal_range("set-cookie"); auto range = response_header.equal_range("set-cookie");
std::string cookie; std::string cookie;
for (auto it = range.first; it != range.second; ++it) { for (auto it = range.first; it != range.second; ++it) {

View File

@ -344,7 +344,6 @@ int main(int, char** argv) {
ADD_METHOD("gpt-4-stream-ChatBase", FreeGpt::chatBase); ADD_METHOD("gpt-4-stream-ChatBase", FreeGpt::chatBase);
ADD_METHOD("gpt-3.5-turbo-stream-GptGo", FreeGpt::gptGo); ADD_METHOD("gpt-3.5-turbo-stream-GptGo", FreeGpt::gptGo);
ADD_METHOD("gpt-3.5-turbo-stream-FreeGpt", FreeGpt::freeGpt); ADD_METHOD("gpt-3.5-turbo-stream-FreeGpt", FreeGpt::freeGpt);
ADD_METHOD("gpt-4-stream-Chatgpt4Online", FreeGpt::chatGpt4Online);
ADD_METHOD("gpt-3.5-turbo-stream-gptalk", FreeGpt::gptalk); ADD_METHOD("gpt-3.5-turbo-stream-gptalk", FreeGpt::gptalk);
ADD_METHOD("gpt-3.5-turbo-stream-ChatForAi", FreeGpt::chatForAi); ADD_METHOD("gpt-3.5-turbo-stream-ChatForAi", FreeGpt::chatForAi);
ADD_METHOD("gpt-3.5-turbo-stream-gptforlove", FreeGpt::gptForLove); ADD_METHOD("gpt-3.5-turbo-stream-gptforlove", FreeGpt::gptForLove);