Skip to content

Commit c154c02

Browse files
author
ochafik
committed
test tools json conversions
1 parent 103c840 commit c154c02

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

tests/test-chat.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,72 @@ static void test_oaicompat_json_conversion() {
387387
};
388388
for (const auto & msg : msgs) {
389389
auto oai_json = common_chat_msgs_to_json_oaicompat<json>({msg});
390-
fprintf(stderr, "OAI JSON: %s\n", oai_json.dump(2).c_str());
391390
auto msgs2 = common_chat_msgs_parse_oaicompat(oai_json);
392391
assert_equals((size_t) 1, msgs2.size());
393392
auto msg2 = msgs2[0];
394393
assert_msg_equals(msg, msg2);
395394
}
395+
assert_equals(
396+
std::string(
397+
"[\n"
398+
" {\n"
399+
" \"role\": \"assistant\",\n"
400+
" \"content\": null,\n"
401+
" \"tool_calls\": [\n"
402+
" {\n"
403+
" \"type\": \"function\",\n"
404+
" \"function\": {\n"
405+
" \"name\": \"python\",\n"
406+
" \"arguments\": \"{\\\"code\\\": \\\"print('hey')\\\"}\"\n"
407+
" }\n"
408+
" }\n"
409+
" ]\n"
410+
" }\n"
411+
"]"
412+
),
413+
common_chat_msgs_to_json_oaicompat<json>({message_assist_call_python}).dump(2));
414+
415+
std::vector<common_chat_tool> tools{
416+
special_function_tool,
417+
python_tool,
418+
code_interpreter_tool,
419+
};
420+
421+
for (const auto & tool : tools) {
422+
auto oai_json = common_chat_tools_to_json_oaicompat<json>({tool});
423+
auto tools2 = common_chat_tools_parse_oaicompat(oai_json);
424+
assert_equals((size_t) 1, tools2.size());
425+
auto tool2 = tools2[0];
426+
assert_equals(tool.name, tool2.name);
427+
assert_equals(tool.description, tool2.description);
428+
assert_equals(json::parse(tool.parameters).dump(2), json::parse(tool2.parameters).dump(2));
429+
}
430+
431+
assert_equals(
432+
std::string(
433+
"[\n"
434+
" {\n"
435+
" \"type\": \"function\",\n"
436+
" \"function\": {\n"
437+
" \"name\": \"special_function\",\n"
438+
" \"description\": \"I'm special\",\n"
439+
" \"parameters\": {\n"
440+
" \"type\": \"object\",\n"
441+
" \"properties\": {\n"
442+
" \"arg1\": {\n"
443+
" \"type\": \"integer\",\n"
444+
" \"description\": \"The arg.\"\n"
445+
" }\n"
446+
" },\n"
447+
" \"required\": [\n"
448+
" \"arg1\"\n"
449+
" ]\n"
450+
" }\n"
451+
" }\n"
452+
" }\n"
453+
"]"
454+
),
455+
common_chat_tools_to_json_oaicompat<json>({special_function_tool}).dump(2));
396456
}
397457

398458
static void test_template_output_parsers() {

0 commit comments

Comments
 (0)