Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Treat <[email protected]>
  • Loading branch information
manyoso committed Nov 14, 2024
1 parent f008e66 commit 7cf7ea0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
8 changes: 7 additions & 1 deletion gpt4all-chat/src/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,18 @@ void Chat::responseStopped(qint64 promptResponseMs)
if (item.type() == ChatItem::Type::Response && parser.state() == ToolEnums::ParseState::Complete) {
const QString toolCall = parser.toolCall();

// Regex to remove the formatting around the code
static const QRegularExpression regex("^\\s*```javascript\\s*|\\s*```\\s*$");
QString code = toolCall;
code.remove(regex);
code = code.trimmed();

// Right now the code interpreter is the only available tool
Tool *toolInstance = ToolModel::globalInstance()->get(ToolCallConstants::CodeInterpreterFunction);
Q_ASSERT(toolInstance);

// The param is the code
const ToolParam param = { "code", ToolEnums::ParamType::String, toolCall };
const ToolParam param = { "code", ToolEnums::ParamType::String, code };
const QString result = toolInstance->run({param}, 10000 /*msecs to timeout*/);

// FIXME: Latest thinking is we *should* generate an error even from 'recoverable' errors in code
Expand Down
13 changes: 10 additions & 3 deletions gpt4all-chat/src/chatmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,21 @@ struct ChatItem
const QString toolCallString = value.mid(parser.startIndex());

// Constants for identifying and formatting the code interpreter tool call
static const QString prefix = ToolCallConstants::CodeInterpreterPrefix;
static const QString prefix = ToolCallConstants::CodeInterpreterTag;

// Check if the tool call is a code interpreter tool call
if (toolCallString.startsWith(prefix)) {
int startCodeIndex = prefix.length();
// Regex to remove the tag and any surrounding whitespace
static const QRegularExpression regex("^"
+ ToolCallConstants::CodeInterpreterTag
+ "\\s*|\\s*"
+ ToolCallConstants::CodeInterpreterEndTag
+ "$");

// Extract the code
const QString code = toolCallString.mid(startCodeIndex, -1);
QString code = toolCallString;
code.remove(regex);
code = code.trimmed();

QString result;

Expand Down
4 changes: 2 additions & 2 deletions gpt4all-chat/src/codeinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ QList<ToolParamInfo> CodeInterpreter::parameters() const

QString CodeInterpreter::symbolicFormat() const
{
return ToolCallConstants::CodeInterpreterFullPrefix + "{code}\n" + ToolCallConstants::CodeInterpreterSuffix;
return ToolCallConstants::CodeInterpreterPrefix + "{code}\n" + ToolCallConstants::CodeInterpreterSuffix;
}

QString CodeInterpreter::examplePrompt() const
Expand All @@ -70,7 +70,7 @@ const number = 7;
console.log(`The number ${number} is prime: ${isPrime(number)}`);
)";

return ToolCallConstants::CodeInterpreterFullPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
return ToolCallConstants::CodeInterpreterPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
}

QString CodeInterpreter::exampleReply() const
Expand Down
4 changes: 2 additions & 2 deletions gpt4all-chat/src/toolcallparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <QDebug>

static const QString ToolCallStart = ToolCallConstants::CodeInterpreterFullPrefix;
static const QString ToolCallEnd = ToolCallConstants::CodeInterpreterSuffix;
static const QString ToolCallStart = ToolCallConstants::CodeInterpreterTag;
static const QString ToolCallEnd = ToolCallConstants::CodeInterpreterEndTag;

ToolCallParser::ToolCallParser()
{
Expand Down
8 changes: 3 additions & 5 deletions gpt4all-chat/src/toolcallparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ namespace ToolCallConstants
{
const QString CodeInterpreterFunction = R"(javascript_interpret)";
const QString CodeInterpreterTag = R"(<)" + CodeInterpreterFunction + R"(>)";
const QString CodeInterpreterPrefix = CodeInterpreterTag + "\n";
const QString CodeInterpreterFullPrefix = CodeInterpreterTag + "\n```javascript\n";
const QString CodeInterpreterSuffix = R"(```)";
// FIXME: See if we can have an actual end tag
// FIXME: The parser should be agnostic to the backticks and formatting of the param
const QString CodeInterpreterEndTag = R"(</)" + CodeInterpreterFunction + R"(>)";
const QString CodeInterpreterPrefix = CodeInterpreterTag + "\n```javascript\n";
const QString CodeInterpreterSuffix = "```\n" + CodeInterpreterEndTag;
}

class ToolCallParser
Expand Down

0 comments on commit 7cf7ea0

Please sign in to comment.