fix: update method calls and improve code clarity in README examples (#27)

This commit is contained in:
Junhui Huang 2025-07-28 17:14:36 +08:00 committed by GitHub
parent fcef854291
commit ade87c0ed0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 42 deletions

View file

@ -148,7 +148,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
ChatCompletionResponse response = client.chat().createChatCompletion(request);
if (response.isSuccess()) {
String content = response.getData().getChoices().get(0).getMessage().getContent();
String content = response.getData().getChoices().get(0).getMessage().getContent().toString();
System.out.println("Response: " + content);
} else {
System.err.println("Error: " + response.getMsg());
@ -201,12 +201,14 @@ ChatTool weatherTool = ChatTool.builder()
.description("Get current weather for a location")
.parameters(ChatFunctionParameters.builder()
.type("object")
.properties(Map.of(
"location", Map.of(
"type", "string",
"description", "City name"
)
))
.properties(new HashMap<String, ChatFunctionParameterProperty>() {
{
put("location", ChatFunctionParameterProperty.builder()
.type("string")
.description("City name")
.build());
}
})
.required(Arrays.asList("location"))
.build())
.build())
@ -240,7 +242,7 @@ EmbeddingCreateParams request = EmbeddingCreateParams.builder()
.build();
// Execute request
EmbeddingResponse response = client.embeddings().create(request);
EmbeddingResponse response = client.embeddings().createEmbeddings(request);
if (response.isSuccess()) {
response.getData().getData().forEach(embedding -> {
@ -259,12 +261,10 @@ CreateImageRequest request = CreateImageRequest.builder()
.model(Constants.ModelCogView3Plus)
.prompt("A beautiful sunset over mountains")
.size("1024x1024")
.quality("standard")
.n(1)
.build();
// Execute request
ImageResponse response = client.images().generate(request);
ImageResponse response = client.images().createImage(request);
if (response.isSuccess()) {
response.getData().getData().forEach(image -> {

View file

@ -147,7 +147,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
ChatCompletionResponse response = client.chat().createChatCompletion(request);
if (response.isSuccess()) {
String content = response.getData().getChoices().get(0).getMessage().getContent();
String content = response.getData().getChoices().get(0).getMessage().getContent().toString();
System.out.println("回复: " + content);
} else {
System.err.println("错误: " + response.getMsg());
@ -200,12 +200,14 @@ ChatTool weatherTool = ChatTool.builder()
.description("获取指定地点的当前天气")
.parameters(ChatFunctionParameters.builder()
.type("object")
.properties(Map.of(
"location", Map.of(
"type", "string",
"description", "城市名称"
)
))
.properties(new HashMap<String, ChatFunctionParameterProperty>() {
{
put("location", ChatFunctionParameterProperty.builder()
.type("string")
.description("城市名称")
.build());
}
})
.required(Arrays.asList("location"))
.build())
.build())
@ -239,7 +241,7 @@ EmbeddingCreateParams request = EmbeddingCreateParams.builder()
.build();
// 执行请求
EmbeddingResponse response = client.embeddings().create(request);
EmbeddingResponse response = client.embeddings().createEmbeddings(request);
if (response.isSuccess()) {
response.getData().getData().forEach(embedding -> {
@ -258,12 +260,10 @@ CreateImageRequest request = CreateImageRequest.builder()
.model(Constants.ModelCogView3Plus)
.prompt("山间美丽的日落")
.size("1024x1024")
.quality("standard")
.n(1)
.build();
// 执行请求
ImageResponse response = client.images().generate(request);
ImageResponse response = client.images().createImage(request);
if (response.isSuccess()) {
response.getData().getData().forEach(image -> {