Skip to content

Commit 3ac77b1

Browse files
committed
Update to version 0.2 with minor code impromenet and standards
- Return rich error message. - Move the api link to properties files. - Structure improvement like adding interface and base class to the reusable/flexible implementations. - add license details to the files.
1 parent c7523fe commit 3ac77b1

File tree

18 files changed

+278
-41
lines changed

18 files changed

+278
-41
lines changed

core/com.intellijava.core/.classpath

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
</classpathentry>
99
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
1010
<attributes>
11+
<attribute name="test" value="true"/>
1112
<attribute name="optional" value="true"/>
1213
<attribute name="maven.pomderived" value="true"/>
13-
<attribute name="test" value="true"/>
1414
</attributes>
1515
</classpathentry>
16+
<classpathentry kind="src" path="resources"/>
1617
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
1718
<attributes>
1819
<attribute name="maven.pomderived" value="true"/>
@@ -25,9 +26,9 @@
2526
</classpathentry>
2627
<classpathentry kind="src" path="target/generated-sources/annotations">
2728
<attributes>
29+
<attribute name="ignore_optional_problems" value="true"/>
2830
<attribute name="optional" value="true"/>
2931
<attribute name="maven.pomderived" value="true"/>
30-
<attribute name="ignore_optional_problems" value="true"/>
3132
<attribute name="m2e-apt" value="true"/>
3233
</attributes>
3334
</classpathentry>

core/com.intellijava.core/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

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

1111
<name>com.intellijava.core</name>
12-
<!-- FIXME change it to the project's website -->
13-
<url>http://www.example.com</url>
12+
<url>https://github.com/Barqawiz/IntelliJava</url>
1413

1514
<properties>
1615
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
url.openai.base=https://api.openai.com
2+
url.openai.completions=/v1/completions
3+
url.openai.testkey=

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
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+
*/
116
package com.intellijava.com.intellijava.core.controller;
217

318
import java.io.IOException;
19+
import java.util.HashMap;
20+
import java.util.Map;
421

