Skip to content

Commit 88ac595

Browse files
committed
support chatGPT communication
- Create chatGPT wrapper. - Create chatGPT input and output. - Create chat bot function. - Create chatGPT test cases. - Update the sample project to support latest changes.
1 parent e4bd045 commit 88ac595

File tree

17 files changed

+804
-22
lines changed

17 files changed

+804
-22
lines changed

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/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");
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.intellijava.core.function;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import com.intellijava.core.model.OpenaiChatResponse;
10+
import com.intellijava.core.model.OpenaiChatResponse.Choice;
11+
import com.intellijava.core.model.SupportedChatModels;
12+
import com.intellijava.core.model.input.ChatGPTInput;
13+
import com.intellijava.core.model.input.ChatModelInput;
14+
import com.intellijava.core.wrappers.OpenAIWrapper;
15+
16+
/**
17+
*
18+
* Chatbot controller for most sophisticated AI chatbots.
19+
*
20+
* @author github.com/Barqawiz
21+
*
22+
*/
23+
public class Chatbot {
24+
25+
private SupportedChatModels keyType;
26+
private OpenAIWrapper openaiWrapper;
27+
28+
/**
29+
*
30+
* Constructor for the Chatbot class.
31+
*
32+
* Creates class instance and set the API type and key.
33+
*
34+
* @param keyValue the API key.
35+
* @param keyTypeString either openai (default) or cohere or send empty string
36+
* for default value.
37+
*
38+
* @throws IllegalArgumentException if the keyType passed is not "openai".
39+
*
40+
*/
41+
public Chatbot(String keyValue, String keyTypeString) {
42+
43+
if (keyTypeString.isEmpty()) {
44+
keyTypeString = SupportedChatModels.openai.toString();
45+
}
46+
47+
List<String> supportedModels = this.getSupportedModels();
48+
49+
if (supportedModels.contains(keyTypeString)) {
50+
this.initiate(keyValue, SupportedChatModels.valueOf(keyTypeString));
51+
} else {
52+
String models = String.join(" - ", supportedModels);
53+
throw new IllegalArgumentException("The received keyValue not supported. Send any model from: " + models);
54+
}
55+
}
56+
57+
/**
58+
* Constructor for the Chatbot class.
59+
*
60+
* Creates class instance and set the API enum type and key.
61+
*
62+
* @param keyValue the API key.
63+
* @param keyType enum version from the key type (SupportedModels).
64+
*
65+
* @throws IllegalArgumentException if the keyType passed is not "openai".
66+
*
67+
*/
68+
public Chatbot(String keyValue, SupportedChatModels keyType) {
69+
this.initiate(keyValue, keyType);
70+
}
71+
72+
/**
73+
* Get the supported models names as array of string
74+
*
75+
* @return supportedModels
76+
*/
77+
public List<String> getSupportedModels() {
78+
SupportedChatModels[] values = SupportedChatModels.values();
79+
List<String> enumValues = new ArrayList<>();
80+
81+
for (int i = 0; i < values.length; i++) {
82+
enumValues.add(values[i].name());
83+
}
84+
85+
return enumValues;
86+
}
87+
88+
/**
89+
* Common function to initiate the class from any constructor.
90+
*
91+
* @param keyValue the API key.
92+
* @param keyType enum version from the key type (SupportedModels).
93+
*/
94+
private void initiate(String keyValue, SupportedChatModels keyType) {
95+
// set the model type
96+
this.keyType = keyType;
97+
98+
// generate the related model
99+
if (keyType.equals(SupportedChatModels.openai)) {
100+
this.openaiWrapper = new OpenAIWrapper(keyValue);
101+
}
102+
}
103+
104+
/**
105+
*
106+
* Call a chat model to generate response based on the received messages history.
107+
*
108+
* To support multiple response call the variation function generateMultiText.
109+
*
110+
* @param modelInput language model parameters.
111+
*
112+
* @return the model response.
113+
*
114+
* @throws IOException if there is an error when connecting to the chat model.
115+
*
116+
*/
117+
public List<String> chat(ChatModelInput modelInput) throws IOException {
118+
119+
if (this.keyType.equals(SupportedChatModels.openai)) {
120+
return this.chatGPT((ChatGPTInput) modelInput);
121+
} else {
122+
throw new IllegalArgumentException("the keyType not supported");
123+
}
124+
}
125+
126+
127+
/**
128+
*
129+
* Call a chatGPT to generate response based on the received messages history.
130+
*
131+
* @param modelInput for chatGPT.
132+
*
133+
* @return string for the model response.
134+
*
135+
* @throws IOException if there is an error when connecting to the chatGPT.
136+
*/
137+
private List<String> chatGPT(ChatGPTInput modelInput) throws IOException {
138+
139+
Map<String, Object> params = new HashMap<>();
140+
params.put("model", modelInput.getModel());
141+
params.put("messages", modelInput.getMessages());
142+
if (modelInput.getNumberOfOutputs() >= 0) {
143+
params.put("n", modelInput.getNumberOfOutputs());
144+
}
145+
146+
if (modelInput.getTemperature() >= 0) {
147+
params.put("temperature", modelInput.getTemperature());
148+
}
149+
150+
if (modelInput.getMaxTokens() >= 1) {
151+
params.put("max_tokens", modelInput.getMaxTokens());
152+
}
153+
154+
OpenaiChatResponse resModel = (OpenaiChatResponse) openaiWrapper.generateChatText(params);
155+
156+
List<String> outputs = new ArrayList<>();
157+
for (Choice item : resModel.getChoices()) {
158+
outputs.add(item.getMessage().getContent());
159+
}
160+
161+
return outputs;
162+
}
163+
164+
165+
}

core/com.intellijava.core/src/main/java/com/intellijava/core/model/OpenaiChatResponse.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public Choice() {
4545
/**
4646
* Gets the model message.
4747
*
48-
* @return message
48+
* @return message model response message.
4949
*/
5050
public Message getMessage() {
5151
return message;
5252
}
5353

5454
/**
55-
* Sets the model message
55+
* Sets the model message.
5656
*
57-
* @param the message object of the model response.
57+
* @param message model response message.
5858
*/
5959
public void setMessage(Message message) {
6060
this.message = message;
@@ -63,7 +63,7 @@ public void setMessage(Message message) {
6363
/**
6464
* Gets the reason to end the message for validating missing response reasons.
6565
*
66-
* @return reason string
66+
* @return reason string.
6767
*/
6868
public String getFinish_reason() {
6969
return finish_reason;
@@ -73,7 +73,7 @@ public String getFinish_reason() {
7373
*
7474
* Sets the reason to end the message text
7575
*
76-
* @param finish_reason
76+
* @param finish_reason string.
7777
*/
7878
public void setFinish_reason(String finish_reason) {
7979
this.finish_reason = finish_reason;
@@ -171,6 +171,9 @@ public static class Message {
171171
private String role;
172172
private String content;
173173

174+
/**
175+
* Message default constructor.
176+
*/
174177
public Message() {}
175178

176179
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.intellijava.core.model;
2+
3+
/**
4+
* Supported chat models.
5+
*
6+
* @author github.com/Barqawiz
7+
*
8+
*/
9+
public enum SupportedChatModels {
10+
/** openai model */openai
11+
}

0 commit comments

Comments
 (0)