|
19 | 19 | using json = nlohmann::json;
|
20 | 20 |
|
21 | 21 | // TODO: Implement xplugin to separate the magics from the main code.
|
22 |
| -// TODO: Add support for open-source models. |
23 | 22 | namespace xcpp
|
24 | 23 | {
|
25 | 24 | class api_key_manager
|
@@ -279,6 +278,53 @@ namespace xcpp
|
279 | 278 | }
|
280 | 279 | };
|
281 | 280 |
|
| 281 | + std::string escape_special_cases(const std::string& input) |
| 282 | + { |
| 283 | + std::string escaped; |
| 284 | + for (char c : input) |
| 285 | + { |
| 286 | + switch (c) |
| 287 | + { |
| 288 | + case '\\': |
| 289 | + escaped += "\\\\"; |
| 290 | + break; |
| 291 | + case '\"': |
| 292 | + escaped += "\\\""; |
| 293 | + break; |
| 294 | + case '\n': |
| 295 | + escaped += "\\n"; |
| 296 | + break; |
| 297 | + case '\t': |
| 298 | + escaped += "\\t"; |
| 299 | + break; |
| 300 | + case '\r': |
| 301 | + escaped += "\\r"; |
| 302 | + break; |
| 303 | + case '\b': |
| 304 | + escaped += "\\b"; |
| 305 | + break; |
| 306 | + case '\f': |
| 307 | + escaped += "\\f"; |
| 308 | + break; |
| 309 | + default: |
| 310 | + if (c < 0x20 || c > 0x7E) |
| 311 | + { |
| 312 | + // Escape non-printable ASCII characters and non-ASCII characters |
| 313 | + std::array<char, 7> buffer{}; |
| 314 | + std::stringstream ss; |
| 315 | + ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (c & 0xFFFF); |
| 316 | + escaped += ss.str(); |
| 317 | + } |
| 318 | + else |
| 319 | + { |
| 320 | + escaped += c; |
| 321 | + } |
| 322 | + break; |
| 323 | + } |
| 324 | + } |
| 325 | + return escaped; |
| 326 | + } |
| 327 | + |
282 | 328 | std::string gemini(const std::string& cell, const std::string& key)
|
283 | 329 | {
|
284 | 330 | curl_helper curl_helper;
|
@@ -369,8 +415,8 @@ namespace xcpp
|
369 | 415 | }
|
370 | 416 |
|
371 | 417 | const std::string post_data = R"({
|
372 |
| - "model": [)" + model |
373 |
| - + R"(], |
| 418 | + "model": ")" + model |
| 419 | + + R"(", |
374 | 420 | "messages": [)" + chat_message
|
375 | 421 | + R"(],
|
376 | 422 | "temperature": 0.7
|
@@ -453,18 +499,21 @@ namespace xcpp
|
453 | 499 | }
|
454 | 500 | }
|
455 | 501 |
|
| 502 | + |
| 503 | + const std::string prompt = escape_special_cases(cell); |
| 504 | + |
456 | 505 | std::string response;
|
457 | 506 | if (model == "gemini")
|
458 | 507 | {
|
459 |
| - response = gemini(cell, key); |
| 508 | + response = gemini(prompt, key); |
460 | 509 | }
|
461 | 510 | else if (model == "openai")
|
462 | 511 | {
|
463 |
| - response = openai(cell, key); |
| 512 | + response = openai(prompt, key); |
464 | 513 | }
|
465 | 514 | else if (model == "ollama")
|
466 | 515 | {
|
467 |
| - response = ollama(cell); |
| 516 | + response = ollama(prompt); |
468 | 517 | }
|
469 | 518 |
|
470 | 519 | std::cout << response;
|
|
0 commit comments