Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 2140754

Browse files
committed
feat: Add handler for remote and local image url
1 parent 3f80512 commit 2140754

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

controllers/llamaCPP.cc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,38 @@ void llamaCPP::chatCompletion(
239239
for (auto content_piece : message["content"]) {
240240
role = user_prompt;
241241

242+
json content_piece_image_data;
243+
242244
auto content_piece_type = content_piece["type"].asString();
243245
if (content_piece_type == "text") {
244246
auto text = content_piece["text"].asString();
245247
formatted_output += text;
246248
} else if (content_piece_type == "image_url") {
247249
auto image_url = content_piece["image_url"]["url"].asString();
248-
auto base64_image_data = nitro_utils::extractBase64(image_url);
249-
LOG_INFO << base64_image_data;
250-
formatted_output += "[img-" + std::to_string(no_images) + "]";
250+
std::string
251+
base64_image_data; // Declare the variable 'base64_image_data'
252+
if (image_url.find("http") != std::string::npos) {
253+
// If image url is a remote link, extract and use convert to
254+
// base64
255+
nitro_utils::processRemoteImage(
256+
image_url, [](const std::string &base64Image) {
257+
auto base64_image_data = base64Image;
258+
LOG_INFO << base64_image_data;
259+
});
260+
} else if (image_url.find("data:image") != std::string::npos) {
261+
// If image url is already in base64, use it directly
262+
auto base64_image_data = nitro_utils::extractBase64(image_url);
263+
LOG_INFO << base64_image_data;
264+
} else {
265+
// If image url is a local file, convert to base64
266+
nitro_utils::processLocalImage(
267+
image_url, [](const std::string &base64Image) {
268+
auto base64_image_data = base64Image;
269+
LOG_INFO << base64_image_data;
270+
});
271+
}
251272

252-
json content_piece_image_data;
273+
formatted_output += "[img-" + std::to_string(no_images) + "]";
253274
content_piece_image_data["data"] = base64_image_data;
254275
content_piece_image_data["id"] = no_images;
255276
data["image_data"].push_back(content_piece_image_data);

0 commit comments

Comments
 (0)