mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-05-28 03:10:28 -04:00
* feat: Add provider settings * feat: Add Claude provider * feat: Add Claude templates * refactor: Setting input sensitivity * fix: Back text after read code block * fix: Add missing system message for ollama fim
239 lines
7.0 KiB
C++
239 lines
7.0 KiB
C++
/*
|
|
* Copyright (C) 2024 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 "ClaudeProvider.hpp"
|
|
|
|
#include <QEventLoop>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QNetworkReply>
|
|
#include <QUrlQuery>
|
|
|
|
#include "llmcore/ValidationUtils.hpp"
|
|
#include "logger/Logger.hpp"
|
|
#include "settings/ChatAssistantSettings.hpp"
|
|
#include "settings/CodeCompletionSettings.hpp"
|
|
#include "settings/GeneralSettings.hpp"
|
|
#include "settings/ProviderSettings.hpp"
|
|
|
|
namespace QodeAssist::Providers {
|
|
|
|
ClaudeProvider::ClaudeProvider() {}
|
|
|
|
QString ClaudeProvider::name() const
|
|
{
|
|
return "Claude";
|
|
}
|
|
|
|
QString ClaudeProvider::url() const
|
|
{
|
|
return "https://api.anthropic.com";
|
|
}
|
|
|
|
QString ClaudeProvider::completionEndpoint() const
|
|
{
|
|
return "/v1/messages";
|
|
}
|
|
|
|
QString ClaudeProvider::chatEndpoint() const
|
|
{
|
|
return "/v1/messages";
|
|
}
|
|
|
|
bool ClaudeProvider::supportsModelListing() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void ClaudeProvider::prepareRequest(QJsonObject &request, LLMCore::RequestType type)
|
|
{
|
|
auto prepareMessages = [](QJsonObject &req) -> QJsonArray {
|
|
QJsonArray messages;
|
|
if (req.contains("messages")) {
|
|
QJsonArray origMessages = req["messages"].toArray();
|
|
for (const auto &msg : origMessages) {
|
|
QJsonObject message = msg.toObject();
|
|
if (message["role"].toString() == "system") {
|
|
req["system"] = message["content"];
|
|
} else {
|
|
messages.append(message);
|
|
}
|
|
}
|
|
} else {
|
|
if (req.contains("system")) {
|
|
req["system"] = req["system"].toString();
|
|
}
|
|
if (req.contains("prompt")) {
|
|
messages.append(
|
|
QJsonObject{{"role", "user"}, {"content", req.take("prompt").toString()}});
|
|
}
|
|
}
|
|
return messages;
|
|
};
|
|
|
|
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();
|
|
request["stream"] = true;
|
|
};
|
|
|
|
QJsonArray messages = prepareMessages(request);
|
|
if (!messages.isEmpty()) {
|
|
request["messages"] = std::move(messages);
|
|
}
|
|
|
|
if (type == LLMCore::RequestType::CodeCompletion) {
|
|
applyModelParams(Settings::codeCompletionSettings());
|
|
} else {
|
|
applyModelParams(Settings::chatAssistantSettings());
|
|
}
|
|
}
|
|
|
|
bool ClaudeProvider::handleResponse(QNetworkReply *reply, QString &accumulatedResponse)
|
|
{
|
|
bool isComplete = false;
|
|
QString tempResponse;
|
|
|
|
while (reply->canReadLine()) {
|
|
QByteArray line = reply->readLine().trimmed();
|
|
if (line.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
if (!line.startsWith("data:")) {
|
|
continue;
|
|
}
|
|
|
|
line = line.mid(6);
|
|
|
|
QJsonDocument jsonResponse = QJsonDocument::fromJson(line);
|
|
if (jsonResponse.isNull()) {
|
|
continue;
|
|
}
|
|
|
|
QJsonObject responseObj = jsonResponse.object();
|
|
QString eventType = responseObj["type"].toString();
|
|
|
|
if (eventType == "message_delta") {
|
|
if (responseObj.contains("delta")) {
|
|
QJsonObject delta = responseObj["delta"].toObject();
|
|
if (delta.contains("stop_reason")) {
|
|
isComplete = true;
|
|
}
|
|
}
|
|
} else if (eventType == "content_block_delta") {
|
|
QJsonObject delta = responseObj["delta"].toObject();
|
|
if (delta["type"].toString() == "text_delta") {
|
|
tempResponse += delta["text"].toString();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!tempResponse.isEmpty()) {
|
|
accumulatedResponse += tempResponse;
|
|
}
|
|
|
|
return isComplete;
|
|
}
|
|
|
|
QList<QString> ClaudeProvider::getInstalledModels(const QString &baseUrl)
|
|
{
|
|
QList<QString> models;
|
|
QNetworkAccessManager manager;
|
|
|
|
QUrl url(baseUrl + "/v1/models");
|
|
QUrlQuery query;
|
|
query.addQueryItem("limit", "1000");
|
|
url.setQuery(query);
|
|
|
|
QNetworkRequest request(url);
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
request.setRawHeader("anthropic-version", "2023-06-01");
|
|
|
|
if (!apiKey().isEmpty()) {
|
|
request.setRawHeader("x-api-key", apiKey().toUtf8());
|
|
}
|
|
|
|
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();
|
|
|
|
if (jsonObject.contains("data")) {
|
|
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
for (const QJsonValue &value : modelArray) {
|
|
QJsonObject modelObject = value.toObject();
|
|
if (modelObject.contains("id")) {
|
|
QString modelId = modelObject["id"].toString();
|
|
models.append(modelId);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
LOG_MESSAGE(QString("Error fetching Claude models: %1").arg(reply->errorString()));
|
|
}
|
|
|
|
reply->deleteLater();
|
|
return models;
|
|
}
|
|
|
|
QList<QString> ClaudeProvider::validateRequest(const QJsonObject &request, LLMCore::TemplateType type)
|
|
{
|
|
const auto templateReq = QJsonObject{
|
|
{"model", {}},
|
|
{"system", {}},
|
|
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
{"temperature", {}},
|
|
{"max_tokens", {}},
|
|
{"anthropic-version", {}},
|
|
{"top_p", {}},
|
|
{"top_k", {}},
|
|
{"stop", QJsonArray{}},
|
|
{"stream", {}}};
|
|
|
|
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
}
|
|
|
|
QString ClaudeProvider::apiKey() const
|
|
{
|
|
return Settings::providerSettings().claudeApiKey();
|
|
}
|
|
|
|
void ClaudeProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
|
{
|
|
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
if (!apiKey().isEmpty()) {
|
|
networkRequest.setRawHeader("x-api-key", apiKey().toUtf8());
|
|
networkRequest.setRawHeader("anthropic-version", "2023-06-01");
|
|
}
|
|
}
|
|
|
|
} // namespace QodeAssist::Providers
|