Skip to content

Commit 2178b99

Browse files
committed
project to use utf-8 encoding to support all languages
1 parent f84b701 commit 2178b99

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

build.gradle.kts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "io.github.jetkai"
7-
version = "1.1.1"
7+
version = "1.1.2"
88

99
java {
1010
//withSourcesJar()
@@ -26,11 +26,26 @@ dependencies {
2626
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
2727
}
2828

29+
/**
30+
* Character Encoding
31+
* --- START ---
32+
*/
33+
tasks.withType<JavaExec> {
34+
jvmArgs = listOf("-Dfile.encoding=UTF-8", "-Dconsole.encoding=UTF-8")
35+
}
36+
37+
tasks.compileJava {
38+
options.encoding = "UTF-8"
39+
}
40+
/**
41+
* Character Encoding
42+
* --- END ---
43+
*/
44+
2945
/**
3046
* Set Environmental Variables for the OPEN_AI_API_KEY
3147
* Called by System.getenv("OPEN_AI_API_KEY"); in source-code
3248
*/
33-
3449
tasks.register("setEnvironmentVariable") {
3550
doFirst {
3651
val env = System.getenv().toMutableMap()
@@ -45,11 +60,8 @@ tasks.register("setEnvironmentVariable") {
4560

4661
tasks.withType<Jar> {
4762
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
48-
4963
archiveFileName.set("openai-binary.jar")
50-
5164
from(sourceSets.main.get().output)
52-
5365
dependsOn(configurations.runtimeClasspath)
5466
from({
5567
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
@@ -64,7 +76,6 @@ tasks.getByName<Test>("test") {
6476
* Jacoco Testing Plugin
6577
* --- START ---
6678
*/
67-
6879
tasks.test {
6980
finalizedBy(tasks.jacocoTestReport)
7081
}
@@ -75,7 +86,6 @@ tasks.jacocoTestReport {
7586
}
7687
dependsOn(tasks.test)
7788
}
78-
7989
/**
8090
* Jacoco Testing Plugin
8191
* --- END ---

src/main/java/examples/ExampleChatGPT.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* ExampleChatGPT
1515
*
1616
* @author <a href="https://github.com/jetkai">Kai</a>
17-
* @version 1.1.0
17+
* @version 1.1.2
1818
* {@code - 07/03/2023}
1919
* @since 1.0.0
2020
* {@code - 05/03/2023}
@@ -32,8 +32,10 @@ final class ExampleChatGPT {
3232
public static void main(String[] args) {
3333
ExampleChatGPT gpt = new ExampleChatGPT();
3434

35+
System.setProperty("console.encoding", "UTF-8");
36+
3537
//The first message that we want to send
36-
String message1 = "Hello ChatGPT!";
38+
String message1 = "こんにちは";
3739
//The second message that we want to send
3840
String message2 = "What was the first thing I said?";
3941

@@ -81,8 +83,8 @@ private String exampleBuilders(String message) {
8183
//Including both our messages and the AI's messages
8284
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
8385

84-
//Parse the response back as plain-text & replace \n & ascii characters (Šťŕĭńġ -> String)
85-
return createChatCompletion.asNormalizedText();
86+
//Parse the response back as plain-text
87+
return createChatCompletion.asText();
8688
}
8789

8890
private String exampleInstanceOrEnum(String message) {
@@ -110,8 +112,8 @@ private String exampleInstanceOrEnum(String message) {
110112
//Including both our messages and the AI's messages
111113
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
112114

113-
//Parse the response back as plain-text & replace \n & ascii characters (Šťŕĭńġ -> String)
114-
return createChatCompletion.asNormalizedText();
115+
//Parse the response back as plain-text
116+
return createChatCompletion.asText();
115117
}
116118

117119
private String exampleHttpClientProxy(String message) {
@@ -143,8 +145,8 @@ private String exampleHttpClientProxy(String message) {
143145
//Including both our messages and the AI's messages
144146
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
145147

146-
//Parse the response back as plain-text & replace \n & ascii characters (Šťŕĭńġ -> String)
147-
return createChatCompletion.asNormalizedText();
148+
//Parse the response back as plain-text
149+
return createChatCompletion.asText();
148150
}
149151

150152
}

src/main/java/io/github/jetkai/openai/api/CreateCompletion.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.text.Normalizer;
1111
import java.util.ArrayList;
1212
import java.util.List;
13+
import java.util.Objects;
1314

1415
/**
1516
* CreateCompletion
@@ -41,12 +42,9 @@ public CreateCompletion(Object completion, OpenAIEndpoints endpoint) {
4142
*/
4243
public List<String> asSentences() {
4344
List<String> sentences = new ArrayList<>();
44-
StringBuilder sentenceBuilder = new StringBuilder();
4545
String text = asText();
46+
Objects.requireNonNull(text, "asText() can not be null");
4647

47-
if(text == null) {
48-
return null;
49-
}
5048
if(text.contains("\n")) {
5149
String[] words = text.split("\n");
5250
sentences.addAll(List.of(words));

src/main/java/io/github/jetkai/openai/net/request/RequestBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class RequestBuilder {
4040

4141
public HttpRequest createGetRequest(URI uri, String apiKey, String organization) {
4242
return HttpRequest.newBuilder()
43-
.headers("Content-Type", "application/json")
43+
.headers("Content-Type", "application/json; charset=utf-8")
4444
.uri(uri)
4545
.header("User-Agent", USER_AGENT)
4646
.header("Authorization", "Bearer " + apiKey)
@@ -53,13 +53,13 @@ public HttpRequest createGetRequest(URI uri, String apiKey, String organization)
5353
public HttpRequest createPostRequest(URI uri, Object data, String apiKey, String organization) {
5454
Object postData = data == null ? "" : data;
5555
return HttpRequest.newBuilder()
56-
.headers("Content-Type", "application/json")
56+
.headers("Content-Type", "application/json; charset=utf-8")
5757
.uri(uri)
5858
.header("User-Agent", USER_AGENT)
5959
.header("Authorization", "Bearer " + apiKey)
6060
.header("OpenAI-Organization", organization)
6161
.version(HttpClient.Version.HTTP_2)
62-
.POST(HttpRequest.BodyPublishers.ofString(String.valueOf(postData)))
62+
.POST(HttpRequest.BodyPublishers.ofString(String.valueOf(postData), StandardCharsets.UTF_8))
6363
.build();
6464
}
6565

src/test/java/CreateInstanceTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import io.github.jetkai.openai.api.ListModel;
2-
import io.github.jetkai.openai.api.ListModels;
32
import io.github.jetkai.openai.net.OpenAIEndpoints;
43
import io.github.jetkai.openai.openai.OpenAI;
54
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)