Add extended context

This commit is contained in:
Petr Mironychev 2024-08-28 10:10:31 +02:00
parent 989718017a
commit fc58c38f63
6 changed files with 58 additions and 10 deletions

View File

@ -19,7 +19,11 @@
#include "DocumentContextReader.hpp"
#include <QFileInfo>
#include <QTextBlock>
#include <languageserverprotocol/lsptypes.h>
#include "QodeAssistSettings.hpp"
namespace QodeAssist {
@ -123,4 +127,29 @@ QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosi
return content.trimmed();
}
QString DocumentContextReader::getLanguageAndFileInfo() const
{
if (!m_textDocument)
return QString();
QString language = LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(
m_textDocument->mimeType());
QString mimeType = m_textDocument->mimeType();
QString filePath = m_textDocument->filePath().toString();
QString fileExtension = QFileInfo(filePath).suffix();
return QString("//Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
.arg(language)
.arg(mimeType)
.arg(filePath)
.arg(fileExtension);
}
QString DocumentContextReader::getSpecificInstructions() const
{
QString specificInstruction = settings().specificInstractions().arg(
LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(m_textDocument->mimeType()));
return QString("//Instructions: %1").arg(specificInstruction);
}
} // namespace QodeAssist

View File

@ -20,14 +20,16 @@
#pragma once
#include <QTextDocument>
#include <texteditor/textdocument.h>
namespace QodeAssist {
class DocumentContextReader
{
public:
DocumentContextReader(QTextDocument *doc)
: m_document(doc)
DocumentContextReader(TextEditor::TextDocument *textDocument)
: m_textDocument(textDocument)
, m_document(textDocument->document())
{}
QString getLineText(int lineNumber, int cursorPosition = -1) const;
@ -35,8 +37,11 @@ public:
QString getContextAfter(int lineNumber, int cursorPosition, int linesCount) const;
QString readWholeFileBefore(int lineNumber, int cursorPosition) const;
QString readWholeFileAfter(int lineNumber, int cursorPosition) const;
QString getLanguageAndFileInfo() const;
QString getSpecificInstructions() const;
private:
TextEditor::TextDocument *m_textDocument;
QTextDocument *m_document;
};

View File

@ -98,8 +98,8 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
if (!widget)
return QString();
QTextDocument *doc = widget->document();
DocumentContextReader reader(doc);
DocumentContextReader reader(widget->textDocument());
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
QString contextBefore;
if (settings().readFullFile()) {
@ -110,7 +110,10 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
settings().readStringsBeforeCursor());
}
return contextBefore;
return QString("%1\n%2\n%3")
.arg(reader.getSpecificInstructions())
.arg(reader.getLanguageAndFileInfo())
.arg(contextBefore);
}
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
@ -120,8 +123,7 @@ QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
if (!widget)
return QString();
QTextDocument *doc = widget->document();
DocumentContextReader reader(doc);
DocumentContextReader reader(widget->textDocument());
QString contextAfter;
if (settings().readFullFile()) {

View File

@ -52,6 +52,7 @@ const char PROVIDER_PATHS[] = "QodeAssist.providerPaths";
const char START_SUGGESTION_TIMER[] = "QodeAssist.startSuggestionTimer";
const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold";
const char OLLAMA_LIVETIME[] = "QodeAssist.ollamaLivetime";
const char SPECIFIC_INSTRUCTIONS[] = "QodeAssist.specificInstractions";
const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions";
const char QODE_ASSIST_GENERAL_OPTIONS_CATEGORY[] = "QodeAssist.Category";

View File

@ -97,7 +97,7 @@ QodeAssistSettings::QodeAssistSettings()
readFullFile.setSettingsKey(Constants::READ_FULL_FILE);
readFullFile.setLabelText(Tr::tr("Read Full File"));
readFullFile.setDefaultValue(true);
readFullFile.setDefaultValue(false);
maxFileThreshold.setSettingsKey(Constants::MAX_FILE_THRESHOLD);
maxFileThreshold.setLabelText(Tr::tr("Max File Threshold:"));
@ -106,11 +106,11 @@ QodeAssistSettings::QodeAssistSettings()
readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR);
readStringsBeforeCursor.setLabelText(Tr::tr("Read Strings Before Cursor"));
readStringsBeforeCursor.setDefaultValue(60);
readStringsBeforeCursor.setDefaultValue(50);
readStringsAfterCursor.setSettingsKey(Constants::READ_STRINGS_AFTER_CURSOR);
readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor"));
readStringsAfterCursor.setDefaultValue(40);
readStringsAfterCursor.setDefaultValue(30);
maxTokens.setSettingsKey(Constants::MAX_TOKENS);
maxTokens.setLabelText(Tr::tr("Max Tokens"));
@ -157,6 +157,14 @@ QodeAssistSettings::QodeAssistSettings()
startSuggestionTimer.setRange(10, 10000);
startSuggestionTimer.setDefaultValue(500);
specificInstractions.setSettingsKey(Constants::SPECIFIC_INSTRUCTIONS);
specificInstractions.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
specificInstractions.setLabelText(
Tr::tr("Instructions: Please keep %1 for languge name, warning, it shouldn't too big"));
specificInstractions.setDefaultValue(
"You are an expert %1 code completion AI."
"CRITICAL: Please provide minimal the best possible code completion suggestions.\n");
resetToDefaults.m_buttonText = Tr::tr("Reset to Defaults");
const auto &manager = LLMProvidersManager::instance();
@ -206,6 +214,7 @@ QodeAssistSettings::QodeAssistSettings()
readFullFile,
maxFileThreshold,
ollamaLivetime,
specificInstractions,
temperature,
maxTokens,
readStringsBeforeCursor,
@ -349,6 +358,7 @@ void QodeAssistSettings::resetSettingsToDefaults()
resetAspect(startSuggestionTimer);
resetAspect(enableLogging);
resetAspect(ollamaLivetime);
resetAspect(specificInstractions);
updateProviderSettings();
apply();

View File

@ -95,6 +95,7 @@ public:
Utils::IntegerAspect maxFileThreshold{this};
Utils::StringAspect ollamaLivetime{this};
Utils::StringAspect specificInstractions{this};
ButtonAspect resetToDefaults{this};