Skip to content

Commit 9f69bd7

Browse files
authored
Merge pull request #7 from Barqawiz/chat-gpt-plus-unification
Chat gpt plus unification
2 parents c836fa1 + b48a230 commit 9f69bd7

28 files changed

+1313
-95
lines changed

README.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img src="images/intelligent_java_header.png">
2+
<img src="images/intelligent_java_header.png" width="500em">
33
</p>
44

55
<p align="center">
@@ -16,22 +16,21 @@
1616
<img src="https://img.shields.io/github/license/Barqawiz/IntelliJava?style=flat-square" />
1717
</a>
1818

19-
<img src="https://awesome.re/badge.svg" />
2019
</p>
2120

2221
# Intelligent Java
23-
Intelligent java (IntelliJava) is the ultimate tool to integrate with the latest language models and deep learning frameworks using java. The library provides an intuitive functions for sending input to models like GPT-3 and DALL·E, and receiving generated text, speech or images. With just a few lines of code, you can easily access the power of cutting-edge AI models to enhance your projects.
22+
Intelligent java (IntelliJava) is the ultimate tool to integrate with the latest language models and deep learning frameworks using java. The library provides an intuitive functions for sending input to models like ChatGPT and DALL·E, and receiving generated text, speech or images. With just a few lines of code, you can easily access the power of cutting-edge AI models to enhance your projects.
2423

2524
The supported models:
26-
- **OpenAI**: Access GPT-3 to generate text and DALL·E to generate images. OpenAI is preferred for quality results without tuning.
25+
- **OpenAI**: Access ChatGPT, GPT3 to generate text and DALL·E to generate images. OpenAI is preferred for quality results without tuning.
2726
- **Cohere.ai**: Generate text; Cohere allows you to custom your language model to suit your specific needs.
2827
- **Google AI**: Generate audio from text; Access DeepMind’s speech models.
2928

3029
# How to use
3130

3231
1. Add the maven dependency or import the core jar file (check the Integration section).
3332
2. Add Gson dependency if using the jar file; otherwise, it's handled by maven or Gradle.
34-
3. Call the ``RemoteLanguageModel`` for the language models, ``RemoteImageModel`` for image generation and ``RemoteSpeechModel`` for text to speech models.
33+
3. Call the ``RemoteLanguageModel`` for the language models, ``RemoteImageModel`` for image generation and ``RemoteSpeechModel`` for text to speech models, ``Chatbot`` for ChatGPT.
3534

3635
## Integration
3736
The package released to Maven Central Repository:
@@ -41,23 +40,23 @@ Maven:
4140
<dependency>
4241
<groupId>io.github.barqawiz</groupId>
4342
<artifactId>intellijava.core</artifactId>
44-
<version>0.7.0</version>
43+
<version>0.8.0</version>
4544
</dependency>
4645
```
4746

4847
Gradle:
4948

5049
```
51-
implementation 'io.github.barqawiz:intellijava.core:0.7.0'
50+
implementation 'io.github.barqawiz:intellijava.core:0.8.0'
5251
```
5352

5453
Gradle(Kotlin):
5554
```
56-
implementation("io.github.barqawiz:intellijava.core:0.7.0")
55+
implementation("io.github.barqawiz:intellijava.core:0.8.0")
5756
```
5857

5958
Jar download:
60-
[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.7.0/intellijava.core-0.7.0.jar).
59+
[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.8.0/intellijava.core-0.8.0.jar).
6160

6261
## Code Example
6362
**Language model code** (2 steps):
@@ -72,7 +71,9 @@ LanguageModelInput langInput = new LanguageModelInput.Builder("Summarize the plo
7271
String resValue = langModel.generateText(langInput);
7372
```
7473
Output:```Inception follows Dom Cobb, a professional thief, who is offered a chance at redemption in exchange for planting an idea in a target's mind. He must navigate a dangerous landscape of dream-sharing technology and battle his inner demons in order to complete the mission and find his way back to reality.```
75-
<br><br>
74+
75+
<br>
76+
7677
**Image generation code** (2 steps):
7778
```java
7879
// 1- initiate the remote image model
@@ -85,7 +86,9 @@ List<String> images = imageModel.generateImages(imageInput);
8586
```
8687
Output:<br>
8788
<img src="images/response_image.png" height="220px">
88-
<br><br>
89+
90+
<br>
91+
8992
**Text to speech code** (2 steps):
9093
```java
9194
// 1- initiate the remote speech model
@@ -101,6 +104,26 @@ Output:<br>
101104
AudioHelper.saveTempAudio(decodedAudio);
102105
```
103106

