add official OpenAI SDK client examples for Go, Ruby, Java, and C#
This commit is contained in:
parent
bfc41a22a0
commit
0cf61ea676
12 changed files with 519 additions and 0 deletions
26
languages/java/openai/Dockerfile
Normal file
26
languages/java/openai/Dockerfile
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# Use official Maven image to build
|
||||
FROM maven:3.9-eclipse-temurin-17-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy POM and source
|
||||
COPY pom.xml ./
|
||||
COPY uncloseai.java ./
|
||||
|
||||
# Build the application
|
||||
RUN mvn clean compile
|
||||
|
||||
# Run stage
|
||||
FROM eclipse-temurin:17-jre-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy compiled classes and dependencies from builder
|
||||
COPY --from=builder /app/target/classes ./classes
|
||||
COPY --from=builder /root/.m2/repository /root/.m2/repository
|
||||
COPY --from=builder /app/pom.xml ./
|
||||
|
||||
# Install maven to have classpath available
|
||||
RUN apk add --no-cache maven
|
||||
|
||||
CMD ["mvn", "-q", "exec:java"]
|
||||
42
languages/java/openai/pom.xml
Normal file
42
languages/java/openai/pom.xml
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.uncloseai</groupId>
|
||||
<artifactId>uncloseai-java-openai</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.openai</groupId>
|
||||
<artifactId>openai-java</artifactId>
|
||||
<version>0.8.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<mainClass>uncloseai</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
94
languages/java/openai/uncloseai.java
Normal file
94
languages/java/openai/uncloseai.java
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
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 ===");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue