|
1 | 1 | #include "harmony.h"
|
| 2 | +#include "regex-partial.h" |
2 | 3 |
|
3 | 4 | harmony_msg_parser::harmony_msg_parser(common_chat_msg_parser & builder)
|
4 |
| - : builder(builder) |
5 |
| -{ |
6 |
| - // TODO |
7 |
| -} |
| 5 | + : builder(builder) {} |
8 | 6 |
|
9 | 7 | void harmony_msg_parser::parse() {
|
10 | 8 | // TODO @ngxson : this won't work with --special enabled, we should fix that
|
11 |
| - builder.try_parse_reasoning("<|channel|>analysis<|message|>", "<|start|>assistant<|channel|>final<|message|>"); |
12 |
| - builder.add_content(builder.consume_rest()); |
| 9 | + //builder.try_parse_reasoning("<|channel|>analysis<|message|>", "<|start|>assistant<|channel|>final<|message|>"); |
| 10 | + //builder.add_content(builder.consume_rest()); |
| 11 | + channel(); |
| 12 | +} |
| 13 | + |
| 14 | +void harmony_msg_parser::channel() { |
| 15 | + if (builder.try_consume_literal("<|channel|>")) { |
| 16 | + static const common_regex channel_type_regexp("(final|analysis|commentary)"); |
| 17 | + if (auto res = builder.try_consume_regex(channel_type_regexp)) { |
| 18 | + auto type = builder.str(res->groups[0]); |
| 19 | + if (type == "analysis") { |
| 20 | + analysis(); |
| 21 | + } else if (type == "final") { |
| 22 | + final(); |
| 23 | + } else if (type == "commentary") { |
| 24 | + commentary(); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +void harmony_msg_parser::analysis() { |
| 31 | + if (builder.try_consume_literal("<|message|>")) { |
| 32 | + static const common_regex end("<\\|end\\|>"); |
| 33 | + if (auto res = builder.try_find_regex(end, std::string::npos, false)) { |
| 34 | + builder.add_reasoning_content(res->prelude); |
| 35 | + if (builder.try_consume_literal("<|start|>")) { |
| 36 | + start(); |
| 37 | + } |
| 38 | + } else { |
| 39 | + builder.add_reasoning_content(builder.consume_rest()); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void harmony_msg_parser::start() { |
| 45 | + if (builder.try_consume_literal("assistant")) { |
| 46 | + channel(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void harmony_msg_parser::final() { |
| 51 | + if (builder.try_consume_literal("<|message|>")) { |
| 52 | + builder.add_content(builder.consume_rest()); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void harmony_msg_parser::commentary() { |
| 57 | + if (builder.try_consume_literal(" to=")) { |
| 58 | + user_function(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void harmony_msg_parser::user_function() { |
| 63 | + static const common_regex tool_call_regex( |
| 64 | + "functions\\.([a-zA-Z_][a-zA-Z0-9_]*)\\s*(<\\|constrain\\|>([a-z]+))?<\\|message\\|>" |
| 65 | + ); |
| 66 | + if (auto res = builder.try_consume_regex(tool_call_regex)) { |
| 67 | + auto name = builder.str(res->groups[1]); |
| 68 | + //auto constrain_type = builder.str(res->groups[3]); |
| 69 | + auto args = builder.consume_rest(); |
| 70 | + |
| 71 | + builder.add_tool_call(name, "", args); |
| 72 | + } |
13 | 73 | }
|
0 commit comments