chore: add custom timeout and audio speech samples (#53)
This commit is contained in:
parent
ea87069d3c
commit
f422602aac
2 changed files with 103 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZhipuAiClient;
|
||||
import ai.z.openapi.core.Constants;
|
||||
import ai.z.openapi.service.audio.AudioSpeechRequest;
|
||||
import ai.z.openapi.service.audio.AudioSpeechResponse;
|
||||
|
||||
/**
|
||||
* Audio Speech Example
|
||||
* Demonstrates how to use ZaiClient for audio speech
|
||||
*/
|
||||
public class AudioSpeechExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create client, recommended to set API Key via environment variable
|
||||
// export ZAI_API_KEY=your.api_key
|
||||
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
|
||||
ZhipuAiClient client = ZhipuAiClient.builder().build();
|
||||
|
||||
// Create request
|
||||
AudioSpeechRequest request = AudioSpeechRequest.builder()
|
||||
.model(Constants.ModelTTS)
|
||||
.input("Hello, this is a test for text-to-speech functionality.")
|
||||
.voice("tongtong")
|
||||
.build();
|
||||
|
||||
try {
|
||||
// Execute request
|
||||
AudioSpeechResponse response = client.audio().createSpeech(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("Response: " + response.getData());
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZhipuAiClient;
|
||||
import ai.z.openapi.core.Constants;
|
||||
import ai.z.openapi.service.model.ChatCompletionCreateParams;
|
||||
import ai.z.openapi.service.model.ChatCompletionResponse;
|
||||
import ai.z.openapi.service.model.ChatMessage;
|
||||
import ai.z.openapi.service.model.ChatMessageRole;
|
||||
import ai.z.openapi.service.model.ChatThinking;
|
||||
import ai.z.openapi.service.model.ChatThinkingType;
|
||||
import ai.z.openapi.service.model.ResponseFormat;
|
||||
import ai.z.openapi.service.model.ResponseFormatType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Chat Completion Example
|
||||
* Demonstrates how to use ZaiClient for basic chat conversations
|
||||
*/
|
||||
public class CustomTimeoutExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create client, recommended to set API Key via environment variable
|
||||
// export ZAI_API_KEY=your.api_key
|
||||
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
|
||||
ZhipuAiClient client = ZhipuAiClient.builder()
|
||||
.networkConfig(30, 10, 30, 30, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
// Create chat request
|
||||
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
|
||||
.model(Constants.ModelChatGLM4_5)
|
||||
.messages(Arrays.asList(
|
||||
ChatMessage.builder()
|
||||
.role(ChatMessageRole.USER.value())
|
||||
.content("Hello, how to learn english?")
|
||||
.build()
|
||||
))
|
||||
.stream(false)
|
||||
.thinking(ChatThinking.builder().type(ChatThinkingType.ENABLED.value()).build())
|
||||
.responseFormat(ResponseFormat.builder().type(ResponseFormatType.TEXT.value()).build())
|
||||
.temperature(0.7f)
|
||||
.maxTokens(1024)
|
||||
.build();
|
||||
|
||||
try {
|
||||
// Execute request
|
||||
ChatCompletionResponse response = client.chat().createChatCompletion(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
Object content = response.getData().getChoices().get(0).getMessage();
|
||||
System.out.println("Response: " + content);
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue