toolCalls = new ArrayList<>();
+ ToolCall lastPreviousTooCall = null;
+ if (previous.toolCalls() != null) {
+ lastPreviousTooCall = previous.toolCalls().get(previous.toolCalls().size() - 1);
+ if (previous.toolCalls().size() > 1) {
+ toolCalls.addAll(previous.toolCalls().subList(0, previous.toolCalls().size() - 1));
+ }
+ }
+ if (current.toolCalls() != null) {
+ if (current.toolCalls().size() > 1) {
+ throw new IllegalStateException("Currently only one tool call is supported per message!");
+ }
+ var currentToolCall = current.toolCalls().iterator().next();
+ if (currentToolCall.id() != null) {
+ if (lastPreviousTooCall != null) {
+ toolCalls.add(lastPreviousTooCall);
+ }
+ toolCalls.add(currentToolCall);
+ }
+ else {
+ toolCalls.add(merge(lastPreviousTooCall, currentToolCall));
+ }
+ }
+ else {
+ if (lastPreviousTooCall != null) {
+ toolCalls.add(lastPreviousTooCall);
+ }
+ }
+ return new ChatCompletionMessage(content, role, name, toolCallId, toolCalls);
+ }
+
+ private ToolCall merge(ToolCall previous, ToolCall current) {
+ if (previous == null) {
+ return current;
+ }
+ String id = (current.id() != null ? current.id() : previous.id());
+ String type = (current.type() != null ? current.type() : previous.type());
+ ChatCompletionFunction function = merge(previous.function(), current.function());
+ return new ToolCall(id, type, function);
+ }
+
+ private ChatCompletionFunction merge(ChatCompletionFunction previous, ChatCompletionFunction current) {
+ if (previous == null) {
+ return current;
+ }
+ String name = (current.name() != null ? current.name() : previous.name());
+ StringBuilder arguments = new StringBuilder();
+ if (previous.arguments() != null) {
+ arguments.append(previous.arguments());
+ }
+ if (current.arguments() != null) {
+ arguments.append(current.arguments());
+ }
+ return new ChatCompletionFunction(name, arguments.toString());
+ }
+
+ /**
+ * @param chatCompletion the ChatCompletionChunk to check
+ * @return true if the ChatCompletionChunk is a streaming tool function call.
+ */
+ public boolean isStreamingToolFunctionCall(ChatCompletionChunk chatCompletion) {
+
+ if (chatCompletion == null || CollectionUtils.isEmpty(chatCompletion.choices())) {
+ return false;
+ }
+
+ var choice = chatCompletion.choices().get(0);
+ if (choice == null || choice.delta() == null) {
+ return false;
+ }
+ return !CollectionUtils.isEmpty(choice.delta().toolCalls());
+ }
+
+ /**
+ * @param chatCompletion the ChatCompletionChunk to check
+ * @return true if the ChatCompletionChunk is a streaming tool function call and it is
+ * the last one.
+ */
+ public boolean isStreamingToolFunctionCallFinish(ChatCompletionChunk chatCompletion) {
+
+ if (chatCompletion == null || CollectionUtils.isEmpty(chatCompletion.choices())) {
+ return false;
+ }
+
+ var choice = chatCompletion.choices().get(0);
+ if (choice == null || choice.delta() == null) {
+ return false;
+ }
+ return choice.finishReason() == ChatCompletionFinishReason.TOOL_CALLS;
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/ResponseFormat.java b/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/ResponseFormat.java
new file mode 100644
index 00000000000..826675545fa
--- /dev/null
+++ b/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/ResponseFormat.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2023-2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.ai.deepseek.api;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.Objects;
+
+/**
+ * An object specifying the format that the model must output. Setting to { "type":
+ * "json_object" } enables JSON Output, which guarantees the message the model generates
+ * is valid JSON.
+ *
+ * Important: When using JSON Output, you must also instruct the model to produce JSON
+ * yourself via a system or user message. Without this, the model may generate an unending
+ * stream of whitespace until the generation reaches the token limit, resulting in a
+ * long-running and seemingly "stuck" request. Also note that the message content may be
+ * partially cut off if finish_reason="length", which indicates the generation exceeded
+ * max_tokens or the conversation exceeded the max context length.
+ *
+ * References:
+ * DeepSeek API -
+ * Create Chat Completion
+ *
+ * @author Geng Rong
+ */
+
+@JsonInclude(Include.NON_NULL)
+public class ResponseFormat {
+
+ /**
+ * Type Must be one of 'text', 'json_object'.
+ */
+ @JsonProperty("type")
+ private Type type;
+
+ public Type getType() {
+ return this.type;
+ }
+
+ public void setType(Type type) {
+ this.type = type;
+ }
+
+ private ResponseFormat(Type type) {
+ this.type = type;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ ResponseFormat that = (ResponseFormat) o;
+ return this.type == that.type;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.type);
+ }
+
+ @Override
+ public String toString() {
+ return "ResponseFormat{" + "type=" + this.type + '}';
+ }
+
+ public static final class Builder {
+
+ private Type type;
+
+ private Builder() {
+ }
+
+ public Builder type(Type type) {
+ this.type = type;
+ return this;
+ }
+
+ public ResponseFormat build() {
+ return new ResponseFormat(this.type);
+ }
+
+ }
+
+ public enum Type {
+
+ /**
+ * Generates a text response. (default)
+ */
+ @JsonProperty("text")
+ TEXT,
+
+ /**
+ * Enables JSON mode, which guarantees the message the model generates is valid
+ * JSON.
+ */
+ @JsonProperty("json_object")
+ JSON_OBJECT,
+
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/common/DeepSeekConstants.java b/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/common/DeepSeekConstants.java
new file mode 100644
index 00000000000..904b8e9a916
--- /dev/null
+++ b/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/common/DeepSeekConstants.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek.api.common;
+
+import org.springframework.ai.observation.conventions.AiProvider;
+
+/**
+ * @author Geng Rong
+ */
+public class DeepSeekConstants {
+
+ public static final String DEFAULT_BASE_URL = "https://api.deepseek.com";
+
+ public static final String DEFAULT_COMPLETIONS_PATH = "/chat/completions";
+
+ public static final String DEFAULT_BETA_PATH = "/beta";
+
+ public static final String PROVIDER_NAME = AiProvider.DEEPSEEK.value();
+
+ private DeepSeekConstants() {
+
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/main/resources/META-INF/spring/aot.factories b/models/spring-ai-deepseek/src/main/resources/META-INF/spring/aot.factories
new file mode 100644
index 00000000000..112c3a5eeb7
--- /dev/null
+++ b/models/spring-ai-deepseek/src/main/resources/META-INF/spring/aot.factories
@@ -0,0 +1,2 @@
+org.springframework.aot.hint.RuntimeHintsRegistrar=\
+ org.springframework.ai.deepseek.aot.DeepSeekRuntimeHints
\ No newline at end of file
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekChatCompletionRequestTests.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekChatCompletionRequestTests.java
new file mode 100644
index 00000000000..c5fafb72eae
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekChatCompletionRequestTests.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+public class DeepSeekChatCompletionRequestTests {
+
+ @Test
+ public void createRequestWithChatOptions() {
+
+ var client = DeepSeekChatModel.builder()
+ .deepSeekApi(DeepSeekApi.builder().apiKey("TEST").build())
+ .defaultOptions(DeepSeekChatOptions.builder().model("DEFAULT_MODEL").temperature(66.6).build())
+ .build();
+
+ var prompt = client.buildRequestPrompt(new Prompt("Test message content"));
+
+ var request = client.createRequest(prompt, false);
+
+ assertThat(request.messages()).hasSize(1);
+ assertThat(request.stream()).isFalse();
+
+ assertThat(request.model()).isEqualTo("DEFAULT_MODEL");
+ assertThat(request.temperature()).isEqualTo(66.6D);
+
+ request = client.createRequest(new Prompt("Test message content",
+ DeepSeekChatOptions.builder().model("PROMPT_MODEL").temperature(99.9D).build()), true);
+
+ assertThat(request.messages()).hasSize(1);
+ assertThat(request.stream()).isTrue();
+
+ assertThat(request.model()).isEqualTo("PROMPT_MODEL");
+ assertThat(request.temperature()).isEqualTo(99.9D);
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekRetryTests.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekRetryTests.java
new file mode 100644
index 00000000000..772f2fe106b
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekRetryTests.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2023-2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.ai.deepseek;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.ai.deepseek.api.DeepSeekApi.*;
+import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.Role;
+import org.springframework.ai.retry.RetryUtils;
+import org.springframework.ai.retry.TransientAiException;
+import org.springframework.http.ResponseEntity;
+import org.springframework.retry.RetryCallback;
+import org.springframework.retry.RetryContext;
+import org.springframework.retry.RetryListener;
+import org.springframework.retry.support.RetryTemplate;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.isA;
+import static org.mockito.BDDMockito.given;
+
+/**
+ * @author Geng Rong
+ */
+@SuppressWarnings("unchecked")
+@ExtendWith(MockitoExtension.class)
+public class DeepSeekRetryTests {
+
+ private TestRetryListener retryListener;
+
+ private @Mock DeepSeekApi deepSeekApi;
+
+ private DeepSeekChatModel chatModel;
+
+ @BeforeEach
+ public void beforeEach() {
+ RetryTemplate retryTemplate = RetryUtils.SHORT_RETRY_TEMPLATE;
+ this.retryListener = new TestRetryListener();
+ retryTemplate.registerListener(this.retryListener);
+
+ this.chatModel = DeepSeekChatModel.builder()
+ .deepSeekApi(this.deepSeekApi)
+ .defaultOptions(DeepSeekChatOptions.builder().build())
+ .retryTemplate(retryTemplate)
+ .build();
+ ;
+ }
+
+ @Test
+ public void deepSeekChatTransientError() {
+
+ var choice = new ChatCompletion.Choice(ChatCompletionFinishReason.STOP, 0,
+ new ChatCompletionMessage("Response", Role.ASSISTANT), null);
+ ChatCompletion expectedChatCompletion = new ChatCompletion("id", List.of(choice), 789L, "model", null,
+ "chat.completion", new DeepSeekApi.Usage(10, 10, 10));
+
+ given(this.deepSeekApi.chatCompletionEntity(isA(ChatCompletionRequest.class)))
+ .willThrow(new TransientAiException("Transient Error 1"))
+ .willThrow(new TransientAiException("Transient Error 2"))
+ .willReturn(ResponseEntity.of(Optional.of(expectedChatCompletion)));
+
+ var result = this.chatModel.call(new Prompt("text"));
+
+ assertThat(result).isNotNull();
+ assertThat(result.getResult().getOutput().getText()).isSameAs("Response");
+ assertThat(this.retryListener.onSuccessRetryCount).isEqualTo(2);
+ assertThat(this.retryListener.onErrorRetryCount).isEqualTo(2);
+ }
+
+ @Test
+ public void deepSeekChatNonTransientError() {
+ given(this.deepSeekApi.chatCompletionEntity(isA(ChatCompletionRequest.class)))
+ .willThrow(new RuntimeException("Non Transient Error"));
+ assertThrows(RuntimeException.class, () -> this.chatModel.call(new Prompt("text")));
+ }
+
+ @Test
+ public void deepSeekChatStreamTransientError() {
+
+ var choice = new ChatCompletion.Choice(ChatCompletionFinishReason.STOP, 0,
+ new ChatCompletionMessage("Response", Role.ASSISTANT), null);
+ ChatCompletion expectedChatCompletion = new ChatCompletion("id", List.of(choice), 666L, "model", null,
+ "chat.completion", new DeepSeekApi.Usage(10, 10, 10));
+
+ given(this.deepSeekApi.chatCompletionEntity(isA(ChatCompletionRequest.class)))
+ .willThrow(new TransientAiException("Transient Error 1"))
+ .willThrow(new TransientAiException("Transient Error 2"))
+ .willReturn(ResponseEntity.of(Optional.of(expectedChatCompletion)));
+
+ var result = this.chatModel.call(new Prompt("text"));
+
+ assertThat(result).isNotNull();
+ assertThat(result.getResult().getOutput().getText()).isSameAs("Response");
+ assertThat(this.retryListener.onSuccessRetryCount).isEqualTo(2);
+ assertThat(this.retryListener.onErrorRetryCount).isEqualTo(2);
+ }
+
+ @Test
+ public void deepSeekChatStreamNonTransientError() {
+ given(this.deepSeekApi.chatCompletionStream(isA(ChatCompletionRequest.class)))
+ .willThrow(new RuntimeException("Non Transient Error"));
+ assertThrows(RuntimeException.class, () -> this.chatModel.stream(new Prompt("text")).collectList().block());
+ }
+
+ private static class TestRetryListener implements RetryListener {
+
+ int onErrorRetryCount = 0;
+
+ int onSuccessRetryCount = 0;
+
+ @Override
+ public void onSuccess(RetryContext context, RetryCallback callback, T result) {
+ this.onSuccessRetryCount = context.getRetryCount();
+ }
+
+ @Override
+ public void onError(RetryContext context, RetryCallback callback,
+ Throwable throwable) {
+ this.onErrorRetryCount = context.getRetryCount();
+ }
+
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekTestConfiguration.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekTestConfiguration.java
new file mode 100644
index 00000000000..6e6cbdc3e49
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/DeepSeekTestConfiguration.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek;
+
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.util.StringUtils;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootConfiguration
+public class DeepSeekTestConfiguration {
+
+ @Bean
+ public DeepSeekApi deepSeekApi() {
+ return DeepSeekApi.builder().apiKey(getApiKey()).build();
+ }
+
+ private String getApiKey() {
+ String apiKey = System.getenv("DEEPSEEK_API_KEY");
+ if (!StringUtils.hasText(apiKey)) {
+ throw new IllegalArgumentException(
+ "You must provide an API key. Put it in an environment variable under the name DEEPSEEK_API_KEY");
+ }
+ return apiKey;
+ }
+
+ @Bean
+ public DeepSeekChatModel deepSeekChatModel(DeepSeekApi api) {
+ return DeepSeekChatModel.builder().deepSeekApi(api).build();
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/aot/DeepSeekRuntimeHintsTests.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/aot/DeepSeekRuntimeHintsTests.java
new file mode 100644
index 00000000000..089db117125
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/aot/DeepSeekRuntimeHintsTests.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek.aot;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.TypeReference;
+
+import java.util.Set;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.springframework.ai.aot.AiRuntimeHints.findJsonAnnotatedClassesInPackage;
+import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
+
+/**
+ * @author Geng Rong
+ */
+class DeepSeekRuntimeHintsTests {
+
+ @Test
+ void registerHints() {
+ RuntimeHints runtimeHints = new RuntimeHints();
+ DeepSeekRuntimeHints deepSeekRuntimeHints = new DeepSeekRuntimeHints();
+ deepSeekRuntimeHints.registerHints(runtimeHints, null);
+
+ Set jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage(DeepSeekApi.class);
+ for (TypeReference jsonAnnotatedClass : jsonAnnotatedClasses) {
+ assertThat(runtimeHints).matches(reflection().onType(jsonAnnotatedClass));
+ }
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/DeepSeekApiIT.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/DeepSeekApiIT.java
new file mode 100644
index 00000000000..0e02625604f
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/DeepSeekApiIT.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek.api;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.ai.deepseek.api.DeepSeekApi.*;
+import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.Role;
+import org.springframework.http.ResponseEntity;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "DEEPSEEK_API_KEY", matches = ".+")
+public class DeepSeekApiIT {
+
+ DeepSeekApi deepSeekApi = DeepSeekApi.builder().apiKey(System.getenv("DEEPSEEK_API_KEY")).build();
+
+ @Test
+ void chatCompletionEntity() {
+ ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
+ ResponseEntity response = deepSeekApi.chatCompletionEntity(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), ChatModel.DEEPSEEK_CHAT.value, 1D, false));
+
+ assertThat(response).isNotNull();
+ assertThat(response.getBody()).isNotNull();
+ }
+
+ @Test
+ void chatCompletionStream() {
+ ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
+ Flux response = deepSeekApi.chatCompletionStream(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), ChatModel.DEEPSEEK_CHAT.value, 1D, true));
+
+ assertThat(response).isNotNull();
+ assertThat(response.collectList().block()).isNotNull();
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/MockWeatherService.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/MockWeatherService.java
new file mode 100644
index 00000000000..060c6594706
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/MockWeatherService.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2023-2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.ai.deepseek.api;
+
+import com.fasterxml.jackson.annotation.JsonClassDescription;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
+
+import java.util.function.Function;
+
+/**
+ * @author Geng Rong
+ */
+public class MockWeatherService implements Function {
+
+ @Override
+ public Response apply(Request request) {
+
+ double temperature = 0;
+ if (request.location().contains("Paris")) {
+ temperature = 15;
+ }
+ else if (request.location().contains("Tokyo")) {
+ temperature = 10;
+ }
+ else if (request.location().contains("San Francisco")) {
+ temperature = 30;
+ }
+
+ return new Response(temperature, 15, 20, 2, 53, 45, request.unit);
+ }
+
+ /**
+ * Temperature units.
+ */
+ public enum Unit {
+
+ /**
+ * Celsius.
+ */
+ C("metric"),
+ /**
+ * Fahrenheit.
+ */
+ F("imperial");
+
+ /**
+ * Human readable unit name.
+ */
+ public final String unitName;
+
+ Unit(String text) {
+ this.unitName = text;
+ }
+
+ }
+
+ /**
+ * Weather Function request.
+ */
+ @JsonInclude(Include.NON_NULL)
+ @JsonClassDescription("Weather API request")
+ public record Request(@JsonProperty(required = true,
+ value = "location") @JsonPropertyDescription("The city and state e.g. San Francisco, CA") String location,
+ @JsonProperty("lat") @JsonPropertyDescription("The city latitude") double lat,
+ @JsonProperty("lon") @JsonPropertyDescription("The city longitude") double lon,
+ @JsonProperty(required = true, value = "unit") @JsonPropertyDescription("Temperature unit") Unit unit) {
+
+ }
+
+ /**
+ * Weather Function response.
+ */
+ public record Response(double temp, double feels_like, double temp_min, double temp_max, int pressure, int humidity,
+ Unit unit) {
+
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/ActorsFilms.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/ActorsFilms.java
new file mode 100644
index 00000000000..53f529ef3e4
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/ActorsFilms.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek.chat;
+
+import java.util.List;
+
+/**
+ * @author Geng Rong
+ */
+public class ActorsFilms {
+
+ private String actor;
+
+ private List movies;
+
+ public ActorsFilms() {
+ }
+
+ public String getActor() {
+ return actor;
+ }
+
+ public void setActor(String actor) {
+ this.actor = actor;
+ }
+
+ public List getMovies() {
+ return movies;
+ }
+
+ public void setMovies(List movies) {
+ this.movies = movies;
+ }
+
+ @Override
+ public String toString() {
+ return "ActorsFilms{" + "actor='" + actor + '\'' + ", movies=" + movies + '}';
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelFunctionCallingIT.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelFunctionCallingIT.java
new file mode 100644
index 00000000000..32306d34079
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelFunctionCallingIT.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2023-2025 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.ai.deepseek.chat;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.Message;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatModel;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.deepseek.DeepSeekChatOptions;
+import org.springframework.ai.deepseek.DeepSeekTestConfiguration;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.ai.deepseek.api.MockWeatherService;
+import org.springframework.ai.tool.function.FunctionToolCallback;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import reactor.core.publisher.Flux;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootTest(classes = DeepSeekTestConfiguration.class)
+// @Disabled("the deepseek-chat model's Function Calling capability is unstable see:
+// https://api-docs.deepseek.com/guides/function_calling")
+@EnabledIfEnvironmentVariable(named = "DEEPSEEK_API_KEY", matches = ".+")
+class DeepSeekChatModelFunctionCallingIT {
+
+ private static final Logger logger = LoggerFactory.getLogger(DeepSeekChatModelFunctionCallingIT.class);
+
+ @Autowired
+ ChatModel chatModel;
+
+ private static final DeepSeekApi.FunctionTool FUNCTION_TOOL = new DeepSeekApi.FunctionTool(
+ DeepSeekApi.FunctionTool.Type.FUNCTION, new DeepSeekApi.FunctionTool.Function(
+ "Get the weather in location. Return temperature in 30°F or 30°C format.", "getCurrentWeather", """
+ {
+ "type": "object",
+ "properties": {
+ "location": {
+ "type": "string",
+ "description": "The city and state e.g. San Francisco, CA"
+ },
+ "lat": {
+ "type": "number",
+ "description": "The city latitude"
+ },
+ "lon": {
+ "type": "number",
+ "description": "The city longitude"
+ },
+ "unit": {
+ "type": "string",
+ "enum": ["C", "F"]
+ }
+ },
+ "required": ["location", "lat", "lon", "unit"]
+ }
+ """));
+
+ @Test
+ void functionCallTest() {
+
+ UserMessage userMessage = new UserMessage(
+ "What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.");
+
+ List messages = new ArrayList<>(List.of(userMessage));
+
+ var promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
+ .description("Get the weather in location")
+ .inputType(MockWeatherService.Request.class)
+ .build()))
+ .build();
+
+ ChatResponse response = this.chatModel.call(new Prompt(messages, promptOptions));
+
+ logger.info("Response: {}", response);
+
+ assertThat(response.getResult().getOutput().getText()).contains("30", "10", "15");
+ }
+
+ @Test
+ void streamFunctionCallTest() {
+
+ UserMessage userMessage = new UserMessage(
+ "What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.");
+
+ List messages = new ArrayList<>(List.of(userMessage));
+
+ var promptOptions = DeepSeekChatOptions.builder()
+ .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
+ .description("Get the weather in location")
+ .inputType(MockWeatherService.Request.class)
+ .build()))
+ .build();
+
+ Flux response = this.chatModel.stream(new Prompt(messages, promptOptions));
+
+ String content = response.collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getText)
+ .filter(Objects::nonNull)
+ .collect(Collectors.joining());
+ logger.info("Response: {}", content);
+
+ assertThat(content).contains("30", "10", "15");
+ }
+
+ @Test
+ public void toolFunctionCallWithUsage() {
+ var promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .tools(Arrays.asList(FUNCTION_TOOL))
+ .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
+ .description("Get the weather in location")
+ .inputType(MockWeatherService.Request.class)
+ .build()))
+ .build();
+ Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.",
+ promptOptions);
+
+ ChatResponse chatResponse = this.chatModel.call(prompt);
+ assertThat(chatResponse).isNotNull();
+ assertThat(chatResponse.getResult().getOutput());
+ assertThat(chatResponse.getResult().getOutput().getText()).contains("San Francisco");
+ assertThat(chatResponse.getResult().getOutput().getText()).contains("30");
+ // 这个 total token 是第一次 chat 以及 tool call 之后的两次请求 token 总和
+
+ // the total token is first chat and tool call request
+ assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(700).isGreaterThan(280);
+ }
+
+ @Test
+ public void testStreamFunctionCallUsage() {
+ var promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .tools(Arrays.asList(FUNCTION_TOOL))
+ .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
+ .description("Get the weather in location")
+ .inputType(MockWeatherService.Request.class)
+ .build()))
+ .build();
+ Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.",
+ promptOptions);
+
+ ChatResponse chatResponse = this.chatModel.stream(prompt).blockLast();
+ assertThat(chatResponse).isNotNull();
+ assertThat(chatResponse.getMetadata()).isNotNull();
+ assertThat(chatResponse.getMetadata().getUsage()).isNotNull();
+ assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(700).isGreaterThan(280);
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelIT.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelIT.java
new file mode 100644
index 00000000000..1909ce808a0
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelIT.java
@@ -0,0 +1,278 @@
+/*
+ * Copyright 2023 - 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ai.deepseek.chat;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.Message;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatModel;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.model.StreamingChatModel;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.chat.prompt.PromptTemplate;
+import org.springframework.ai.chat.prompt.SystemPromptTemplate;
+import org.springframework.ai.converter.BeanOutputConverter;
+import org.springframework.ai.converter.ListOutputConverter;
+import org.springframework.ai.converter.MapOutputConverter;
+import org.springframework.ai.deepseek.DeepSeekChatOptions;
+import org.springframework.ai.deepseek.DeepSeekTestConfiguration;
+import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.ai.deepseek.api.MockWeatherService;
+import org.springframework.ai.tool.function.FunctionToolCallback;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.convert.support.DefaultConversionService;
+import org.springframework.core.io.Resource;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootTest(classes = DeepSeekTestConfiguration.class)
+@EnabledIfEnvironmentVariable(named = "DEEPSEEK_API_KEY", matches = ".+")
+class DeepSeekChatModelIT {
+
+ @Autowired
+ protected ChatModel chatModel;
+
+ @Autowired
+ protected StreamingChatModel streamingChatModel;
+
+ private static final Logger logger = LoggerFactory.getLogger(DeepSeekChatModelIT.class);
+
+ @Value("classpath:/prompts/system-message.st")
+ private Resource systemResource;
+
+ @Test
+ void roleTest() {
+ UserMessage userMessage = new UserMessage(
+ "Tell me about 3 famous pirates from the Golden Age of Piracy and what they did.");
+ SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
+ Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
+ Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
+ ChatResponse response = chatModel.call(prompt);
+ assertThat(response.getResults()).hasSize(1);
+ assertThat(response.getResults().get(0).getOutput().getText()).contains("Blackbeard");
+ // needs fine tuning... evaluateQuestionAndAnswer(request, response, false);
+ }
+
+ @Test
+ void listOutputConverter() {
+ DefaultConversionService conversionService = new DefaultConversionService();
+ ListOutputConverter outputConverter = new ListOutputConverter(conversionService);
+
+ String format = outputConverter.getFormat();
+ String template = """
+ List five {subject}
+ {format}
+ """;
+ PromptTemplate promptTemplate = PromptTemplate.builder()
+ .template(template)
+ .variables(Map.of("subject", "ice cream flavors", "format", format))
+ .build();
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = this.chatModel.call(prompt).getResult();
+
+ List list = outputConverter.convert(generation.getOutput().getText());
+ assertThat(list).hasSize(5);
+
+ }
+
+ @Test
+ void mapOutputConverter() {
+ MapOutputConverter outputConverter = new MapOutputConverter();
+
+ String format = outputConverter.getFormat();
+ String template = """
+ Please provide the JSON response without any code block markers such as ```json```.
+ Provide me a List of {subject}
+ {format}
+ """;
+ PromptTemplate promptTemplate = PromptTemplate.builder()
+ .template(template)
+ .variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
+ format))
+ .build();
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ Map result = outputConverter.convert(generation.getOutput().getText());
+ assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
+
+ }
+
+ @Test
+ void beanOutputConverter() {
+
+ BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class);
+
+ String format = outputConverter.getFormat();
+ String template = """
+ Generate the filmography for a random actor.
+ Please provide the JSON response without any code block markers such as ```json```.
+ {format}
+ """;
+ PromptTemplate promptTemplate = PromptTemplate.builder()
+ .template(template)
+ .variables(Map.of("format", format))
+ .build();
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ ActorsFilms actorsFilms = outputConverter.convert(generation.getOutput().getText());
+ }
+
+ record ActorsFilmsRecord(String actor, List movies) {
+ }
+
+ @Test
+ void beanOutputConverterRecords() {
+
+ BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class);
+
+ String format = outputConverter.getFormat();
+ String template = """
+ Generate the filmography of 5 movies for Tom Hanks.
+ Please provide the JSON response without any code block markers such as ```json```.
+ {format}
+ """;
+ PromptTemplate promptTemplate = PromptTemplate.builder()
+ .template(template)
+ .variables(Map.of("format", format))
+ .build();
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ ActorsFilmsRecord actorsFilms = outputConverter.convert(generation.getOutput().getText());
+ logger.info("" + actorsFilms);
+ assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
+ assertThat(actorsFilms.movies()).hasSize(5);
+ }
+
+ @Test
+ void beanStreamOutputConverterRecords() {
+
+ BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class);
+
+ String format = outputConverter.getFormat();
+ String template = """
+ Generate the filmography of 5 movies for Tom Hanks.
+ Please provide the JSON response without any code block markers such as ```json```.
+ {format}
+ """;
+ PromptTemplate promptTemplate = PromptTemplate.builder()
+ .template(template)
+ .variables(Map.of("format", format))
+ .build();
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+
+ String generationTextFromStream = streamingChatModel.stream(prompt)
+ .collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(m -> m.getText() != null ? m.getText() : "")
+ .collect(Collectors.joining());
+
+ ActorsFilmsRecord actorsFilms = outputConverter.convert(generationTextFromStream);
+ logger.info("" + actorsFilms);
+ assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
+ assertThat(actorsFilms.movies()).hasSize(5);
+ }
+
+ @Test
+ void prefixCompletionTest() {
+ String userMessageContent = """
+ Please return this yaml data to json.
+
+ data:
+ ```yaml
+ code: 200
+ result:
+ total: 1
+ data:
+ - 1
+ - 2
+ - 3
+ ```
+ """;
+ UserMessage userMessage = new UserMessage(userMessageContent);
+ Message assistantMessage = new DeepSeekAssistantMessage("{\"code\":200,\"result\":{\"total\":1,\"data\":[1");
+ Prompt prompt = new Prompt(List.of(userMessage, assistantMessage));
+ ChatResponse response = chatModel.call(prompt);
+ assertThat(response.getResult().getOutput().getText().equals(",2,3]}}"));
+ }
+
+ /**
+ * For deepseek-reasoner model only. The reasoning contents of the assistant message,
+ * before the final answer.
+ */
+ @Test
+ void reasonerModelTest() {
+ var promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_REASONER.getValue())
+ .build();
+ Prompt prompt = new Prompt("9.11 and 9.8, which is greater?", promptOptions);
+ ChatResponse response = chatModel.call(prompt);
+
+ DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) response.getResult().getOutput();
+ assertThat(deepSeekAssistantMessage.getReasoningContent()).isNotEmpty();
+ assertThat(deepSeekAssistantMessage.getText()).isNotEmpty();
+ }
+
+ /**
+ * the deepseek-reasoner model Multi-round Conversation.
+ */
+ @Test
+ void reasonerModelMultiRoundTest() {
+ List messages = new ArrayList<>();
+ messages.add(new UserMessage("9.11 and 9.8, which is greater?"));
+ var promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_REASONER.getValue())
+ .build();
+
+ Prompt prompt = new Prompt(messages, promptOptions);
+ ChatResponse response = chatModel.call(prompt);
+
+ DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) response.getResult().getOutput();
+ assertThat(deepSeekAssistantMessage.getReasoningContent()).isNotEmpty();
+ assertThat(deepSeekAssistantMessage.getText()).isNotEmpty();
+
+ messages.add(new AssistantMessage(Objects.requireNonNull(deepSeekAssistantMessage.getText())));
+ messages.add(new UserMessage("How many Rs are there in the word 'strawberry'?"));
+ Prompt prompt2 = new Prompt(messages, promptOptions);
+ ChatResponse response2 = chatModel.call(prompt2);
+
+ DeepSeekAssistantMessage deepSeekAssistantMessage2 = (DeepSeekAssistantMessage) response2.getResult()
+ .getOutput();
+ assertThat(deepSeekAssistantMessage2.getReasoningContent()).isNotEmpty();
+ assertThat(deepSeekAssistantMessage2.getText()).isNotEmpty();
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelObservationIT.java b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelObservationIT.java
new file mode 100644
index 00000000000..e95cc46b885
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/chat/DeepSeekChatModelObservationIT.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2023-2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.ai.deepseek.chat;
+
+import io.micrometer.observation.tck.TestObservationRegistry;
+import io.micrometer.observation.tck.TestObservationRegistryAssert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.ai.chat.metadata.ChatResponseMetadata;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.deepseek.DeepSeekChatModel;
+import org.springframework.ai.deepseek.DeepSeekChatOptions;
+import org.springframework.ai.deepseek.api.DeepSeekApi;
+import org.springframework.ai.model.tool.ToolCallingManager;
+import org.springframework.ai.observation.conventions.AiOperationType;
+import org.springframework.ai.observation.conventions.AiProvider;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.retry.support.RetryTemplate;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.HighCardinalityKeyNames;
+import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.LowCardinalityKeyNames;
+
+/**
+ * Integration tests for observation instrumentation in {@link DeepSeekChatModel}.
+ *
+ * @author Geng Rong
+ */
+@SpringBootTest(classes = DeepSeekChatModelObservationIT.Config.class)
+@EnabledIfEnvironmentVariable(named = "DEEPSEEK_API_KEY", matches = ".+")
+public class DeepSeekChatModelObservationIT {
+
+ @Autowired
+ TestObservationRegistry observationRegistry;
+
+ @Autowired
+ DeepSeekChatModel chatModel;
+
+ @BeforeEach
+ void beforeEach() {
+ this.observationRegistry.clear();
+ }
+
+ @Test
+ void observationForChatOperation() {
+ var options = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .frequencyPenalty(0.0)
+ .maxTokens(2048)
+ .presencePenalty(0.0)
+ .stop(List.of("this-is-the-end"))
+ .temperature(0.7)
+ .topP(1.0)
+ .build();
+
+ Prompt prompt = new Prompt("Why does a raven look like a desk?", options);
+
+ ChatResponse chatResponse = this.chatModel.call(prompt);
+ assertThat(chatResponse.getResult().getOutput().getText()).isNotEmpty();
+
+ ChatResponseMetadata responseMetadata = chatResponse.getMetadata();
+ assertThat(responseMetadata).isNotNull();
+
+ validate(responseMetadata);
+ }
+
+ @Test
+ void observationForStreamingChatOperation() {
+ var options = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .frequencyPenalty(0.0)
+ .maxTokens(2048)
+ .presencePenalty(0.0)
+ .stop(List.of("this-is-the-end"))
+ .temperature(0.7)
+ .topP(1.0)
+ .build();
+
+ Prompt prompt = new Prompt("Why does a raven look like a desk?", options);
+
+ Flux chatResponseFlux = this.chatModel.stream(prompt);
+
+ List responses = chatResponseFlux.collectList().block();
+ assertThat(responses).isNotEmpty();
+ assertThat(responses).hasSizeGreaterThan(10);
+
+ String aggregatedResponse = responses.subList(0, responses.size() - 1)
+ .stream()
+ .map(r -> r.getResult().getOutput().getText())
+ .collect(Collectors.joining());
+ assertThat(aggregatedResponse).isNotEmpty();
+
+ ChatResponse lastChatResponse = responses.get(responses.size() - 1);
+
+ ChatResponseMetadata responseMetadata = lastChatResponse.getMetadata();
+ assertThat(responseMetadata).isNotNull();
+
+ validate(responseMetadata);
+ }
+
+ private void validate(ChatResponseMetadata responseMetadata) {
+ TestObservationRegistryAssert.assertThat(this.observationRegistry)
+ .doesNotHaveAnyRemainingCurrentObservation()
+ .hasObservationWithNameEqualTo(DefaultChatModelObservationConvention.DEFAULT_NAME)
+ .that()
+ .hasContextualNameEqualTo("chat " + DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(),
+ AiOperationType.CHAT.value())
+ .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.DEEPSEEK.value())
+ .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(),
+ DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel())
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_FREQUENCY_PENALTY.asString(), "0.0")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_MAX_TOKENS.asString(), "2048")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_PRESENCE_PENALTY.asString(), "0.0")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_STOP_SEQUENCES.asString(),
+ "[\"this-is-the-end\"]")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_TEMPERATURE.asString(), "0.7")
+ .doesNotHaveHighCardinalityKeyValueWithKey(HighCardinalityKeyNames.REQUEST_TOP_K.asString())
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_TOP_P.asString(), "1.0")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_ID.asString(), responseMetadata.getId())
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_FINISH_REASONS.asString(), "[\"STOP\"]")
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_INPUT_TOKENS.asString(),
+ String.valueOf(responseMetadata.getUsage().getPromptTokens()))
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_OUTPUT_TOKENS.asString(),
+ String.valueOf(responseMetadata.getUsage().getCompletionTokens()))
+ .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_TOTAL_TOKENS.asString(),
+ String.valueOf(responseMetadata.getUsage().getTotalTokens()))
+ .hasBeenStarted()
+ .hasBeenStopped();
+ }
+
+ @SpringBootConfiguration
+ static class Config {
+
+ @Bean
+ public TestObservationRegistry observationRegistry() {
+ return TestObservationRegistry.create();
+ }
+
+ @Bean
+ public DeepSeekApi deepSeekApi() {
+ return DeepSeekApi.builder().apiKey(System.getenv("DEEPSEEK_API_KEY")).build();
+ }
+
+ @Bean
+ public DeepSeekChatModel deepSeekChatModel(DeepSeekApi deepSeekApi,
+ TestObservationRegistry observationRegistry) {
+ return new DeepSeekChatModel(deepSeekApi, DeepSeekChatOptions.builder().build(),
+ ToolCallingManager.builder().build(), RetryTemplate.defaultInstance(), observationRegistry);
+ }
+
+ }
+
+}
diff --git a/models/spring-ai-deepseek/src/test/resources/prompts/system-message.st b/models/spring-ai-deepseek/src/test/resources/prompts/system-message.st
new file mode 100644
index 00000000000..dc2cf2dcd84
--- /dev/null
+++ b/models/spring-ai-deepseek/src/test/resources/prompts/system-message.st
@@ -0,0 +1,4 @@
+"You are a helpful AI assistant. Your name is {name}.
+You are an AI assistant that helps people find information.
+Your name is {name}
+You should reply to the user's request with your name and also in the style of a {voice}.
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 654c178521d..689361dd1fb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -76,6 +76,7 @@
auto-configurations/models/spring-ai-autoconfigure-model-transformers
auto-configurations/models/spring-ai-autoconfigure-model-vertex-ai
auto-configurations/models/spring-ai-autoconfigure-model-zhipuai
+ auto-configurations/models/spring-ai-autoconfigure-model-deepseek
auto-configurations/mcp/spring-ai-autoconfigure-mcp-client
auto-configurations/mcp/spring-ai-autoconfigure-mcp-server
@@ -172,6 +173,7 @@
models/spring-ai-vertex-ai-embedding
models/spring-ai-vertex-ai-gemini
models/spring-ai-zhipuai
+ models/spring-ai-deepseek
spring-ai-spring-boot-starters/spring-ai-starter-model-anthropic
spring-ai-spring-boot-starters/spring-ai-starter-model-azure-openai
@@ -192,6 +194,7 @@
spring-ai-spring-boot-starters/spring-ai-starter-model-vertex-ai-embedding
spring-ai-spring-boot-starters/spring-ai-starter-model-vertex-ai-gemini
spring-ai-spring-boot-starters/spring-ai-starter-model-zhipuai
+ spring-ai-spring-boot-starters/spring-ai-starter-model-deepseek
spring-ai-spring-boot-starters/spring-ai-starter-mcp-client
spring-ai-spring-boot-starters/spring-ai-starter-mcp-server
diff --git a/spring-ai-bom/pom.xml b/spring-ai-bom/pom.xml
index 64f1aa749a6..3255a4cfa85 100644
--- a/spring-ai-bom/pom.xml
+++ b/spring-ai-bom/pom.xml
@@ -322,6 +322,12 @@
${project.version}
+
+ org.springframework.ai
+ spring-ai-deepseek
+ ${project.version}
+
+
@@ -624,6 +630,12 @@
${project.version}
+
+ org.springframework.ai
+ spring-ai-autoconfigure-model-deepseek
+ ${project.version}
+
+
org.springframework.ai
@@ -973,6 +985,12 @@
${project.version}
+
+ org.springframework.ai
+ spring-ai-starter-model-deepseek
+ ${project.version}
+
+
diff --git a/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java b/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java
index 680896b9cfd..52abf2adc5b 100644
--- a/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java
+++ b/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java
@@ -78,6 +78,11 @@ public enum AiProvider {
*/
ZHIPUAI("zhipuai"),
+ /**
+ * AI system provided by DeepSeek.
+ */
+ DEEPSEEK("deepseek"),
+
/**
* AI system provided by Spring AI.
*/
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/images/deepseek_r1_multiround_example.png b/spring-ai-docs/src/main/antora/modules/ROOT/images/deepseek_r1_multiround_example.png
new file mode 100644
index 00000000000..2ad59127c6e
Binary files /dev/null and b/spring-ai-docs/src/main/antora/modules/ROOT/images/deepseek_r1_multiround_example.png differ
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
index 0f22f138829..a1e2ddbeb16 100644
--- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
+++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
@@ -16,6 +16,7 @@
*** xref:api/chat/google-vertexai.adoc[Google VertexAI]
**** xref:api/chat/vertexai-gemini-chat.adoc[VertexAI Gemini]
*** xref:api/chat/groq-chat.adoc[Groq]
+*** xref:api/chat/deepseek-chat.adoc[DeepSeek]
*** xref:api/chat/huggingface.adoc[Hugging Face]
*** xref:api/chat/mistralai-chat.adoc[Mistral AI]
**** xref:api/chat/functions/mistralai-chat-functions.adoc[Mistral Function Calling (Deprecated)]
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/deepseek-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/deepseek-chat.adoc
index b16e1fe5f20..fc937e5a034 100644
--- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/deepseek-chat.adoc
+++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/deepseek-chat.adoc
@@ -1,76 +1,49 @@
= DeepSeek Chat
-https://www.deepseek.com/[DeepSeek AI] provides the open-source DeepSeek V3 model, renowned for its cutting-edge reasoning and problem-solving capabilities.
-
-Spring AI integrates with DeepSeek AI by reusing the existing xref::api/chat/openai-chat.adoc[OpenAI] client. To get started, you'll need to obtain a https://api-docs.deepseek.com/[DeepSeek API Key], configure the base URL, and select one of the supported models.
-
-image::spring-ai-deepseek-integration.jpg[w=800,align="center"]
-
-NOTE: The current version of the deepseek-chat model's Function Calling capability is unstable, which may result in looped calls or empty responses.
-
-Check the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/proxy/DeepSeekWithOpenAiChatModelIT.java[DeepSeekWithOpenAiChatModelIT.java] tests for examples of using DeepSeek with Spring AI.
-
+Spring AI supports the various AI language models from DeepSeek. You can interact with DeepSeek language models and create a multilingual conversational assistant based on DeepSeek models.
== Prerequisites
-* **Create an API Key**:
-Visit https://api-docs.deepseek.com/[here] to create an API Key. Configure it using the `spring.ai.openai.api-key` property in your Spring AI project.
-
-* **Set the DeepSeek Base URL**:
-Set the `spring.ai.openai.base-url` property to `https://api.deepseek.com`.
-
-* **Select a DeepSeek Model**:
-Use the `spring.ai.openai.chat.options.model=` property to specify the model. Refer to https://api-docs.deepseek.com/quick_start/pricing[Supported Models] for available options.
-
-Example environment variables configuration:
+You will need to create an API with DeepSeek to access DeepSeek language models.
+Create an account at https://platform.deepseek.com/sign_up[DeepSeek registration page] and generate the token on the https://platform.deepseek.com/api_keys[API Keys page].
+The Spring AI project defines a configuration property named `spring.ai.deepseek.api-key` that you should set to the value of the `API Key` obtained from https://platform.deepseek.com/api_keys[API Keys page].
+Exporting an environment variable is one way to set that configuration property:
[source,shell]
----
-export SPRING_AI_OPENAI_API_KEY=
-export SPRING_AI_OPENAI_BASE_URL=https://api.deepseek.com
-export SPRING_AI_OPENAI_CHAT_MODEL=deepseek-chat
+export SPRING_AI_DEEPSEEK_AI_API_KEY=
----
=== Add Repositories and BOM
-Spring AI artifacts are published in Maven Central and Spring Snapshot repositories.
+Spring AI artifacts are published in Spring Milestone and Snapshot repositories.
Refer to the xref:getting-started.adoc#repositories[Repositories] section to add these repositories to your build system.
To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system.
-== Auto-configuration
-[NOTE]
-====
-There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names.
-Please refer to the https://docs.spring.io/spring-ai/reference/upgrade-notes.html[upgrade notes] for more information.
-====
+== Auto-configuration
-Spring AI provides Spring Boot auto-configuration for the OpenAI Chat Client.
-To enable it add the following dependency to your project's Maven `pom.xml` or Gradle `build.gradle` build files:
+Spring AI provides Spring Boot auto-configuration for the DeepSeek Chat Model.
+To enable it add the following dependency to your project's Maven `pom.xml` file:
-[tabs]
-======
-Maven::
-+
[source, xml]
----
org.springframework.ai
- spring-ai-starter-model-openai
+ spring-ai-deepseek-spring-boot-starter
----
-Gradle::
-+
+or to your Gradle `build.gradle` build file.
+
[source,groovy]
----
dependencies {
- implementation 'org.springframework.ai:spring-ai-starter-model-openai'
+ implementation 'org.springframework.ai:spring-ai-deepseek-spring-boot-starter'
}
----
-======
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
@@ -78,9 +51,9 @@ TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Man
==== Retry Properties
-The prefix `spring.ai.retry` is used as the property prefix that lets you configure the retry mechanism for the OpenAI chat model.
+The prefix `spring.ai.retry` is used as the property prefix that lets you configure the retry mechanism for the DeepSeek Chat model.
-[cols="3,5,1", stripes=even]
+[cols="3,5,1"]
|====
| Property | Description | Default
@@ -95,65 +68,51 @@ The prefix `spring.ai.retry` is used as the property prefix that lets you config
==== Connection Properties
-The prefix `spring.ai.openai` is used as the property prefix that lets you connect to OpenAI.
+The prefix `spring.ai.deepseek` is used as the property prefix that lets you connect to DeepSeek.
-[cols="3,5,1", stripes=even]
+[cols="3,5,1"]
|====
| Property | Description | Default
-| spring.ai.openai.base-url | The URL to connect to. Must be set to `https://api.deepseek.com` | -
-| spring.ai.openai.api-key | Your DeepSeek API Key | -
+| spring.ai.deepseek.base-url | The URL to connect to | https://api.deepseek.com
+| spring.ai.deepseek.api-key | The API Key | -
|====
-
==== Configuration Properties
-[NOTE]
-====
-Enabling and disabling of the chat auto-configurations are now configured via top level properties with the prefix `spring.ai.model.chat`.
+The prefix `spring.ai.deepseek.chat` is the property prefix that lets you configure the chat model implementation for DeepSeek.
-To enable, spring.ai.model.chat=openai (It is enabled by default)
-
-To disable, spring.ai.model.chat=none (or any value which doesn't match openai)
-
-This change is done to allow configuration of multiple models.
-====
-
-
-The prefix `spring.ai.openai.chat` is the property prefix that lets you configure the chat model implementation for OpenAI.
-[cols="3,5,1", stripes=even]
+[cols="3,5,1"]
|====
| Property | Description | Default
-| spring.ai.openai.chat.enabled (Removed and no longer valid) | Enable OpenAI chat model. | true
-| spring.ai.model.chat | Enable OpenAI chat model. | openai
-| spring.ai.openai.chat.base-url | Optional overrides the spring.ai.openai.base-url to provide chat specific url. Must be set to `https://api.deepseek.com` | -
-| spring.ai.openai.chat.api-key | Optional overrides the spring.ai.openai.api-key to provide chat specific api-key | -
-| spring.ai.openai.chat.options.model | The link:https://api-docs.deepseek.com/quick_start/pricing[DeepSeek LLM model] to use | -
-| spring.ai.openai.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict. | 0.8
-| spring.ai.openai.chat.options.frequencyPenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. | 0.0f
-| spring.ai.openai.chat.options.maxTokens | The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length. | -
-| spring.ai.openai.chat.options.n | How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs. | 1
-| spring.ai.openai.chat.options.presencePenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. | -
-| spring.ai.openai.chat.options.responseFormat | An object specifying the format that the model must output. Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON.| -
-| spring.ai.openai.chat.options.seed | This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. | -
-| spring.ai.openai.chat.options.stop | Up to 4 sequences where the API will stop generating further tokens. | -
-| spring.ai.openai.chat.options.topP | An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both. | -
-| spring.ai.openai.chat.options.tools | A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. | -
-| spring.ai.openai.chat.options.toolChoice | Controls which (if any) function is called by the model. none means the model will not call a function and instead generates a message. auto means the model can pick between generating a message or calling a function. Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function. none is the default when no functions are present. auto is the default if functions are present. | -
-| spring.ai.openai.chat.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | -
-| spring.ai.openai.chat.options.functions | List of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry. | -
-| spring.ai.openai.chat.options.stream-usage | (For streaming only) Set to add an additional chunk with token usage statistics for the entire request. The `choices` field for this chunk is an empty array and all other chunks will also include a usage field, but with a null value. | false
-| spring.ai.openai.chat.options.proxy-tool-calls | If true, the Spring AI will not handle the function calls internally, but will proxy them to the client. Then is the client's responsibility to handle the function calls, dispatch them to the appropriate function, and return the results. If false (the default), the Spring AI will handle the function calls internally. Applicable only for chat models with function calling support | false
+| spring.ai.deepseek.chat.enabled | Enable DeepSeek chat model. | true
+| spring.ai.deepseek.chat.base-url | Optional overrides the spring.ai.deepseek.base-url to provide chat specific url | https://api.deepseek.com/
+| spring.ai.deepseek.chat.api-key | Optional overrides the spring.ai.deepseek.api-key to provide chat specific api-key | -
+| spring.ai.deepseek.chat.completions-path | the path to the chat completions endpoint | /chat/completions
+| spring.ai.deepseek.chat.beta-prefix-path | the prefix path to the beta feature endpoint | /beta/chat/completions
+| spring.ai.deepseek.chat.options.model | ID of the model to use. You can use either use deepseek-coder or deepseek-chat. | deepseek-chat
+| spring.ai.deepseek.chat.options.frequencyPenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. | 0.0f
+| spring.ai.deepseek.chat.options.maxTokens | The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length. | -
+| spring.ai.deepseek.chat.options.presencePenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. | 0.0f
+| spring.ai.deepseek.chat.options.stop | Up to 4 sequences where the API will stop generating further tokens. | -
+| spring.ai.deepseek.chat.options.temperature | What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both. | 1.0F
+| spring.ai.deepseek.chat.options.topP | An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both. | 1.0F
+| spring.ai.deepseek.chat.options.logprobs | Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message. | -
+| spring.ai.deepseek.chat.options.topLogprobs | An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used. | -
|====
-TIP: All properties prefixed with `spring.ai.openai.chat.options` can be overridden at runtime by adding a request specific <> to the `Prompt` call.
+NOTE: You can override the common `spring.ai.deepseek.base-url` and `spring.ai.deepseek.api-key` for the `ChatModel` implementations.
+The `spring.ai.deepseek.chat.base-url` and `spring.ai.deepseek.chat.api-key` properties if set take precedence over the common properties.
+This is useful if you want to use different DeepSeek accounts for different models and different model endpoints.
+
+TIP: All properties prefixed with `spring.ai.deepseek.chat.options` can be overridden at runtime by adding a request specific <> to the `Prompt` call.
== Runtime Options [[chat-options]]
-The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions.java] provides model configurations, such as the model to use, the temperature, the frequency penalty, etc.
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/DeepSeekChatOptions.java[DeepSeekChatOptions.java] provides model configurations, such as the model to use, the temperature, the frequency penalty, etc.
-On start-up, the default options can be configured with the `OpenAiChatModel(api, options)` constructor or the `spring.ai.openai.chat.options.*` properties.
+On start-up, the default options can be configured with the `DeepSeekChatModel(api, options)` constructor or the `spring.ai.deepseek.chat.options.*` properties.
At run-time you can override the default options by adding new, request specific, options to the `Prompt` call.
For example to override the default model and temperature for a specific request:
@@ -162,44 +121,32 @@ For example to override the default model and temperature for a specific request
----
ChatResponse response = chatModel.call(
new Prompt(
- "Generate the names of 5 famous pirates.",
- OpenAiChatOptions.builder()
- .model("deepseek-chat")
- .temperature(0.4)
+ "Generate the names of 5 famous pirates. Please provide the JSON response without any code block markers such as ```json```.",
+ DeepSeekChatOptions.builder()
+ .withModel(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .withTemperature(0.8f)
.build()
));
----
-TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions#builder()].
-
-== Function Calling
+TIP: In addition to the model specific link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/DeepSeekChatOptions.java[DeepSeekChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()].
-NOTE: The current version of the deepseek-chat model's Function Calling capability is unstable, which may result in looped calls or empty responses.
+== Sample Controller (Auto-configuration)
-== Multimodal
+https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-deepseek-spring-boot-starter` to your pom (or gradle) dependencies.
-NOTE: Currently, the DeepSeek API doesn't support media content.
-
-== Sample Controller
-
-https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-starter-model-openai` to your pom (or gradle) dependencies.
-
-Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the OpenAi chat model:
+Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the DeepSeek Chat model:
[source,application.properties]
----
-spring.ai.openai.api-key=
-spring.ai.openai.base-url=https://api.deepseek.com
-spring.ai.openai.chat.options.model=deepseek-chat
-spring.ai.openai.chat.options.temperature=0.7
-
-# The DeepSeek API doesn't support embeddings, so we need to disable it.
-spring.ai.openai.embedding.enabled=false
+spring.ai.deepseek.api-key=YOUR_API_KEY
+spring.ai.deepseek.chat.options.model=deepseek-chat
+spring.ai.deepseek.chat.options.temperature=0.8
----
-TIP: replace the `api-key` with your DeepSeek Api key.
+TIP: replace the `api-key` with your DeepSeek credentials.
-This will create a `OpenAiChatModel` implementation that you can inject into your class.
+This will create a `DeepSeekChatModel` implementation that you can inject into your class.
Here is an example of a simple `@Controller` class that uses the chat model for text generations.
[source,java]
@@ -207,28 +154,180 @@ Here is an example of a simple `@Controller` class that uses the chat model for
@RestController
public class ChatController {
- private final OpenAiChatModel chatModel;
+ private final DeepSeekChatModel chatModel;
@Autowired
- public ChatController(OpenAiChatModel chatModel) {
+ public ChatController(DeepSeekChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
- return Map.of("generation", this.chatModel.call(message));
+ return Map.of("generation", chatModel.call(message));
}
@GetMapping("/ai/generateStream")
- public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
- Prompt prompt = new Prompt(new UserMessage(message));
- return this.chatModel.stream(prompt);
+ public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
+ var prompt = new Prompt(new UserMessage(message));
+ return chatModel.stream(prompt);
}
}
----
-== References
+== Chat Prefix Completion
+The chat prefix completion follows the Chat Completion API, where users provide an assistant's prefix message for the model to complete the rest of the message.
+
+When using prefix completion, the user must ensure that the last message in the messages list is a DeepSeekAssistantMessage.
+
+Below is a complete Python code example for chat prefix completion. In this example, we set the prefix message of the assistant to "```python\n" to force the model to output Python code, and set the stop parameter to ['```'] to prevent additional explanations from the model.
+
+[source,java]
+----
+@RestController
+public class CodeGenerateController {
+
+ private final DeepSeekChatModel chatModel;
+
+ @Autowired
+ public ChatController(DeepSeekChatModel chatModel) {
+ this.chatModel = chatModel;
+ }
+
+ @GetMapping("/ai/generatePythonCode")
+ public String generate(@RequestParam(value = "message", defaultValue = "Please write quick sort code") String message) {
+ UserMessage userMessage = new UserMessage(message);
+ Message assistantMessage = DeepSeekAssistantMessage.prefixAssistantMessage("```python\\n");
+ Prompt prompt = new Prompt(List.of(userMessage, assistantMessage), ChatOptions.builder().stopSequences(List.of("```")).build());
+ ChatResponse response = chatModel.call(prompt);
+ return response.getResult().getOutput().getText();
+ }
+}
+----
+
+== Reasoning Model (deepseek-reasoner)
+The `deepseek-reasoner` is a reasoning model developed by DeepSeek. Before delivering the final answer, the model first generates a Chain of Thought (CoT) to enhance the accuracy of its responses. Our API provides users with access to the CoT content generated by `deepseek-reasoner`, enabling them to view, display, and distill it.
+
+You can use the `DeepSeekAssistantMessage` to get the CoT content generated by `deepseek-reasoner`.
+[source,java]
+----
+public void deepSeekReasonerExample() {
+ DeepSeekChatOptions promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_REASONER.getValue())
+ .build();
+ Prompt prompt = new Prompt("9.11 and 9.8, which is greater?", promptOptions);
+ ChatResponse response = chatModel.call(prompt);
+
+ // Get the CoT content generated by deepseek-reasoner, only available when using deepseek-reasoner model
+ DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) response.getResult().getOutput();
+ String reasoningContent = deepSeekAssistantMessage.getReasoningContent();
+ String text = deepSeekAssistantMessage.getText();
+}
+----
+== Reasoning Model Multi-round Conversation
+In each round of the conversation, the model outputs the CoT (reasoning_content) and the final answer (content). In the next round of the conversation, the CoT from previous rounds is not concatenated into the context, as illustrated in the following diagram:
+
+image::deepseek_r1_multiround_example.png[Multimodal Test Image, align="center"]
+
+Please note that if the reasoning_content field is included in the sequence of input messages, the API will return a 400 error. Therefore, you should remove the reasoning_content field from the API response before making the API request, as demonstrated in the API example.
+[source,java]
+----
+public String deepSeekReasonerMultiRoundExample() {
+ List messages = new ArrayList<>();
+ messages.add(new UserMessage("9.11 and 9.8, which is greater?"));
+ DeepSeekChatOptions promptOptions = DeepSeekChatOptions.builder()
+ .model(DeepSeekApi.ChatModel.DEEPSEEK_REASONER.getValue())
+ .build();
+
+ Prompt prompt = new Prompt(messages, promptOptions);
+ ChatResponse response = chatModel.call(prompt);
+
+ DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) response.getResult().getOutput();
+ String reasoningContent = deepSeekAssistantMessage.getReasoningContent();
+ String text = deepSeekAssistantMessage.getText();
+
+ messages.add(new AssistantMessage(Objects.requireNonNull(text)));
+ messages.add(new UserMessage("How many Rs are there in the word 'strawberry'?"));
+ Prompt prompt2 = new Prompt(messages, promptOptions);
+ ChatResponse response2 = chatModel.call(prompt2);
+
+ DeepSeekAssistantMessage deepSeekAssistantMessage2 = (DeepSeekAssistantMessage) response2.getResult().getOutput();
+ String reasoningContent2 = deepSeekAssistantMessage2.getReasoningContent();
+ return deepSeekAssistantMessage2.getText();
+}
+----
+
+== Manual Configuration
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/DeepSeekChatModel.java[DeepSeekChatModel] implements the `ChatModel` and `StreamingChatModel` and uses the <> to connect to the DeepSeek service.
+
+Add the `spring-ai-deepseek` dependency to your project's Maven `pom.xml` file:
+
+[source, xml]
+----
+
+ org.springframework.ai
+ spring-ai-deepseek
+
+----
+
+or to your Gradle `build.gradle` build file.
+
+[source,groovy]
+----
+dependencies {
+ implementation 'org.springframework.ai:spring-ai-deepseek'
+}
+----
+
+TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
+
+Next, create a `DeepSeekChatModel` and use it for text generations:
+
+[source,java]
+----
+var deepSeekApi = new DeepSeekApi(System.getenv("DEEPSEEK_API_KEY"));
+
+var chatModel = new DeepSeekChatModel(deepSeekApi, DeepSeekChatOptions.builder()
+ .withModel(DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue())
+ .withTemperature(0.4f)
+ .withMaxTokens(200)
+ .build());
+
+ChatResponse response = chatModel.call(
+ new Prompt("Generate the names of 5 famous pirates."));
+
+// Or with streaming responses
+Flux streamResponse = chatModel.stream(
+ new Prompt("Generate the names of 5 famous pirates."));
+----
+
+The `DeepSeekChatOptions` provides the configuration information for the chat requests.
+The `DeepSeekChatOptions.Builder` is fluent options builder.
+
+=== Low-level DeepSeekApi Client [[low-level-api]]
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/DeepSeekApi.java[DeepSeekApi] provides is lightweight Java client for link:https://platform.deepseek.com/api-docs/[DeepSeek API].
+
+Here is a simple snippet how to use the api programmatically:
+
+[source,java]
+----
+DeepSeekApi deepSeekApi =
+ new DeepSeekApi(System.getenv("DEEPSEEK_API_KEY"));
+
+ChatCompletionMessage chatCompletionMessage =
+ new ChatCompletionMessage("Hello world", Role.USER);
+
+// Sync request
+ResponseEntity response = deepSeekApi.chatCompletionEntity(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue(), 0.7f, false));
+
+// Streaming request
+Flux streamResponse = deepSeekApi.chatCompletionStream(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue(), 0.7f, true));
+----
+
+Follow the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/DeepSeekApi.java[DeepSeekApi.java]'s JavaDoc for further information.
-* https://api-docs.deepseek.com/[Documentation Home]
-* https://api-docs.deepseek.com/quick_start/error_codes[Error Codes]
-* https://api-docs.deepseek.com/quick_start/rate_limit[Rate Limits]
+==== DeepSeekApi Samples
+* The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-deepseek/src/test/java/org/springframework/ai/deepseek/api/DeepSeekApiIT.java[DeepSeekApiIT.java] test provides some general examples how to use the lightweight library.
diff --git a/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java b/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java
index 07ef6e4fa49..0e53a9195c2 100644
--- a/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java
+++ b/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java
@@ -54,4 +54,6 @@ private SpringAIModels() {
public static final String ZHIPUAI = "zhipuai";
+ public static final String DEEPSEEK = "deepseek";
+
}
diff --git a/spring-ai-spring-boot-starters/spring-ai-starter-deepseek/pom.xml b/spring-ai-spring-boot-starters/spring-ai-starter-deepseek/pom.xml
new file mode 100644
index 00000000000..4bd4986fbfc
--- /dev/null
+++ b/spring-ai-spring-boot-starters/spring-ai-starter-deepseek/pom.xml
@@ -0,0 +1,58 @@
+
+
+
+
+ 4.0.0
+
+ org.springframework.ai
+ spring-ai
+ 1.0.0-SNAPSHOT
+ ../../pom.xml
+
+ spring-ai-deepseek-spring-boot-starter
+ jar
+ Spring AI Starter - DeepSeek
+ Spring AI DeepSeek Auto Configuration
+ https://github.com/spring-projects/spring-ai
+
+
+ https://github.com/spring-projects/spring-ai
+ git://github.com/spring-projects/spring-ai.git
+ git@github.com:spring-projects/spring-ai.git
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.ai
+ spring-ai-spring-boot-autoconfigure
+ ${project.parent.version}
+
+
+
+ org.springframework.ai
+ spring-ai-deepseek
+ ${project.parent.version}
+
+
+
+
diff --git a/spring-ai-spring-boot-starters/spring-ai-starter-model-deepseek/pom.xml b/spring-ai-spring-boot-starters/spring-ai-starter-model-deepseek/pom.xml
new file mode 100644
index 00000000000..df02c772ee6
--- /dev/null
+++ b/spring-ai-spring-boot-starters/spring-ai-starter-model-deepseek/pom.xml
@@ -0,0 +1,70 @@
+
+
+
+
+ 4.0.0
+
+ org.springframework.ai
+ spring-ai-parent
+ 1.0.0-SNAPSHOT
+ ../../pom.xml
+
+ spring-ai-starter-model-deepseek
+ jar
+ Spring AI Starter - DeepSeek
+ Spring AI DeepSeek Spring Boot Starter
+ https://github.com/spring-projects/spring-ai
+
+
+ https://github.com/spring-projects/spring-ai
+ git://github.com/spring-projects/spring-ai.git
+ git@github.com:spring-projects/spring-ai.git
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.ai
+ spring-ai-autoconfigure-model-deepseek
+ ${project.parent.version}
+
+
+
+ org.springframework.ai
+ spring-ai-deepseek
+ ${project.parent.version}
+
+
+
+ org.springframework.ai
+ spring-ai-autoconfigure-model-chat-client
+ ${project.parent.version}
+
+
+
+ org.springframework.ai
+ spring-ai-autoconfigure-model-chat-memory
+ ${project.parent.version}
+
+
+
+