Get base64 image position if found in prompt.
| 27 | |
| 28 | // Get base64 image position if found in prompt. |
| 29 | std::optional<std::tuple<size_t, size_t, size_t>> |
| 30 | findBase64ImagePayload(std::string_view Prompt, bool IsDebugLog) noexcept { |
| 31 | // Find `<img src="data:image/` |
| 32 | auto BeginTagPos = Prompt.find(Base64ImageTagPrefix); |
| 33 | if (BeginTagPos == std::string::npos) { |
| 34 | // Not print debug log here because not expect image must occur in every |
| 35 | // prompt. |
| 36 | return std::nullopt; |
| 37 | } |
| 38 | // Find `;base64,` (skip the image type part) |
| 39 | auto PayloadPos = Prompt.find(Base64ImageBytesPrefix, BeginTagPos); |
| 40 | if (PayloadPos == std::string::npos) { |
| 41 | LOG_DEBUG(IsDebugLog, "base64: Cannot locate the payload."sv) |
| 42 | return std::nullopt; |
| 43 | } |
| 44 | // Find `">` |
| 45 | auto EndTagPos = Prompt.find(Base64ImageTagSuffix, PayloadPos); |
| 46 | if (EndTagPos == std::string::npos) { |
| 47 | LOG_DEBUG(IsDebugLog, "base64: image tag unclosed."sv) |
| 48 | return std::nullopt; |
| 49 | } |
| 50 | return std::make_tuple(BeginTagPos, PayloadPos, EndTagPos); |
| 51 | } |
| 52 | |
| 53 | // Extract base64 image payload and image type. Replace it with placeholder. |
| 54 | std::optional<std::pair<std::vector<uint8_t>, std::string>> |