Compare commits

...

14 Commits

Author SHA1 Message Date
6703a7026d Merge pull request #5 from Palm1r/version-0.0.7
Version 0.0.7
- Add DeepSeekV2 template
- Add performance benchmark
- Move instructions and file path from FIM
2024-09-01 12:09:15 +02:00
d7fc62f94b Move instractoins out from FIM request 2024-09-01 12:06:55 +02:00
dec8967df2 Add performance benchmark 2024-09-01 00:06:11 +02:00
4f0f9338dc Add DeepSeekCoderV2 template 2024-08-31 23:20:23 +02:00
ec26a31ec5 Merge pull request #4 from Palm1r/version-0.0.6
Version 0.0.6
- Fix authorization to API
- Fix reset to provider default settings after reboot qtc
2024-08-30 08:48:09 +02:00
55d359e44e Fix reset to provider default settings on restart qtc 2024-08-30 08:28:27 +02:00
46258a11f6 Fix authorization to the API. 2024-08-30 07:47:33 +02:00
4bccd8db91 Add new provider to README.md 2024-08-29 23:35:34 +02:00
e3495e10f0 Merge pull request #3 from Palm1r/version-0.0.5
Version 0.0.5
- Fix typo
- Fix remove comments from request
- Add support CodeQwenChat template (experimental)
- Add support OpenAI compatible provider (experimental)
2024-08-29 23:14:17 +02:00
d97a3514cc Add experimental prefix 2024-08-29 23:10:57 +02:00
fa79803836 Add OpenAI Compatible provider 2024-08-29 23:06:58 +02:00
b4f969908f Fix typo 2024-08-29 22:46:03 +02:00
9beb48ee97 Add CodeQwenChat template 2024-08-29 22:28:18 +02:00
d6320b04f7 Fix remove comments from request 2024-08-29 22:26:40 +02:00
24 changed files with 460 additions and 97 deletions

View File

