Skip to main content

ZhiPu AI

ZhiPu AI is a platform to provide model service including text generation, text embedding, image generation and so on. You can refer to ZhiPu AI Open Platform for more details. LangChain4j integrates with ZhiPu AI by using HTTP endpoint. We are consider migrating it from HTTP endpoint to official SDK and are appreciated of any help!

Maven Dependency

You can use ZhiPu AI with LangChain4j in plain Java or Spring Boot applications.

Plain Java

note

Since 1.0.0-alpha1, langchain4j-zhipu-ai has migrated to langchain4j-community and is renamed to langchain4j-community-zhipu-ai

Before 1.0.0-alpha1:


<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-zhipu-ai</artifactId>
<version>${previous version here}</version>
</dependency>

1.0.0-alpha1 and later:


<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-zhipu-ai</artifactId>
<version>1.0.0-beta1</version>
</dependency>

Or, you can use BOM to manage dependencies consistently:


<dependencyManagement>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>1.0.0-beta1</version>
<typ>pom</typ>
<scope>import</scope>
</dependency>
</dependencyManagement>

Configurable parameters

ZhipuAiChatModel

ZhipuAiChatModel has following parameters to configure when you initialize it:

PropertyDescriptionDefault Value
baseUrlThe URL to connect to. You can use HTTP or websocket to connect to DashScopehttps://open.bigmodel.cn/
apiKeyThe API Key
modelThe model to use.glm-4-flash
topPThe probability threshold for kernel sampling controls the diversity of texts generated by the model. the higher the top_p, the more diverse the generated texts, and vice versa. Value range: (0, 1.0]. We generally recommend altering this or temperature but not both.
maxRetriesThe maximum retry times to request3
temperatureSampling temperature that controls the diversity of the text generated by the model. the higher the temperature, the more diverse the generated text, and vice versa. Value range: [0, 2)0.7
stopsWith the stop parameter, the model will automatically stop generating text when it is about to contain the specified string or token_id.
maxTokenThe maximum number of tokens returned by this request.512
listenersListeners that listen for request, response and errors.
callTimeoutOKHttp timeout config for request
connectTimeoutOKHttp timeout config for request
writeTimeoutOKHttp timeout config for request
readTimeoutOKHttp timeout config for request
logRequestsWhether to log request or notfalse
logResponsesWhether to log response or notfalse

ZhipuAiStreamingChatModel

Same as ZhipuAiChatModel, except maxRetries.

Examples

Plain Java

You can initialize ZhipuAiChatModel by using following code:

ChatLanguageModel qwenModel = ZhipuAiChatModel.builder()
.apiKey("You API key here")
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.build();

Or more custom for other parameters:

ChatLanguageModel qwenModel = ZhipuAiChatModel.builder()
.apiKey("You API key here")
.model("glm-4")
.temperature(0.6)
.maxToken(1024)
.maxRetries(1)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.build();

More Examples

You can check more examples in: