diff --git a/bin/cpp-freegpt-webui b/bin/cpp-freegpt-webui index 1a8b889..9297ae5 100755 Binary files a/bin/cpp-freegpt-webui and b/bin/cpp-freegpt-webui differ diff --git a/src/free_gpt.cpp b/src/free_gpt.cpp index daf6be5..0d450a6 100644 --- a/src/free_gpt.cpp +++ b/src/free_gpt.cpp @@ -769,6 +769,8 @@ boost::asio::awaitable FreeGpt::chatGptAi(std::shared_ptr ch, request.set("sec-fetch-mode", "cors"); request.set("sec-fetch-site", "same-origin"); request.set(boost::beast::http::field::user_agent, user_agent); + request.set("Accept-Encoding", "gzip, deflate"); + request.set("Content-Type", "application/x-www-form-urlencoded"); nlohmann::json data{ {"message", fmt::format("user: {}\nassistant: ", prompt)}, @@ -907,41 +909,27 @@ boost::asio::awaitable FreeGpt::chatFree(std::shared_ptr ch, } auto& stream_ = *handle.get()->stream; - std::string recv; auto ret = co_await send_recv_chunk( - ch, stream_, req, 200, [&ch, &recv](std::string chunk_str) { - ok: - 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()) - break; - auto fields = split_string(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, fmt::format("json parse error: [{}]", - fields.back())); - break; - } - auto str = line_json["choices"][0]["delta"]["content"] - .get(); - if (str.empty()) - break; - ch->try_send(err, str); - } - if (!chunk_str.contains("content")) { - return; - } - recv.append(chunk_str); - chunk_str.clear(); - goto ok; + ch, stream_, req, 200, [&ch](std::string chunk_str) { + std::cout << "收到:" << chunk_str << std::endl; + if (!chunk_str.contains("content")) { + return; + } + auto fields = split_string(chunk_str, "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, fmt::format("json parse error: [{}]", fields.back())); + return; + } + auto str = + line_json["choices"][0]["delta"]["content"].get(); + if (str.empty()) + return; + ch->try_send(err, str); }); if (ret == Status::Close && recreate_num == 0) { recreate_num++;