Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Jul 31, 2023
1 parent c83cbb6 commit 1da0fd2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
Binary file modified bin/cpp-freegpt-webui
Binary file not shown.
56 changes: 22 additions & 34 deletions src/free_gpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ boost::asio::awaitable<void> FreeGpt::chatGptAi(std::shared_ptr<Channel> 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)},
Expand Down Expand Up @@ -907,41 +909,27 @@ boost::asio::awaitable<void> FreeGpt::chatFree(std::shared_ptr<Channel> 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<std::string>();
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<std::string>();
if (str.empty())
return;
ch->try_send(err, str);
});
if (ret == Status::Close && recreate_num == 0) {
recreate_num++;
Expand Down

0 comments on commit 1da0fd2

Please sign in to comment.