Skip to content

Commit 23e4ff7

Browse files
committed
Refactor xassist, test framework
1 parent baba783 commit 23e4ff7

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/xmagics/xassist.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,52 @@ namespace xcpp
279279
}
280280
};
281281

282+
std::string escape_special_cases(const std::string& input)
283+
{
284+
std::string escaped;
285+
for (char c : input)
286+
{
287+
switch (c)
288+
{
289+
case '\\':
290+
escaped += "\\\\";
291+
break;
292+
case '\"':
293+
escaped += "\\\"";
294+
break;
295+
case '\n':
296+
escaped += "\\n";
297+
break;
298+
case '\t':
299+
escaped += "\\t";
300+
break;
301+
case '\r':
302+
escaped += "\\r";
303+
break;
304+
case '\b':
305+
escaped += "\\b";
306+
break;
307+
case '\f':
308+
escaped += "\\f";
309+
break;
310+
default:
311+
if (c < 0x20 || c > 0x7E)
312+
{
313+
// Escape non-printable ASCII characters and non-ASCII characters
314+
char buffer[7];
315+
snprintf(buffer, sizeof(buffer), "\\u%04x", c & 0xFFFF);
316+
escaped += buffer;
317+
}
318+
else
319+
{
320+
escaped += c;
321+
}
322+
break;
323+
}
324+
}
325+
return escaped;
326+
}
327+
282328
std::string gemini(const std::string& cell, const std::string& key)
283329
{
284330
curl_helper curl_helper;
@@ -369,8 +415,8 @@ namespace xcpp
369415
}
370416

371417
const std::string post_data = R"({
372-
"model": [)" + model
373-
+ R"(],
418+
"model": ")" + model
419+
+ R"(",
374420
"messages": [)" + chat_message
375421
+ R"(],
376422
"temperature": 0.7
@@ -453,18 +499,21 @@ namespace xcpp
453499
}
454500
}
455501

502+
503+
const std::string prompt = escape_special_cases(cell);
504+
456505
std::string response;
457506
if (model == "gemini")
458507
{
459-
response = gemini(cell, key);
508+
response = gemini(prompt, key);
460509
}
461510
else if (model == "openai")
462511
{
463-
response = openai(cell, key);
512+
response = openai(prompt, key);
464513
}
465514
else if (model == "ollama")
466515
{
467-
response = ollama(cell);
516+
response = ollama(prompt);
468517
}
469518

470519
std::cout << response;

test/test_xcpp_kernel.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ def test_notebooks(self):
167167
with open(out) as f:
168168
output_nb = nbformat.read(f, as_version=4)
169169

170-
check = True
171-
172170
# Iterate over the cells in the input and output notebooks
173171
for i, (input_cell, output_cell) in enumerate(zip(input_nb.cells, output_nb.cells)):
174172
if input_cell.cell_type == 'code' and output_cell.cell_type == 'code':

0 commit comments

Comments
 (0)