diff --git a/liboai/components/chat.cpp b/liboai/components/chat.cpp index 4963d93..472fd83 100644 --- a/liboai/components/chat.cpp +++ b/liboai/components/chat.cpp @@ -491,6 +491,11 @@ std::vector liboai::Conversation::SplitFullStreamedData(std::string } bool liboai::Conversation::ParseStreamData(std::string data, std::string& delta_content, bool& completed){ + if (!_last_incomplete_buffer.empty()) { + data = _last_incomplete_buffer + data; + _last_incomplete_buffer.clear(); + } + std::vector data_lines = SplitFullStreamedData(data); if (data_lines.empty()){ @@ -521,8 +526,13 @@ bool liboai::Conversation::ParseStreamData(std::string data, std::string& delta_ */ this->RemoveStrings(line, "data: "); - // for (auto& json_object : objects) { - nlohmann::json j = nlohmann::json::parse(line); + nlohmann::json j; + try { + j = nlohmann::json::parse(line); + } catch (const std::exception& e) { + _last_incomplete_buffer = line; + continue; + } if (j.contains("choices")) { if (j["choices"][0].contains("delta")) { diff --git a/liboai/core/netimpl.cpp b/liboai/core/netimpl.cpp index 15d4634..6a274f0 100644 --- a/liboai/core/netimpl.cpp +++ b/liboai/core/netimpl.cpp @@ -701,6 +701,7 @@ void liboai::netimpl::Session::ClearContext() { url_.clear(); response_string_.clear(); header_string_.clear(); + write_ = netimpl::components::WriteCallback{}; } void liboai::netimpl::Session::ParseResponseHeader(const std::string& headers, std::string* status_line, std::string* reason) { diff --git a/liboai/include/components/chat.h b/liboai/include/components/chat.h index 278b761..e29744d 100644 --- a/liboai/include/components/chat.h +++ b/liboai/include/components/chat.h @@ -803,6 +803,7 @@ namespace liboai { nlohmann::json _conversation; std::optional _functions = std::nullopt; bool _last_resp_is_fc = false; + std::string _last_incomplete_buffer; }; class ChatCompletion final : private Network {