mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-10-01 01:24:49 -04:00
228 lines
6.4 KiB
C++
228 lines
6.4 KiB
C++
/*
|
|
* Copyright (C) 2024-2025 Petr Mironychev
|
|
*
|
|
* This file is part of QodeAssist.
|
|
*
|
|
* QodeAssist is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* QodeAssist is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "OpenAICompatProvider.hpp"
|
|
|
|
#include "settings/ChatAssistantSettings.hpp"
|
|
#include "settings/CodeCompletionSettings.hpp"
|
|
#include "settings/ProviderSettings.hpp"
|
|
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QNetworkReply>
|
|
|
|
#include "llmcore/OpenAIMessage.hpp"
|
|
#include "llmcore/ValidationUtils.hpp"
|
|
#include "logger/Logger.hpp"
|
|
|
|
namespace QodeAssist::Providers {
|
|
|
|
QString OpenAICompatProvider::name() const
|
|
{
|
|
return "OpenAI Compatible";
|
|
}
|
|
|
|
QString OpenAICompatProvider::url() const
|
|
{
|
|
return "http://localhost:1234";
|
|
}
|
|
|
|
QString OpenAICompatProvider::completionEndpoint() const
|
|
{
|
|
return "/v1/chat/completions";
|
|
}
|
|
|
|
QString OpenAICompatProvider::chatEndpoint() const
|
|
{
|
|
return "/v1/chat/completions";
|
|
}
|
|
|
|
bool OpenAICompatProvider::supportsModelListing() const
|
|
{
|
|
return false;
|
|
}
|
|
|
|
void OpenAICompatProvider::prepareRequest(
|
|
QJsonObject &request,
|
|
LLMCore::PromptTemplate *prompt,
|
|
LLMCore::ContextData context,
|
|
LLMCore::RequestType type)
|
|
{
|
|
if (!prompt->isSupportProvider(providerID())) {
|
|
LOG_MESSAGE(QString("Template %1 doesn't support %2 provider").arg(name(), prompt->name()));
|
|
}
|
|
|
|
prompt->prepareRequest(request, context);
|
|
|
|
auto applyModelParams = [&request](const auto &settings) {
|
|
request["max_tokens"] = settings.maxTokens();
|
|
request["temperature"] = settings.temperature();
|
|
|
|
if (settings.useTopP())
|
|
request["top_p"] = settings.topP();
|
|
if (settings.useTopK())
|
|
request["top_k"] = settings.topK();
|
|
if (settings.useFrequencyPenalty())
|
|
request["frequency_penalty"] = settings.frequencyPenalty();
|
|
if (settings.usePresencePenalty())
|
|
request["presence_penalty"] = settings.presencePenalty();
|
|
};
|
|
|
|
if (type == LLMCore::RequestType::CodeCompletion) {
|
|
applyModelParams(Settings::codeCompletionSettings());
|
|
} else {
|
|
applyModelParams(Settings::chatAssistantSettings());
|
|
}
|
|
}
|
|
|
|
QList<QString> OpenAICompatProvider::getInstalledModels(const QString &url)
|
|
{
|
|
return QStringList();
|
|
}
|
|
|
|
QList<QString> OpenAICompatProvider::validateRequest(
|
|
const QJsonObject &request, LLMCore::TemplateType type)
|
|
{
|
|
const auto templateReq = QJsonObject{
|
|
{"model", {}},
|
|
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
{"temperature", {}},
|
|
{"max_tokens", {}},
|
|
{"top_p", {}},
|
|
{"top_k", {}},
|
|
{"frequency_penalty", {}},
|
|
{"presence_penalty", {}},
|
|
{"stop", QJsonArray{}},
|
|
{"stream", {}}};
|
|
|
|
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
}
|
|
|
|
QString OpenAICompatProvider::apiKey() const
|
|
{
|
|
return Settings::providerSettings().openAiCompatApiKey();
|
|
}
|
|
|
|
void OpenAICompatProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
|
{
|
|
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
if (!apiKey().isEmpty()) {
|
|
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
}
|
|
}
|
|
|
|
LLMCore::ProviderID OpenAICompatProvider::providerID() const
|
|
{
|
|
return LLMCore::ProviderID::OpenAICompatible;
|
|
}
|
|
|
|
void OpenAICompatProvider::sendRequest(
|
|
const QString &requestId, const QUrl &url, const QJsonObject &payload)
|
|
{
|
|
m_dataBuffers[requestId].clear();
|
|
m_requestUrls[requestId] = url;
|
|
|
|
QNetworkRequest networkRequest(url);
|
|
prepareNetworkRequest(networkRequest);
|
|
|
|
LLMCore::HttpRequest
|
|
request{.networkRequest = networkRequest, .requestId = requestId, .payload = payload};
|
|
|
|
LOG_MESSAGE(
|
|
QString("OpenAICompatProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
|
|
emit httpClient()->sendRequest(request);
|
|
}
|
|
|
|
void OpenAICompatProvider::onDataReceived(const QString &requestId, const QByteArray &data)
|
|
{
|
|
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
|
|
if (data.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
bool isDone = false;
|
|
QString tempResponse;
|
|
|
|
for (const QString &line : lines) {
|
|
if (line.trimmed().isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
if (line == "data: [DONE]") {
|
|
isDone = true;
|
|
continue;
|
|
}
|
|
|
|
QJsonObject responseObj = parseEventLine(line);
|
|
if (responseObj.isEmpty())
|
|
continue;
|
|
|
|
auto message = LLMCore::OpenAIMessage::fromJson(responseObj);
|
|
if (message.hasError()) {
|
|
LOG_MESSAGE("Error in OpenAI response: " + message.error);
|
|
continue;
|
|
}
|
|
|
|
QString content = message.getContent();
|
|
if (!content.isEmpty()) {
|
|
tempResponse += content;
|
|
}
|
|
|
|
if (message.isDone()) {
|
|
isDone = true;
|
|
}
|
|
}
|
|
|
|
if (!tempResponse.isEmpty()) {
|
|
buffers.responseContent += tempResponse;
|
|
emit partialResponseReceived(requestId, tempResponse);
|
|
}
|
|
|
|
if (isDone) {
|
|
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
m_dataBuffers.remove(requestId);
|
|
}
|
|
}
|
|
|
|
void OpenAICompatProvider::onRequestFinished(
|
|
const QString &requestId, bool success, const QString &error)
|
|
{
|
|
if (!success) {
|
|
LOG_MESSAGE(QString("OpenAICompatProvider request %1 failed: %2").arg(requestId, error));
|
|
emit requestFailed(requestId, error);
|
|
} else {
|
|
if (m_dataBuffers.contains(requestId)) {
|
|
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
if (!buffers.responseContent.isEmpty()) {
|
|
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
}
|
|
}
|
|
}
|
|
|
|
m_dataBuffers.remove(requestId);
|
|
m_requestUrls.remove(requestId);
|
|
}
|
|
|
|
} // namespace QodeAssist::Providers
|