@ -34,9 +34,12 @@ add_qtc_plugin(QodeAssist
templates/PromptTemplate.hpp
templates/CodeLLamaTemplate.hpp
templates/StarCoder2Template.hpp
templates/CodeQwenChat.hpp
templates/DeepSeekCoderV2.hpp
providers/LLMProvider.hpp
providers/OllamaProvider.hpp providers/OllamaProvider.cpp
providers/LMStudioProvider.hpp providers/LMStudioProvider.cpp
providers/OpenAICompatProvider.hpp providers/OpenAICompatProvider.cpp
LLMProvidersManager.hpp LLMProvidersManager.cpp
QodeAssistSettings.hpp QodeAssistSettings.cpp
QodeAssist.qrc
@ -46,4 +49,5 @@ add_qtc_plugin(QodeAssist
QodeAssistClient.hpp QodeAssistClient.cpp
QodeAssistUtils.hpp
DocumentContextReader.hpp DocumentContextReader.cpp
QodeAssistData.hpp
)

View File

@ -25,6 +25,26 @@
#include "QodeAssistSettings.hpp"
const QRegularExpression &getYearRegex()
{
static const QRegularExpression yearRegex("\\b(19|20)\\d{2}\\b");
return yearRegex;
}
const QRegularExpression &getNameRegex()
{
static const QRegularExpression nameRegex("\\b[A-Z][a-z.]+ [A-Z][a-z.]+\\b");
return nameRegex;
}
const QRegularExpression &getCommentRegex()
{
static const QRegularExpression
commentRegex(R"((/\*[\s\S]*?\*/|//.*$|#.*$|//{2,}[\s\S]*?//{2,}))",
QRegularExpression::MultilineOption);
return commentRegex;
}
namespace QodeAssist {
DocumentContextReader::DocumentContextReader(TextEditor::TextDocument *textDocument)
@ -110,7 +130,7 @@ QString DocumentContextReader::getLanguageAndFileInfo() const
QString filePath = m_textDocument->filePath().toString();
QString fileExtension = QFileInfo(filePath).suffix();
return QString("//Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
return QString("Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
.arg(language, mimeType, filePath, fileExtension);
}
@ -118,7 +138,7 @@ QString DocumentContextReader::getSpecificInstructions() const
{
QString specificInstruction = settings().specificInstractions().arg(
LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(m_textDocument->mimeType()));
return QString("//Instructions: %1").arg(specificInstruction);
return QString("Instructions: %1").arg(specificInstruction);
}
CopyrightInfo DocumentContextReader::findCopyright()
@ -126,21 +146,27 @@ CopyrightInfo DocumentContextReader::findCopyright()
CopyrightInfo result = {-1, -1, false};
QString text = m_document->toPlainText();
QRegularExpressionMatchIterator matchIterator = getCopyrightRegex().globalMatch(text);
QRegularExpressionMatchIterator matchIterator = getCommentRegex().globalMatch(text);
QList<CopyrightInfo> copyrightBlocks;
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
int startPos = match.capturedStart();
int endPos = match.capturedEnd();
QString matchedText = match.captured().toLower();
CopyrightInfo info;
info.startLine = m_document->findBlock(startPos).blockNumber();
info.endLine = m_document->findBlock(endPos).blockNumber();
info.found = true;
if (matchedText.contains("copyright") || matchedText.contains("(C)")
|| matchedText.contains("(c)") || matchedText.contains("©")
|| getYearRegex().match(text).hasMatch() || getNameRegex().match(text).hasMatch()) {
int startPos = match.capturedStart();
int endPos = match.capturedEnd();
copyrightBlocks.append(info);
CopyrightInfo info;
info.startLine = m_document->findBlock(startPos).blockNumber();
info.endLine = m_document->findBlock(endPos).blockNumber();
info.found = true;
copyrightBlocks.append(info);
}
}
for (int i = 0; i < copyrightBlocks.size() - 1; ++i) {
@ -178,12 +204,9 @@ QString DocumentContextReader::getContextBetween(int startLine,
return context;
}
const QRegularExpression &DocumentContextReader::getCopyrightRegex()
CopyrightInfo DocumentContextReader::copyrightInfo() const
{
static const QRegularExpression copyrightRegex(
R"((?:/\*[\s\S]*?Copyright[\s\S]*?\*/| // Copyright[\s\S]*?(?:\n\s*//.*)*|///.*Copyright[\s\S]*?(?:\n\s*///.*)*)|(?://))",
QRegularExpression::MultilineOption | QRegularExpression::CaseInsensitiveOption);
return copyrightRegex;
return m_copyrightInfo;
}
} // namespace QodeAssist

View File

@ -46,12 +46,12 @@ public:
CopyrightInfo findCopyright();
QString getContextBetween(int startLine, int endLine, int cursorPosition) const;
CopyrightInfo copyrightInfo() const;
private:
TextEditor::TextDocument *m_textDocument;
QTextDocument *m_document;
CopyrightInfo m_copyrightInfo;
static const QRegularExpression &getCopyrightRegex();
};
} // namespace QodeAssist

View File

