mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-09-29 00:26:22 -04:00
250 lines
7.1 KiB
C++
250 lines
7.1 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 "LMStudioProvider.hpp"
|
|
|
|
#include <QEventLoop>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QNetworkReply>
|
|
|
|
#include "llmcore/OpenAIMessage.hpp"
|
|
#include "llmcore/ValidationUtils.hpp"
|
|
#include "logger/Logger.hpp"
|
|
#include "settings/ChatAssistantSettings.hpp"
|
|
#include "settings/CodeCompletionSettings.hpp"
|
|
|
|
namespace QodeAssist::Providers {
|
|
|
|
QString LMStudioProvider::name() const
|
|
{
|
|
return "LM Studio";
|
|
}
|
|
|
|
QString LMStudioProvider::url() const
|
|
{
|
|
return "http://localhost:1234";
|
|
}
|
|
|
|
QString LMStudioProvider::completionEndpoint() const
|
|
{
|
|
return "/v1/completions";
|
|
}
|
|
|
|
QString LMStudioProvider::chatEndpoint() const
|
|
{
|
|
return "/v1/chat/completions";
|
|
}
|
|
|
|
bool LMStudioProvider::supportsModelListing() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
QList<QString> LMStudioProvider::getInstalledModels(const QString &url)
|
|
{
|
|
QList<QString> models;
|
|
QNetworkAccessManager manager;
|
|
QNetworkRequest request(QString("%1%2").arg(url, "/v1/models"));
|
|
|
|
QNetworkReply *reply = manager.get(request);
|
|
|
|
QEventLoop loop;
|
|
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
loop.exec();
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
QByteArray responseData = reply->readAll();
|
|
QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
|
|
QJsonObject jsonObject = jsonResponse.object();
|
|
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
|
|
for (const QJsonValue &value : modelArray) {
|
|
QJsonObject modelObject = value.toObject();
|
|
QString modelId = modelObject["id"].toString();
|
|
models.append(modelId);
|
|
}
|
|
} else {
|
|
LOG_MESSAGE(QString("Error fetching models: %1").arg(reply->errorString()));
|
|
}
|
|
|
|
reply->deleteLater();
|
|
return models;
|
|
}
|
|
|
|
QList<QString> LMStudioProvider::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 LMStudioProvider::apiKey() const
|
|
{
|
|
return {};
|
|
}
|
|
|
|
void LMStudioProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
|
{
|
|
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
}
|
|
|
|
LLMCore::ProviderID LMStudioProvider::providerID() const
|
|
{
|
|
return LLMCore::ProviderID::LMStudio;
|
|
}
|
|
|
|
void LMStudioProvider::sendRequest(
|
|
const LLMCore::RequestID &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("LMStudioProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
|
|
emit httpClient()->sendRequest(request);
|
|
}
|
|
|
|
void LMStudioProvider::onDataReceived(
|
|
const QodeAssist::LLMCore::RequestID &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 LMStudio 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 LMStudioProvider::onRequestFinished(
|
|
const QodeAssist::LLMCore::RequestID &requestId, bool success, const QString &error)
|
|
{
|
|
if (!success) {
|
|
LOG_MESSAGE(QString("LMStudioProvider 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);
|
|
}
|
|
|
|
void QodeAssist::Providers::LMStudioProvider::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());
|
|
}
|
|
}
|
|
|
|
} // namespace QodeAssist::Providers
|