Skip to content

Commit 991afc6

Browse files
committed
Abstract the built-in web search completely away from ChatLLM.
Signed-off-by: Adam Treat <[email protected]>
1 parent 75dbf9d commit 991afc6

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

gpt4all-chat/bravesearch.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "bravesearch.h"
2+
#include "mysettings.h"
23

34
#include <QCoreApplication>
45
#include <QDebug>
@@ -18,9 +19,9 @@ using namespace Qt::Literals::StringLiterals;
1819

1920
QString BraveSearch::run(const QJsonObject &parameters, qint64 timeout)
2021
{
21-
const QString apiKey = parameters["apiKey"].toString();
22+
const QString apiKey = MySettings::globalInstance()->braveSearchAPIKey();
2223
const QString query = parameters["query"].toString();
23-
const int count = parameters["count"].toInt();
24+
const int count = 2; // FIXME: This should be a setting
2425
QThread workerThread;
2526
BraveAPIWorker worker;
2627
worker.moveToThread(&workerThread);

gpt4all-chat/bravesearch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private Q_SLOTS:
3434
private:
3535
QNetworkAccessManager *m_networkManager;
3636
QString m_response;
37-
ToolEnums::Error m_error;
37+
ToolEnums::Error m_error = ToolEnums::Error::NoError;
3838
QString m_errorString;
3939
};
4040

gpt4all-chat/chatllm.cpp

+22-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "chatllm.h"
22

3-
#include "bravesearch.h"
43
#include "chat.h"
54
#include "chatapi.h"
65
#include "localdocssearch.h"
@@ -893,36 +892,31 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
893892
const QString tool = toolCallDoc["name"].toString();
894893
const QJsonObject args = toolCallDoc["parameters"].toObject();
895894

896-
// FIXME: In the future this will try to match the tool call to a list of tools that are supported
897-
// according to MySettings, but for now only brave search is supported
898-
if (tool != "web_search" || !args.contains("query")) {
899-
// FIXME: Need to surface errors to the UI
900-
qWarning() << "ERROR: Could not find the tool and correct parameters for " << toolCall;
895+
Tool *toolInstance = ToolModel::globalInstance()->get(tool);
896+
if (!toolInstance) {
897+
qWarning() << "ERROR: Could not find the tool for " << toolCall;
901898
return handleFailedToolCall(trimmed, totalTime);
902899
}
903900

904-
const QString query = args["query"].toString();
901+
// Inform the chat that we're executing a tool call
902+
emit toolCalled(toolInstance->name().toLower());
905903

906-
emit toolCalled(tr("searching web..."));
907-
const QString apiKey = MySettings::globalInstance()->braveSearchAPIKey();
908-
Q_ASSERT(apiKey != "");
909-
BraveSearch brave;
910-
911-
QJsonObject parameters;
912-
parameters.insert("apiKey", apiKey);
913-
parameters.insert("query", query);
914-
parameters.insert("count", 2);
915-
916-
// FIXME: Need to surface errors to the UI
917-
const QString braveResponse = brave.run(parameters, 2000 /*msecs to timeout*/);
904+
const QString response = toolInstance->run(args, 2000 /*msecs to timeout*/);
905+
if (toolInstance->error() != ToolEnums::Error::NoError) {
906+
qWarning() << "ERROR: Tool call produced error:" << toolInstance->errorString();
907+
return handleFailedToolCall(trimmed, totalTime);
908+
}
918909

919-
QString parseError;
920-
QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson(braveResponse, parseError);
921-
if (!parseError.isEmpty()) {
922-
qWarning() << "ERROR: Could not parse source excerpts for brave response:" << parseError;
923-
} else if (!sourceExcerpts.isEmpty()) {
924-
producedSourceExcerpts = true;
925-
emit sourceExcerptsChanged(sourceExcerpts);
910+
// If the tool supports excerpts then try to parse them here
911+
if (toolInstance->excerpts()) {
912+
QString parseError;
913+
QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson(response, parseError);
914+
if (!parseError.isEmpty()) {
915+
qWarning() << "ERROR: Could not parse source excerpts for response:" << parseError;
916+
} else if (!sourceExcerpts.isEmpty()) {
917+
producedSourceExcerpts = true;
918+
emit sourceExcerptsChanged(sourceExcerpts);
919+
}
926920
}
927921

928922
m_promptResponseTokens = 0;
@@ -931,7 +925,7 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
931925

932926
// This is a recursive call but isRecursiveCall is checked above to arrest infinite recursive
933927
// tool calls
934-
return promptRecursive(QList<QString>()/*collectionList*/, braveResponse, toolTemplate,
928+
return promptRecursive(QList<QString>()/*tool context*/, response, toolTemplate,
935929
n_predict, top_k, top_p, min_p, temp, n_batch, repeat_penalty, repeat_penalty_tokens, totalTime,
936930
producedSourceExcerpts, true /*isRecursiveCall*/);
937931
} else {
@@ -946,6 +940,7 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
946940

947941
bool ChatLLM::handleFailedToolCall(const std::string &response, qint64 elapsed)
948942
{
943+
// FIXME: Need to surface errors to the UI
949944
// Restore the strings that we excluded previously when detecting the tool call
950945
m_response = "<tool_call>" + response + "</tool_call>";
951946
emit responseChanged(QString::fromStdString(m_response));

gpt4all-chat/qml/ChatView.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,8 @@ Rectangle {
881881
case Chat.PromptProcessing: return qsTr("processing ...")
882882
case Chat.ResponseGeneration: return qsTr("generating response ...");
883883
case Chat.GeneratingQuestions: return qsTr("generating questions ...");
884-
case Chat.ToolCalled: return currentChat.toolDescription;
885-
case Chat.ToolProcessing: return qsTr("processing web results ..."); // FIXME should not be hardcoded!
884+
case Chat.ToolCalled: return qsTr("executing %1 ...").arg(currentChat.toolDescription);
885+
case Chat.ToolProcessing: return qsTr("processing %1 results ...").arg(currentChat.toolDescription);
886886
default: return ""; // handle unexpected values
887887
}
888888
}

0 commit comments

Comments
 (0)