Skip to content

Commit df003a6

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

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/xmagics/xassist.cpp

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,53 @@ 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+
std::array<char, 7> buffer{};
315+
std::stringstream ss;
316+
ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (c & 0xFFFF);
317+
escaped += ss.str();
318+
}
319+
else
320+
{
321+
escaped += c;
322+
}
323+
break;
324+
}
325+
}
326+
return escaped;
327+
}
328+
282329
std::string gemini(const std::string& cell, const std::string& key)
283330
{
284331
curl_helper curl_helper;
@@ -369,8 +416,8 @@ namespace xcpp
369416
}
370417

371418
const std::string post_data = R"({
372-
"model": [)" + model
373-
+ R"(],
419+
"model": ")" + model
420+
+ R"(",
374421
"messages": [)" + chat_message
375422
+ R"(],
376423
"temperature": 0.7
@@ -453,18 +500,21 @@ namespace xcpp
453500
}
454501
}
455502

503+
504+
const std::string prompt = escape_special_cases(cell);
505+
456506
std::string response;
457507
if (model == "gemini")
458508
{
459-
response = gemini(cell, key);
509+
response = gemini(prompt, key);
460510
}
461511
else if (model == "openai")
462512
{
463-
response = openai(cell, key);
513+
response = openai(prompt, key);
464514
}
465515
else if (model == "ollama")
466516
{
467-
response = ollama(cell);
517+
response = ollama(prompt);
468518
}
469519

470520
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)