From 227dbfd18b74372600db3f7caf2ccbe1a880d50e Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Tue, 13 Aug 2024 12:27:59 -0400 Subject: [PATCH] Use an enum for tool usage mode. Signed-off-by: Adam Treat --- gpt4all-chat/bravesearch.cpp | 12 +++--------- gpt4all-chat/bravesearch.h | 3 +-- gpt4all-chat/chatllm.cpp | 4 +++- gpt4all-chat/localdocssearch.h | 3 +-- gpt4all-chat/qml/ModelSettings.qml | 8 ++++---- gpt4all-chat/tool.h | 22 ++++++++++++---------- gpt4all-chat/toolmodel.h | 12 ++++-------- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/gpt4all-chat/bravesearch.cpp b/gpt4all-chat/bravesearch.cpp index 2b8c20ce..197ed383 100644 --- a/gpt4all-chat/bravesearch.cpp +++ b/gpt4all-chat/bravesearch.cpp @@ -80,16 +80,10 @@ QJsonObject BraveSearch::exampleParams() const return exampleDoc.object(); } -bool BraveSearch::isEnabled() const +ToolEnums::UsageMode BraveSearch::usageMode() const { - // FIXME: Refer to mysettings - return true; -} - -bool BraveSearch::forceUsage() const -{ - // FIXME: Refer to mysettings - return false; + // FIXME: This needs to be a setting + return ToolEnums::UsageMode::Enabled; } void BraveAPIWorker::request(const QString &apiKey, const QString &query, int count) diff --git a/gpt4all-chat/bravesearch.h b/gpt4all-chat/bravesearch.h index a1817412..45cd0a6e 100644 --- a/gpt4all-chat/bravesearch.h +++ b/gpt4all-chat/bravesearch.h @@ -53,9 +53,8 @@ public: QString function() const override { return "brave_search"; } QJsonObject paramSchema() const override; QJsonObject exampleParams() const override; - bool isEnabled() const override; bool isBuiltin() const override { return true; } - bool forceUsage() const override; + ToolEnums::UsageMode usageMode() const override; bool excerpts() const override { return true; } private: diff --git a/gpt4all-chat/chatllm.cpp b/gpt4all-chat/chatllm.cpp index 7d42dbf5..5a0e03ff 100644 --- a/gpt4all-chat/chatllm.cpp +++ b/gpt4all-chat/chatllm.cpp @@ -766,6 +766,8 @@ bool ChatLLM::promptInternal(const QList &collectionList, const QString if (!isModelLoaded()) return false; + // FIXME: This should be made agnostic to localdocs and rely upon the force usage usage mode + // and also we have to honor the ask before running mode. QList localDocsExcerpts; if (!collectionList.isEmpty() && !isToolCallResponse) { LocalDocsSearch localdocs; @@ -1345,7 +1347,7 @@ void ChatLLM::processSystemPrompt() int c = ToolModel::globalInstance()->count(); for (int i = 0; i < c; ++i) { Tool *t = ToolModel::globalInstance()->get(i); - if (t->isEnabled() && !t->forceUsage()) + if (t->usageMode() == ToolEnums::UsageMode::Enabled) toolList.push_back(t->jinjaValue()); } params.insert({"toolList", toolList}); diff --git a/gpt4all-chat/localdocssearch.h b/gpt4all-chat/localdocssearch.h index 23f64215..66c63414 100644 --- a/gpt4all-chat/localdocssearch.h +++ b/gpt4all-chat/localdocssearch.h @@ -38,9 +38,8 @@ public: QString description() const override { return tr("Search the local docs"); } QString function() const override { return "localdocs_search"; } QJsonObject paramSchema() const override; - bool isEnabled() const override { return true; } bool isBuiltin() const override { return true; } - bool forceUsage() const override { return true; } + ToolEnums::UsageMode usageMode() const override { return ToolEnums::UsageMode::ForceUsage; } bool excerpts() const override { return true; } private: diff --git a/gpt4all-chat/qml/ModelSettings.qml b/gpt4all-chat/qml/ModelSettings.qml index 99489349..d2f47976 100644 --- a/gpt4all-chat/qml/ModelSettings.qml +++ b/gpt4all-chat/qml/ModelSettings.qml @@ -174,21 +174,21 @@ MySettingsTab { MyTextArea { id: systemPromptArea anchors.fill: parent - text: root.currentModelInfo.systemPrompt + text: root.currentModelInfo.systemPromptTemplate Connections { target: MySettings function onSystemPromptChanged() { - systemPromptArea.text = root.currentModelInfo.systemPrompt; + systemPromptArea.text = root.currentModelInfo.systemPromptTemplate; } } Connections { target: root function onCurrentModelInfoChanged() { - systemPromptArea.text = root.currentModelInfo.systemPrompt; + systemPromptArea.text = root.currentModelInfo.systemPromptTemplate; } } onTextChanged: { - MySettings.setModelSystemPrompt(root.currentModelInfo, text) + MySettings.setModelSystemPromptTemplate(root.currentModelInfo, text) } Accessible.role: Accessible.EditableText } diff --git a/gpt4all-chat/tool.h b/gpt4all-chat/tool.h index 6728f587..92a3b758 100644 --- a/gpt4all-chat/tool.h +++ b/gpt4all-chat/tool.h @@ -15,6 +15,14 @@ namespace ToolEnums { UnknownError = 499, }; Q_ENUM_NS(Error) + + enum class UsageMode { + Disabled, // Completely disabled + Enabled, // Enabled and the model decides whether to run + AskBeforeRunning, // Enabled and model decides but the user is queried whether they want the tool to run in every instance + ForceUsage, // Attempt to force usage of the tool rather than let the LLM decide. NOTE: Not always possible. + }; + Q_ENUM_NS(UsageMode) } class Tool : public QObject { @@ -25,9 +33,8 @@ class Tool : public QObject { Q_PROPERTY(QJsonObject paramSchema READ paramSchema CONSTANT) Q_PROPERTY(QJsonObject exampleParams READ exampleParams CONSTANT) Q_PROPERTY(QUrl url READ url CONSTANT) - Q_PROPERTY(bool isEnabled READ isEnabled NOTIFY isEnabledChanged) Q_PROPERTY(bool isBuiltin READ isBuiltin CONSTANT) - Q_PROPERTY(bool forceUsage READ forceUsage NOTIFY forceUsageChanged) + Q_PROPERTY(ToolEnums::UsageMode usageMode READ usageMode NOTIFY usageModeChanged) Q_PROPERTY(bool excerpts READ excerpts CONSTANT) public: @@ -61,14 +68,11 @@ public: // [Optional] The local file or remote resource use to invoke the tool. virtual QUrl url() const { return QUrl(); } - // [Optional] Whether the tool is currently enabled - virtual bool isEnabled() const { return false; } - // [Optional] Whether the tool is built-in virtual bool isBuiltin() const { return false; } - // [Optional] Whether we should attempt to force usage of the tool rather than let the LLM decide. NOTE: Not always possible. - virtual bool forceUsage() const { return false; } + // [Optional] The current usage mode + virtual ToolEnums::UsageMode usageMode() const { return ToolEnums::UsageMode::Disabled; } // [Optional] Whether json result produces source excerpts. virtual bool excerpts() const { return false; } @@ -83,9 +87,7 @@ public: jinja2::Value jinjaValue() const; Q_SIGNALS: - void isEnabledChanged(); - void forceUsageChanged(); - + void usageModeChanged(); }; #endif // TOOL_H diff --git a/gpt4all-chat/toolmodel.h b/gpt4all-chat/toolmodel.h index d31bb1bc..f1b59922 100644 --- a/gpt4all-chat/toolmodel.h +++ b/gpt4all-chat/toolmodel.h @@ -21,9 +21,8 @@ public: UrlRole, ApiKeyRole, KeyRequiredRole, - IsEnabledRole, IsBuiltinRole, - ForceUsageRole, + UsageModeRole, ExcerptsRole, }; @@ -50,12 +49,10 @@ public: return item->paramSchema(); case UrlRole: return item->url(); - case IsEnabledRole: - return item->isEnabled(); case IsBuiltinRole: return item->isBuiltin(); - case ForceUsageRole: - return item->forceUsage(); + case UsageModeRole: + return QVariant::fromValue(item->usageMode()); case ExcerptsRole: return item->excerpts(); } @@ -73,9 +70,8 @@ public: roles[UrlRole] = "url"; roles[ApiKeyRole] = "apiKey"; roles[KeyRequiredRole] = "keyRequired"; - roles[IsEnabledRole] = "isEnabled"; roles[IsBuiltinRole] = "isBuiltin"; - roles[ForceUsageRole] = "forceUsage"; + roles[UsageModeRole] = "usageMode"; roles[ExcerptsRole] = "excerpts"; return roles; }