feat: add deepseek starter and docs - fix pom module ordering
- Add remaining deepseek modules - Fix build order to put autoconfig modules after model modules to fix javadoc build Signed-off-by: GR <gr@fastball.dev>
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
@@ -11,7 +11,7 @@
|
||||
**** xref:api/chat/functions/anthropic-chat-functions.adoc[Anthropic Function Calling (Deprecated)]
|
||||
*** xref:api/chat/azure-openai-chat.adoc[Azure OpenAI]
|
||||
**** xref:api/chat/functions/azure-open-ai-chat-functions.adoc[Azure OpenAI Function Calling]
|
||||
*** xref:api/chat/deepseek-chat.adoc[DeepSeek AI]
|
||||
*** xref:api/chat/deepseek-chat.adoc[DeepSeek]
|
||||
*** xref:api/chat/dmr-chat.adoc[Docker Model Runner]
|
||||
*** xref:api/chat/google-vertexai.adoc[Google VertexAI]
|
||||
**** xref:api/chat/vertexai-gemini-chat.adoc[VertexAI Gemini]
|
||||
|
||||
@@ -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=<model name>` 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 key with DeepSeek to access DeepSeek language models.
|
||||
Create an account at https://platform.deepseek.com/sign_up[DeepSeek registration page] and generate a 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 the https://platform.deepseek.com/api_keys[API Keys page].
|
||||
Exporting an environment variable is one way to set this configuration property:
|
||||
|
||||
[source,shell]
|
||||
----
|
||||
export SPRING_AI_OPENAI_API_KEY=<INSERT DEEPSEEK API KEY HERE>
|
||||
export SPRING_AI_OPENAI_BASE_URL=https://api.deepseek.com
|
||||
export SPRING_AI_OPENAI_CHAT_MODEL=deepseek-chat
|
||||
export SPRING_AI_DEEPSEEK_API_KEY=<INSERT KEY HERE>
|
||||
----
|
||||
|
||||
=== Add Repositories and BOM
|
||||
|
||||
Spring AI artifacts are published in Maven Central and Spring Snapshot repositories.
|
||||
Spring AI artifacts are published in the 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.
|
||||
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 your 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.
|
||||
====
|
||||
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:
|
||||
|
||||
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:
|
||||
|
||||
[tabs]
|
||||
======
|
||||
Maven::
|
||||
+
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-starter-model-openai</artifactId>
|
||||
<artifactId>spring-ai-deepseek-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
Gradle::
|
||||
+
|
||||
or to your Gradle `build.gradle` 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
|
||||
|
||||
@@ -88,147 +61,273 @@ The prefix `spring.ai.retry` is used as the property prefix that lets you config
|
||||
| spring.ai.retry.backoff.initial-interval | Initial sleep duration for the exponential backoff policy. | 2 sec.
|
||||
| spring.ai.retry.backoff.multiplier | Backoff interval multiplier. | 5
|
||||
| spring.ai.retry.backoff.max-interval | Maximum backoff duration. | 3 min.
|
||||
| spring.ai.retry.on-client-errors | If false, throw a NonTransientAiException, and do not attempt retry for `4xx` client error codes | false
|
||||
| spring.ai.retry.on-client-errors | If false, throws a NonTransientAiException, and does not attempt a retry for `4xx` client error codes | false
|
||||
| spring.ai.retry.exclude-on-http-codes | List of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException). | empty
|
||||
| spring.ai.retry.on-http-codes | List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException). | empty
|
||||
|====
|
||||
|
||||
==== 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 | Enables the DeepSeek chat model. | true
|
||||
| spring.ai.deepseek.chat.base-url | Optionally overrides the spring.ai.deepseek.base-url to provide a chat-specific URL | https://api.deepseek.com/
|
||||
| spring.ai.deepseek.chat.api-key | Optionally overrides the spring.ai.deepseek.api-key to provide a 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 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 | Which 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 the 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 <<chat-options>> 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 <<chat-options>> 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 startup, 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:
|
||||
At runtime, 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:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
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()].
|
||||
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()].
|
||||
|
||||
== Function Calling
|
||||
== Sample Controller (Auto-configuration)
|
||||
|
||||
NOTE: The current version of the deepseek-chat model's Function Calling capability is unstable, which may result in looped calls or empty responses.
|
||||
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.
|
||||
|
||||
== Multimodal
|
||||
|
||||
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 an `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=<DEEPSEEK_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.
|
||||
Here is an example of a simple `@Controller` class that uses the chat model for text generations.
|
||||
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 generation.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@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<ChatResponse> 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<ChatResponse> 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.
|
||||
|
||||
* 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]
|
||||
When using prefix completion, the user must ensure that the last message in the messages list is a DeepSeekAssistantMessage.
|
||||
|
||||
Below is a complete Java 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<Message> 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 <<low-level-api>> to connect to the DeepSeek service.
|
||||
|
||||
Add the `spring-ai-deepseek` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-deepseek</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
or to your Gradle `build.gradle` 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 generation:
|
||||
|
||||
[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<ChatResponse> 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 a 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] is a lightweight Java client for link:https://platform.deepseek.com/api-docs/[DeepSeek API].
|
||||
|
||||
Here is a simple snippet showing 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<ChatCompletion> response = deepSeekApi.chatCompletionEntity(
|
||||
new ChatCompletionRequest(List.of(chatCompletionMessage), DeepSeekApi.ChatModel.DEEPSEEK_CHAT.getValue(), 0.7f, false));
|
||||
|
||||
// Streaming request
|
||||
Flux<ChatCompletionChunk> 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.
|
||||
|
||||
==== 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 of how to use the lightweight library.
|
||||
|
||||
Reference in New Issue
Block a user