Add ZhipuAi no argument constructor and fix IT (#1699)

## Issue
Closes #1698

## Change
Add a parameterless constructor and some get methods.
Modify the default model to `glm-4-flash`(free model)

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [ ] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
This commit is contained in:
二毛 2024-09-04 17:54:02 +08:00 committed by GitHub
parent 6efae378fe
commit b8222abe59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 54 additions and 8 deletions

View File

@ -26,7 +26,7 @@ import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.internal.Utils.isNullOrEmpty;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static dev.langchain4j.model.zhipu.DefaultZhipuAiHelper.*;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4_FLASH;
import static dev.langchain4j.model.zhipu.chat.ToolChoiceMode.AUTO;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
import static java.util.Collections.emptyList;
@ -68,7 +68,7 @@ public class ZhipuAiChatModel implements ChatLanguageModel {
this.temperature = getOrDefault(temperature, 0.7);
this.topP = topP;
this.stops = stops;
this.model = getOrDefault(model, GLM_4.toString());
this.model = getOrDefault(model, GLM_4_FLASH.toString());
this.maxRetries = getOrDefault(maxRetries, 3);
this.maxToken = getOrDefault(maxToken, 512);
this.listeners = listeners == null ? emptyList() : new ArrayList<>(listeners);

View File

@ -40,4 +40,9 @@ public class ZhipuAiException extends RuntimeException {
public String getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}

View File

@ -27,7 +27,7 @@ import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.internal.Utils.isNullOrEmpty;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static dev.langchain4j.model.zhipu.DefaultZhipuAiHelper.*;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4_FLASH;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
@ -62,7 +62,7 @@ public class ZhipuAiStreamingChatModel implements StreamingChatLanguageModel {
this.temperature = getOrDefault(temperature, 0.7);
this.topP = topP;
this.stops = stops;
this.model = getOrDefault(model, GLM_4.toString());
this.model = getOrDefault(model, GLM_4_FLASH.toString());
this.maxToken = getOrDefault(maxToken, 512);
this.listeners = listeners == null ? emptyList() : new ArrayList<>(listeners);
this.client = ZhipuAiClient.builder()

View File

@ -17,6 +17,8 @@ public final class ChatCompletionChoice {
private Delta delta;
private String finishReason;
public ChatCompletionChoice() {}
public ChatCompletionChoice(Integer index, AssistantMessage message, Delta delta, String finishReason) {
this.index = index;
this.message = message;

View File

@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4;
import static dev.langchain4j.model.zhipu.chat.ChatCompletionModel.GLM_4_FLASH;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
@ -94,7 +94,7 @@ public final class ChatCompletionRequest {
public static final class Builder {
private String model = GLM_4.toString();
private String model = GLM_4_FLASH.toString();
private List<Message> messages;
private Double temperature;
private Double topP;

View File

@ -20,6 +20,8 @@ public final class ChatCompletionResponse {
private List<ChatCompletionChoice> choices;
private Usage usage;
public ChatCompletionResponse() {}
public ChatCompletionResponse(String id, Integer created, String model, List<ChatCompletionChoice> choices, Usage usage) {
this.id = id;
this.created = created;

View File

@ -1,4 +1,5 @@
package dev.langchain4j.model.zhipu.chat;
public interface Content {
String getType();
}

View File

@ -18,6 +18,9 @@ public final class Delta {
private String content;
private List<ToolCall> toolCalls;
public Delta() {
}
private Delta(Builder builder) {
this.content = builder.content;
this.toolCalls = builder.toolCalls;

View File

@ -20,6 +20,9 @@ public final class FunctionCall {
this.arguments = arguments;
}
public FunctionCall() {
}
public static FunctionCallBuilder builder() {
return new FunctionCallBuilder();
}

View File

@ -20,6 +20,22 @@ public class ImageContent implements Content {
this.imageUrl = imageUrl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Image getImageUrl() {
return imageUrl;
}
public void setImageUrl(Image imageUrl) {
this.imageUrl = imageUrl;
}
public static ImageContentBuilder builder() {
return new ImageContentBuilder();
}

View File

@ -20,6 +20,15 @@ public final class TextContent implements Content {
this.text = text;
}
@Override
public String getType() {
return type;
}
public String getText() {
return text;
}
public static TextContentBuilder builder() {
return new TextContentBuilder();
}

View File

@ -15,6 +15,9 @@ public final class Usage {
private Integer completionTokens;
private Integer totalTokens;
public Usage() {
}
private Usage(Builder builder) {
this.promptTokens = builder.promptTokens;
this.completionTokens = builder.completionTokens;

View File

@ -173,7 +173,8 @@ class ZhipuAiChatModelIT {
// then
AiMessage secondAiMessage = secondResponse.content();
assertThat(secondAiMessage.text()).contains("2024-04-23 12:00:20");
assertThat(secondAiMessage.text()).contains("12:00:20");
assertThat(secondAiMessage.text()).contains("2024");
assertThat(secondAiMessage.toolExecutionRequests()).isNull();
TokenUsage secondTokenUsage = secondResponse.tokenUsage();

View File

@ -207,7 +207,8 @@ public class ZhipuAiStreamingChatModelIT {
// then
Response<AiMessage> secondResponse = secondHandler.get();
AiMessage secondAiMessage = secondResponse.content();
assertThat(secondAiMessage.text()).contains("2024-04-23 12:00:20");
assertThat(secondAiMessage.text()).contains("12:00:20");
assertThat(secondAiMessage.text()).contains("2024");
assertThat(secondAiMessage.toolExecutionRequests()).isNull();
TokenUsage secondTokenUsage = secondResponse.tokenUsage();