uncloseai.

Java Examples - Free LLM & TTS AI Service

Java Examples

This page demonstrates how to use the uncloseai. API endpoints with Java using the official OpenAI Java library. All examples use the same OpenAI-compatible API interface, making it easy to switch between different models and endpoints.

Available Endpoints:

Both chat hostnames currently serve the same model, turboderp/Qwen3.8-27B-exl3 (read live from /v1/models). Hostnames are routing labels, not model names: every example below asks the endpoint which model it serves instead of hard-coding an id, so a model swap on the cards never breaks your code. The served model reasons before it answers (the reasoning arrives in a separate reasoning field), so give it room: the samples ask for 600 tokens because at 150 the budget ran out before the answer began.

Many endpoints from environment variables

Our clients (uncloseai-cli, uncloseai.js, opencompletion and the downloadable implementation for every language on this site) share one convention, so an endpoint is configuration, never code:

VariableMeaning
MODEL_ENDPOINT_NBase URL of the N-th OpenAI-compatible endpoint, ending in /v1. The only required variable. Scan N from 0 upward and skip gaps: uncloseai-cli counts from 0, the per-language implementations from 1.
MODEL_API_KEY_NBearer key for endpoint N. Unset means no key, which is all our free relay needs; a cloud endpoint sets one.
MODEL_ID_NModel id to send. Unset means ask /v1/models and take the first id, the rule used by every sample on this page.
MODEL_LABEL_NA display name for endpoint N; defaults to its hostname.
TTS_ENDPOINT_NThe same shape for text-to-speech endpoints.
export MODEL_ENDPOINT_0="https://hermes.ai.unturf.com/v1"
export MODEL_ENDPOINT_1="https://qwen.ai.unturf.com/v1"
# export MODEL_API_KEY_1="sk-..."      # only for an endpoint that needs one
export TTS_ENDPOINT_0="https://speech.ai.unturf.com/v1"

The first endpoint is the default; the rest fail over, in order. The Java implementation under /languages/ reads these variables and discovers each endpoint's models at startup.

Java Client Installation

Add the official OpenAI Java library to your Maven pom.xml:

<dependency>
    <groupId>com.openai</groupId>
    <artifactId>openai-java</artifactId>
    <version>4.3.0</version>
</dependency>

Or for Gradle:

implementation 'com.openai:openai-java:4.3.0'

Non-Streaming Examples

Non-streaming mode waits for the complete response before returning. This is simpler to use but provides no intermediate feedback during generation.

Using hermes.ai.unturf.com (RTX 4090)

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;
import java.util.List;

public class HermesExample {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey("choose-any-value")
            .baseURL("https://hermes.ai.unturf.com/v1")
            .build();

        // Ask the endpoint which model it serves; never hard-code an id.
        String model = client.models().list().data().get(0).id();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .model(model)
            .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(600L)
            .build();

        ChatCompletion completion = client.chat().completions().create(params);
        System.out.println(completion.choices().get(0).message().content().get());
    }
}

Using qwen.ai.unturf.com (RTX 3090)

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;
import java.util.List;

public class QwenExample {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey("choose-any-value")
            .baseURL("https://qwen.ai.unturf.com/v1")
            .build();

        // Ask the endpoint which model it serves; never hard-code an id.
        String model = client.models().list().data().get(0).id();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .model(model)
            .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(600L)
            .build();

        ChatCompletion completion = client.chat().completions().create(params);
        System.out.println(completion.choices().get(0).message().content().get());
    }
}

Streaming Examples

Streaming mode returns chunks of the response as they are generated, providing real-time feedback. This is ideal for interactive applications and long responses.

Using hermes.ai.unturf.com (RTX 4090)

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;

public class HermesStreamExample {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey("choose-any-value")
            .baseURL("https://hermes.ai.unturf.com/v1")
            .build();

        // Ask the endpoint which model it serves; never hard-code an id.
        String model = client.models().list().data().get(0).id();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .model(model)
            .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(600L)
            .build();

        client.chat().completions().createStreaming(params)
            .forEach(chunk -> {
                String content = chunk.choices().get(0).delta().content().orElse("");
                if (!content.isEmpty()) {
                    System.out.print(content);
                }
            });
    }
}

Using qwen.ai.unturf.com (RTX 3090)

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;

public class QwenStreamExample {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey("choose-any-value")
            .baseURL("https://qwen.ai.unturf.com/v1")
            .build();

        // Ask the endpoint which model it serves; never hard-code an id.
        String model = client.models().list().data().get(0).id();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .model(model)
            .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(600L)
            .build();

        client.chat().completions().createStreaming(params)
            .forEach(chunk -> {
                String content = chunk.choices().get(0).delta().content().orElse("");
                if (!content.isEmpty()) {
                    System.out.print(content);
                }
            });
    }
}

Text-to-Speech Example

Generate audio speech from text using the TTS endpoint. The audio is saved as an MP3 file.

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class TTSExample {
    public static void main(String[] args) throws IOException {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey("YOLO")
            .baseURL("https://speech.ai.unturf.com/v1")
            .build();

        SpeechCreateParams params = SpeechCreateParams.builder()
            .model(SpeechModel.of("tts-1-f5"))
            .voice(SpeechCreateParams.Voice.of("foxhop"))
            .input("I think so therefore, Today is a wonderful day to grow something people love!")
            .speed(0.9)
            .build();

        byte[] audioBytes = client.audio().speech().create(params);

        try (FileOutputStream fos = new FileOutputStream("speech.mp3")) {
            fos.write(audioBytes);
        }
    }
}