-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExampleTranslation.java
More file actions
61 lines (48 loc) · 2.07 KB
/
ExampleTranslation.java
File metadata and controls
61 lines (48 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package examples;
import io.github.jetkai.openai.api.CreateCompletion;
import io.github.jetkai.openai.api.CreateTranslation;
import io.github.jetkai.openai.api.data.completion.CompletionData;
import io.github.jetkai.openai.openai.OpenAI;
import java.util.Optional;
/**
* ExampleTranslation
*
* @author <a href="https://github.com/jetkai">Kai</a>
* @version 1.1.0
* {@code - 07/03/2023}
* @since 1.0.0
* {@code - 05/03/2023}
*
* <p>
* Note - This is just a test class, it is recommended to import this project as a library
* <p>
* You can get a free API key from <a href="https://platform.openai.com/account/api-keys">here</a>
*/
final class ExampleTranslation {
public static void main(String[] args) {
//Initialize ExampleTranslation class
ExampleTranslation translation = new ExampleTranslation();
//This is the language we want to translate our audio file to
String toLanguage = "French";
//Example audio file that we are going to upload to OpenAI to be translated
String message = "Hello, how are you today?";
//Response from OpenAI with the translated string
String response = translation.communicate(message, toLanguage);
//Print the translation to the console
System.out.println(response);
}
private String communicate(String message, String toLanguage) {
//TranslationData, ready to send to the OpenAI API
CompletionData translationData = CompletionData.translation(message, toLanguage);
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.createTranslation(translationData)
.build()
//Sends the request to OpenAI's endpoint & parses the response data
.sendRequest();
//Call the CreateTranslation API from OpenAI & create instance
Optional<CreateTranslation> createTranslation = openAI.translation();
//Return as text, do not replace \n or ascii characters
return createTranslation.map(CreateCompletion::asText).orElse(null);
}
}