Skip to content

Commit

Permalink
Add max message and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Oct 11, 2024
1 parent 47f192a commit dfcaf64
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 23 deletions.
35 changes: 34 additions & 1 deletion chatview/ChatModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "ChatModel.hpp"
#include <QtCore/qjsonobject.h>
#include <QtQml>
#include <utils/aspects.h>

#include "GeneralSettings.hpp"
#include "PromptTemplateManager.hpp"

namespace QodeAssist::Chat {

Expand All @@ -28,6 +32,17 @@ ChatModel::ChatModel(QObject *parent)
, m_totalTokens(0)
{
m_systemPrompt = "You are a helpful C++ and QML programming assistant.";
auto &settings = Settings::generalSettings();

connect(&settings.chatMessagesThreshold,
&Utils::BaseAspect::changed,
this,
&ChatModel::historyThresholdChanged);

connect(&settings.chatTokensThreshold,
&Utils::BaseAspect::changed,
this,
&ChatModel::tokensThresholdChanged);
}

int ChatModel::rowCount(const QModelIndex &parent) const
Expand Down Expand Up @@ -76,7 +91,7 @@ void ChatModel::setSystemPrompt(const QString &prompt)

void ChatModel::trim()
{
while (m_messages.size() > MAX_HISTORY_SIZE || m_totalTokens > MAX_TOKENS) {
while (m_messages.size() > historyThreshold() || m_totalTokens > tokensThreshold()) {
if (!m_messages.isEmpty()) {
m_totalTokens -= m_messages.first().tokenCount;
beginRemoveRows(QModelIndex(), 0, 0);
Expand All @@ -102,6 +117,7 @@ void ChatModel::addMessage(const QString &content, ChatRole role)
endInsertRows();
trim();
emit totalTokensChanged();
emit msgCountChanged();
}

void ChatModel::clear()
Expand Down Expand Up @@ -141,4 +157,21 @@ int ChatModel::totalTokens() const
return m_totalTokens;
}

int ChatModel::historyThreshold() const
{
auto &settings = Settings::generalSettings();
return settings.chatMessagesThreshold();
}

int ChatModel::tokensThreshold() const
{
auto &settings = Settings::generalSettings();
return settings.chatTokensThreshold();
}

int ChatModel::msgCount() const
{
return m_messages.size();
}

} // namespace QodeAssist::Chat
17 changes: 15 additions & 2 deletions chatview/ChatModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ChatModel : public QAbstractListModel
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(int totalTokens READ totalTokens NOTIFY totalTokensChanged FINAL)
Q_PROPERTY(int tokensThreshold READ tokensThreshold NOTIFY tokensThresholdChanged FINAL)
Q_PROPERTY(int msgCount READ msgCount NOTIFY msgCountChanged FINAL)
Q_PROPERTY(int historyThreshold READ historyThreshold NOTIFY historyThresholdChanged FINAL)

public:
enum Roles { RoleType = Qt::UserRole, Content };

Expand Down Expand Up @@ -59,8 +63,19 @@ class ChatModel : public QAbstractListModel

int totalTokens() const;

int historyThreshold() const;

int tokensThreshold() const;

int msgCount() const;

QString currentModel() const;

signals:
void totalTokensChanged();
void historyThresholdChanged();
void tokensThresholdChanged();
void msgCountChanged();

private:
void trim();
Expand All @@ -69,8 +84,6 @@ class ChatModel : public QAbstractListModel
QVector<Message> m_messages;
QString m_systemPrompt;
int m_totalTokens = 0;
static const int MAX_HISTORY_SIZE = 50;
static const int MAX_TOKENS = 4000;
};

} // namespace QodeAssist::Chat
24 changes: 20 additions & 4 deletions chatview/qml/RootItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ ChatRootView {
}

Rectangle {
width: tokensCounter.implicitWidth + 4
anchors {
left: parent.left
leftMargin: 10
}
width: bar.implicitWidth + 4
height: childrenRect.height
color: "lightgreen"
radius: 4

Text {
id: tokensCounter
Row {
id: bar

anchors.horizontalCenter: parent.horizontalCenter
text: root.chatModel.totalTokens
spacing: 10

Text {
id: messagesCounter

text: "%1/%2".arg(root.chatModel.msgCount).arg(root.chatModel.historyThreshold)
}

Text {
id: tokensCounter

text: "%1/%2".arg(root.chatModel.totalTokens).arg(root.chatModel.tokensThreshold)
}
}
}

Expand Down
6 changes: 1 addition & 5 deletions settings/ContextSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ ContextSettings::ContextSettings()

specificInstractions.setSettingsKey(Constants::SYSTEM_PROMPT);
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");
specificInstractions.setDefaultValue("You are an expert in C++ Qt QML code completion AI.\n");

resetToDefaults.m_buttonText = Tr::tr("Reset Page to Defaults");

Expand Down
37 changes: 26 additions & 11 deletions settings/GeneralSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ GeneralSettings::GeneralSettings()
chatPrompts.setSettingsKey(Constants::CHAT_PROMPTS);
chatPrompts.setDisplayStyle(Utils::SelectionAspect::DisplayStyle::ComboBox);

chatTokensThreshold.setSettingsKey(Constants::CHAT_TOKENS_THRESHOLD);
chatTokensThreshold.setLabelText(Tr::tr("Chat History Token Limit"));
chatTokensThreshold.setToolTip(Tr::tr("Maximum number of tokens in chat history. When "
"exceeded, oldest messages will be removed."));
chatTokensThreshold.setRange(1000, 16000);
chatTokensThreshold.setDefaultValue(4000);

chatMessagesThreshold.setSettingsKey(Constants::CHAT_MESSAGES_THRESHOLD);
chatMessagesThreshold.setLabelText(Tr::tr("Chat History Message Limit"));
chatMessagesThreshold.setToolTip(Tr::tr("Maximum number of messages in chat history. When "
"exceeded, oldest messages will be removed."));
chatMessagesThreshold.setRange(10, 10000);
chatMessagesThreshold.setDefaultValue(50);

loadProviders();
loadPrompts();

Expand Down Expand Up @@ -158,26 +172,27 @@ GeneralSettings::GeneralSettings()

auto rootLayout
= Column{Row{enableQodeAssist, Stretch{1}, resetToDefaults},
enableAutoComplete,
multiLineCompletion,
Row{autoCompletionCharThreshold,
autoCompletionTypingInterval,
startSuggestionTimer,
Stretch{1}},
Space{8},
enableLogging,
Row{enableLogging, Stretch{1}},
Space{8},
Group{title(Tr::tr("AI Suggestions")),
Column{Row{llmProviders, Stretch{1}},
Column{enableAutoComplete,
multiLineCompletion,
Row{autoCompletionCharThreshold,
autoCompletionTypingInterval,
startSuggestionTimer,
Stretch{1}},
Row{llmProviders, Stretch{1}},
Row{url, endPoint, fimUrlIndicator},
Row{selectModels, modelName, fimModelIndicator},
Row{fimPrompts, Stretch{1}}}},
Space{16},
Group{title(Tr::tr("AI Chat(experimental)")),
Group{title(Tr::tr("AI Chat")),
Column{Row{chatLlmProviders, Stretch{1}},
Row{chatUrl, chatEndPoint, chatUrlIndicator},
Row{chatSelectModels, chatModelName, chatModelIndicator},
Row{chatPrompts, Stretch{1}}}},
Row{chatPrompts, Stretch{1}},
Row{chatTokensThreshold, Stretch{1}},
Row{chatMessagesThreshold, Stretch{1}}}},
Stretch{1}};
return rootLayout;
});
Expand Down
3 changes: 3 additions & 0 deletions settings/GeneralSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class GeneralSettings : public Utils::AspectContainer
Utils::StringAspect chatModelIndicator{this};
Utils::StringAspect chatUrlIndicator{this};

Utils::IntegerAspect chatTokensThreshold{this};
Utils::IntegerAspect chatMessagesThreshold{this};

private:
void setupConnections();
void showModelSelectionDialog(Utils::StringAspect *modelNameObj, LLMCore::Provider *provider);
Expand Down
2 changes: 2 additions & 0 deletions settings/SettingsConstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ 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 CHAT_TOKENS_THRESHOLD[] = "QodeAssist.chatTokensThreshold";
const char CHAT_MESSAGES_THRESHOLD[] = "QodeAssist.chatMessagesThreshold";

const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions";
const char QODE_ASSIST_GENERAL_SETTINGS_PAGE_ID[] = "QodeAssist.1GeneralSettingsPageId";
Expand Down

0 comments on commit dfcaf64

Please sign in to comment.