522
import com.intellijava.com.intellijava.core.model.OpenaiResponseModel;
623
import com.intellijava.com.intellijava.core.wrappers.OpenAIWrapper;
@@ -66,7 +83,13 @@ public String generateText(String model, String prompt, float temperature, int m
6683
*/
6784
private String generateOpensiText(String model, String prompt, float temperature, int maxTokens) throws IOException {
6885

69-
OpenaiResponseModel resModel = openaiWrapper.generateText(model, prompt, temperature, maxTokens);
86+
Map<String, Object> params = new HashMap<>();
87+
params.put("model", model);
88+
params.put("prompt", prompt);
89+
params.put("temperature", temperature);
90+
params.put("max_tokens", maxTokens);
91+
92+
OpenaiResponseModel resModel = (OpenaiResponseModel) openaiWrapper.generateText(params);
7093

7194
return resModel.getChoices().get(0).getText();
7295

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.model;
17+
18+
public abstract class BaseRemoteModel {
19+
private String id;
20+
21+
public String getId() {
22+
return id;
23+
}
24+
25+
public void setId(String id) {
26+
this.id = id;
27+
}
28+
}

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package com.intellijava.com.intellijava.core.model;
217

318
import java.util.List;
@@ -9,8 +24,8 @@
924
* A model class to parse openai completion API response.
1025
*
1126
*/
12-
public class OpenaiResponseModel {
13-
private String id;
27+
public class OpenaiResponseModel extends BaseRemoteModel {
28+
1429
private String object;
1530
private long created;
1631
private String model;
@@ -79,14 +94,6 @@ public void setTotal_tokens(int total_tokens) {
7994

8095
}
8196

82-
public String getId() {
83-
return id;
84-
}
85-
86-
public void setId(String id) {
87-
this.id = id;
88-
}
89-
9097
public String getObject() {
9198
return object;
9299
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.utils;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.Properties;
21+
22+
/**
23+
*
24+
* @author github.com/Barqawiz
25+
*
26+
* Read the configuration
27+
*
28+
*/
29+
public class Config2 {
30+
private static Config2 instance;
31+
32+
private Properties prop;
33+
34+
private Config2() {
35+
prop = new Properties();
36+
try (InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties")) {
37+
prop.load(input);
38+
} catch (IOException ex) {
39+
ex.printStackTrace();
40+
}
41+
}
42+
43+
public static Config2 getInstance() {
44+
if (instance == null) {
45+
instance = new Config2();
46+
}
47+
return instance;
48+
}
49+
50+
public String getProperty(String key) {
51+
return prop.getProperty(key);
52+
}
53+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
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+
*/
116
package com.intellijava.com.intellijava.core.utils;
217

18+
import java.io.BufferedReader;
19+
import java.io.IOException;
320
import java.io.InputStream;
421
import java.io.InputStreamReader;
522
import java.io.Reader;
23+
import java.net.HttpURLConnection;
624
import java.nio.charset.StandardCharsets;
725
import java.util.Map;
826

@@ -45,4 +63,30 @@ public static <T> T convertSteamToModel(InputStream stream, Class<T> classOfT) {
4563

4664
return resModel;
4765
}
66+
67+
68+
public static String getErrorMessage(HttpURLConnection connection) throws IOException {
69+
70+
String apiErrorMessage = readStream(connection.getErrorStream());
71+
72+
String errorMessage = "Unexpected HTTP response: " + connection.getResponseCode();
73+
74+
// add extra error details - if any
75+
if (apiErrorMessage != null) {
76+
errorMessage += " Error details:"+apiErrorMessage;
77+
}
78+
79+
return errorMessage;
80+
}
81+
82+
public static String readStream(InputStream stream) throws IOException {
83+
StringBuilder result = new StringBuilder();
84+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
85+
String line;
86+
while ((line = reader.readLine()) != null) {
87+
result.append(line);
88+
}
89+
}
90+
return result.toString();
91+
}
4892
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.wrappers;
17+
18+
import java.io.IOException;
19+
import java.util.Map;
20+
import com.intellijava.com.intellijava.core.model.BaseRemoteModel;
21+
22+
public interface LanguageModelInterface {
23+
24+
/**
25+
*
26+
* Generate text from remote large language model based on the received prompt.
27+
*
28+
* @param params key and value for the API parameters
29+
* @return BaseRemoteModel or any sub class
30+
* @throws IOException
31+
*/
32+
public BaseRemoteModel generateText(Map<String, Object> params) throws IOException;
33+
}

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package com.intellijava.com.intellijava.core.wrappers;
217

318
import java.io.IOException;
@@ -11,7 +26,9 @@
1126
import java.util.Map;
1227

1328
import com.google.gson.Gson;
29+
import com.intellijava.com.intellijava.core.model.BaseRemoteModel;
1430
import com.intellijava.com.intellijava.core.model.OpenaiResponseModel;
31+
import com.intellijava.com.intellijava.core.utils.Config2;
1532
import com.intellijava.com.intellijava.core.utils.ConnHelper;
1633

1734
/**
@@ -21,9 +38,9 @@
2138
* A wrapper to hide the complexity of openai API.
2239
*
2340
*/
24-
public class OpenAIWrapper {
41+
public class OpenAIWrapper implements LanguageModelInterface {
2542

26-
private final String API_BASE_URL = "https://api.openai.com";
43+
private final String API_BASE_URL = Config2.getInstance().getProperty("url.openai.base");
2744
private String API_KEY;
2845

2946
/**
@@ -43,14 +60,9 @@ public OpenAIWrapper(String apiKey) {
4360
* @return the model response.
4461
* @throws IOException
4562
*/
46-
public OpenaiResponseModel generateText(String model, String prompt, float temperature, int maxTokens) throws IOException {
47-
String url = API_BASE_URL + "/v1/completions";
48-
49-
Map<String, Object> params = new HashMap<>();
50-
params.put("model", model);
51-
params.put("prompt", prompt);
52-
params.put("temperature", temperature);
53-
params.put("max_tokens", maxTokens);
63+
public BaseRemoteModel generateText(Map<String, Object> params) throws IOException {
64+
65+
String url = API_BASE_URL + Config2.getInstance().getProperty("url.openai.completions");
5466

5567
String json = ConnHelper.convertMaptToJson(params);
5668

@@ -65,7 +77,8 @@ public OpenaiResponseModel generateText(String model, String prompt, float tempe
6577
}
6678

6779
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
68-
throw new IOException("Unexpected HTTP response: " + connection.getResponseCode());
80+
String errorMessage = ConnHelper.getErrorMessage(connection);
81+
throw new IOException(errorMessage);
6982
}
7083

7184
// get the response and convert to model

0 commit comments

Comments
 (0)