11package dev .langchain4j .community .model .zhipu ;
22
3- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .aiMessageFrom ;
4- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .finishReasonFrom ;
5- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .isSuccessFinishReason ;
6- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .toTools ;
7- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .toZhipuAiMessages ;
8- import static dev .langchain4j .community .model .zhipu .DefaultZhipuAiHelper .tokenUsageFrom ;
9- import static dev .langchain4j .community .model .zhipu .chat .ChatCompletionModel .GLM_4_FLASH ;
3+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .aiMessageFrom ;
4+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .finishReasonFrom ;
5+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .isSuccessFinishReason ;
6+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .toTools ;
7+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .toZhipuAiMessages ;
8+ import static dev .langchain4j .community .model .zhipu .InternalZhipuAiHelper .tokenUsageFrom ;
109import static dev .langchain4j .community .model .zhipu .chat .ToolChoiceMode .AUTO ;
1110import static dev .langchain4j .internal .RetryUtils .withRetry ;
11+ import static dev .langchain4j .internal .Utils .copy ;
1212import static dev .langchain4j .internal .Utils .getOrDefault ;
1313import static dev .langchain4j .internal .Utils .isNullOrEmpty ;
14+ import static dev .langchain4j .internal .ValidationUtils .ensureNotNull ;
1415import static dev .langchain4j .spi .ServiceHelper .loadFactories ;
15- import static java .util .Collections .emptyList ;
1616
1717import dev .langchain4j .agent .tool .ToolSpecification ;
1818import dev .langchain4j .community .model .zhipu .chat .ChatCompletionModel ;
1919import dev .langchain4j .community .model .zhipu .chat .ChatCompletionRequest ;
2020import dev .langchain4j .community .model .zhipu .chat .ChatCompletionResponse ;
2121import dev .langchain4j .community .model .zhipu .spi .ZhipuAiChatModelBuilderFactory ;
2222import dev .langchain4j .data .message .ChatMessage ;
23- import dev .langchain4j .internal .ValidationUtils ;
2423import dev .langchain4j .model .chat .ChatModel ;
2524import dev .langchain4j .model .chat .listener .ChatModelListener ;
2625import dev .langchain4j .model .chat .request .ChatRequest ;
2726import dev .langchain4j .model .chat .request .ChatRequestParameters ;
2827import dev .langchain4j .model .chat .response .ChatResponse ;
2928import dev .langchain4j .model .output .FinishReason ;
3029import java .time .Duration ;
31- import java .util .ArrayList ;
3230import java .util .List ;
33- import org .slf4j .Logger ;
34- import org .slf4j .LoggerFactory ;
3531
3632/**
3733 * Represents an ZhipuAi language model with a chat completion interface, such as glm-3-turbo and glm-4.
3834 * You can find description of parameters <a href="https://open.bigmodel.cn/dev/api">here</a>.
3935 */
4036public class ZhipuAiChatModel implements ChatModel {
4137
42- private static final Logger log = LoggerFactory .getLogger (ZhipuAiChatModel .class );
43-
44- private final Double temperature ;
45- private final Double topP ;
46- private final String model ;
47- private final Integer maxRetries ;
48- private final Integer maxToken ;
49- private final List <String > stops ;
5038 private final ZhipuAiClient client ;
5139 private final List <ChatModelListener > listeners ;
40+ private final Integer maxRetries ;
5241
5342 private final ChatRequestParameters defaultRequestParameters ;
5443
@@ -68,13 +57,8 @@ public ZhipuAiChatModel(
6857 Duration connectTimeout ,
6958 Duration readTimeout ,
7059 Duration writeTimeout ) {
71- this .temperature = getOrDefault (temperature , 0.7 );
72- this .topP = topP ;
73- this .stops = stops ;
74- this .model = getOrDefault (model , GLM_4_FLASH .toString ());
7560 this .maxRetries = getOrDefault (maxRetries , 3 );
76- this .maxToken = getOrDefault (maxToken , 512 );
77- this .listeners = listeners == null ? emptyList () : new ArrayList <>(listeners );
61+ this .listeners = copy (listeners );
7862 this .client = ZhipuAiClient .builder ()
7963 .baseUrl (getOrDefault (baseUrl , "https://open.bigmodel.cn/" ))
8064 .apiKey (apiKey )
@@ -86,11 +70,11 @@ public ZhipuAiChatModel(
8670 .logResponses (getOrDefault (logResponses , false ))
8771 .build ();
8872 this .defaultRequestParameters = ChatRequestParameters .builder ()
89- .temperature (this . temperature )
73+ .temperature (getOrDefault ( temperature , 0.7 ) )
9074 .topP (topP )
9175 .stopSequences (stops )
92- .modelName (this . model )
93- .maxOutputTokens (this . maxToken )
76+ .modelName (ensureNotNull ( model , "model" ) )
77+ .maxOutputTokens (getOrDefault ( maxToken , 512 ) )
9478 .build ();
9579 }
9680
@@ -178,7 +162,6 @@ public ZhipuAiChatModelBuilder model(ChatCompletionModel model) {
178162 }
179163
180164 public ZhipuAiChatModelBuilder model (String model ) {
181- ValidationUtils .ensureNotBlank (model , "model" );
182165 this .model = model ;
183166 return this ;
184167 }
@@ -233,6 +216,10 @@ public ZhipuAiChatModelBuilder listeners(List<ChatModelListener> listeners) {
233216 return this ;
234217 }
235218
219+ /**
220+ * @deprecated This method is deprecated due to {@link ZhipuAiClient} use {@link dev.langchain4j.http.client.HttpClient} as an http client.
221+ */
222+ @ Deprecated (since = "1.0.0-beta4" , forRemoval = true )
236223 public ZhipuAiChatModelBuilder callTimeout (Duration callTimeout ) {
237224 this .callTimeout = callTimeout ;
238225 return this ;
@@ -248,6 +235,10 @@ public ZhipuAiChatModelBuilder readTimeout(Duration readTimeout) {
248235 return this ;
249236 }
250237
238+ /**
239+ * @deprecated This method is deprecated due to {@link ZhipuAiClient} use {@link dev.langchain4j.http.client.HttpClient} as an http client.
240+ */
241+ @ Deprecated (since = "1.0.0-beta4" , forRemoval = true )
251242 public ZhipuAiChatModelBuilder writeTimeout (Duration writeTimeout ) {
252243 this .writeTimeout = writeTimeout ;
253244 return this ;
0 commit comments