
prerna.reactor.model.VisionReactor Maven / Gradle / Ivy
The newest version!
package prerna.reactor.model;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import prerna.auth.User;
import prerna.auth.utils.SecurityEngineUtils;
import prerna.engine.api.IModelEngine;
import prerna.reactor.AbstractReactor;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Utility;
public class VisionReactor extends AbstractReactor {
public VisionReactor() {
this.keysToGet = new String[] { ReactorKeysEnum.ENGINE.getKey(), ReactorKeysEnum.COMMAND.getKey(),
ReactorKeysEnum.IMAGE.getKey(), ReactorKeysEnum.PARAM_VALUES_MAP.getKey() };
this.keyRequired = new int[] { 1, 1, 1, 0 };
}
@Override
public NounMetadata execute() {
organizeKeys();
String engineId = this.keyValue.get(this.keysToGet[0]);
User user = this.insight.getUser();
if (!SecurityEngineUtils.userCanViewEngine(user, engineId)) {
throw new IllegalArgumentException(
"Model " + engineId + " does not exist or user does not have access to this model");
}
String prompt = Utility.decodeURIComponent(this.keyValue.get(this.keysToGet[1]));
String image = this.keyValue.get(this.keysToGet[2]);
// We do NOT want to decode base64 encoded images
if (image.startsWith("http")) {
image = Utility.decodeURIComponent(image);
}
Map paramMap = getMap();
IModelEngine modelEngine = Utility.getModel(engineId);
if (paramMap == null) {
paramMap = new HashMap();
}
paramMap.put("image_url", image);
Map output = modelEngine.ask(prompt, null, this.insight, paramMap).toMap();
return new NounMetadata(output, PixelDataType.MAP, PixelOperationType.OPERATION);
}
/**
*
* @return
*/
private Map getMap() {
GenRowStruct mapGrs = this.store.getNoun(keysToGet[3]);
if(mapGrs != null && !mapGrs.isEmpty()) {
List mapInputs = mapGrs.getNounsOfType(PixelDataType.MAP);
if(mapInputs != null && !mapInputs.isEmpty()) {
return (Map) mapInputs.get(0).getValue();
}
}
List mapInputs = this.curRow.getNounsOfType(PixelDataType.MAP);
if(mapInputs != null && !mapInputs.isEmpty()) {
return (Map) mapInputs.get(0).getValue();
}
return null;
}
@Override
public String getReactorDescription() {
return "This method is used to run a vision task with an Image-Text-to-Text model";
}
@Override
protected String getDescriptionForKey(String key) {
if(key.equals(ReactorKeysEnum.COMMAND.getKey())) {
return "This is the vision prompt to execute against the model";
} else if(key.equals(ReactorKeysEnum.IMAGE.getKey())) {
return "The image URL or base64 string for the vision task.";
}
return super.getDescriptionForKey(key);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy