Skip to content

Commit 7cf7ea0

Browse files
committed
WIP
Signed-off-by: Adam Treat <[email protected]>
1 parent f008e66 commit 7cf7ea0

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

gpt4all-chat/src/chat.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,18 @@ void Chat::responseStopped(qint64 promptResponseMs)
250250
if (item.type() == ChatItem::Type::Response && parser.state() == ToolEnums::ParseState::Complete) {
251251
const QString toolCall = parser.toolCall();
252252

253+
// Regex to remove the formatting around the code
254+
static const QRegularExpression regex("^\\s*```javascript\\s*|\\s*```\\s*$");
255+
QString code = toolCall;
256+
code.remove(regex);
257+
code = code.trimmed();
258+
253259
// Right now the code interpreter is the only available tool
254260
Tool *toolInstance = ToolModel::globalInstance()->get(ToolCallConstants::CodeInterpreterFunction);
255261
Q_ASSERT(toolInstance);
256262

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

261267
// FIXME: Latest thinking is we *should* generate an error even from 'recoverable' errors in code

gpt4all-chat/src/chatmodel.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,21 @@ struct ChatItem
186186
const QString toolCallString = value.mid(parser.startIndex());
187187

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

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

195200
// Extract the code
196-
const QString code = toolCallString.mid(startCodeIndex, -1);
201+
QString code = toolCallString;
202+
code.remove(regex);
203+
code = code.trimmed();
197204

198205
QString result;
199206

gpt4all-chat/src/codeinterpreter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ QList<ToolParamInfo> CodeInterpreter::parameters() const
4444

4545
QString CodeInterpreter::symbolicFormat() const
4646
{
47-
return ToolCallConstants::CodeInterpreterFullPrefix + "{code}\n" + ToolCallConstants::CodeInterpreterSuffix;
47+
return ToolCallConstants::CodeInterpreterPrefix + "{code}\n" + ToolCallConstants::CodeInterpreterSuffix;
4848
}
4949

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

73-
return ToolCallConstants::CodeInterpreterFullPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
73+
return ToolCallConstants::CodeInterpreterPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
7474
}
7575

7676
QString CodeInterpreter::exampleReply() const

gpt4all-chat/src/toolcallparser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include <QDebug>
44

5-
static const QString ToolCallStart = ToolCallConstants::CodeInterpreterFullPrefix;
6-
static const QString ToolCallEnd = ToolCallConstants::CodeInterpreterSuffix;
5+
static const QString ToolCallStart = ToolCallConstants::CodeInterpreterTag;
6+
static const QString ToolCallEnd = ToolCallConstants::CodeInterpreterEndTag;
77

88
ToolCallParser::ToolCallParser()
99
{

gpt4all-chat/src/toolcallparser.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ namespace ToolCallConstants
99
{
1010
const QString CodeInterpreterFunction = R"(javascript_interpret)";
1111
const QString CodeInterpreterTag = R"(<)" + CodeInterpreterFunction + R"(>)";
12-
const QString CodeInterpreterPrefix = CodeInterpreterTag + "\n";
13-
const QString CodeInterpreterFullPrefix = CodeInterpreterTag + "\n```javascript\n";
14-
const QString CodeInterpreterSuffix = R"(```)";
15-
// FIXME: See if we can have an actual end tag
16-
// FIXME: The parser should be agnostic to the backticks and formatting of the param
12+
const QString CodeInterpreterEndTag = R"(</)" + CodeInterpreterFunction + R"(>)";
13+
const QString CodeInterpreterPrefix = CodeInterpreterTag + "\n```javascript\n";
14+
const QString CodeInterpreterSuffix = "```\n" + CodeInterpreterEndTag;
1715
}
1816

1917
class ToolCallParser

0 commit comments

Comments
 (0)