Reorganize project structure: move public files to public/ directory
This commit is contained in:
parent
8de043bfef
commit
38545721a0
253 changed files with 0 additions and 0 deletions
9
public/languages/java/http/Dockerfile
Normal file
9
public/languages/java/http/Dockerfile
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
FROM openjdk:17-jdk-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY UncloseAI.java .
|
||||
|
||||
RUN javac UncloseAI.java
|
||||
|
||||
CMD ["java", "UncloseAI"]
|
||||
366
public/languages/java/http/UncloseAI.java
Normal file
366
public/languages/java/http/UncloseAI.java
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class ModelInfo {
|
||||
String id;
|
||||
String endpoint;
|
||||
int maxTokens;
|
||||
|
||||
ModelInfo(String id, String endpoint, int maxTokens) {
|
||||
this.id = id;
|
||||
this.endpoint = endpoint;
|
||||
this.maxTokens = maxTokens;
|
||||
}
|
||||
}
|
||||
|
||||
public class UncloseAI {
|
||||
private List<ModelInfo> models = new ArrayList<>();
|
||||
private List<String> ttsEndpoints = new ArrayList<>();
|
||||
private String apiKey;
|
||||
private int timeout = 30000;
|
||||
private boolean debug = false;
|
||||
|
||||
public UncloseAI() {
|
||||
this(null, null, null, 30000, false);
|
||||
}
|
||||
|
||||
public UncloseAI(List<String> endpoints, List<String> ttsEndpoints, String apiKey, int timeout, boolean debug) {
|
||||
this.apiKey = apiKey;
|
||||
this.timeout = timeout;
|
||||
this.debug = debug;
|
||||
|
||||
if (endpoints == null) {
|
||||
endpoints = discoverEndpointsFromEnv("MODEL_ENDPOINT");
|
||||
}
|
||||
if (ttsEndpoints == null) {
|
||||
ttsEndpoints = discoverEndpointsFromEnv("TTS_ENDPOINT");
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
System.out.println("[DEBUG] Initialized with " + endpoints.size() + " endpoint(s)");
|
||||
}
|
||||
|
||||
discoverModels(endpoints);
|
||||
this.ttsEndpoints = ttsEndpoints;
|
||||
}
|
||||
|
||||
public List<ModelInfo> listModels() {
|
||||
return new ArrayList<>(models);
|
||||
}
|
||||
|
||||
public String chat(List<Map<String, String>> messages, String model, int maxTokens) throws IOException {
|
||||
ModelInfo modelInfo = resolveModel(model);
|
||||
String jsonRequest = buildChatRequest(modelInfo.id, messages, maxTokens, false);
|
||||
String response = postJSON(modelInfo.endpoint + "/chat/completions", jsonRequest);
|
||||
return extractContent(response);
|
||||
}
|
||||
|
||||
public void chatStream(List<Map<String, String>> messages, String model, int maxTokens, Consumer<String> callback) throws IOException {
|
||||
ModelInfo modelInfo = resolveModel(model);
|
||||
String jsonRequest = buildChatRequest(modelInfo.id, messages, maxTokens, true);
|
||||
|
||||
URL url = new URL(modelInfo.endpoint + "/chat/completions");
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setReadTimeout(timeout);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(jsonRequest.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (line.startsWith("data: ")) {
|
||||
String data = line.substring(6).trim();
|
||||
if ("[DONE]".equals(data)) {
|
||||
break;
|
||||
}
|
||||
String content = extractStreamContent(data);
|
||||
if (content != null && !content.isEmpty()) {
|
||||
callback.accept(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] tts(String text, String voice, String model) throws IOException {
|
||||
if (ttsEndpoints.isEmpty()) {
|
||||
throw new IOException("No TTS endpoints available");
|
||||
}
|
||||
|
||||
String jsonRequest = String.format(
|
||||
"{\"model\":\"%s\",\"voice\":\"%s\",\"input\":\"%s\"}",
|
||||
model, voice, text.replace("\"", "\\\"")
|
||||
);
|
||||
|
||||
return postJSONBinary(ttsEndpoints.get(0) + "/audio/speech", jsonRequest);
|
||||
}
|
||||
|
||||
private List<String> discoverEndpointsFromEnv(String prefix) {
|
||||
List<String> endpoints = new ArrayList<>();
|
||||
for (int i = 1; i < 10000; i++) {
|
||||
String endpoint = System.getenv(prefix + "_" + i);
|
||||
if (endpoint == null || endpoint.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
endpoints.add(endpoint);
|
||||
}
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
private void discoverModels(List<String> endpoints) {
|
||||
for (String endpoint : endpoints) {
|
||||
if (debug) {
|
||||
System.out.println("[DEBUG] Discovering from: " + endpoint);
|
||||
}
|
||||
|
||||
try {
|
||||
String response = getJSON(endpoint + "/models");
|
||||
parseModels(response, endpoint);
|
||||
} catch (Exception e) {
|
||||
if (debug) {
|
||||
System.out.println("[DEBUG] Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void parseModels(String jsonResponse, String endpoint) {
|
||||
int dataIndex = jsonResponse.indexOf("\"data\":[");
|
||||
if (dataIndex == -1) return;
|
||||
|
||||
String dataSection = jsonResponse.substring(dataIndex + 8);
|
||||
int pos = 0;
|
||||
while (pos < dataSection.length()) {
|
||||
int idIndex = dataSection.indexOf("\"id\":\"", pos);
|
||||
if (idIndex == -1) break;
|
||||
|
||||
int idStart = idIndex + 6;
|
||||
int idEnd = dataSection.indexOf("\"", idStart);
|
||||
String modelId = dataSection.substring(idStart, idEnd);
|
||||
|
||||
if (modelId.startsWith("modelperm-")) {
|
||||
pos = idEnd + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
int maxTokens = 8192;
|
||||
int maxLenIndex = dataSection.indexOf("\"max_model_len\":", idEnd);
|
||||
if (maxLenIndex != -1 && maxLenIndex < dataSection.indexOf("}", idEnd)) {
|
||||
int maxLenStart = maxLenIndex + 16;
|
||||
int maxLenEnd = dataSection.indexOf(",", maxLenStart);
|
||||
if (maxLenEnd == -1) maxLenEnd = dataSection.indexOf("}", maxLenStart);
|
||||
if (maxLenEnd != -1) {
|
||||
try {
|
||||
maxTokens = Integer.parseInt(dataSection.substring(maxLenStart, maxLenEnd).trim());
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
models.add(new ModelInfo(modelId, endpoint, maxTokens));
|
||||
if (debug) {
|
||||
System.out.println("[DEBUG] Discovered: " + modelId);
|
||||
}
|
||||
|
||||
pos = idEnd + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private ModelInfo resolveModel(String model) throws IOException {
|
||||
if (models.isEmpty()) {
|
||||
throw new IOException("No models available");
|
||||
}
|
||||
if (model == null || model.isEmpty()) {
|
||||
return models.get(0);
|
||||
}
|
||||
for (ModelInfo m : models) {
|
||||
if (m.id.equals(model)) {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
throw new IOException("Model '" + model + "' not found");
|
||||
}
|
||||
|
||||
private String buildChatRequest(String modelId, List<Map<String, String>> messages, int maxTokens, boolean stream) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\"model\":\"").append(modelId).append("\",");
|
||||
sb.append("\"messages\":[");
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (i > 0) sb.append(",");
|
||||
Map<String, String> msg = messages.get(i);
|
||||
sb.append("{\"role\":\"").append(msg.get("role")).append("\",");
|
||||
sb.append("\"content\":\"").append(msg.get("content").replace("\"", "\\\"")).append("\"}");
|
||||
}
|
||||
sb.append("],\"max_tokens\":").append(maxTokens);
|
||||
if (stream) {
|
||||
sb.append(",\"stream\":true");
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getJSON(String urlString) throws IOException {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(10000);
|
||||
conn.setReadTimeout(10000);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
response.append(line.trim());
|
||||
}
|
||||
return response.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private String postJSON(String urlString, String jsonRequest) throws IOException {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setReadTimeout(timeout);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(jsonRequest.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
response.append(line.trim());
|
||||
}
|
||||
return response.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] postJSONBinary(String urlString, String jsonRequest) throws IOException {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setReadTimeout(timeout);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(jsonRequest.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
try (InputStream is = conn.getInputStream()) {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
byte[] data = new byte[1024];
|
||||
int nRead;
|
||||
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
private String extractContent(String jsonResponse) {
|
||||
int contentIndex = jsonResponse.indexOf("\"content\":\"");
|
||||
if (contentIndex == -1) return jsonResponse;
|
||||
|
||||
int startIndex = contentIndex + 11;
|
||||
int endIndex = jsonResponse.indexOf("\"", startIndex);
|
||||
while (endIndex > 0 && jsonResponse.charAt(endIndex - 1) == '\\') {
|
||||
endIndex = jsonResponse.indexOf("\"", endIndex + 1);
|
||||
}
|
||||
if (endIndex == -1) return jsonResponse.substring(startIndex);
|
||||
|
||||
String content = jsonResponse.substring(startIndex, endIndex);
|
||||
return content.replace("\\n", "\n").replace("\\\"", "\"").replace("\\\\", "\\");
|
||||
}
|
||||
|
||||
private String extractStreamContent(String jsonChunk) {
|
||||
int contentIndex = jsonChunk.indexOf("\"content\":\"");
|
||||
if (contentIndex == -1) return null;
|
||||
|
||||
int startIndex = contentIndex + 11;
|
||||
int endIndex = jsonChunk.indexOf("\"", startIndex);
|
||||
if (endIndex == -1) return null;
|
||||
|
||||
return jsonChunk.substring(startIndex, endIndex)
|
||||
.replace("\\n", "\n").replace("\\\"", "\"").replace("\\\\", "\\");
|
||||
}
|
||||
|
||||
// Demo when run as application
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== UncloseAI Java Client (with Streaming) ===\n");
|
||||
|
||||
UncloseAI client = new UncloseAI(null, null, null, 30000, true);
|
||||
|
||||
if (client.models.isEmpty()) {
|
||||
System.out.println("ERROR: No models discovered. Set environment variables:");
|
||||
System.out.println(" MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("\nDiscovered " + client.models.size() + " model(s):");
|
||||
for (ModelInfo m : client.models) {
|
||||
System.out.println(" - " + m.id + " (max_tokens: " + m.maxTokens + ")");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// Non-streaming chat
|
||||
System.out.println("=== Non-Streaming Chat ===");
|
||||
try {
|
||||
List<Map<String, String>> messages = Arrays.asList(
|
||||
new HashMap<String, String>() {{ put("role", "system"); put("content", "You are a helpful AI assistant."); }},
|
||||
new HashMap<String, String>() {{ put("role", "user"); put("content", "Explain quantum computing in one sentence."); }}
|
||||
);
|
||||
String response = client.chat(messages, null, 100);
|
||||
System.out.println("Response: " + response + "\n");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error: " + e.getMessage() + "\n");
|
||||
}
|
||||
|
||||
// Streaming chat
|
||||
System.out.println("=== Streaming Chat ===");
|
||||
String modelId = client.models.size() > 1 ? client.models.get(1).id : null;
|
||||
System.out.println("Model: " + (modelId != null ? modelId : client.models.get(0).id));
|
||||
System.out.print("Response: ");
|
||||
try {
|
||||
List<Map<String, String>> messages = Arrays.asList(
|
||||
new HashMap<String, String>() {{ put("role", "system"); put("content", "You are a coding assistant."); }},
|
||||
new HashMap<String, String>() {{ put("role", "user"); put("content", "Write a Java function to check if a number is prime"); }}
|
||||
);
|
||||
client.chatStream(messages, modelId, 200, content -> System.out.print(content));
|
||||
System.out.println("\n");
|
||||
} catch (IOException e) {
|
||||
System.out.println("\nError: " + e.getMessage() + "\n");
|
||||
}
|
||||
|
||||
// TTS
|
||||
if (!client.ttsEndpoints.isEmpty()) {
|
||||
System.out.println("=== TTS Speech Generation ===");
|
||||
try {
|
||||
byte[] audio = client.tts("Hello from UncloseAI Java client! This demonstrates streaming support.", "alloy", "tts-1");
|
||||
Files.write(Paths.get("speech.mp3"), audio);
|
||||
System.out.println("[OK] Speech file created: speech.mp3 (" + audio.length + " bytes)\n");
|
||||
} catch (IOException e) {
|
||||
System.out.println("[ERROR] TTS Error: " + e.getMessage() + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("=== Examples Complete ===");
|
||||
}
|
||||
}
|
||||
30
public/languages/java/openai/Dockerfile
Normal file
30
public/languages/java/openai/Dockerfile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Use official Maven image to build
|
||||
FROM maven:3.9-eclipse-temurin-17-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy POM
|
||||
COPY pom.xml ./
|
||||
|
||||
# Create Maven directory structure and copy source
|
||||
RUN mkdir -p src/main/java
|
||||
COPY uncloseai.java ./src/main/java/
|
||||
|
||||
# Build the application (package to create jar with dependencies)
|
||||
RUN mvn clean package
|
||||
|
||||
# Run stage
|
||||
FROM eclipse-temurin:17-jre-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy jar and dependencies from builder
|
||||
COPY --from=builder /app/target/*.jar ./uncloseai.jar
|
||||
COPY --from=builder /app/target/lib ./lib
|
||||
|
||||
# Set environment variables for testing
|
||||
ENV MODEL_ENDPOINT_1=https://hermes.ai.unturf.com/v1
|
||||
ENV MODEL_ENDPOINT_2=https://qwen.ai.unturf.com/v1
|
||||
ENV TTS_ENDPOINT_1=https://speech.ai.unturf.com/v1
|
||||
|
||||
CMD ["java", "-cp", "uncloseai.jar:lib/*", "uncloseai"]
|
||||
76
public/languages/java/openai/pom.xml
Normal file
76
public/languages/java/openai/pom.xml
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?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>4.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20240303</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.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<mainClass>uncloseai</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>uncloseai</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
137
public/languages/java/openai/uncloseai.java
Normal file
137
public/languages/java/openai/uncloseai.java
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import com.openai.client.OpenAIClient;
|
||||
import com.openai.client.okhttp.OpenAIOkHttpClient;
|
||||
import com.openai.models.chat.completions.ChatCompletion;
|
||||
import com.openai.models.chat.completions.ChatCompletionCreateParams;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class uncloseai {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== uncloseai. Java Client (Official OpenAI SDK) ===\n");
|
||||
|
||||
// Discover endpoints from environment variables
|
||||
String modelEndpoint1 = System.getenv("MODEL_ENDPOINT_1");
|
||||
String modelEndpoint2 = System.getenv("MODEL_ENDPOINT_2");
|
||||
String ttsEndpoint1 = System.getenv("TTS_ENDPOINT_1");
|
||||
|
||||
if (modelEndpoint1 == null || modelEndpoint2 == null || ttsEndpoint1 == null) {
|
||||
System.err.println("ERROR: No models discovered. Set environment variables:");
|
||||
System.err.println(" MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, TTS_ENDPOINT_1");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
// Discover models from endpoint 1
|
||||
System.out.println("Discovering models from " + modelEndpoint1 + "...");
|
||||
HttpRequest req1 = HttpRequest.newBuilder()
|
||||
.uri(URI.create(modelEndpoint1 + "/models"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> res1 = httpClient.send(req1, HttpResponse.BodyHandlers.ofString());
|
||||
JSONObject json1 = new JSONObject(res1.body());
|
||||
JSONArray models1 = json1.getJSONArray("data");
|
||||
String model1Id = models1.getJSONObject(0).getString("id");
|
||||
System.out.println("Model 1: " + model1Id + "\n");
|
||||
|
||||
// Discover models from endpoint 2
|
||||
System.out.println("Discovering models from " + modelEndpoint2 + "...");
|
||||
HttpRequest req2 = HttpRequest.newBuilder()
|
||||
.uri(URI.create(modelEndpoint2 + "/models"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> res2 = httpClient.send(req2, HttpResponse.BodyHandlers.ofString());
|
||||
JSONObject json2 = new JSONObject(res2.body());
|
||||
JSONArray models2 = json2.getJSONArray("data");
|
||||
String model2Id = models2.getJSONObject(0).getString("id");
|
||||
System.out.println("Model 2: " + model2Id + "\n");
|
||||
|
||||
// Non-streaming chat with Model 1
|
||||
System.out.println("=== Non-Streaming Chat (Model 1) ===");
|
||||
OpenAIClient client1 = OpenAIOkHttpClient.builder()
|
||||
.apiKey("dummy-key")
|
||||
.baseUrl(modelEndpoint1)
|
||||
.build();
|
||||
|
||||
ChatCompletionCreateParams params1 = ChatCompletionCreateParams.builder()
|
||||
.model(model1Id)
|
||||
.addUserMessage("Give a Python Fizzbuzz solution in one line of code?")
|
||||
.temperature(0.5)
|
||||
.maxTokens(150L)
|
||||
.build();
|
||||
|
||||
ChatCompletion response1 = client1.chat().completions().create(params1);
|
||||
System.out.println("Response: " + response1.choices().get(0).message().content().get() + "\n");
|
||||
|
||||
// Streaming chat with Model 1
|
||||
System.out.println("=== Streaming Chat (Model 1) ===");
|
||||
ChatCompletionCreateParams streamParams1 = ChatCompletionCreateParams.builder()
|
||||
.model(model1Id)
|
||||
.addUserMessage("Explain quantum entanglement in one sentence.")
|
||||
.temperature(0.5)
|
||||
.maxTokens(150L)
|
||||
.build();
|
||||
|
||||
System.out.print("Response: ");
|
||||
client1.chat().completions().createStreaming(streamParams1).stream().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");
|
||||
|
||||
// Non-streaming chat with Model 2
|
||||
System.out.println("=== Non-Streaming Chat (Model 2) ===");
|
||||
OpenAIClient client2 = OpenAIOkHttpClient.builder()
|
||||
.apiKey("dummy-key")
|
||||
.baseUrl(modelEndpoint2)
|
||||
.build();
|
||||
|
||||
ChatCompletionCreateParams params2 = ChatCompletionCreateParams.builder()
|
||||
.model(model2Id)
|
||||
.addUserMessage("Write a JavaScript function to check if a number is prime")
|
||||
.temperature(0.5)
|
||||
.maxTokens(150L)
|
||||
.build();
|
||||
|
||||
ChatCompletion response2 = client2.chat().completions().create(params2);
|
||||
System.out.println("Response: " + response2.choices().get(0).message().content().get() + "\n");
|
||||
|
||||
// Streaming chat with Model 2
|
||||
System.out.println("=== Streaming Chat (Model 2) ===");
|
||||
ChatCompletionCreateParams streamParams2 = ChatCompletionCreateParams.builder()
|
||||
.model(model2Id)
|
||||
.addUserMessage("Give a Python Fizzbuzz solution in one line of code?")
|
||||
.temperature(0.5)
|
||||
.maxTokens(150L)
|
||||
.build();
|
||||
|
||||
System.out.print("Response: ");
|
||||
client2.chat().completions().createStreaming(streamParams2).stream().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 ===");
|
||||
System.out.println("[SKIPPED] TTS implementation needs audio API research\n");
|
||||
|
||||
System.out.println("=== Examples Complete ===");
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue