Skip to content

Commit

Permalink
Add chat components
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Sep 22, 2024
1 parent 36f29ca commit fe3db00
Show file tree
Hide file tree
Showing 31 changed files with 596 additions and 225 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ add_qtc_plugin(QodeAssist
LLMClientInterface.hpp LLMClientInterface.cpp
PromptTemplateManager.hpp PromptTemplateManager.cpp
templates/PromptTemplate.hpp
templates/CodeLLamaTemplate.hpp
templates/CodeLlamaFimTemplate.hpp
templates/StarCoder2Template.hpp
templates/DeepSeekCoderV2.hpp
templates/CustomTemplate.hpp
templates/DeepSeekCoderChatTemplate.hpp
templates/CodeLlamaInstruct.hpp
providers/LLMProvider.hpp
providers/OllamaProvider.hpp providers/OllamaProvider.cpp
providers/LMStudioProvider.hpp providers/LMStudioProvider.cpp
Expand Down
9 changes: 6 additions & 3 deletions LLMClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,16 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
auto updatedContext = prepareContext(request);

LLMConfig config;
config.provider = LLMProvidersManager::instance().getCurrentProvider();
config.promptTemplate = PromptTemplateManager::instance().getCurrentTemplate();
config.requestType = RequestType::Fim;
config.provider = LLMProvidersManager::instance().getCurrentFimProvider();
config.promptTemplate = PromptTemplateManager::instance().getCurrentFimTemplate();
config.url = QUrl(QString("%1%2").arg(Settings::generalSettings().url(),
Settings::generalSettings().endPoint()));

config.providerRequest = {{"model", Settings::generalSettings().modelName.value()},
{"stream", true}};
{"stream", true},
{"stop",
QJsonArray::fromStringList(config.promptTemplate->stopWords())}};

config.promptTemplate->prepareRequest(config.providerRequest, updatedContext);
config.provider->prepareRequest(config.providerRequest);
Expand Down
46 changes: 38 additions & 8 deletions LLMProvidersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "LLMProvidersManager.hpp"

#include "QodeAssistUtils.hpp"

namespace QodeAssist {

LLMProvidersManager &LLMProvidersManager::instance()
Expand All @@ -27,25 +29,53 @@ LLMProvidersManager &LLMProvidersManager::instance()
return instance;
}

QStringList LLMProvidersManager::getProviderNames() const
Providers::LLMProvider *LLMProvidersManager::setCurrentFimProvider(const QString &name)
{
return m_providers.keys();
logMessage("Setting current FIM provider to: " + name);
if (!m_providers.contains(name)) {
logMessage("Can't find provider with name: " + name);
return nullptr;
}

m_currentFimProvider = m_providers[name];
return m_currentFimProvider;
}

void LLMProvidersManager::setCurrentProvider(const QString &name)
Providers::LLMProvider *LLMProvidersManager::setCurrentChatProvider(const QString &name)
{
if (m_providers.contains(name)) {
m_currentProviderName = name;
logMessage("Setting current chat provider to: " + name);
if (!m_providers.contains(name)) {
logMessage("Can't find chat provider with name: " + name);
return nullptr;
}

m_currentChatProvider = m_providers[name];
return m_currentChatProvider;
}

Providers::LLMProvider *LLMProvidersManager::getCurrentProvider()
Providers::LLMProvider *LLMProvidersManager::getCurrentFimProvider()
{
if (m_currentProviderName.isEmpty()) {
if (m_currentFimProvider == nullptr) {
logMessage("Current fim provider is null");
return nullptr;
}

return m_providers[m_currentProviderName];
return m_currentFimProvider;
}

Providers::LLMProvider *LLMProvidersManager::getCurrentChatProvider()
{
if (m_currentChatProvider == nullptr) {
logMessage("Current chat provider is null");
return nullptr;
}

return m_currentChatProvider;
}

QStringList LLMProvidersManager::providersNames() const
{
return m_providers.keys();
}

LLMProvidersManager::~LLMProvidersManager()
Expand Down
14 changes: 9 additions & 5 deletions LLMProvidersManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LLMProvidersManager
{
public:
static LLMProvidersManager &instance();
~LLMProvidersManager();

template<typename T>
void registerProvider()
Expand All @@ -40,19 +41,22 @@ class LLMProvidersManager
m_providers[name] = provider;
}

QStringList getProviderNames() const;
void setCurrentProvider(const QString &name);
Providers::LLMProvider *getCurrentProvider();
Providers::LLMProvider *setCurrentFimProvider(const QString &name);
Providers::LLMProvider *setCurrentChatProvider(const QString &name);

~LLMProvidersManager();
Providers::LLMProvider *getCurrentFimProvider();
Providers::LLMProvider *getCurrentChatProvider();

QStringList providersNames() const;

private:
LLMProvidersManager() = default;
LLMProvidersManager(const LLMProvidersManager &) = delete;
LLMProvidersManager &operator=(const LLMProvidersManager &) = delete;

QMap<QString, Providers::LLMProvider *> m_providers;
QString m_currentProviderName;
Providers::LLMProvider *m_currentFimProvider = nullptr;
Providers::LLMProvider *m_currentChatProvider = nullptr;
};

} // namespace QodeAssist
53 changes: 44 additions & 9 deletions PromptTemplateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "PromptTemplateManager.hpp"

#include "QodeAssistUtils.hpp"

namespace QodeAssist {

PromptTemplateManager &PromptTemplateManager::instance()
Expand All @@ -27,27 +29,60 @@ PromptTemplateManager &PromptTemplateManager::instance()
return instance;
}

void PromptTemplateManager::setCurrentTemplate(const QString &name)
void PromptTemplateManager::setCurrentFimTemplate(const QString &name)
{
logMessage("Setting current FIM provider to: " + name);
if (!m_fimTemplates.contains(name) || m_fimTemplates[name] == nullptr) {
logMessage("Error to set current FIM template" + name);
return;
}

m_currentFimTemplate = m_fimTemplates[name];
}

Templates::PromptTemplate *PromptTemplateManager::getCurrentFimTemplate()
{
if (m_currentFimTemplate == nullptr) {
logMessage("Current fim provider is null");
return nullptr;
}

return m_currentFimTemplate;
}

void PromptTemplateManager::setCurrentChatTemplate(const QString &name)
{
if (m_templates.contains(name)) {
m_currentTemplateName = name;
logMessage("Setting current chat provider to: " + name);
if (!m_chatTemplates.contains(name) || m_chatTemplates[name] == nullptr) {
logMessage("Error to set current chat template" + name);
return;
}

m_currentChatTemplate = m_chatTemplates[name];
}

Templates::PromptTemplate *PromptTemplateManager::getCurrentChatTemplate()
{
if (m_currentChatTemplate == nullptr)
logMessage("Current chat provider is null");

return m_currentChatTemplate;
}

Templates::PromptTemplate *PromptTemplateManager::getCurrentTemplate()
QStringList PromptTemplateManager::fimTemplatesNames() const
{
auto it = m_templates.find(m_currentTemplateName);
return it != m_templates.end() ? it.value() : nullptr;
return m_fimTemplates.keys();
}

QStringList PromptTemplateManager::getTemplateNames() const
QStringList PromptTemplateManager::chatTemplatesNames() const
{
return m_templates.keys();
return m_chatTemplates.keys();
}

PromptTemplateManager::~PromptTemplateManager()
{
qDeleteAll(m_templates);
qDeleteAll(m_fimTemplates);
qDeleteAll(m_chatTemplates);
}

} // namespace QodeAssist
24 changes: 17 additions & 7 deletions PromptTemplateManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PromptTemplateManager
{
public:
static PromptTemplateManager &instance();
~PromptTemplateManager();

template<typename T>
void registerTemplate()
Expand All @@ -38,22 +39,31 @@ class PromptTemplateManager
"T must inherit from PromptTemplate");
T *template_ptr = new T();
QString name = template_ptr->name();
m_templates[name] = template_ptr;
if (template_ptr->type() == Templates::TemplateType::Fim) {
m_fimTemplates[name] = template_ptr;
} else if (template_ptr->type() == Templates::TemplateType::Chat) {
m_chatTemplates[name] = template_ptr;
}
}

void setCurrentTemplate(const QString &name);
Templates::PromptTemplate *getCurrentTemplate();
QStringList getTemplateNames() const;
void setCurrentFimTemplate(const QString &name);
Templates::PromptTemplate *getCurrentFimTemplate();

~PromptTemplateManager();
void setCurrentChatTemplate(const QString &name);
Templates::PromptTemplate *getCurrentChatTemplate();

QStringList fimTemplatesNames() const;
QStringList chatTemplatesNames() const;

private:
PromptTemplateManager() = default;
PromptTemplateManager(const PromptTemplateManager &) = delete;
PromptTemplateManager &operator=(const PromptTemplateManager &) = delete;

QMap<QString, Templates::PromptTemplate *> m_templates;
QString m_currentTemplateName;
QMap<QString, Templates::PromptTemplate *> m_fimTemplates;
QMap<QString, Templates::PromptTemplate *> m_chatTemplates;
Templates::PromptTemplate *m_currentFimTemplate;
Templates::PromptTemplate *m_currentChatTemplate;
};

} // namespace QodeAssist
6 changes: 6 additions & 0 deletions QodeAssistConstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ const char USE_FILE_PATH_IN_CONTEXT[] = "QodeAssist.useFilePathInContext";
const char CUSTOM_JSON_TEMPLATE[] = "QodeAssist.customJsonTemplate";
const char USE_PROJECT_CHANGES_CACHE[] = "QodeAssist.useProjectChangesCache";
const char MAX_CHANGES_CACHE_SIZE[] = "QodeAssist.maxChangesCacheSize";
const char CHAT_LLM_PROVIDERS[] = "QodeAssist.chatLlmProviders";
const char CHAT_URL[] = "QodeAssist.chatUrl";
const char CHAT_END_POINT[] = "QodeAssist.chatEndPoint";
const char CHAT_MODEL_NAME[] = "QodeAssist.chatModelName";
const char CHAT_SELECT_MODELS[] = "QodeAssist.chatSelectModels";
const char CHAT_PROMPTS[] = "QodeAssist.chatPrompts";

const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions";
const char QODE_ASSIST_GENERAL_SETTINGS_PAGE_ID[] = "QodeAssist.1GeneralSettingsPageId";
Expand Down
3 changes: 3 additions & 0 deletions QodeAssistUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ inline void logMessage(const QString &message, bool silent = true)
return;

const QString prefixedMessage = QLatin1String("[Qode Assist] ") + message;
qDebug() << prefixedMessage;
if (silent) {
Core::MessageManager::writeSilently(prefixedMessage);
} else {
Expand All @@ -60,6 +61,8 @@ inline void logMessages(const QStringList &messages, bool silent = true)
return;

QStringList prefixedMessages;
qDebug() << prefixedMessages;

for (const QString &message : messages) {
prefixedMessages << (QLatin1String("[Qode Assist] ") + message);
}
Expand Down
Loading

0 comments on commit fe3db00

Please sign in to comment.