| 802 | } |
| 803 | |
| 804 | std::string Tokenizer::format_youtu_style(const std::vector<ChatMessage>& messages, bool add_generation_prompt, const std::string& tools_json) const { |
| 805 | std::string result = "<|begin_of_text|>"; |
| 806 | |
| 807 | std::string system_block; |
| 808 | for (const auto& msg : messages) { |
| 809 | if (msg.role == "system") { system_block = msg.content; break; } |
| 810 | } |
| 811 | |
| 812 | if (!tools_json.empty()) { |
| 813 | std::string tool_desc = |
| 814 | "<|begin_of_tool_description|>Tool calling capabilities.\n" |
| 815 | "You may call one or more functions to assist with the user query. " |
| 816 | "You have the following functions available:\n" |
| 817 | "```json\n" + tools_json + "\n```\n" |
| 818 | "For tool call returns, you MUST use the following format:\n" |
| 819 | "<tool_call>{\"name\": \"function-name\", \"arguments\": {\"param1\": \"value1\", \"param2\": \"value2\"}}</tool_call>\n" |
| 820 | "<|end_of_tool_description|>"; |
| 821 | system_block = system_block.empty() ? tool_desc : system_block + "\n\n" + tool_desc; |
| 822 | } |
| 823 | |
| 824 | result += system_block; |
| 825 | |
| 826 | bool is_last_user = false; |
| 827 | bool is_tool = false; |
| 828 | bool is_output_first = true; |
| 829 | |
| 830 | for (const auto& msg : messages) { |
| 831 | if (msg.role == "system") { |
| 832 | continue; |
| 833 | } else if (msg.role == "user") { |
| 834 | is_last_user = true; is_tool = false; |
| 835 | result += "<|User|>" + msg.content; |
| 836 | } else if (msg.role == "tool") { |
| 837 | is_tool = true; is_last_user = false; |
| 838 | if (is_output_first) { |
| 839 | result += "<|User|><tool_response>" + msg.content + "</tool_response>"; |
| 840 | is_output_first = false; |
| 841 | } else { |
| 842 | result += "\n<tool_response>" + msg.content + "</tool_response>"; |
| 843 | } |
| 844 | } else if (msg.role == "assistant" || msg.role == "model") { |
| 845 | is_last_user = false; is_tool = false; is_output_first = true; |
| 846 | result += "<|Assistant|>" + msg.content; |
| 847 | if (!msg.tool_calls.empty()) { |
| 848 | for (const auto& tc : msg.tool_calls) { |
| 849 | result += "<tool_call>{\"name\": \"" + tc.name |
| 850 | + "\", \"arguments\": " + tc.arguments + "}</tool_call>"; |
| 851 | } |
| 852 | } |
| 853 | result += "<|end_of_text|>"; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | if (add_generation_prompt && (is_last_user || is_tool)) { |
| 858 | result += "<|Assistant|>"; |
| 859 | } |
| 860 | |
| 861 | return result; |
nothing calls this directly
no outgoing calls
no test coverage detected