Use an enum for tool usage mode.

Signed-off-by: Adam Treat <treat.adam@gmail.com>
This commit is contained in:
Adam Treat 2024-08-13 12:27:59 -04:00
parent 587dd55b73
commit 227dbfd18b
7 changed files with 28 additions and 36 deletions

View File

@ -80,16 +80,10 @@ QJsonObject BraveSearch::exampleParams() const
return exampleDoc.object(); return exampleDoc.object();
} }
bool BraveSearch::isEnabled() const ToolEnums::UsageMode BraveSearch::usageMode() const
{ {
// FIXME: Refer to mysettings // FIXME: This needs to be a setting
return true; return ToolEnums::UsageMode::Enabled;
}
bool BraveSearch::forceUsage() const
{
// FIXME: Refer to mysettings
return false;
} }
void BraveAPIWorker::request(const QString &apiKey, const QString &query, int count) void BraveAPIWorker::request(const QString &apiKey, const QString &query, int count)

View File

@ -53,9 +53,8 @@ public:
QString function() const override { return "brave_search"; } QString function() const override { return "brave_search"; }
QJsonObject paramSchema() const override; QJsonObject paramSchema() const override;
QJsonObject exampleParams() const override; QJsonObject exampleParams() const override;
bool isEnabled() const override;
bool isBuiltin() const override { return true; } bool isBuiltin() const override { return true; }
bool forceUsage() const override; ToolEnums::UsageMode usageMode() const override;
bool excerpts() const override { return true; } bool excerpts() const override { return true; }
private: private:

View File

@ -766,6 +766,8 @@ bool ChatLLM::promptInternal(const QList<QString> &collectionList, const QString
if (!isModelLoaded()) if (!isModelLoaded())
return false; 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<SourceExcerpt> localDocsExcerpts; QList<SourceExcerpt> localDocsExcerpts;
if (!collectionList.isEmpty() && !isToolCallResponse) { if (!collectionList.isEmpty() && !isToolCallResponse) {
LocalDocsSearch localdocs; LocalDocsSearch localdocs;
@ -1345,7 +1347,7 @@ void ChatLLM::processSystemPrompt()
int c = ToolModel::globalInstance()->count(); int c = ToolModel::globalInstance()->count();
for (int i = 0; i < c; ++i) { for (int i = 0; i < c; ++i) {
Tool *t = ToolModel::globalInstance()->get(i); Tool *t = ToolModel::globalInstance()->get(i);
if (t->isEnabled() && !t->forceUsage()) if (t->usageMode() == ToolEnums::UsageMode::Enabled)
toolList.push_back(t->jinjaValue()); toolList.push_back(t->jinjaValue());
} }
params.insert({"toolList", toolList}); params.insert({"toolList", toolList});

View File

@ -38,9 +38,8 @@ public:
QString description() const override { return tr("Search the local docs"); } QString description() const override { return tr("Search the local docs"); }
QString function() const override { return "localdocs_search"; } QString function() const override { return "localdocs_search"; }
QJsonObject paramSchema() const override; QJsonObject paramSchema() const override;
bool isEnabled() const override { return true; }
bool isBuiltin() 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; } bool excerpts() const override { return true; }
private: private:

View File

@ -174,21 +174,21 @@ MySettingsTab {
MyTextArea { MyTextArea {
id: systemPromptArea id: systemPromptArea
anchors.fill: parent anchors.fill: parent
text: root.currentModelInfo.systemPrompt text: root.currentModelInfo.systemPromptTemplate
Connections { Connections {
target: MySettings target: MySettings
function onSystemPromptChanged() { function onSystemPromptChanged() {
systemPromptArea.text = root.currentModelInfo.systemPrompt; systemPromptArea.text = root.currentModelInfo.systemPromptTemplate;
} }
} }
Connections { Connections {
target: root target: root
function onCurrentModelInfoChanged() { function onCurrentModelInfoChanged() {
systemPromptArea.text = root.currentModelInfo.systemPrompt; systemPromptArea.text = root.currentModelInfo.systemPromptTemplate;
} }
} }
onTextChanged: { onTextChanged: {
MySettings.setModelSystemPrompt(root.currentModelInfo, text) MySettings.setModelSystemPromptTemplate(root.currentModelInfo, text)
} }
Accessible.role: Accessible.EditableText Accessible.role: Accessible.EditableText
} }

View File

@ -15,6 +15,14 @@ namespace ToolEnums {
UnknownError = 499, UnknownError = 499,
}; };
Q_ENUM_NS(Error) 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 { class Tool : public QObject {
@ -25,9 +33,8 @@ class Tool : public QObject {
Q_PROPERTY(QJsonObject paramSchema READ paramSchema CONSTANT) Q_PROPERTY(QJsonObject paramSchema READ paramSchema CONSTANT)
Q_PROPERTY(QJsonObject exampleParams READ exampleParams CONSTANT) Q_PROPERTY(QJsonObject exampleParams READ exampleParams CONSTANT)
Q_PROPERTY(QUrl url READ url 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 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) Q_PROPERTY(bool excerpts READ excerpts CONSTANT)
public: public:
@ -61,14 +68,11 @@ public:
// [Optional] The local file or remote resource use to invoke the tool. // [Optional] The local file or remote resource use to invoke the tool.
virtual QUrl url() const { return QUrl(); } 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 // [Optional] Whether the tool is built-in
virtual bool isBuiltin() const { return false; } 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. // [Optional] The current usage mode
virtual bool forceUsage() const { return false; } virtual ToolEnums::UsageMode usageMode() const { return ToolEnums::UsageMode::Disabled; }
// [Optional] Whether json result produces source excerpts. // [Optional] Whether json result produces source excerpts.
virtual bool excerpts() const { return false; } virtual bool excerpts() const { return false; }
@ -83,9 +87,7 @@ public:
jinja2::Value jinjaValue() const; jinja2::Value jinjaValue() const;
Q_SIGNALS: Q_SIGNALS:
void isEnabledChanged(); void usageModeChanged();
void forceUsageChanged();
}; };
#endif // TOOL_H #endif // TOOL_H

View File

@ -21,9 +21,8 @@ public:
UrlRole, UrlRole,
ApiKeyRole, ApiKeyRole,
KeyRequiredRole, KeyRequiredRole,
IsEnabledRole,
IsBuiltinRole, IsBuiltinRole,
ForceUsageRole, UsageModeRole,
ExcerptsRole, ExcerptsRole,
}; };
@ -50,12 +49,10 @@ public:
return item->paramSchema(); return item->paramSchema();
case UrlRole: case UrlRole:
return item->url(); return item->url();
case IsEnabledRole:
return item->isEnabled();
case IsBuiltinRole: case IsBuiltinRole:
return item->isBuiltin(); return item->isBuiltin();
case ForceUsageRole: case UsageModeRole:
return item->forceUsage(); return QVariant::fromValue(item->usageMode());
case ExcerptsRole: case ExcerptsRole:
return item->excerpts(); return item->excerpts();
} }
@ -73,9 +70,8 @@ public:
roles[UrlRole] = "url"; roles[UrlRole] = "url";
roles[ApiKeyRole] = "apiKey"; roles[ApiKeyRole] = "apiKey";
roles[KeyRequiredRole] = "keyRequired"; roles[KeyRequiredRole] = "keyRequired";
roles[IsEnabledRole] = "isEnabled";
roles[IsBuiltinRole] = "isBuiltin"; roles[IsBuiltinRole] = "isBuiltin";
roles[ForceUsageRole] = "forceUsage"; roles[UsageModeRole] = "usageMode";
roles[ExcerptsRole] = "excerpts"; roles[ExcerptsRole] = "excerpts";
return roles; return roles;
} }