Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.jspecify.annotations.Nullable;

import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionRequest.ReasoningEffort;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionRequest.Thinking;
import org.springframework.ai.deepseek.api.ResponseFormat;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
Expand All @@ -30,6 +32,7 @@
*
* @author Geng Rong
* @author Sebastien Deleuze
* @author guan xu
*/
@ConfigurationProperties(DeepSeekChatProperties.CONFIG_PREFIX)
public class DeepSeekChatProperties extends DeepSeekParentProperties {
Expand Down Expand Up @@ -69,6 +72,10 @@ public class DeepSeekChatProperties extends DeepSeekParentProperties {

private @Nullable Integer topLogprobs;

private @Nullable Thinking thinking;

private @Nullable ReasoningEffort reasoningEffort;

public boolean isEnabled() {
return this.enabled;
}
Expand Down Expand Up @@ -173,6 +180,22 @@ public void setTopLogprobs(@Nullable Integer topLogprobs) {
this.topLogprobs = topLogprobs;
}

public @Nullable Thinking getThinking() {
return this.thinking;
}

public void setThinking(@Nullable Thinking thinking) {
this.thinking = thinking;
}

public @Nullable ReasoningEffort getReasoningEffort() {
return this.reasoningEffort;
}

public void setReasoningEffort(@Nullable ReasoningEffort reasoningEffort) {
this.reasoningEffort = reasoningEffort;
}

public DeepSeekChatOptions toOptions() {
return DeepSeekChatOptions.builder()
.model(this.model)
Expand All @@ -185,6 +208,8 @@ public DeepSeekChatOptions toOptions() {
.topP(this.topP)
.logprobs(this.logprobs)
.topLogprobs(this.topLogprobs)
.thinking(this.thinking)
.reasoningEffort(this.reasoningEffort)
.build();
}

Expand Down Expand Up @@ -302,6 +327,26 @@ public void setTopLogprobs(@Nullable Integer topLogprobs) {
DeepSeekChatProperties.this.setTopLogprobs(topLogprobs);
}

@DeprecatedConfigurationProperty(replacement = "spring.ai.deepseek.chat.thinking")
@Deprecated(since = "2.0.0", forRemoval = true)
public @Nullable Thinking getThinking() {
return DeepSeekChatProperties.this.getThinking();
}

public void setThinking(@Nullable Thinking thinking) {
DeepSeekChatProperties.this.setThinking(thinking);
}

@DeprecatedConfigurationProperty(replacement = "spring.ai.deepseek.chat.reasoning-effort")
@Deprecated(since = "2.0.0", forRemoval = true)
public @Nullable ReasoningEffort getReasoningEffort() {
return DeepSeekChatProperties.this.getReasoningEffort();
}

public void setReasoningEffort(@Nullable ReasoningEffort reasoningEffort) {
DeepSeekChatProperties.this.setReasoningEffort(reasoningEffort);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.jupiter.api.Test;

import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionRequest.ReasoningEffort;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionRequest.Thinking;
import org.springframework.ai.model.tool.autoconfigure.ToolCallingAutoConfiguration;
import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand Down Expand Up @@ -112,7 +114,9 @@ public void chatOptionsTest() {
"spring.ai.deepseek.chat.stop=boza,koza",
"spring.ai.deepseek.chat.temperature=0.55",
"spring.ai.deepseek.chat.top-p=0.56",
"spring.ai.deepseek.chat.user=userXYZ"
"spring.ai.deepseek.chat.user=userXYZ",
"spring.ai.deepseek.chat.thinking.type=disabled",
"spring.ai.deepseek.chat.reasoning-effort=max"
)
// @formatter:on
.withConfiguration(AutoConfigurations.of(DeepSeekChatAutoConfiguration.class,
Expand All @@ -132,6 +136,8 @@ public void chatOptionsTest() {
assertThat(chatProperties.getStop()).contains("boza", "koza");
assertThat(chatProperties.getTemperature()).isEqualTo(0.55);
assertThat(chatProperties.getTopP()).isEqualTo(0.56);
assertThat(chatProperties.getThinking()).isEqualTo(Thinking.DISABLED);
assertThat(chatProperties.getReasoningEffort()).isEqualTo(ReasoningEffort.MAX);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import io.micrometer.observation.Observation;
Expand Down Expand Up @@ -54,7 +55,6 @@
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.ToolCall;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionRequest;
import org.springframework.ai.deepseek.api.common.DeepSeekConstants;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.ai.support.UsageCalculator;
Expand All @@ -71,6 +71,7 @@
* @author Geng Rong
* @author Thomas Vitale
* @author Sebastien Deleuze
* @author guan xu
*/
public class DeepSeekChatModel implements ChatModel {

Expand Down Expand Up @@ -227,38 +228,36 @@ private Flux<ChatResponse> internalStream(Prompt prompt, @Nullable ChatResponse
observation.start();
}

// @formatter:off
Flux<ChatResponse> chatResponse = completionChunks.map(this::chunkToChatCompletion)
.map(chatCompletion2 -> {
try {
String id = chatCompletion2.id();
Flux<ChatResponse> chatResponse = completionChunks.map(this::chunkToChatCompletion).map(chatCompletion2 -> {
try {
String id = chatCompletion2.id();

List<Generation> generations = chatCompletion2.choices().stream().map(choice -> {
if (choice.message().role() != null) {
roleMap.putIfAbsent(id, choice.message().role().name());
}
List<Generation> generations = chatCompletion2.choices().stream().map(choice -> {
if (choice.message().role() != null) {
roleMap.putIfAbsent(id, choice.message().role().name());
}

// @formatter:off
// @formatter:off
Map<String, Object> metadata = Map.of(
"id", chatCompletion2.id(),
"role", roleMap.getOrDefault(id, ""),
"finishReason", choice.finishReason() != null ? choice.finishReason().name() : ""
);
// @formatter:on
return buildGeneration(choice, metadata);
}).toList();
DeepSeekApi.Usage usage = chatCompletion2.usage();
Usage currentUsage = (usage != null) ? getDefaultUsage(usage) : new EmptyUsage();
Usage cumulativeUsage = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse);

return new ChatResponse(generations, from(chatCompletion2, cumulativeUsage));
}
catch (Exception e) {
logger.error("Error processing chat completion", e);
return new ChatResponse(List.of());
}
// @formatter:on
return buildGeneration(choice, metadata);
}).toList();
DeepSeekApi.Usage usage = chatCompletion2.usage();
Usage currentUsage = (usage != null) ? getDefaultUsage(usage) : new EmptyUsage();
Usage cumulativeUsage = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse);

return new ChatResponse(generations, from(chatCompletion2, cumulativeUsage));
}
catch (Exception e) {
logger.error("Error processing chat completion", e);
return new ChatResponse(List.of());
}

});
});

// @formatter:off
Flux<ChatResponse> flux = chatResponse
Expand Down Expand Up @@ -386,34 +385,62 @@ else if (message.getMessageType() == MessageType.TOOL) {
}
}).flatMap(List::stream).toList();

ChatCompletionRequest request = new ChatCompletionRequest(chatCompletionMessages, stream);
ChatCompletionRequest.Builder requestBuilder = ChatCompletionRequest.builder()
.messages(chatCompletionMessages)
.stream(stream);

DeepSeekChatOptions options = (DeepSeekChatOptions) prompt.getOptions();
Assert.state(options != null, "requestOptions must not be null");
request = new ChatCompletionRequest(request.messages(),
ModelOptionsUtils.mergeOption(options.getModel(), request.model()),
ModelOptionsUtils.mergeOption(options.getFrequencyPenalty(), request.frequencyPenalty()),
ModelOptionsUtils.mergeOption(options.getMaxTokens(), request.maxTokens()),
ModelOptionsUtils.mergeOption(options.getPresencePenalty(), request.presencePenalty()),
ModelOptionsUtils.mergeOption(options.getResponseFormat(), request.responseFormat()),
ModelOptionsUtils.mergeOption(options.getStop(), request.stop()), request.stream(),
ModelOptionsUtils.mergeOption(options.getTemperature(), request.temperature()),
ModelOptionsUtils.mergeOption(options.getTopP(), request.topP()),
ModelOptionsUtils.mergeOption(options.getLogprobs(), request.logprobs()),
ModelOptionsUtils.mergeOption(options.getTopLogprobs(), request.topLogprobs()),
ModelOptionsUtils.mergeOption(options.getTools(), request.tools()),
ModelOptionsUtils.mergeOption(options.getToolChoice(), request.toolChoice()));
if (options.getModel() != null) {
requestBuilder.model(options.getModel());
}
if (options.getFrequencyPenalty() != null) {
requestBuilder.frequencyPenalty(options.getFrequencyPenalty());
}
if (options.getMaxTokens() != null) {
requestBuilder.maxTokens(options.getMaxTokens());
}
if (options.getPresencePenalty() != null) {
requestBuilder.presencePenalty(options.getPresencePenalty());
}
if (options.getResponseFormat() != null) {
requestBuilder.responseFormat(options.getResponseFormat());
}
if (options.getStop() != null) {
requestBuilder.stop(options.getStop());
}
if (options.getTemperature() != null) {
requestBuilder.temperature(options.getTemperature());
}
if (options.getTopP() != null) {
requestBuilder.topP(options.getTopP());
}
if (options.getLogprobs() != null) {
requestBuilder.logprobs(options.getLogprobs());
}
if (options.getTopLogprobs() != null) {
requestBuilder.topLogprobs(options.getTopLogprobs());
}
if (options.getTools() != null) {
requestBuilder.tools(options.getTools());
}
if (options.getToolChoice() != null) {
requestBuilder.toolChoice(options.getToolChoice());
}
if (options.getThinking() != null) {
requestBuilder.thinking(options.getThinking());
}
if (options.getReasoningEffort() != null) {
requestBuilder.reasoningEffort(options.getReasoningEffort());
}

// Add the tool definitions to the request's tools parameter.
List<ToolDefinition> toolDefinitions = this.toolCallingManager.resolveToolDefinitions(options);
if (!CollectionUtils.isEmpty(toolDefinitions)) {
request = new ChatCompletionRequest(request.messages(), request.model(), request.frequencyPenalty(),
request.maxTokens(), request.presencePenalty(), request.responseFormat(), request.stop(),
request.stream(), request.temperature(), request.topP(), request.logprobs(), request.topLogprobs(),
this.getFunctionTools(toolDefinitions), request.toolChoice());
requestBuilder.tools(this.getFunctionTools(toolDefinitions));
}

return request;
return requestBuilder.build();
}

private List<DeepSeekApi.FunctionTool> getFunctionTools(List<ToolDefinition> toolDefinitions) {
Expand Down Expand Up @@ -512,11 +539,8 @@ public Builder observationRegistry(ObservationRegistry observationRegistry) {

public DeepSeekChatModel build() {
Assert.state(this.deepSeekApi != null, "DeepSeekApi must not be null");
if (this.toolCallingManager != null) {
return new DeepSeekChatModel(this.deepSeekApi, this.options, this.toolCallingManager,
this.retryTemplate, this.observationRegistry);
}
return new DeepSeekChatModel(this.deepSeekApi, this.options, DEFAULT_TOOL_CALLING_MANAGER,
return new DeepSeekChatModel(this.deepSeekApi, this.options,
Objects.requireNonNullElse(this.toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER),
this.retryTemplate, this.observationRegistry);
}

Expand Down
Loading
Loading