@ -69,6 +69,8 @@ void LLMClientInterface::sendData(const QByteArray &data)
} else if (method == "textDocument/didOpen") {
handleTextDocumentDidOpen(request);
} else if (method == "getCompletionsCycling") {
QString requestId = request["id"].toString();
startTimeMeasurement(requestId);
handleCompletion(request);
} else if (method == "$/cancelRequest") {
handleCancelRequest(request);
@ -120,8 +122,10 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
return QString();
DocumentContextReader reader(widget->textDocument());
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
const auto &copyright = reader.copyrightInfo();
logMessage(QString{"Line Number: %1"}.arg(lineNumber));
logMessage(QString("Copyright found %1 %2").arg(copyright.found).arg(copyright.endLine));
if (lineNumber < reader.findCopyright().endLine)
return QString();
@ -134,10 +138,7 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
settings().readStringsBeforeCursor());
}
return QString("%1\n%2\n%3")
.arg(reader.getSpecificInstructions())
.arg(reader.getLanguageAndFileInfo())
.arg(contextBefore);
return contextBefore;
}
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
@ -177,7 +178,7 @@ void LLMClientInterface::handleInitialize(const QJsonObject &request)
result["capabilities"] = capabilities;
QJsonObject serverInfo;
serverInfo["name"] = "Ollama LSP Server";
serverInfo["name"] = "QodeAssist LSP Server";
serverInfo["version"] = "0.1";
result["serverInfo"] = serverInfo;
@ -249,8 +250,8 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request,
sendLLMRequest(request, updatedContext);
}
LLMClientInterface::ContextPair LLMClientInterface::prepareContext(
const QJsonObject &request, const QString &accumulatedCompletion)
ContextData LLMClientInterface::prepareContext(const QJsonObject &request,
const QString &accumulatedCompletion)
{
QJsonObject params = request["params"].toObject();
QJsonObject doc = params["doc"].toObject();
@ -272,20 +273,25 @@ LLMClientInterface::ContextPair LLMClientInterface::prepareContext(
auto textEditor = TextEditor::BaseTextEditor::currentTextEditor();
TextEditor::TextEditorWidget *widget = textEditor->editorWidget();
DocumentContextReader reader(widget->textDocument());
QString contextBefore = сontextBefore(widget, lineNumber, cursorPosition);
QString contextAfter = сontextAfter(widget, lineNumber, cursorPosition);
QString instructions = QString("%1%2").arg(settings().useSpecificInstructions()
? reader.getSpecificInstructions()
: QString(),
settings().useFilePathInContext()
? reader.getLanguageAndFileInfo()
: QString());
QString updatedContextBefore = contextBefore + accumulatedCompletion;
return {updatedContextBefore, contextAfter};
return {updatedContextBefore, contextAfter, instructions};
}
void LLMClientInterface::updateProvider()
{
m_serverUrl = QUrl(QString("%1:%2%3")
.arg(settings().url.value())
.arg(settings().port.value())
.arg(settings().endPoint.value()));
m_serverUrl = QUrl(QString("%1%2").arg(settings().url(), settings().endPoint()));
}
void LLMClientInterface::sendCompletionToClient(const QString &completion,
@ -319,27 +325,36 @@ void LLMClientInterface::sendCompletionToClient(const QString &completion,
logMessage(QString("Full response: \n%1")
.arg(QString::fromUtf8(QJsonDocument(response).toJson(QJsonDocument::Indented))));
QString requestId = request["id"].toString();
endTimeMeasurement(requestId);
emit messageReceived(LanguageServerProtocol::JsonRpcMessage(response));
}
void LLMClientInterface::sendLLMRequest(const QJsonObject &request, const ContextPair &prompt)
void LLMClientInterface::sendLLMRequest(const QJsonObject &request, const ContextData &prompt)
{
QJsonObject ollamaRequest = {{"model", settings().modelName.value()}, {"stream", true}};
QJsonObject providerRequest = {{"model", settings().modelName.value()}, {"stream", true}};
auto currentTemplate = PromptTemplateManager::instance().getCurrentTemplate();
currentTemplate->prepareRequest(ollamaRequest, prompt.prefix, prompt.suffix);
currentTemplate->prepareRequest(providerRequest, prompt);
auto &providerManager = LLMProvidersManager::instance();
providerManager.getCurrentProvider()->prepareRequest(ollamaRequest);
providerManager.getCurrentProvider()->prepareRequest(providerRequest);
logMessage(
QString("Sending request to llm: \nurl: %1\nRequest body:\n%2")
.arg(m_serverUrl.toString())
.arg(QString::fromUtf8(QJsonDocument(ollamaRequest).toJson(QJsonDocument::Indented))));
logMessage(QString("Sending request to llm: \nurl: %1\nRequest body:\n%2")
.arg(m_serverUrl.toString(),
QString::fromUtf8(
QJsonDocument(providerRequest).toJson(QJsonDocument::Indented))));
QNetworkRequest networkRequest(m_serverUrl);
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *reply = m_manager->post(networkRequest, QJsonDocument(ollamaRequest).toJson());
if (providerRequest.contains("api_key")) {
QString apiKey = providerRequest["api_key"].toString();
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey).toUtf8());
providerRequest.remove("api_key");
}
QNetworkReply *reply = m_manager->post(networkRequest, QJsonDocument(providerRequest).toJson());
if (!reply) {
logMessage("Error: Failed to create network reply");
return;
@ -356,7 +371,7 @@ void LLMClientInterface::sendLLMRequest(const QJsonObject &request, const Contex
reply->deleteLater();
m_activeRequests.remove(requestId);
if (reply->error() != QNetworkReply::NoError) {
logMessage(QString("Error in Ollama request: %1").arg(reply->errorString()));
logMessage(QString("Error in QodeAssist request: %1").arg(reply->errorString()));
} else {
logMessage("Request finished successfully");
}
@ -377,6 +392,29 @@ QString LLMClientInterface::removeStopWords(const QString &completion)
return filteredCompletion;
}
void LLMClientInterface::startTimeMeasurement(const QString &requestId)
{
m_requestStartTimes[requestId] = QDateTime::currentMSecsSinceEpoch();
}
void LLMClientInterface::endTimeMeasurement(const QString &requestId)
{
if (m_requestStartTimes.contains(requestId)) {
qint64 startTime = m_requestStartTimes[requestId];
qint64 endTime = QDateTime::currentMSecsSinceEpoch();
qint64 totalTime = endTime - startTime;
logPerformance(requestId, "TotalCompletionTime", totalTime);
m_requestStartTimes.remove(requestId);
}
}
void LLMClientInterface::logPerformance(const QString &requestId,
const QString &operation,
qint64 elapsedMs)
{
logMessage(QString("Performance: %1 %2 took %3 ms").arg(requestId, operation).arg(elapsedMs));
}
void LLMClientInterface::parseCurrentMessage() {}
} // namespace QodeAssist

View File

@ -22,6 +22,8 @@
#include <languageclient/languageclientinterface.h>
#include <texteditor/texteditor.h>
#include "QodeAssistData.hpp"
class QNetworkReply;
class QNetworkAccessManager;
@ -35,12 +37,6 @@ public:
LLMClientInterface();
public:
struct ContextPair
{
QString prefix;
QString suffix;
};
Utils::FilePath serverDeviceTemplate() const override;
void sendCompletionToClient(const QString &completion,
@ -50,10 +46,10 @@ public:
void handleCompletion(const QJsonObject &request,
const QString &accumulatedCompletion = QString());
void sendLLMRequest(const QJsonObject &request, const ContextPair &prompt);
void sendLLMRequest(const QJsonObject &request, const ContextData &prompt);
void handleLLMResponse(QNetworkReply *reply, const QJsonObject &request);
ContextPair prepareContext(const QJsonObject &request,
ContextData prepareContext(const QJsonObject &request,
const QString &accumulatedCompletion = QString{});
void updateProvider();
@ -81,6 +77,13 @@ private:
QNetworkAccessManager *m_manager;
QMap<QString, QNetworkReply *> m_activeRequests;
QMap<QNetworkReply *, QString> m_accumulatedResponses;
QElapsedTimer m_completionTimer;
QMap<QString, qint64> m_requestStartTimes;
void startTimeMeasurement(const QString &requestId);
void endTimeMeasurement(const QString &requestId);
void logPerformance(const QString &requestId, const QString &operation, qint64 elapsedMs);
};
} // namespace QodeAssist

View File

@ -1,6 +1,6 @@
{
"Name" : "QodeAssist",
"Version" : "0.0.4",
"Version" : "0.0.7",
"CompatVersion" : "${IDE_VERSION_COMPAT}",
"Vendor" : "Petr Mironychev",
"Copyright" : "(C) ${IDE_COPYRIGHT_YEAR} Petr Mironychev, (C) ${IDE_COPYRIGHT_YEAR} The Qt Company Ltd",

View File

@ -30,7 +30,6 @@ const char ENABLE_AUTO_COMPLETE[] = "QodeAssist.enableAutoComplete";
const char ENABLE_LOGGING[] = "QodeAssist.enableLogging";
const char LLM_PROVIDERS[] = "QodeAssist.llmProviders";
const char URL[] = "QodeAssist.url";
const char PORT[] = "QodeAssist.port";
const char END_POINT[] = "QodeAssist.endPoint";
const char MODEL_NAME[] = "QodeAssist.modelName";
const char SELECT_MODELS[] = "QodeAssist.selectModels";
@ -54,6 +53,9 @@ const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold";
const char OLLAMA_LIVETIME[] = "QodeAssist.ollamaLivetime";
const char SPECIFIC_INSTRUCTIONS[] = "QodeAssist.specificInstractions";
const char MULTILINE_COMPLETION[] = "QodeAssist.multilineCompletion";
const char API_KEY[] = "QodeAssist.apiKey";
const char USE_SPECIFIC_INSTRUCTIONS[] = "QodeAssist.useSpecificInstructions";
const char USE_FILE_PATH_IN_CONTEXT[] = "QodeAssist.useFilePathInContext";
const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions";
const char QODE_ASSIST_GENERAL_OPTIONS_CATEGORY[] = "QodeAssist.Category";

33
QodeAssistData.hpp Normal file
View File

@ -0,0 +1,33 @@
/*
* 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/>.
*/
#pragma once
#include <QString>
namespace QodeAssist {
struct ContextData
{
QString prefix;
QString suffix;
QString instriuctions;
};
} // namespace QodeAssist

View File

@ -68,10 +68,6 @@ QodeAssistSettings::QodeAssistSettings()
endPoint.setLabelText(Tr::tr("Endpoint:"));
endPoint.setDisplayStyle(Utils::StringAspect::LineEditDisplay);
port.setSettingsKey(Constants::PORT);
port.setLabelText(Tr::tr("Port:"));
port.setRange(1, 65535);
modelName.setSettingsKey(Constants::MODEL_NAME);
modelName.setLabelText(Tr::tr("LLM Name:"));
modelName.setDisplayStyle(Utils::StringAspect::LineEditDisplay);
@ -123,15 +119,15 @@ QodeAssistSettings::QodeAssistSettings()
topP.setSettingsKey(Constants::TOP_P);
topP.setLabelText(Tr::tr("top_p"));
topP.setDefaultValue(0.9);
topP.setRange(0.0, 10.0);
topP.setRange(0.0, 1.0);
useTopK.setSettingsKey(Constants::USE_TOP_K);
useTopK.setDefaultValue(false);
topK.setSettingsKey(Constants::TOP_K);
topK.setLabelText(Tr::tr("top_k"));
topK.setDefaultValue(0.1);
topK.setRange(0, 10.0);
topK.setDefaultValue(50);
topK.setRange(1, 1000);
usePresencePenalty.setSettingsKey(Constants::USE_PRESENCE_PENALTY);
usePresencePenalty.setDefaultValue(false);
@ -157,6 +153,14 @@ QodeAssistSettings::QodeAssistSettings()
startSuggestionTimer.setRange(10, 10000);
startSuggestionTimer.setDefaultValue(500);
useFilePathInContext.setSettingsKey(Constants::USE_FILE_PATH_IN_CONTEXT);
useFilePathInContext.setDefaultValue(false);
useFilePathInContext.setLabelText(Tr::tr("Use File Path in Context"));
useSpecificInstructions.setSettingsKey(Constants::USE_SPECIFIC_INSTRUCTIONS);
useSpecificInstructions.setDefaultValue(false);
useSpecificInstructions.setLabelText(Tr::tr("Use Specific Instructions"));
specificInstractions.setSettingsKey(Constants::SPECIFIC_INSTRUCTIONS);
specificInstractions.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
specificInstractions.setLabelText(
@ -170,6 +174,11 @@ QodeAssistSettings::QodeAssistSettings()
multiLineCompletion.setDefaultValue(true);
multiLineCompletion.setLabelText(Tr::tr("Enable Multiline Completion"));
apiKey.setSettingsKey(Constants::API_KEY);
apiKey.setLabelText(Tr::tr("API Key:"));
apiKey.setDisplayStyle(Utils::StringAspect::LineEditDisplay);
apiKey.setPlaceHolderText(Tr::tr("Enter your API key here"));
const auto &manager = LLMProvidersManager::instance();
if (!manager.getProviderNames().isEmpty()) {
const auto providerNames = manager.getProviderNames();
@ -194,9 +203,9 @@ QodeAssistSettings::QodeAssistSettings()
frequencyPenalty.setEnabled(useFrequencyPenalty());
readStringsAfterCursor.setEnabled(!readFullFile());
readStringsBeforeCursor.setEnabled(!readFullFile());
specificInstractions.setEnabled(useSpecificInstructions());
PromptTemplateManager::instance().setCurrentTemplate(fimPrompts.stringValue());
LLMProvidersManager::instance().setCurrentProvider(llmProviders.stringValue());
updateProviderSettings();
setLoggingEnabled(enableLogging());
@ -210,19 +219,22 @@ QodeAssistSettings::QodeAssistSettings()
enableLogging,
Row{Stretch{1}, resetToDefaults}}}},
Group{title(Tr::tr("LLM Providers")),
Form{Column{llmProviders, Row{url, port, endPoint}, providerPaths}}},
Form{Column{llmProviders, Row{url, endPoint}, providerPaths}}},
Group{title(Tr::tr("LLM Model Settings")),
Form{Column{Row{selectModels, modelName}}}},
Group{title(Tr::tr("FIM Prompt Settings")),
Form{Column{fimPrompts,
readFullFile,
maxFileThreshold,
readStringsBeforeCursor,
readStringsAfterCursor,
ollamaLivetime,
apiKey,
useFilePathInContext,
useSpecificInstructions,
specificInstractions,
temperature,
maxTokens,
readFullFile,
readStringsBeforeCursor,
readStringsAfterCursor,
startSuggestionTimer,
Row{useTopP, topP, Stretch{1}},
Row{useTopK, topK, Stretch{1}},
@ -273,6 +285,9 @@ void QodeAssistSettings::setupConnections()
connect(&enableLogging, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
setLoggingEnabled(enableLogging.volatileValue());
});
connect(&useSpecificInstructions, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
specificInstractions.setEnabled(useSpecificInstructions.volatileValue());
});
}
void QodeAssistSettings::updateProviderSettings()
@ -282,7 +297,6 @@ void QodeAssistSettings::updateProviderSettings()
if (provider) {
logMessage(QString("currentProvider %1").arg(provider->name()));
url.setValue(provider->url());
port.setValue(provider->defaultPort());
endPoint.setValue(provider->completionEndpoint());
ollamaLivetime.setEnabled(provider->name() == "Ollama");
}
@ -341,7 +355,6 @@ void QodeAssistSettings::resetSettingsToDefaults()
resetAspect(enableAutoComplete);
resetAspect(llmProviders);
resetAspect(url);
resetAspect(port);
resetAspect(endPoint);
resetAspect(modelName);
resetAspect(fimPrompts);

