Skip to content

Commit 3616dbd

Browse files
authored
Merge pull request #1813 from quarkiverse/#1812
Disable TopK in Anthropic module when thinking is enabled
2 parents 0474af2 + 07554f9 commit 3616dbd

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

model-providers/anthropic/runtime/src/main/java/io/quarkiverse/langchain4j/anthropic/runtime/AnthropicRecorder.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.time.Duration;
66
import java.util.function.Supplier;
77

8+
import org.jboss.logging.Logger;
9+
810
import dev.langchain4j.model.anthropic.AnthropicChatModel;
911
import dev.langchain4j.model.anthropic.AnthropicStreamingChatModel;
1012
import dev.langchain4j.model.chat.ChatModel;
@@ -20,6 +22,8 @@
2022

2123
@Recorder
2224
public class AnthropicRecorder {
25+
private static final Logger LOG = Logger.getLogger(AnthropicRecorder.class);
26+
2327
private static final String DUMMY_KEY = "dummy";
2428

2529
private final RuntimeValue<LangChain4jAnthropicConfig> runtimeConfig;
@@ -47,14 +51,15 @@ public Supplier<ChatModel> chatModel(String configName) {
4751
.logRequests(firstOrDefault(false, chatModelConfig.logRequests(), anthropicConfig.logRequests()))
4852
.logResponses(firstOrDefault(false, chatModelConfig.logResponses(), anthropicConfig.logResponses()))
4953
.timeout(anthropicConfig.timeout().orElse(Duration.ofSeconds(10)))
50-
.topK(chatModelConfig.topK())
5154
.maxTokens(chatModelConfig.maxTokens())
5255
.maxRetries(chatModelConfig.maxRetries());
5356

5457
if (chatModelConfig.temperature().isPresent()) {
5558
builder.temperature(chatModelConfig.temperature().getAsDouble());
5659
}
5760

61+
builder.topK(chatModelConfig.topK().orElse(40));
62+
5863
if (chatModelConfig.topP().isPresent()) {
5964
builder.topP(chatModelConfig.topP().getAsDouble());
6065
}
@@ -65,6 +70,10 @@ public Supplier<ChatModel> chatModel(String configName) {
6570

6671
ChatModelConfig.ThinkingConfig thinkingConfig = chatModelConfig.thinking();
6772
if (thinkingConfig.type().isPresent()) {
73+
if (chatModelConfig.topK().isPresent()) {
74+
LOG.warn("TopK was not configured because thinking was enabled");
75+
}
76+
builder.topK(null);
6877
builder.thinkingType(thinkingConfig.type().get());
6978
}
7079

@@ -115,7 +124,7 @@ public Supplier<StreamingChatModel> streamingChatModel(String configName) {
115124
.logRequests(firstOrDefault(false, chatModelConfig.logRequests(), anthropicConfig.logRequests()))
116125
.logResponses(firstOrDefault(false, chatModelConfig.logResponses(), anthropicConfig.logResponses()))
117126
.timeout(anthropicConfig.timeout().orElse(Duration.ofSeconds(10)))
118-
.topK(chatModelConfig.topK())
127+
.topK(chatModelConfig.topK().orElse(40))
119128
.maxTokens(chatModelConfig.maxTokens());
120129

121130
if (chatModelConfig.temperature().isPresent()) {

model-providers/anthropic/runtime/src/main/java/io/quarkiverse/langchain4j/anthropic/runtime/config/ChatModelConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Optional;
55
import java.util.OptionalDouble;
6+
import java.util.OptionalInt;
67

78
import io.quarkus.runtime.annotations.ConfigDocDefault;
89
import io.quarkus.runtime.annotations.ConfigGroup;
@@ -47,8 +48,8 @@ public interface ChatModelConfig {
4748
* Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower
4849
* value (e.g. 10) will be more conservative
4950
*/
50-
@WithDefault("40")
51-
Integer topK();
51+
@ConfigDocDefault("40")
52+
OptionalInt topK();
5253

5354
/**
5455
* The maximum number of times to retry. 1 means exactly one attempt, with retrying disabled.

0 commit comments

Comments
 (0)