跳到主要内容

ZhiPu AI

ZhiPu AIは、テキスト生成、テキスト埋め込み、画像生成などのモデルサービスを提供するプラットフォームです。詳細についてはZhiPu AIオープンプラットフォームを参照してください。 LangChain4jはHTTPエンドポイントを使用してZhiPu AIと統合しています。HTTPエンドポイントから公式SDKへの移行を検討しており、どのような協力も歓迎します!

Maven依存関係

LangChain4jでは、通常のJavaまたはSpring BootアプリケーションでZhiPu AIを使用できます。

通常のJava

备注

1.0.0-alpha1以降、langchain4j-zhipu-ailangchain4j-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でDashScopeに接続できますhttps://open.bigmodel.cn/
apiKeyAPIキー
model使用するモデルglm-4-flash
topPカーネルサンプリングの確率しきい値で、モデルが生成するテキストの多様性を制御します。top_pが高いほど生成テキストは多様になり、低いほどその逆です。値の範囲:(0, 1.0]。通常はこの値かtemperatureのどちらか一方を変更することを推奨します。
maxRetriesリクエストの最大リトライ回数3
temperatureモデルが生成するテキストの多様性を制御するサンプリング温度。temperatureが高いほど生成テキストは多様になり、低いほどその逆です。値の範囲:[0, 2)0.7
stopsstopパラメータを指定すると、モデルは指定された文字列またはtoken_idを含みそうになった時点で自動的にテキスト生成を停止します
maxTokenこのリクエストで返される最大トークン数512
listenersリクエスト、レスポンス、エラーをリッスンするリスナー
callTimeoutリクエスト用のOKHttpタイムアウト設定
connectTimeoutリクエスト用のOKHttpタイムアウト設定
writeTimeoutリクエスト用のOKHttpタイムアウト設定
readTimeoutリクエスト用のOKHttpタイムアウト設定
logRequestsリクエストをログに記録するかどうかfalse
logResponsesレスポンスをログに記録するかどうかfalse
doSampleサンプリングを使用するかどうか。falseに設定すると、モデルは貪欲復号を使用します
toolStream部分的なツールストリーミングを有効にするかどうか。trueに設定すると、ツール呼び出しを増分的にストリームできますfalse

ZhipuAiChatRequestParameters

チャットリクエスト送信時に、ZhipuAiChatRequestParametersを使用して追加パラメータを設定できます:

プロパティ説明デフォルト値
doSampleサンプリングを使用するかどうか。falseに設定すると、モデルは貪欲復号を使用します
toolStream部分的なツールストリーミングを有効にするかどうか。trueに設定すると、ツール呼び出しを増分的にストリームできますfalse
thinking推論モードの設定。typeは推論タイプを指定し、clearThinkingはレスポンス内に内部思考プロセスを表示するかどうかを制御します

ZhipuAiStreamingChatModel

maxRetriesを除き、ZhipuAiChatModelと同じです。

通常の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);

その他の例

その他の例は次を参照してください: