Skip to content

Commit

Permalink
Abstract the built-in web search completely away from ChatLLM.
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Treat <[email protected]>
  • Loading branch information
manyoso committed Aug 14, 2024
1 parent 75dbf9d commit 991afc6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
5 changes: 3 additions & 2 deletions gpt4all-chat/bravesearch.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "bravesearch.h"
#include "mysettings.h"

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

QString BraveSearch::run(const QJsonObject &parameters, qint64 timeout)
{
const QString apiKey = parameters["apiKey"].toString();
const QString apiKey = MySettings::globalInstance()->braveSearchAPIKey();
const QString query = parameters["query"].toString();
const int count = parameters["count"].toInt();
const int count = 2; // FIXME: This should be a setting
QThread workerThread;
BraveAPIWorker worker;
worker.moveToThread(&workerThread);
Expand Down
2 changes: 1 addition & 1 deletion gpt4all-chat/bravesearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private Q_SLOTS:
private:
QNetworkAccessManager *m_networkManager;
QString m_response;
ToolEnums::Error m_error;
ToolEnums::Error m_error = ToolEnums::Error::NoError;
QString m_errorString;
};

Expand Down
49 changes: 22 additions & 27 deletions gpt4all-chat/chatllm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "chatllm.h"

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

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

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

emit toolCalled(tr("searching web..."));
const QString apiKey = MySettings::globalInstance()->braveSearchAPIKey();
Q_ASSERT(apiKey != "");
BraveSearch brave;

QJsonObject parameters;
parameters.insert("apiKey", apiKey);
parameters.insert("query", query);
parameters.insert("count", 2);

// FIXME: Need to surface errors to the UI
const QString braveResponse = brave.run(parameters, 2000 /*msecs to timeout*/);
const QString response = toolInstance->run(args, 2000 /*msecs to timeout*/);
if (toolInstance->error() != ToolEnums::Error::NoError) {
qWarning() << "ERROR: Tool call produced error:" << toolInstance->errorString();
return handleFailedToolCall(trimmed, totalTime);
}

QString parseError;
QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson(braveResponse, parseError);
if (!parseError.isEmpty()) {
qWarning() << "ERROR: Could not parse source excerpts for brave response:" << parseError;
} else if (!sourceExcerpts.isEmpty()) {
producedSourceExcerpts = true;
emit sourceExcerptsChanged(sourceExcerpts);
// If the tool supports excerpts then try to parse them here
if (toolInstance->excerpts()) {
QString parseError;
QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson(response, parseError);
if (!parseError.isEmpty()) {
qWarning() << "ERROR: Could not parse source excerpts for response:" << parseError;
} else if (!sourceExcerpts.isEmpty()) {
producedSourceExcerpts = true;
emit sourceExcerptsChanged(sourceExcerpts);
}
}

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

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

bool ChatLLM::handleFailedToolCall(const std::string &response, qint64 elapsed)
{
// FIXME: Need to surface errors to the UI
// Restore the strings that we excluded previously when detecting the tool call
m_response = "<tool_call>" + response + "</tool_call>";
emit responseChanged(QString::fromStdString(m_response));
Expand Down
4 changes: 2 additions & 2 deletions gpt4all-chat/qml/ChatView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ Rectangle {
case Chat.PromptProcessing: return qsTr("processing ...")
case Chat.ResponseGeneration: return qsTr("generating response ...");
case Chat.GeneratingQuestions: return qsTr("generating questions ...");
case Chat.ToolCalled: return currentChat.toolDescription;
case Chat.ToolProcessing: return qsTr("processing web results ..."); // FIXME should not be hardcoded!
case Chat.ToolCalled: return qsTr("executing %1 ...").arg(currentChat.toolDescription);
case Chat.ToolProcessing: return qsTr("processing %1 results ...").arg(currentChat.toolDescription);
default: return ""; // handle unexpected values
}
}
Expand Down

0 comments on commit 991afc6

Please sign in to comment.