Skip to content

Commit

Permalink
fix: tojson string
Browse files Browse the repository at this point in the history
  • Loading branch information
sangjanai committed Jan 14, 2025
1 parent 3364989 commit da2b857
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions engine/extensions/template_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ TemplateRenderer::TemplateRenderer() {
const auto& value = *args[0];

if (value.is_string()) {
return nlohmann::json(std::string("\"") + value.get<std::string>() +
"\"");
std::string v = value.get<std::string>();
v = std::regex_replace(v, std::regex("\""), "\\\"");
return nlohmann::json(std::string("\"") + v + "\"");
}
return value;
});
Expand Down
8 changes: 4 additions & 4 deletions engine/test/components/test_remote_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ TEST_F(RemoteEngineTest, CohereResponse) {
{% if input_request.stream %}
{"object": "chat.completion.chunk",
"model": "{{ input_request.model }}",
"choices": [{"index": 0, "delta": { {% if input_request.event_type == "text-generation" %} "role": "assistant", "content": "{{ input_request.text }}" {% else %} "role": "assistant", "content": null {% endif %} },
"choices": [{"index": 0, "delta": { {% if input_request.event_type == "text-generation" %} "role": "assistant", "content": {{ tojson(input_request.text) }} {% else %} "role": "assistant", "content": null {% endif %} },
{% if input_request.event_type == "stream-end" %} "finish_reason": "{{ input_request.finish_reason }}" {% else %} "finish_reason": null {% endif %} }]
}
{% else %}
{"id": "{{ input_request.generation_id }}",
"created": null,
"object": "chat.completion",
"model": "{{ input_request.model }}",
"choices": [{ "index": 0, "message": { "role": "assistant", "content": {% if not input_request.text %} null {% else %} "{{input_request.text}}" {% endif %}, "refusal": null }, "logprobs": null, "finish_reason": "{{ input_request.finish_reason }}" } ], "usage": { "prompt_tokens": {{ input_request.meta.tokens.input_tokens }}, "completion_tokens": {{ input_request.meta.tokens.output_tokens }}, "total_tokens": {{ input_request.meta.tokens.input_tokens + input_request.meta.tokens.output_tokens }}, "prompt_tokens_details": { "cached_tokens": 0 }, "completion_tokens_details": { "reasoning_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 } }, "system_fingerprint": "fp_6b68a8204b"} {% endif %})";
"choices": [{ "index": 0, "message": { "role": "assistant", "content": {% if not input_request.text %} null {% else %} {{ tojson(input_request.text) }} {% endif %}, "refusal": null }, "logprobs": null, "finish_reason": "{{ input_request.finish_reason }}" } ], "usage": { "prompt_tokens": {{ input_request.meta.tokens.input_tokens }}, "completion_tokens": {{ input_request.meta.tokens.output_tokens }}, "total_tokens": {{ input_request.meta.tokens.input_tokens + input_request.meta.tokens.output_tokens }}, "prompt_tokens_details": { "cached_tokens": 0 }, "completion_tokens_details": { "reasoning_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 } }, "system_fingerprint": "fp_6b68a8204b"} {% endif %})";
std::string message = R"({
"event_type": "text-generation",
"text": " help"
Expand Down Expand Up @@ -337,7 +337,7 @@ TEST_F(RemoteEngineTest, CohereResponse) {
// non-stream
message = R"(
{
"text": "Isaac Newton was born on 25 December 1642 (Old Style) \n\nor 4 January 1643 (New Style).",
"text": "Isaac Newton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 January 1643 (New Style).",
"generation_id": "0385c7cf-4247-43a3-a450-b25b547a31e1",
"citations": [
{
Expand Down Expand Up @@ -410,7 +410,7 @@ TEST_F(RemoteEngineTest, CohereResponse) {
res = rdr.Render(tpl, data);
res_json = json_helper::ParseJsonString(res);
EXPECT_EQ(res_json["choices"][0]["message"]["content"].asString(),
"Isaac Newton was born on 25 December 1642 (Old Style) \n\nor 4 "
"Isaac Newton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 "
"January 1643 (New Style).");
}

Expand Down

0 comments on commit da2b857

Please # to comment.