From c97c0f62e8feeeabbbeb656320087c4b123c7e9d Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Sun, 19 Jan 2025 01:16:33 +0100 Subject: [PATCH] fix: Handling full input message from OpenAI compatible providers --- providers/LMStudioProvider.cpp | 33 ++++++++++++++++++++--------- providers/OpenAICompatProvider.cpp | 31 +++++++++++++++++++-------- providers/OpenRouterAIProvider.cpp | 34 ++++++++++++++++++++---------- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/providers/LMStudioProvider.cpp b/providers/LMStudioProvider.cpp index 4dc1d07..168cbd3 100644 --- a/providers/LMStudioProvider.cpp +++ b/providers/LMStudioProvider.cpp @@ -108,15 +108,22 @@ bool LMStudioProvider::handleResponse(QNetworkReply *reply, QString &accumulated return false; } - QByteArrayList chunks = data.split('\n'); - for (const QByteArray &chunk : chunks) { - if (chunk.trimmed().isEmpty() || chunk == "data: [DONE]") { + bool isDone = false; + QByteArrayList lines = data.split('\n'); + + for (const QByteArray &line : lines) { + if (line.trimmed().isEmpty()) { continue; } - QByteArray jsonData = chunk; - if (chunk.startsWith("data: ")) { - jsonData = chunk.mid(6); + if (line == "data: [DONE]") { + isDone = true; + continue; + } + + QByteArray jsonData = line; + if (line.startsWith("data: ")) { + jsonData = line.mid(6); } QJsonParseError error; @@ -128,15 +135,21 @@ bool LMStudioProvider::handleResponse(QNetworkReply *reply, QString &accumulated auto message = LLMCore::OpenAIMessage::fromJson(doc.object()); if (message.hasError()) { - LOG_MESSAGE("Error in LMStudioProvider response: " + message.error); + LOG_MESSAGE("Error in OpenAI response: " + message.error); continue; } - accumulatedResponse += message.getContent(); - return message.isDone(); + QString content = message.getContent(); + if (!content.isEmpty()) { + accumulatedResponse += content; + } + + if (message.isDone()) { + isDone = true; + } } - return false; + return isDone; } QList LMStudioProvider::getInstalledModels(const QString &url) diff --git a/providers/OpenAICompatProvider.cpp b/providers/OpenAICompatProvider.cpp index 2e0f9ab..f515fd4 100644 --- a/providers/OpenAICompatProvider.cpp +++ b/providers/OpenAICompatProvider.cpp @@ -109,15 +109,22 @@ bool OpenAICompatProvider::handleResponse(QNetworkReply *reply, QString &accumul return false; } - QByteArrayList chunks = data.split('\n'); - for (const QByteArray &chunk : chunks) { - if (chunk.trimmed().isEmpty() || chunk == "data: [DONE]") { + bool isDone = false; + QByteArrayList lines = data.split('\n'); + + for (const QByteArray &line : lines) { + if (line.trimmed().isEmpty()) { continue; } - QByteArray jsonData = chunk; - if (chunk.startsWith("data: ")) { - jsonData = chunk.mid(6); + if (line == "data: [DONE]") { + isDone = true; + continue; + } + + QByteArray jsonData = line; + if (line.startsWith("data: ")) { + jsonData = line.mid(6); } QJsonParseError error; @@ -133,11 +140,17 @@ bool OpenAICompatProvider::handleResponse(QNetworkReply *reply, QString &accumul continue; } - accumulatedResponse += message.getContent(); - return message.isDone(); + QString content = message.getContent(); + if (!content.isEmpty()) { + accumulatedResponse += content; + } + + if (message.isDone()) { + isDone = true; + } } - return false; + return isDone; } QList OpenAICompatProvider::getInstalledModels(const QString &url) diff --git a/providers/OpenRouterAIProvider.cpp b/providers/OpenRouterAIProvider.cpp index 99fd94f..faa2a88 100644 --- a/providers/OpenRouterAIProvider.cpp +++ b/providers/OpenRouterAIProvider.cpp @@ -93,16 +93,22 @@ bool OpenRouterProvider::handleResponse(QNetworkReply *reply, QString &accumulat return false; } - QByteArrayList chunks = data.split('\n'); - for (const QByteArray &chunk : chunks) { - if (chunk.trimmed().isEmpty() || chunk.contains("OPENROUTER PROCESSING") - || chunk == "data: [DONE]") { + bool isDone = false; + QByteArrayList lines = data.split('\n'); + + for (const QByteArray &line : lines) { + if (line.trimmed().isEmpty() || line.contains("OPENROUTER PROCESSING")) { continue; } - QByteArray jsonData = chunk; - if (chunk.startsWith("data: ")) { - jsonData = chunk.mid(6); + if (line == "data: [DONE]") { + isDone = true; + continue; + } + + QByteArray jsonData = line; + if (line.startsWith("data: ")) { + jsonData = line.mid(6); } QJsonParseError error; @@ -114,15 +120,21 @@ bool OpenRouterProvider::handleResponse(QNetworkReply *reply, QString &accumulat auto message = LLMCore::OpenAIMessage::fromJson(doc.object()); if (message.hasError()) { - LOG_MESSAGE("Error in OpenRouter response: " + message.error); + LOG_MESSAGE("Error in OpenAI response: " + message.error); continue; } - accumulatedResponse += message.getContent(); - return message.isDone(); + QString content = message.getContent(); + if (!content.isEmpty()) { + accumulatedResponse += content; + } + + if (message.isDone()) { + isDone = true; + } } - return false; + return isDone; } QString OpenRouterProvider::apiKey() const