View File

@ -63,7 +63,6 @@ public:
Utils::SelectionAspect llmProviders{this};
Utils::StringAspect url{this};
Utils::IntegerAspect port{this};
Utils::StringAspect endPoint{this};
Utils::StringAspect modelName{this};
@ -81,7 +80,7 @@ public:
Utils::DoubleAspect topP{this};
Utils::BoolAspect useTopK{this};
Utils::DoubleAspect topK{this};
Utils::IntegerAspect topK{this};
Utils::BoolAspect usePresencePenalty{this};
Utils::DoubleAspect presencePenalty{this};
@ -96,8 +95,12 @@ public:
Utils::StringAspect ollamaLivetime{this};
Utils::StringAspect specificInstractions{this};
Utils::BoolAspect useSpecificInstructions{this};
Utils::BoolAspect useFilePathInContext{this};
Utils::BoolAspect multiLineCompletion{this};
Utils::StringAspect apiKey{this};
ButtonAspect resetToDefaults{this};
private:

View File

@ -7,9 +7,10 @@ QodeAssist is an AI-powered coding assistant plugin for Qt Creator. It provides
QodeAssist currently supports the following LLM (Large Language Model) providers:
- [Ollama](https://ollama.com)
- [LM Studio](https://lmstudio.ai)
- OpenAI compatible providers
## Supported Models
QodeAssist has been tested with the following language models:
QodeAssist has been tested with the following language models, all trained for Fill-in-theMiddle:
Ollama:
- [starcoder2](https://ollama.com/library/starcoder2)

View File

@ -34,7 +34,6 @@ public:
virtual QString name() const = 0;
virtual QString url() const = 0;
virtual int defaultPort() const = 0;
virtual QString completionEndpoint() const = 0;
virtual void prepareRequest(QJsonObject &request) = 0;

View File

@ -39,12 +39,7 @@ QString LMStudioProvider::name() const
QString LMStudioProvider::url() const
{
return "http://localhost";
}
int LMStudioProvider::defaultPort() const
{
return 1234;
return "http://localhost:1234";
}
QString LMStudioProvider::completionEndpoint() const

View File

@ -30,7 +30,6 @@ public:
QString name() const override;
QString url() const override;
int defaultPort() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;

View File

@ -39,12 +39,7 @@ QString OllamaProvider::name() const
QString OllamaProvider::url() const
{
return "http://localhost";
}
int OllamaProvider::defaultPort() const
{
return 11434;
return "http://localhost:11434";
}
QString OllamaProvider::completionEndpoint() const

View File

@ -30,7 +30,6 @@ public:
QString name() const override;
QString url() const override;
int defaultPort() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;

View File

@ -0,0 +1,124 @@
/*
* 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 "OpenAICompatProvider.hpp"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QProcess>
#include "PromptTemplateManager.hpp"
#include "QodeAssistSettings.hpp"
namespace QodeAssist::Providers {
OpenAICompatProvider::OpenAICompatProvider() {}
QString OpenAICompatProvider::name() const
{
return "OpenAI Compatible (experimental)";
}
QString OpenAICompatProvider::url() const
{
return "http://localhost:1234";
}
QString OpenAICompatProvider::completionEndpoint() const
{
return "/v1/chat/completions";
}
void OpenAICompatProvider::prepareRequest(QJsonObject &request)
{
const auto &currentTemplate = PromptTemplateManager::instance().getCurrentTemplate();
if (request.contains("prompt")) {
QJsonArray messages{
{QJsonObject{{"role", "user"}, {"content", request.take("prompt").toString()}}}};
request["messages"] = std::move(messages);
}
request["max_tokens"] = settings().maxTokens();
request["temperature"] = settings().temperature();
request["stop"] = QJsonArray::fromStringList(currentTemplate->stopWords());
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();
const QString &apiKey = settings().apiKey.value();
if (!apiKey.isEmpty()) {
request["api_key"] = apiKey;
}
}
bool OpenAICompatProvider::handleResponse(QNetworkReply *reply, QString &accumulatedResponse)
{
bool isComplete = false;
while (reply->canReadLine()) {
QByteArray line = reply->readLine().trimmed();
if (line.isEmpty()) {
continue;
}
if (line == "data: [DONE]") {
isComplete = true;
break;
}
if (line.startsWith("data: ")) {
line = line.mid(6); // Remove "data: " prefix
}
QJsonDocument jsonResponse = QJsonDocument::fromJson(line);
if (jsonResponse.isNull()) {
qWarning() << "Invalid JSON response from LM Studio:" << line;
continue;
}
QJsonObject responseObj = jsonResponse.object();
if (responseObj.contains("choices")) {
QJsonArray choices = responseObj["choices"].toArray();
if (!choices.isEmpty()) {
QJsonObject choice = choices.first().toObject();
QJsonObject delta = choice["delta"].toObject();
if (delta.contains("content")) {
QString completion = delta["content"].toString();
accumulatedResponse += completion;
}
if (choice["finish_reason"].toString() == "stop") {
isComplete = true;
break;
}
}
}
}
return isComplete;
}
QList<QString> OpenAICompatProvider::getInstalledModels(const Utils::Environment &env)
{
return QStringList();
}
} // namespace QodeAssist::Providers

View File

@ -0,0 +1,39 @@
/*
* 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/>.
*/
#pragma once
#include "LLMProvider.hpp"
namespace QodeAssist::Providers {
class OpenAICompatProvider : public LLMProvider
{
public:
OpenAICompatProvider();
QString name() const override;
QString url() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;
QList<QString> getInstalledModels(const Utils::Environment &env) override;
};
} // namespace QodeAssist::Providers

View File

@ -43,7 +43,10 @@
#include "QodeAssistClient.hpp"
#include "providers/LMStudioProvider.hpp"
#include "providers/OllamaProvider.hpp"
#include "providers/OpenAICompatProvider.hpp"
#include "templates/CodeLLamaTemplate.hpp"
#include "templates/CodeQwenChat.hpp"
#include "templates/DeepSeekCoderV2.hpp"
#include "templates/StarCoder2Template.hpp"
using namespace Utils;
@ -72,10 +75,13 @@ public:
auto &providerManager = LLMProvidersManager::instance();
providerManager.registerProvider<Providers::OllamaProvider>();
providerManager.registerProvider<Providers::LMStudioProvider>();
providerManager.registerProvider<Providers::OpenAICompatProvider>();
auto &templateManager = PromptTemplateManager::instance();
templateManager.registerTemplate<Templates::CodeLLamaTemplate>();
templateManager.registerTemplate<Templates::StarCoder2Template>();
templateManager.registerTemplate<Templates::CodeQwenChatTemplate>();
templateManager.registerTemplate<Templates::DeepSeekCoderV2Template>();
Utils::Icon QCODEASSIST_ICON(
{{":/resources/images/qoderassist-icon.png", Utils::Theme::IconsBaseColor}});
@ -83,7 +89,7 @@ public:
ActionBuilder requestAction(this, Constants::QODE_ASSIST_REQUEST_SUGGESTION);
requestAction.setToolTip(
Tr::tr("Generate Qode Assist suggestion at the current cursor position."));
requestAction.setText(Tr::tr("Request Ollama Suggestion"));
requestAction.setText(Tr::tr("Request QodeAssist Suggestion"));
requestAction.setIcon(QCODEASSIST_ICON.icon());
const QKeySequence defaultShortcut = QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Q);
requestAction.setDefaultKeySequence(defaultShortcut);

View File

@ -27,17 +27,17 @@ class CodeLLamaTemplate : public PromptTemplate
{
public:
QString name() const override { return "CodeLlama"; }
QString promptTemplate() const override { return "<PRE> %1 <SUF>%2 <MID>"; }
QString promptTemplate() const override { return "%1<PRE> %2 <SUF>%3 <MID>"; }
QStringList stopWords() const override
{
return QStringList() << "<EOT>" << "<PRE>" << "<SUF" << "<MID>";
}
void prepareRequest(QJsonObject &request,
const QString &prefix,
const QString &suffix) const override
void prepareRequest(QJsonObject &request, const ContextData &context) const override
{
QString formattedPrompt = promptTemplate().arg(prefix, suffix);
QString formattedPrompt = promptTemplate().arg(context.instriuctions,
context.prefix,
context.suffix);
request["prompt"] = formattedPrompt;
}
};

View File

@ -0,0 +1,44 @@
/*
* 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/>.
*/
#pragma once
#include "PromptTemplate.hpp"
namespace QodeAssist::Templates {
class CodeQwenChatTemplate : public PromptTemplate
{
public:
QString name() const override { return "CodeQwenChat (experimental)"; }
QString promptTemplate() const override { return "%1\n### Instruction:%2%3 ### Response:\n"; }
QStringList stopWords() const override
{
return QStringList() << "### Instruction:" << "### Response:" << "\n\n### ";
}
void prepareRequest(QJsonObject &request, const ContextData &context) const override
{
QString formattedPrompt = promptTemplate().arg(context.instriuctions,
context.prefix,
context.suffix);
request["prompt"] = formattedPrompt;
}
};
} // namespace QodeAssist::Templates

View File

@ -0,0 +1,44 @@
/*
* 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/>.
*/
#pragma once
#include "PromptTemplate.hpp"
namespace QodeAssist::Templates {
class DeepSeekCoderV2Template : public PromptTemplate
{
public:
QString name() const override { return "DeepSeekCoderV2"; }
QString promptTemplate() const override
{
return "%1<fim▁begin>%2<fim▁hole>%3<fim▁end>";
}
QStringList stopWords() const override { return QStringList(); }
void prepareRequest(QJsonObject &request, const ContextData &context) const override
{
QString formattedPrompt = promptTemplate().arg(context.instriuctions,
context.prefix,
context.suffix);
request["prompt"] = formattedPrompt;
}
};
} // namespace QodeAssist::Templates

View File

@ -23,6 +23,8 @@
#include <QList>
#include <QString>
#include "QodeAssistData.hpp"
namespace QodeAssist::Templates {
class PromptTemplate
@ -32,9 +34,6 @@ public:
virtual QString name() const = 0;
virtual QString promptTemplate() const = 0;
virtual QStringList stopWords() const = 0;
virtual void prepareRequest(QJsonObject &request,
const QString &prefix,
const QString &suffix) const
= 0;
virtual void prepareRequest(QJsonObject &request, const ContextData &context) const = 0;
};
} // namespace QodeAssist::Templates

View File

@ -27,17 +27,17 @@ class StarCoder2Template : public PromptTemplate
{
public:
QString name() const override { return "StarCoder2"; }
QString promptTemplate() const override { return "<fim_prefix>%1<fim_suffix>%2<fim_middle>"; }
QString promptTemplate() const override { return "%1<fim_prefix>%2<fim_suffix>%3<fim_middle>"; }
QStringList stopWords() const override
{
return QStringList() << "<|endoftext|>" << "<file_sep>" << "<fim_prefix>" << "<fim_suffix>"
<< "<fim_middle>";
}
void prepareRequest(QJsonObject &request,
const QString &prefix,
const QString &suffix) const override
void prepareRequest(QJsonObject &request, const ContextData &context) const override
{
QString formattedPrompt = promptTemplate().arg(prefix, suffix);
QString formattedPrompt = promptTemplate().arg(context.instriuctions,
context.prefix,
context.suffix);
request["prompt"] = formattedPrompt;
}
};