Skip to content

Commit a9ed1d4

Browse files
committed
Add support to image generation
- support image generation API. - update the documentation. - minor structure updates. - update the examples.
1 parent d50ddaf commit a9ed1d4

File tree

19 files changed

+410
-43
lines changed

19 files changed

+410
-43
lines changed

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# IntelliJava-OpenaiAPI
2-
*IntelliJava V0.2 beta*
2+
*IntelliJava V0.3*
33

44
IntelliJava allows java developers to easily integrate with the latest language models and deep learning frameworks like GPT-3 using few lines of code.
5-
The first version supports only Openai APIs. It provides a simple and intuitive API with convenient methods for sending text input to GPT-3 and receiving generated text in return.
5+
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 in return.
66

77

88
# How to use
9-
1. Import the core jar file to your project [intellijava.jar](https://insta-answer-public.s3.amazonaws.com/opensource/IntelliJava/version0.2/com.intellijava.core-0.2.jar).
9+
1. Import the core jar file to your project [intellijava.jar](https://insta-answer-public.s3.amazonaws.com/opensource/IntelliJava/version0.3/com.intellijava.core-0.3.jar).
1010
2. Add gson dependency using maven or the jar file (check the dependencies section).
11-
3. Call the RemoteLanguageModel as described in the code example.
11+
3. Call the ``RemoteLanguageModel`` for the language model and ``RemoateImageModel`` for image generation.
1212

13-
# Code Example
13+
## Code Example
14+
**Language model code** (2 steps):
1415
```
1516
// 1- initiate the remote language model
16-
String apiKey = "<todo-add-api-key>";
17-
RemoteLanguageModel wrapper = new RemoteLanguageModel(apiKey, "openai");
17+
String apiKey = "<add-openai-api-key>";
18+
RemoteLanguageModel langModel = new RemoteLanguageModel(apiKey, "openai");
1819
1920
// 2- call generateText with any command !
2021
String command = "return a java code that says hello wrold";
21-
String resValue = wrapper.generateText("text-davinci-002", command, 0.5F, 100);
22+
String resValue = langModel.generateText("text-davinci-002", command, 0.5F, 100);
2223
```
23-
For full example check the samples projects inside sample_code folder.
24+
Output:
25+
``` System.out.println("Hello, World!");```<br><br>
26+
**Image generation code** (2 steps):
27+
```
28+
// 1- initiate the remote image model
29+
RemoateImageModel imageModel = new RemoateImageModel(apiKey, "openai");
30+
31+
// 2- call generateImages with any command !
32+
String prompt = "teddy writing a blog in times square";
33+
List<String> images = imageModel.generateImages(prompt, 2/*number of images*/, "1024x1024");
34+
```
35+
Output:<br>
36+
<img src="images/response_image.png" height="250px">
37+
38+
For full example check the code inside sample_code project.
2439

25-
# Dependencies
40+
## Third-party dependencies
2641
The only dependencies is **GSON**.
2742

2843
For Maven:
@@ -48,7 +63,7 @@ For jar download:
4863
# Roadmap
4964
Call for contributors:
5065
- [x] Add support to OpenAI Completion API.
51-
- [ ] Add support to OpenAI DALL·E 2 (under development).
66+
- [x] Add support to OpenAI DALL·E 2.
5267
- [ ] Add support to other OpenAI functions.
5368
- [ ] Add support to Google language models.
5469
- [ ] Add support to Amazon language models.

core/com.intellijava.core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.intellijava</groupId>
88
<artifactId>com.intellijava.core</artifactId>
9-
<version>0.2</version>
9+
<version>0.3</version>
1010

1111
<name>com.intellijava.core</name>
1212
<url>https://github.com/Barqawiz/IntelliJava</url>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
url.openai.base=https://api.openai.com
22
url.openai.completions=/v1/completions
3+
url.openai.imagegenerate=/v1/images/generations
34
url.openai.testkey=
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.com.intellijava.core.controller;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
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;
26+
27+
/**
28+
*
29+
* @author github.com/Barqawiz
30+
*
31+
* A class to call the most sophisticated remote image models.
32+
*
33+
* This version support Openai GPT model only, with a plan to add more models in the future.
34+
*
35+
*/
36+
public class RemoateImageModel {
37+
38+
private String keyType;
39+
private OpenAIWrapper openaiWrapper;
40+
41+
/**
42+
*
43+
* @param keyValue the API key.
44+
* @param keyType support openai only.
45+
*/
46+
public RemoateImageModel(String keyValue, String keyType) {
47+
48+
if (keyType == "" || keyType == "openai") {
49+
this.keyType = "openai";
50+
openaiWrapper = new OpenAIWrapper(keyValue);
51+
} else {
52+
throw new IllegalArgumentException("This version support openai keyType only");
53+
}
54+
}
55+
56+
57+
/**
58+
*
59+
* Generate images from any text description.
60+
*
61+
* @param prompt text of the required action or the question.
62+
* @param numberOfImages number of the generated images.
63+
* @param imageSize 256x256, 512x512, or 1024x1024.
64+
* @return List of images URL.
65+
* @throws IOException
66+
*/
67+
public List<String> generateImages(String prompt, int numberOfImages, String imageSize) throws IOException {
68+
69+
if (this.keyType == "openai") {
70+
return this.generateOpenaiImage(prompt, numberOfImages, imageSize);
71+
} else {
72+
throw new IllegalArgumentException("This version support openai keyType only");
73+
}
74+
75+
}
76+
77+
/**
78+
*
79+
* @param prompt text of the required action or the question.
80+
* @param numberOfImages number of the generated images.
81+
* @param imageSize 256x256, 512x512, or 1024x1024.
82+
* @return List of images URL.
83+
* @throws IOException
84+
*/
85+
private List<String> generateOpenaiImage(String prompt, int numberOfImages, String imageSize) throws IOException {
86+
87+
List<String> images = new ArrayList<>();
88+
89+
Map<String, Object> params = new HashMap<>();
90+
params.put("prompt", prompt);
91+
params.put("n", numberOfImages);
92+
params.put("size", imageSize);
93+
94+
OpenaiImageResponse resModel = (OpenaiImageResponse) openaiWrapper.generateImages(params);
95+
96+
List<Data> responseImages = resModel.getData();
97+
for (Data data: responseImages) {
98+
images.add(data.getUrl().toString());
99+
}
100+
101+
return images;
102+
103+
}
104+
}

core/com.intellijava.core/src/main/java/com/intellijava/com/intellijava/core/controller/RemoteLanguageModel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22-
import com.intellijava.com.intellijava.core.model.OpenaiResponseModel;
22+
import com.intellijava.com.intellijava.core.model.OpenaiLanguageResponse;
2323
import com.intellijava.com.intellijava.core.wrappers.OpenAIWrapper;
2424

2525
/**
@@ -65,7 +65,7 @@ public RemoteLanguageModel(String keyValue, String keyType) {
6565
public String generateText(String model, String prompt, float temperature, int maxTokens) throws IOException {
6666

6767
if (this.keyType == "openai") {
68-
return this.generateOpensiText(model, prompt, temperature, maxTokens);
68+
return this.generateOpenaiText(model, prompt, temperature, maxTokens);
6969
} else {
7070
throw new IllegalArgumentException("This version support openai keyType only");
7171
}
@@ -78,18 +78,18 @@ public String generateText(String model, String prompt, float temperature, int m
7878
* @param prompt text of the required action or the question.
7979
* @param temperature higher values means more risks and creativity.
8080
* @param maxTokens maximum size of the model input and output.
81-
* @return string model response
81+
* @return string model response.
8282
* @throws IOException
8383
*/
84-
private String generateOpensiText(String model, String prompt, float temperature, int maxTokens) throws IOException {
84+
private String generateOpenaiText(String model, String prompt, float temperature, int maxTokens) throws IOException {
8585

8686
Map<String, Object> params = new HashMap<>();
8787
params.put("model", model);
8888
params.put("prompt", prompt);
8989
params.put("temperature", temperature);
9090
params.put("max_tokens", maxTokens);
9191

92-
OpenaiResponseModel resModel = (OpenaiResponseModel) openaiWrapper.generateText(params);
92+
OpenaiLanguageResponse resModel = (OpenaiLanguageResponse) openaiWrapper.generateText(params);
9393

9494
return resModel.getChoices().get(0).getText();
9595

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.intellijava.com.intellijava.core.model;
2+
3+
import java.util.List;
4+
import com.google.gson.annotations.SerializedName;
5+
6+
public class OpenaiImageResponse extends BaseRemoteModel {
7+
8+
9+
private long created;
10+
11+
12+
private List<Data> data;
13+
14+
public static class Data {
15+
private String url;
16+
17+
public String getUrl() {
18+
return url;
19+
}
20+
}
21+
22+
public long getCreated() {
23+
return created;
24+
}
25+
26+
public List<Data> getData() {
27+
return data;
28+
}
29+
30+
}

core/com.intellijava.core/src/main/java/com/intellijava/com/intellijava/core/model/OpenaiResponseModel.java renamed to core/com.intellijava.core/src/main/java/com/intellijava/com/intellijava/core/model/OpenaiLanguageResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* A model class to parse openai completion API response.
2525
*
2626
*/
27-
public class OpenaiResponseModel extends BaseRemoteModel {
27+
public class OpenaiLanguageResponse extends BaseRemoteModel {
2828

2929
private String object;
3030
private long created;

core/com.intellijava.core/src/main/java/com/intellijava/com/intellijava/core/utils/ConnHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.Map;
2626

2727
import com.google.gson.Gson;
28-
import com.intellijava.com.intellijava.core.model.OpenaiResponseModel;
28+
import com.intellijava.com.intellijava.core.model.OpenaiLanguageResponse;
2929

3030
/**
3131
*
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.intellijava.com.intellijava.core.wrappers;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
6+
import com.intellijava.com.intellijava.core.model.BaseRemoteModel;
7+
8+
public interface ImageModelInterface {
9+
10+
/**
11+
*
12+
* @param params map of input parameters keys and values
13+
* @return
14+
* @throws IOException
15+
*/
16+
public BaseRemoteModel generateImages(Map<String, Object> params) throws IOException;
17+
}

core/com.intellijava.core/src/main/java/com/intellijava/com/intellijava/core/wrappers/OpenAIWrapper.java

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
import com.google.gson.Gson;
2929
import com.intellijava.com.intellijava.core.model.BaseRemoteModel;
30-
import com.intellijava.com.intellijava.core.model.OpenaiResponseModel;
30+
import com.intellijava.com.intellijava.core.model.OpenaiImageResponse;
31+
import com.intellijava.com.intellijava.core.model.OpenaiLanguageResponse;
3132
import com.intellijava.com.intellijava.core.utils.Config2;
3233
import com.intellijava.com.intellijava.core.utils.ConnHelper;
3334

@@ -38,7 +39,7 @@
3839
* A wrapper to hide the complexity of openai API.
3940
*
4041
*/
41-
public class OpenAIWrapper implements LanguageModelInterface {
42+
public class OpenAIWrapper implements LanguageModelInterface, ImageModelInterface {
4243

4344
private final String API_BASE_URL = Config2.getInstance().getProperty("url.openai.base");
4445
private String API_KEY;
@@ -52,14 +53,17 @@ public OpenAIWrapper(String apiKey) {
5253
}
5354

5455
/**
55-
*
56-
* @param model the model name, example: text-davinci-002. For more details about GPT3 models: https://beta.openai.com/docs/models/gpt-3
57-
* @param prompt text of the required action or the question.
58-
* @param temperature higher values means more risks and creativity.
59-
* @param maxTokens maximum size of the model input and output.
60-
* @return the model response.
61-
* @throws IOException
62-
*/
56+
*
57+
* Generate text from remote large language model based on the received prompt.
58+
*
59+
* @param params key and value for the API parameters
60+
* model the model name, example: text-davinci-002. For more details about GPT3 models: https://beta.openai.com/docs/models/gpt-3
61+
* prompt text of the required action or the question.
62+
* temperature higher values means more risks and creativity.
63+
* maxTokens maximum size of the model input and output.
64+
* @return BaseRemoteModel for model response
65+
* @throws IOException
66+
*/
6367
public BaseRemoteModel generateText(Map<String, Object> params) throws IOException {
6468

6569
String url = API_BASE_URL + Config2.getInstance().getProperty("url.openai.completions");
@@ -82,7 +86,42 @@ public BaseRemoteModel generateText(Map<String, Object> params) throws IOExcepti
8286
}
8387

8488
// get the response and convert to model
85-
OpenaiResponseModel resModel = ConnHelper.convertSteamToModel(connection.getInputStream(), OpenaiResponseModel.class);
89+
OpenaiLanguageResponse resModel = ConnHelper.convertSteamToModel(connection.getInputStream(), OpenaiLanguageResponse.class);
90+
return resModel;
91+
}
92+
93+
/**
94+
*
95+
* @param params should include prompt, n, size
96+
* prompt: text of the required action or the question.
97+
* n: number of the generated images.
98+
* size: 256x256, 512x512, or 1024x1024.
99+
* @return
100+
* @throws IOException
101+
*/
102+
public BaseRemoteModel generateImages(Map<String, Object> params) throws IOException {
103+
104+
String url = API_BASE_URL + Config2.getInstance().getProperty("url.openai.imagegenerate");
105+
106+
String json = ConnHelper.convertMaptToJson(params);
107+
108+
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
109+
connection.setRequestMethod("POST");
110+
connection.setRequestProperty("Content-Type", "application/json");
111+
connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
112+
connection.setDoOutput(true);
113+
114+
try (OutputStream outputStream = connection.getOutputStream()) {
115+
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
116+
}
117+
118+
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
119+
String errorMessage = ConnHelper.getErrorMessage(connection);
120+
throw new IOException(errorMessage);
121+
}
122+
123+
// get the response and convert to model
124+
OpenaiImageResponse resModel = ConnHelper.convertSteamToModel(connection.getInputStream(), OpenaiImageResponse.class);
86125
return resModel;
87126
}
88127
}

0 commit comments

Comments
 (0)