Skip to content

Commit bafe539

Browse files
committed
make the input flexible and update the package
- update the package name to resolve repeated words. - update the group id to prepare for maven deployment. - update the model input as part of 0.4 release. - update readme documentation. Extra description: The model input is now more flexible to prepare to add more API beyond Openai with minimum change from the user to switch between the APIs. The update use builder patter for the input.
1 parent f4eff2f commit bafe539

File tree

24 files changed

+391
-171
lines changed

24 files changed

+391
-171
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# IntelliJava-OpenaiAPI
2-
*IntelliJava V0.3*
2+
*IntelliJava V0.4.0*
33

44
IntelliJava allows java developers to easily integrate with the latest language models and deep learning frameworks using few lines of java code.
55
The first version supports only Openai APIs. It provides a simple and intuitive API with convenient methods for sending text input to models like (GPT-3 and DALL·E) and receiving generated text or images in return.
@@ -16,34 +16,38 @@ For jar integration download:
1616
[intellijava.jar](https://insta-answer-public.s3.amazonaws.com/opensource/IntelliJava/version0.3/com.intellijava.core-0.3.jar).
1717

1818
For maven:
19-
[Add github dependency package](https://github.com/Barqawiz/IntelliJava/packages/1767035).
19+
```
20+
to be updated.
21+
```
2022

2123
For ready integration: try the sample_code.
2224

2325
## Code Example
2426
**Language model code** (2 steps):
25-
```
27+
```java
2628
// 1- initiate the remote language model
2729
String apiKey = "<add-openai-api-key>";
2830
RemoteLanguageModel langModel = new RemoteLanguageModel(apiKey, "openai");
2931

3032
// 2- call generateText with any command !
31-
String command = "return a java code that says hello wrold";
32-
String resValue = langModel.generateText("text-davinci-002", command, 0.5F, 100);
33+
LanguageModelInput langInput = new LanguageModelInput.Builder("return a java code that says hello world")
34+
.setModel("text-davinci-002").setTemperature(0.7f).setMaxTokens(50).build();
35+
String resValue = langModel.generateText(langInput);
3336
```
3437
Output:
3538
``` System.out.println("Hello, World!");```<br><br>
3639
**Image generation code** (2 steps):
37-
```
40+
```java
3841
// 1- initiate the remote image model
3942
RemoateImageModel imageModel = new RemoateImageModel(apiKey, "openai");
4043

4144
// 2- call generateImages with any command !
42-
String prompt = "teddy writing a blog in times square";
43-
List<String> images = imageModel.generateImages(prompt, 2/*number of images*/, "1024x1024");
45+
ImageModelInput imageInput = new ImageModelInput.Builder("teddy writing a blog in times square")
46+
.setNumberOfImages(2).setImageSize("1024x1024").build();
47+
List<String> images = imageModel.generateImages(imageInput);
4448
```
4549
Output:<br>
46-
<img src="images/response_image.png" height="250px">
50+
<img src="images/response_image.png" height="220px">
4751

4852
For full example check the code inside sample_code project.
4953

core/com.intellijava.core/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>com.intellijava</groupId>
8-
<artifactId>com.intellijava.core</artifactId>
9-
<version>0.3.1</version>
7+
<groupId>io.github.barqawiz</groupId>
8+
<artifactId>intellijava.core</artifactId>
9+
<version>0.4.0</version>
1010

1111
<name>Intellijava</name>
1212
<description>IntelliJava allows java developers to easily integrate with the latest language models, image generation, and deep learning frameworks.</description>
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.intellijava.com.intellijava.core.controller;
16+
package com.intellijava.core.controller;
1717

1818
import java.io.IOException;
1919
import java.util.ArrayList;
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23-
import com.intellijava.com.intellijava.core.model.OpenaiImageResponse;
24-
import com.intellijava.com.intellijava.core.model.OpenaiImageResponse.Data;
25-
import com.intellijava.com.intellijava.core.wrappers.OpenAIWrapper;
23+
24+
import com.intellijava.core.model.OpenaiImageResponse;
25+
import com.intellijava.core.model.OpenaiImageResponse.Data;
26+
import com.intellijava.core.model.input.ImageModelInput;
27+
import com.intellijava.core.wrappers.OpenAIWrapper;
2628

2729
/**
2830
*
@@ -58,23 +60,20 @@ public RemoateImageModel(String keyValue, String keyType) {
5860
throw new IllegalArgumentException("This version support openai keyType only");
5961
}
6062
}
61-
6263

6364
/**
6465
*
6566
* Generates images from a given text description.
6667
*
67-
* @param prompt text of the required action or the question.
68-
* @param numberOfImages number of the generated images.
69-
* @param imageSize size of the generated images, options are: 256x256, 512x512, or 1024x1024.
70-
* @return list of URLs of the generated images
71-
* @throws IOException if there is a problem with the API connection
72-
*
68+
* @param imageInput flexible builder for image model parameters.
69+
* @return list of URLs of the generated images.
70+
* @throws IOException if there is a problem with the API connection.
7371
*/
74-
public List<String> generateImages(String prompt, int numberOfImages, String imageSize) throws IOException {
72+
public List<String> generateImages(ImageModelInput imageInput) throws IOException {
7573

7674
if (this.keyType == "openai") {
77-
return this.generateOpenaiImage(prompt, numberOfImages, imageSize);
75+
return this.generateOpenaiImage(imageInput.getPrompt(),
76+
imageInput.getNumberOfImages(), imageInput.getImageSize());
7877
} else {
7978
throw new IllegalArgumentException("This version support openai keyType only");
8079
}
@@ -89,7 +88,8 @@ public List<String> generateImages(String prompt, int numberOfImages, String ima
8988
* @param numberOfImages number of the generated images.
9089
* @param imageSize size of the generated images, options are: 256x256, 512x512, or 1024x1024.
9190
* @return list of URLs of the generated images
92-
* @throws IOException if there is a problem with the API connection
91+
* @throws IOException if there is a problem with the API connection.
92+
*
9393
*/
9494
private List<String> generateOpenaiImage(String prompt, int numberOfImages, String imageSize) throws IOException {
9595

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.intellijava.com.intellijava.core.controller;
16+
package com.intellijava.core.controller;
1717

1818
import java.io.IOException;
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22-
import com.intellijava.com.intellijava.core.model.OpenaiLanguageResponse;
23-
import com.intellijava.com.intellijava.core.wrappers.OpenAIWrapper;
22+
import com.intellijava.core.model.OpenaiLanguageResponse;
23+
import com.intellijava.core.model.input.LanguageModelInput;
24+
import com.intellijava.core.wrappers.OpenAIWrapper;
2425

2526
/**
2627
* A class to call the most sophisticated remote language models.
@@ -60,23 +61,20 @@ public RemoteLanguageModel(String keyValue, String keyType) {
6061

6162

6263
/**
63-
* Call a remote large model to generate any text based on the received prompt.
64-
*
65-
* This method takes in a model name, prompt, temperature and maxTokens and generates text using the OpenAI GPT-3 model.
66-
*
67-
* @param model the model name. The largest OpenAI model is text-davinci-002
68-
* @param prompt text of the required action or the question.
69-
* @param temperature higher values means more risks and creativity.
70-
* @param maxTokens maximum size of the model input and output.
71-
* @return string model response.
72-
* @throws IOException if there is an error when connecting to the OpenAI API.
73-
* @throws IllegalArgumentException if the keyType passed in the constructor is not "openai".
74-
*
75-
*/
76-
public String generateText(String model, String prompt, float temperature, int maxTokens) throws IOException {
64+
*
65+
* Call a remote large model to generate any text based on the received prompt.
66+
*
67+
* @param langInput flexible builder for language model parameters.
68+
* @return string for the model response.
69+
* @throws IOException if there is an error when connecting to the OpenAI API.
70+
* @throws IllegalArgumentException if the keyType passed in the constructor is not "openai".
71+
*
72+
*/
73+
public String generateText(LanguageModelInput langInput) throws IOException {
7774

7875
if (this.keyType == "openai") {
79-
return this.generateOpenaiText(model, prompt, temperature, maxTokens);
76+
return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(),
77+
langInput.getTemperature(), langInput.getMaxTokens());
8078
} else {
8179
throw new IllegalArgumentException("This version support openai keyType only");
8280
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.intellijava.com.intellijava.core.model;
16+
package com.intellijava.core.model;
1717

1818

1919
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.intellijava.com.intellijava.core.model;
1+
package com.intellijava.core.model;
22

33
import java.util.List;
44
import com.google.gson.annotations.SerializedName;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.intellijava.com.intellijava.core.model;
16+
package com.intellijava.core.model;
1717

1818
import java.util.List;
1919

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright 2023 Github.com/Barqawiz/IntelliJava
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.intellijava.core.model.input;
17+
18+
/**
19+
*
20+
* ImageModelInput handle the input parameters for the majority of the remote image models.
21+
*
22+
* @author github.com/Barqawiz
23+
*
24+
*/
25+
public class ImageModelInput {
26+
//Fields
27+
private String prompt;
28+
private int numberOfImages;
29+
private String imageSize;
30+
31+
/**
32+
* Private Constructor for the Builder
33+
* @param builder: instance of Builder
34+
*/
35+
private ImageModelInput(Builder builder) {
36+
this.prompt = builder.prompt;
37+
this.numberOfImages = builder.numberOfImages;
38+
this.imageSize = builder.imageSize;
39+
}
40+
/**
41+
*
42+
* Builder class for ImageModelInput
43+
*/
44+
public static class Builder {
45+
private String prompt;
46+
private int numberOfImages;
47+
private String imageSize;
48+
/**
49+
* Constructor
50+
*/
51+
public Builder(String prompt) {
52+
this.prompt = prompt;
53+
}
54+
55+
/**
56+
* Setter for prompt
57+
* @param prompt : the text of the required action or the question.
58+
* @return: instance of Builder
59+
*/
60+
public Builder setPrompt(String prompt) {
61+
this.prompt = prompt;
62+
return this;
63+
}
64+
65+
/**
66+
* Setter for numberOfImages
67+
* @param numberOfImages : the number of the generated images.
68+
* @return: instance of Builder
69+
*/
70+
public Builder setNumberOfImages(int numberOfImages) {
71+
this.numberOfImages = numberOfImages;
72+
return this;
73+
}
74+
75+
/**
76+
* Setter for imageSize
77+
* @param imageSize : the size of the generated images, options are: 256x256, 512x512, or 1024x1024.
78+
* @return: instance of Builder
79+
*/
80+
public Builder setImageSize(String imageSize) {
81+
this.imageSize = imageSize;
82+
return this;
83+
}
84+
85+
/**
86+
* Build the final ImageModelInput object
87+
* @return: final ImageModelInput object
88+
*/
89+
public ImageModelInput build() {
90+
return new ImageModelInput(this);
91+
}
92+
}
93+
/**
94+
* Getter for prompt
95+
* @return: prompt
96+
*/
97+
public String getPrompt() {
98+
return prompt;
99+
}
100+
101+
/**
102+
* Getter for numberOfImages
103+
* @return: numberOfImages
104+
*/
105+
public int getNumberOfImages() {
106+
return numberOfImages;
107+
}
108+
109+
/**
110+
* Getter for imageSize
111+
* @return: imageSize
112+
*/
113+
public String getImageSize() {
114+
return imageSize;
115+
}
116+
}
117+

0 commit comments

Comments
 (0)