跳到主要内容

智谱 AI

智谱 AI 是一个提供模型服务的平台,包括文本生成、文本嵌入、图像生成等。您可以参考 智谱 AI 开放平台 了解更多详情。 LangChain4j 通过使用 HTTP 端点 与智谱 AI 集成。我们正在考虑将其从 HTTP 端点迁移到官方 SDK,并感谢任何帮助!

Maven 依赖

您可以在纯 Java 或 Spring Boot 应用程序中通过 LangChain4j 使用智谱 AI。

纯 Java

备注

1.0.0-alpha1 起,langchain4j-zhipu-ai 已迁移到 langchain4j-community 并更名为 langchain4j-community-zhipu-ai

1.0.0-alpha1 之前:


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

1.0.0-alpha1 及之后:


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

或者,您可以使用 BOM 来一致地管理依赖项:


<dependencyManagement>
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>${latest version here}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

可配置参数

ZhipuAiChatModel

初始化 ZhipuAiChatModel 时可配置以下参数:

属性说明默认值
baseUrl要连接的 URL。您可以使用 HTTP 或 WebSocket 连接到 DashScopehttps://open.bigmodel.cn/
apiKeyAPI Key
model要使用的模型glm-4-flash
topP核采样的概率阈值,控制模型生成文本的多样性。top_p 越高,生成文本越多样,反之亦然。取值范围:(0, 1.0]。一般建议只调整该参数或 temperature,不要同时调整。
maxRetries请求的最大重试次数3
temperature采样温度,控制模型生成文本的多样性。temperature 越高,生成文本越多样,反之亦然。取值范围:[0, 2)0.7
stops使用 stop 参数时,模型在即将生成指定字符串或 token_id 时会自动停止生成文本
maxToken本次请求返回的最大 token 数512
listeners监听请求、响应和错误的监听器
callTimeoutOKHttp 请求超时配置
connectTimeoutOKHttp 请求超时配置
writeTimeoutOKHttp 请求超时配置
readTimeoutOKHttp 请求超时配置
logRequests是否记录请求日志false
logResponses是否记录响应日志false
doSample是否使用采样。设为 false 时,模型将使用贪心解码
toolStream是否启用部分工具流式输出。设为 true 时,工具调用可增量流式返回false

ZhipuAiChatRequestParameters

发送聊天请求时可使用 ZhipuAiChatRequestParameters 配置额外参数:

属性说明默认值
doSample是否使用采样。设为 false 时,模型将使用贪心解码
toolStream是否启用部分工具流式输出。设为 true 时,工具调用可增量流式返回false
thinking推理模式配置。type 指定推理类型,clearThinking 控制是否在响应中展示内部思考过程

ZhipuAiStreamingChatModel

ZhipuAiChatModel 相同,但不包含 maxRetries

示例

纯 Java

您可以使用以下代码初始化 ZhipuAiChatModel

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

或者针对其他参数进行更多自定义:

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

推理(Reasoning)

您可以启用推理模式以获取模型的内部思考过程:

ChatModel model = ZhipuAiChatModel.builder()
.apiKey("You API key here")
.model(ChatCompletionModel.GLM_4_7) // Use GLM-4-5 or upper model for reasoning support
.build();

ChatResponse response = model.chat(
ChatRequest.builder()
.messages(UserMessage.from("What is the capital of Germany?"))
.parameters(ZhipuAiChatRequestParameters.builder()
.thinking(Thinking.builder()
.type("reasoning")
.clearThinking(true)
.build())
.build())
.build());

AiMessage aiMessage = response.aiMessage();
System.out.println("Answer: "+aiMessage.text());
System.out.println("Thinking: "+aiMessage.thinking());

部分工具调用(流式)

您可以使用 toolStream 增量流式输出部分工具调用:

ZhipuAiStreamingChatModel model = ZhipuAiStreamingChatModel.builder()
.apiKey("You API key here")
.model(ChatCompletionModel.GLM_4_7)
.build();

ToolSpecification calculator = ToolSpecification.builder()
.name("calculator")
.description("returns a sum of two numbers")
.parameters(JsonObjectSchema.builder()
.addIntegerProperty("first")
.addIntegerProperty("second")
.build())
.build();

TestStreamingChatResponseHandler handler = new TestStreamingChatResponseHandler() {
@Override
public void onPartialToolCall(ToolExecutionRequest partialToolCall) {
System.out.println("Partial tool call: " + partialToolCall.name() + " - " + partialToolCall.arguments());
}
};

model.chat(
ChatRequest.builder()
.messages(UserMessage.from("2+2=?"))
.parameters(ZhipuAiChatRequestParameters.builder()
.toolSpecifications(calculator)
.toolStream(true)
.build())
.build(),
handler);

更多示例

您可以在以下位置查看更多示例: