2323
2424import com .intellijava .core .model .OpenaiImageResponse ;
2525import com .intellijava .core .model .OpenaiImageResponse .Data ;
26+ import com .intellijava .core .model .SupportedImageModels ;
2627import com .intellijava .core .model .input .ImageModelInput ;
2728import com .intellijava .core .wrappers .OpenAIWrapper ;
2829
2930/**
3031 *
3132 * The RemoteImageModel class is used to generate images from text descriptions using an API key.
3233 * It currently supports OpenAI only.
34+ *
3335 * The class uses the OpenAIWrapper to generate the images and returns a list of URLs for the generated images.
3436 *
3537 * @author github.com/Barqawiz
3638 */
3739public class RemoteImageModel {
3840
39- private String keyType ;
41+ private SupportedImageModels keyType ;
4042 private OpenAIWrapper openaiWrapper ;
4143
4244 /**
4345 *
4446 * Constructor for creating a new RemoteImageModel object.
4547 *
46- * Creates an instance of the class and sets up the API key and the key type.
48+ * Creates an instance of the class and set the API key value and type.
4749 *
4850 * @param keyValue the API key.
49- * @param keyType support openai only.
51+ * @param keyTypeString support openai only.
5052 *
5153 * @throws IllegalArgumentException if the keyType passed is not "openai".
5254 *
5355 */
54- public RemoteImageModel (String keyValue , String keyType ) {
56+ public RemoteImageModel (String keyValue , String keyTypeString ) {
5557
56- if (keyType .isEmpty () || keyType .equals ("openai" )) {
57- this .keyType = "openai" ;
58- openaiWrapper = new OpenAIWrapper (keyValue );
58+ if (keyTypeString .isEmpty ()) {
59+ keyTypeString = SupportedImageModels .openai .toString ();
60+ }
61+
62+ List <String > supportedModels = this .getSupportedModels ();
63+
64+ if (supportedModels .contains (keyTypeString )) {
65+ this .initiate (keyValue , SupportedImageModels .valueOf (keyTypeString ));
5966 } else {
60- throw new IllegalArgumentException ("This version support openai keyType only" );
67+ String models = String .join (" - " , supportedModels );
68+ throw new IllegalArgumentException ("The received keyValue not supported. Send any model from: " + models );
6169 }
6270 }
6371
72+ /**
73+ *
74+ * Constructor for creating a new RemoteImageModel object.
75+ *
76+ * Creates an instance of the class and set the API key value and type.
77+ *
78+ * @param keyValue the API key.
79+ * @param keyType the model type from SupportedImageModels enum.
80+ */
81+ public RemoteImageModel (String keyValue , SupportedImageModels keyType ) {
82+ initiate (keyValue , keyType );
83+ }
84+
85+ /**
86+ * Get the supported models names as array of string
87+ *
88+ * @return supportedModels
89+ */
90+ public List <String > getSupportedModels () {
91+ SupportedImageModels [] values = SupportedImageModels .values ();
92+ List <String > enumValues = new ArrayList <>();
93+
94+ for (int i = 0 ; i < values .length ; i ++) {
95+ enumValues .add (values [i ].name ());
96+ }
97+
98+ return enumValues ;
99+ }
100+
101+ /**
102+ * Common function to initiate the class from any constructor.
103+ *
104+ * @param keyValue the API key.
105+ * @param keyType enum of supported models.
106+ */
107+ private void initiate (String keyValue , SupportedImageModels keyType ) {
108+ // set the model type
109+ this .keyType = keyType ;
110+
111+ // generate the related model
112+ if (keyType .equals (SupportedImageModels .openai )) {
113+ this .openaiWrapper = new OpenAIWrapper (keyValue );
114+ }
115+ }
116+
64117 /**
65118 *
66119 * Generates images from a given text description.
@@ -71,7 +124,7 @@ public RemoteImageModel(String keyValue, String keyType) {
71124 */
72125 public List <String > generateImages (ImageModelInput imageInput ) throws IOException {
73126
74- if (this .keyType . equals ( " openai" ) ) {
127+ if (this .keyType == SupportedImageModels . openai ) {
75128 return this .generateOpenaiImage (imageInput .getPrompt (),
76129 imageInput .getNumberOfImages (), imageInput .getImageSize ());
77130 } else {
0 commit comments