Java SSE Client Example
This example shows how to call the Bitseek Chat SSE service using the popular openai-java library by Theo Kanning.
Dependency (Maven)
Add this to your pom.xml:
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.18.2</version>
</dependency>
Code Example
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.service.OpenAiService;
import java.time.Duration;
import java.util.List;
class SseExample {
public static void main(String[] args) {
// 1. Initialize the service
// Use your authentication key for the token.
// Customize the base URL to point to the Bitseek Chat service.
// Note: The base URL should NOT include the /v1 path for this library.
String token = "<API-KEY>";
String baseUrl = "https://chat-proxy.bitseek.ai";
OpenAiService service = new OpenAiService(token, baseUrl, Duration.ofSeconds(60));
// 2. Create a streaming request
// Set stream to true to enable SSE.
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("default")
.messages(List.of(new ChatMessage("user", "Write a short poem about the starry sky")))
.stream(true)
.build();
// 3. Process the streaming response
// The streamChatCompletion method returns a Flowable that emits events.
// We block and print each chunk's content to the console.
System.out.print("AI: ");
service.streamChatCompletion(request)
.blockingForEach(chunk -> {
chunk.getChoices().forEach(choice -> {
String content = choice.getMessage().getContent();
if (content != null) {
System.out.print(content);
}
});
});
System.out.println(); // Newline at the end
service.shutdownExecutor();
}
}
Key Parameters
baseUrl: Set tohttps://chat-proxy.bitseek.ai(without/v1for this library)token: Your valid API key for authenticationstream(true): Enables SSE streaming response