mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2026-02-04 16:50:38 +03:00
auto load environment variables (#72)
* auto load environment variables * add providers gpt6 & chatxyz
This commit is contained in:
@@ -2097,3 +2097,178 @@ boost::asio::awaitable<void> FreeGpt::aura(std::shared_ptr<Channel> ch, nlohmann
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::gpt6(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));
|
||||
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>();
|
||||
|
||||
boost::system::error_code err{};
|
||||
std::unordered_multimap<std::string, std::string> headers{
|
||||
{"Accept", "*/*"},
|
||||
{"content-type", "application/json"},
|
||||
{"Referer", "https://gpt6.ai/"},
|
||||
{"Origin", "https://gpt6.ai"},
|
||||
{"Sec-Fetch-Dest", "empty"},
|
||||
{"Sec-Fetch-Mode", "cors"},
|
||||
{"Sec-Fetch-Site", "cross-site"},
|
||||
{"TE", "trailers"},
|
||||
};
|
||||
std::string recv;
|
||||
auto ret = Curl()
|
||||
.setUrl("https://seahorse-app-d29hu.ondigitalocean.app/api/v1/query")
|
||||
.setProxy(m_cfg.http_proxy)
|
||||
.setRecvHeadersCallback([](std::string) { return; })
|
||||
.setRecvBodyCallback([&](std::string chunk_str) mutable {
|
||||
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);
|
||||
}
|
||||
})
|
||||
.setBody([&] {
|
||||
constexpr std::string_view ask_json_str = R"({
|
||||
"prompts":[
|
||||
{
|
||||
"role":"user",
|
||||
"content":"Hello"
|
||||
}
|
||||
],
|
||||
"geoInfo":{
|
||||
"ip":"100.90.100.222",
|
||||
"hostname":"ip-100-090-100-222.um36.pools.vodafone-ip.de",
|
||||
"city":"Muenchen",
|
||||
"region":"North Rhine-Westphalia",
|
||||
"country":"DE",
|
||||
"loc":"44.0910,5.5827",
|
||||
"org":"AS3209 Vodafone GmbH",
|
||||
"postal":"41507",
|
||||
"timezone":"Europe/Berlin"
|
||||
},
|
||||
"paid":false,
|
||||
"character":{
|
||||
"textContent":"",
|
||||
"id":"52690ad6-22e4-4674-93d4-1784721e9944",
|
||||
"name":"GPT6",
|
||||
"htmlContent":""
|
||||
}
|
||||
})";
|
||||
nlohmann::json ask_request = nlohmann::json::parse(ask_json_str, nullptr, false);
|
||||
ask_request["prompts"] = getConversationJson(json);
|
||||
std::string ask_request_str = ask_request.dump();
|
||||
SPDLOG_INFO("request: [{}]", ask_request_str);
|
||||
return ask_request_str;
|
||||
}())
|
||||
.clearHeaders()
|
||||
.setHttpHeaders(headers)
|
||||
.perform();
|
||||
if (ret.has_value()) {
|
||||
SPDLOG_ERROR("{}", ret.value());
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, ret.value());
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> FreeGpt::chatxyz(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));
|
||||
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>();
|
||||
|
||||
boost::system::error_code err{};
|
||||
std::unordered_multimap<std::string, std::string> headers{
|
||||
{"Accept", "text/event-stream"},
|
||||
{"content-type", "application/json"},
|
||||
{"Referer", "https://chat.3211000.xyz/"},
|
||||
{"Origin", "https://chat.3211000.xyz"},
|
||||
{"Sec-Fetch-Dest", "empty"},
|
||||
{"Sec-Fetch-Mode", "cors"},
|
||||
{"Sec-Fetch-Site", "same-origin"},
|
||||
{"TE", "trailers"},
|
||||
{"x-requested-with", "XMLHttpRequest"},
|
||||
};
|
||||
std::string recv;
|
||||
auto ret = Curl()
|
||||
.setUrl("https://chat.3211000.xyz/api/openai/v1/chat/completions")
|
||||
.setProxy(m_cfg.http_proxy)
|
||||
.setRecvHeadersCallback([](std::string) { return; })
|
||||
.setRecvBodyCallback([&](std::string chunk_str) mutable {
|
||||
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;
|
||||
}
|
||||
if (line_json["choices"][0]["delta"]["content"].is_null())
|
||||
continue;
|
||||
auto str = line_json["choices"][0]["delta"]["content"].get<std::string>();
|
||||
if (!str.empty())
|
||||
ch->try_send(err, str);
|
||||
}
|
||||
})
|
||||
.setBody([&] {
|
||||
constexpr std::string_view ask_json_str = R"({
|
||||
"messages":[
|
||||
{
|
||||
"role":"system",
|
||||
"content":"\nYou are ChatGPT, a large language model trained by OpenAI.\nCarefully heed the user's instructions.\nRespond using Markdown.\nKnowledge cutoff: 2021-09\nCurrent model: gpt-3.5-turbo\nCurrent time: 2023/12/26 14:12:34\nLatex inline: $x^2$ \nLatex block: $$e=mc^2$$\n\n"
|
||||
},
|
||||
{
|
||||
"role":"user",
|
||||
"content":"hello"
|
||||
}
|
||||
],
|
||||
"stream":true,
|
||||
"model":"gpt-3.5-turbo",
|
||||
"temperature":0.5,
|
||||
"presence_penalty":0,
|
||||
"frequency_penalty":0,
|
||||
"top_p":1
|
||||
})";
|
||||
nlohmann::json ask_request = nlohmann::json::parse(ask_json_str, nullptr, false);
|
||||
ask_request["messages"][1]["content"] = prompt;
|
||||
std::string ask_request_str = ask_request.dump();
|
||||
SPDLOG_INFO("request: [{}]", ask_request_str);
|
||||
return ask_request_str;
|
||||
}())
|
||||
.clearHeaders()
|
||||
.setHttpHeaders(headers)
|
||||
.perform();
|
||||
if (ret.has_value()) {
|
||||
SPDLOG_ERROR("{}", ret.value());
|
||||
co_await boost::asio::post(boost::asio::bind_executor(ch->get_executor(), boost::asio::use_awaitable));
|
||||
ch->try_send(err, ret.value());
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
@@ -39,36 +39,8 @@ void setEnvironment(auto& cfg) {
|
||||
if (!upper_http_proxy.empty())
|
||||
cfg.http_proxy = std::move(upper_http_proxy);
|
||||
}
|
||||
if (auto [chat_path] = getEnv("CHAT_PATH"); !chat_path.empty()) {
|
||||
cfg.chat_path = std::move(chat_path);
|
||||
}
|
||||
if (cfg.chat_path.back() == '/')
|
||||
cfg.chat_path.pop_back();
|
||||
if (auto [port] = getEnv("PORT"); !port.empty())
|
||||
cfg.port = std::move(port);
|
||||
if (auto [host] = getEnv("HOST"); !host.empty())
|
||||
cfg.host = std::move(host);
|
||||
if (auto [work_thread_num] = getEnv("WORK_THREAD_NUM"); !work_thread_num.empty())
|
||||
cfg.work_thread_num = std::atol(work_thread_num.c_str());
|
||||
if (auto [providers] = getEnv("PROVIDERS"); !providers.empty()) {
|
||||
nlohmann::json providers_list = nlohmann::json::parse(providers, nullptr, false);
|
||||
if (!providers_list.is_discarded())
|
||||
cfg.providers = providers_list.get<std::vector<std::string>>();
|
||||
}
|
||||
if (auto [api_key] = getEnv("API_KEY"); !api_key.empty())
|
||||
cfg.api_key = std::move(api_key);
|
||||
if (auto [interval] = getEnv("INTERVAL"); !interval.empty())
|
||||
cfg.interval = std::atol(interval.c_str());
|
||||
// export IP_WHITE_LIST="[\"127.0.0.1\",\"192.168.1.1\"]"
|
||||
if (auto [ip_white_list_str] = getEnv("IP_WHITE_LIST"); !ip_white_list_str.empty()) {
|
||||
nlohmann::json ip_white_list = nlohmann::json::parse(ip_white_list_str, nullptr, false);
|
||||
if (!ip_white_list.is_discarded())
|
||||
cfg.ip_white_list = ip_white_list.get<std::vector<std::string>>();
|
||||
}
|
||||
if (auto [zeus] = getEnv("ZEUS"); !zeus.empty())
|
||||
cfg.zeus = std::move(zeus);
|
||||
if (auto [flaresolverr] = getEnv("FLARESOLVERR"); !flaresolverr.empty())
|
||||
cfg.flaresolverr = std::move(flaresolverr);
|
||||
}
|
||||
|
||||
std::string createIndexHtml(const std::string& file, const Config& cfg) {
|
||||
@@ -321,7 +293,7 @@ int main(int, char** argv) {
|
||||
ScopeExit cleanup{[=] { curl_global_cleanup(); }};
|
||||
|
||||
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e][thread %t][%!][%s:%#][%l] %v");
|
||||
auto [config, error] = yaml_cpp_struct::from_yaml<Config>(argv[1]);
|
||||
auto [config, error] = yaml_cpp_struct::from_yaml_env<Config>(argv[1], "");
|
||||
if (!config) {
|
||||
SPDLOG_ERROR("{}", error);
|
||||
return EXIT_FAILURE;
|
||||
@@ -350,6 +322,8 @@ int main(int, char** argv) {
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-AiChatOnline", FreeGpt::aiChatOnline);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-fakeGpt", FreeGpt::fakeGpt);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-aura", FreeGpt::aura);
|
||||
ADD_METHOD("gpt6", FreeGpt::gpt6);
|
||||
ADD_METHOD("gpt-3.5-turbo-stream-chatxyz", FreeGpt::chatxyz);
|
||||
|
||||
SPDLOG_INFO("active provider:");
|
||||
for (auto& [provider, _] : gpt_function)
|
||||
|
||||
Reference in New Issue
Block a user