uncloseai.com/languages/java/openai/uncloseai.java

94 lines
3.9 KiB
Java

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;
import com.openai.core.JsonValue;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
public class uncloseai {
public static void main(String[] args) {
System.out.println("=== UncloseAI Java Client (Official OpenAI SDK) ===\n");
// Non-streaming chat with Hermes
System.out.println("=== Non-Streaming Chat (Hermes) ===");
OpenAIClient hermesClient = OpenAIOkHttpClient.builder()
.apiKey("dummy-key")
.baseURL("https://hermes.ai.unturf.com/v1")
.build();
ChatCompletionCreateParams hermesParams = ChatCompletionCreateParams.builder()
.model("adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic")
.addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
ChatCompletionUserMessageParam.builder()
.role(ChatCompletionUserMessageParam.Role.USER)
.content(ChatCompletionUserMessageParam.Content.ofTextContent(
"Give a Python Fizzbuzz solution in one line of code?"
))
.build()
))
.temperature(0.5)
.maxTokens(150L)
.build();
ChatCompletion hermesResponse = hermesClient.chat().completions().create(hermesParams);
System.out.println("Response: " + hermesResponse.choices().get(0).message().content().get() + "\n");
// Streaming chat with Qwen
System.out.println("=== Streaming Chat (Qwen) ===");
OpenAIClient qwenClient = OpenAIOkHttpClient.builder()
.apiKey("dummy-key")
.baseURL("https://qwen.ai.unturf.com/v1")
.build();
ChatCompletionCreateParams qwenParams = ChatCompletionCreateParams.builder()
.model("hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M")
.addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
ChatCompletionUserMessageParam.builder()
.role(ChatCompletionUserMessageParam.Role.USER)
.content(ChatCompletionUserMessageParam.Content.ofTextContent(
"Give a Python Fizzbuzz solution in one line of code?"
))
.build()
))
.temperature(0.5)
.maxTokens(150L)
.build();
System.out.print("Response: ");
Stream<ChatCompletionChunk> qwenStream = qwenClient.chat().completions().createStreaming(qwenParams);
qwenStream.forEach(chunk -> {
if (!chunk.choices().isEmpty() && chunk.choices().get(0).delta().content().isPresent()) {
System.out.print(chunk.choices().get(0).delta().content().get());
}
});
System.out.println("\n");
// TTS example
System.out.println("=== TTS Speech Generation ===");
OpenAIClient ttsClient = OpenAIOkHttpClient.builder()
.apiKey("YOLO")
.baseURL("https://speech.ai.unturf.com/v1")
.build();
SpeechCreateParams speechParams = SpeechCreateParams.builder()
.model(SpeechModel.TTS_1)
.voice(SpeechCreateParams.Voice.ALLOY)
.input("I think so therefore, Today is a wonderful day to grow something people love!")
.speed(0.9)
.build();
byte[] audioBytes = ttsClient.audio().speech().create(speechParams);
try (FileOutputStream fos = new FileOutputStream("speech.mp3")) {
fos.write(audioBytes);
System.out.println("[OK] Speech file created: speech.mp3 (" + audioBytes.length + " bytes)\n");
} catch (IOException e) {
System.err.println("Error writing audio file: " + e.getMessage());
}
System.out.println("=== Examples Complete ===");
}
}