mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 02:20:50 +01:00 
			
		
		
		
	Refactors loadImageBytesFromUrl to Utils class
This commit is contained in:
		@@ -413,7 +413,7 @@ public class OllamaAPI {
 | 
			
		||||
      throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
 | 
			
		||||
    List<String> images = new ArrayList<>();
 | 
			
		||||
    for (String imageURL : imageURLs) {
 | 
			
		||||
      images.add(encodeByteArrayToBase64(loadImageBytesFromUrl(imageURL)));
 | 
			
		||||
      images.add(encodeByteArrayToBase64(Utils.loadImageBytesFromUrl(imageURL)));
 | 
			
		||||
    }
 | 
			
		||||
    OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(model, prompt, images);
 | 
			
		||||
    ollamaRequestModel.setOptions(options.getOptionsMap());
 | 
			
		||||
@@ -469,20 +469,6 @@ public class OllamaAPI {
 | 
			
		||||
    return Base64.getEncoder().encodeToString(bytes);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static byte[] loadImageBytesFromUrl(String imageUrl)
 | 
			
		||||
      throws IOException, URISyntaxException {
 | 
			
		||||
    URL url = new URI(imageUrl).toURL();
 | 
			
		||||
    try (InputStream in = url.openStream();
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
 | 
			
		||||
      byte[] buffer = new byte[1024];
 | 
			
		||||
      int bytesRead;
 | 
			
		||||
      while ((bytesRead = in.read(buffer)) != -1) {
 | 
			
		||||
        out.write(buffer, 0, bytesRead);
 | 
			
		||||
      }
 | 
			
		||||
      return out.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private OllamaResult generateSyncForOllamaRequestModel(OllamaRequestModel ollamaRequestModel)
 | 
			
		||||
      throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        OllamaGenerateEndpointCaller requestCaller = new OllamaGenerateEndpointCaller(host, basicAuth, requestTimeoutSeconds, verbose);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,30 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.utils;
 | 
			
		||||
 | 
			
		||||
import java.io.ByteArrayOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
public class Utils {
 | 
			
		||||
  public static ObjectMapper getObjectMapper() {
 | 
			
		||||
    return new ObjectMapper();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static byte[] loadImageBytesFromUrl(String imageUrl)
 | 
			
		||||
      throws IOException, URISyntaxException {
 | 
			
		||||
    URL url = new URI(imageUrl).toURL();
 | 
			
		||||
    try (InputStream in = url.openStream();
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
 | 
			
		||||
      byte[] buffer = new byte[1024];
 | 
			
		||||
      int bytesRead;
 | 
			
		||||
      while ((bytesRead = in.read(buffer)) != -1) {
 | 
			
		||||
        out.write(buffer, 0, bytesRead);
 | 
			
		||||
      }
 | 
			
		||||
      return out.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user