File tree 5 files changed +24
-13
lines changed
5 files changed +24
-13
lines changed Original file line number Diff line number Diff line change @@ -250,12 +250,18 @@ void Chat::responseStopped(qint64 promptResponseMs)
250
250
if (item.type () == ChatItem::Type::Response && parser.state () == ToolEnums::ParseState::Complete) {
251
251
const QString toolCall = parser.toolCall ();
252
252
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
+
253
259
// Right now the code interpreter is the only available tool
254
260
Tool *toolInstance = ToolModel::globalInstance ()->get (ToolCallConstants::CodeInterpreterFunction);
255
261
Q_ASSERT (toolInstance);
256
262
257
263
// The param is the code
258
- const ToolParam param = { " code" , ToolEnums::ParamType::String, toolCall };
264
+ const ToolParam param = { " code" , ToolEnums::ParamType::String, code };
259
265
const QString result = toolInstance->run ({param}, 10000 /* msecs to timeout*/ );
260
266
261
267
// FIXME: Latest thinking is we *should* generate an error even from 'recoverable' errors in code
Original file line number Diff line number Diff line change @@ -186,14 +186,21 @@ struct ChatItem
186
186
const QString toolCallString = value.mid (parser.startIndex ());
187
187
188
188
// Constants for identifying and formatting the code interpreter tool call
189
- static const QString prefix = ToolCallConstants::CodeInterpreterPrefix ;
189
+ static const QString prefix = ToolCallConstants::CodeInterpreterTag ;
190
190
191
191
// Check if the tool call is a code interpreter tool call
192
192
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
+ + " $" );
194
199
195
200
// Extract the code
196
- const QString code = toolCallString.mid (startCodeIndex, -1 );
201
+ QString code = toolCallString;
202
+ code.remove (regex);
203
+ code = code.trimmed ();
197
204
198
205
QString result;
199
206
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ QList<ToolParamInfo> CodeInterpreter::parameters() const
44
44
45
45
QString CodeInterpreter::symbolicFormat () const
46
46
{
47
- return ToolCallConstants::CodeInterpreterFullPrefix + " {code}\n " + ToolCallConstants::CodeInterpreterSuffix;
47
+ return ToolCallConstants::CodeInterpreterPrefix + " {code}\n " + ToolCallConstants::CodeInterpreterSuffix;
48
48
}
49
49
50
50
QString CodeInterpreter::examplePrompt () const
@@ -70,7 +70,7 @@ const number = 7;
70
70
console.log(`The number ${number} is prime: ${isPrime(number)}`);
71
71
)" ;
72
72
73
- return ToolCallConstants::CodeInterpreterFullPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
73
+ return ToolCallConstants::CodeInterpreterPrefix + example + ToolCallConstants::CodeInterpreterSuffix;
74
74
}
75
75
76
76
QString CodeInterpreter::exampleReply () const
Original file line number Diff line number Diff line change 2
2
3
3
#include < QDebug>
4
4
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 ;
7
7
8
8
ToolCallParser::ToolCallParser ()
9
9
{
Original file line number Diff line number Diff line change @@ -9,11 +9,9 @@ namespace ToolCallConstants
9
9
{
10
10
const QString CodeInterpreterFunction = R"( javascript_interpret)" ;
11
11
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;
17
15
}
18
16
19
17
class ToolCallParser
You can’t perform that action at this time.
0 commit comments