| 714 | } |
| 715 | |
| 716 | server_tokens process_mtmd_prompt(mtmd_context * mctx, std::string prompt, std::vector<raw_buffer> files) { |
| 717 | mtmd::bitmaps bitmaps; |
| 718 | for (auto & file : files) { |
| 719 | mtmd::bitmap bmp(mtmd_helper_bitmap_init_from_buf(mctx, file.data(), file.size())); |
| 720 | if (!bmp.ptr) { |
| 721 | throw std::runtime_error("Failed to load image or audio file"); |
| 722 | } |
| 723 | // calculate bitmap hash (for KV caching) |
| 724 | std::string hash = fnv_hash(bmp.data(), bmp.n_bytes()); |
| 725 | bmp.set_id(hash.c_str()); |
| 726 | bitmaps.entries.push_back(std::move(bmp)); |
| 727 | } |
| 728 | // process prompt |
| 729 | std::vector<server_tokens> inputs; |
| 730 | // multimodal |
| 731 | mtmd_input_text inp_txt = { |
| 732 | prompt.c_str(), |
| 733 | /* add_special */ true, |
| 734 | /* parse_special */ true, |
| 735 | }; |
| 736 | mtmd::input_chunks chunks(mtmd_input_chunks_init()); |
| 737 | auto bitmaps_c_ptr = bitmaps.c_ptr(); |
| 738 | int32_t tokenized = mtmd_tokenize(mctx, |
| 739 | chunks.ptr.get(), |
| 740 | &inp_txt, |
| 741 | bitmaps_c_ptr.data(), |
| 742 | bitmaps_c_ptr.size()); |
| 743 | if (tokenized != 0) { |
| 744 | throw std::runtime_error("Failed to tokenize prompt"); |
| 745 | } |
| 746 | auto result = server_tokens(chunks, true); |
| 747 | return result; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * break the input "prompt" object into multiple prompt if needed, then tokenize them |
no test coverage detected