107+
<br>
108+
109+
**ChatGPT code**:
110+
```java
111+
// 1- initiate the chat model.
112+
Chatbot bot = new Chatbot(apiKey, SupportedChatModels.openai);
113+
114+
// 2- prepare the chat history by calling addMessage.
115+
String mode = "You are a helpful astronomy assistant.";
116+
ChatModelInput input = new ChatGPTInput.Builder(mode)
117+
.addUserMessage("what is the space between moon and earth")
118+
.build();
119+
120+
// 3- call chat!
121+
List<String> resValues = bot.chat(input);
122+
```
123+
Output:``` The average distance between the Moon and the Earth is about 238,855 miles (384,400 kilometers). ```
124+
125+
<br>
126+
104127
For full examples and ready integration try [the sample_code](https://github.com/Barqawiz/IntelliJava/tree/main/sample_code).
105128

106129
## Third-party dependencies
@@ -116,7 +139,7 @@ For jar download:
116139

117140
# Roadmap
118141
Call for contributors:
119-
- [x] Add support to OpenAI Completion API.
142+
- [x] Add support to OpenAI Completion.
120143
- [x] Add support to OpenAI DALL·E 2.
121144
- [ ] Add support to other OpenAI functions.
122145
- [x] Add support to cohere generate API.

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>io.github.barqawiz</groupId>
88
<artifactId>intellijava.core</artifactId>
9-
<version>0.7.0</version>
9+
<version>0.8.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>

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

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,97 @@
2323

2424
import com.intellijava.core.model.OpenaiImageResponse;
2525
import com.intellijava.core.model.OpenaiImageResponse.Data;
26+
import com.intellijava.core.model.SupportedImageModels;
2627
import com.intellijava.core.model.input.ImageModelInput;
2728
import 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
*/
3739
public 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 {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.intellijava.core.model.OpenaiLanguageResponse;
2626
import com.intellijava.core.model.OpenaiLanguageResponse.Choice;
2727
import com.intellijava.core.model.SupportedLangModels;
28-
import com.intellijava.core.model.OpenaiImageResponse.Data;
2928
import com.intellijava.core.model.input.LanguageModelInput;
3029
import com.intellijava.core.wrappers.CohereAIWrapper;
3130
import com.intellijava.core.wrappers.OpenAIWrapper;
@@ -55,7 +54,7 @@ public class RemoteLanguageModel {
5554
/**
5655
* Constructor for the RemoteLanguageModel class.
5756
*
58-
* Creates an instance of the class and sets up the key and the API type.
57+
* Creates an instance of the class and set the API type and key.
5958
*
6059
* @param keyValue the API key.
6160
* @param keyTypeString either openai (default) or cohere or send empty string
@@ -83,8 +82,8 @@ public RemoteLanguageModel(String keyValue, String keyTypeString) {
8382
/**
8483
* Constructor for the RemoteLanguageModel class.
8584
*
86-
* Creates an instance of the class and sets up the API key and the enum key
87-
* type.
85+
* Creates an instance of the class and set the API enum type and key.
86+
*
8887
*
8988
* @param keyValue the API key.
9089
* @param keyType enum version from the key type (SupportedModels).

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import java.util.Map;
2323
import com.intellijava.core.model.AudioResponse;
2424
import com.intellijava.core.model.SpeechModels;
25-
import com.intellijava.core.model.input.SpeechInput;
26-
import com.intellijava.core.model.input.SpeechInput.Gender;
25+
import com.intellijava.core.model.input.Text2SpeechInput;
26+
import com.intellijava.core.model.input.Text2SpeechInput.Gender;
2727
import com.intellijava.core.utils.AudioHelper;
2828
import com.intellijava.core.wrappers.GoogleAIWrapper;
2929

@@ -118,7 +118,7 @@ public List<String> getSupportedModels() {
118118
* @return byte array of the decoded audio content.
119119
* @throws IOException in case of communication error.
120120
*/
121-
public byte[] generateEnglishText(SpeechInput input) throws IOException {
121+
public byte[] generateEnglishText(Text2SpeechInput input) throws IOException {
122122

123123
if (this.keyType == SpeechModels.google) {
124124
return this.generateGoogleText(input.getText(), input.getGender(), "en-gb");

0 commit comments

Comments
 (0)