Spring AI接入DeepSeek R1实践

586 阅读1分钟

最近DeepSeek也是非常的火爆,也有很多人高举爱国主义旗帜。恰巧,前两年Spring也有了一个新的模块Spring AI,所以就试了试。JDK17上使用。

官网文档

Spring AI文档:docs.spring.io/spring-ai/r…

DeepSeek文档说明:docs.spring.io/spring-ai/r…

直接代码

引用包

spring-ai-openai-spring-boot-starter 引包

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

根据官网提示增加pom配置

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

以及增加包下载的仓库配置

<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</repository>
</repositories>

可能这里里面的仓库不生效,我的解决办法:

我使用的是阿里的仓库配置,所以配置增加了!spring-milestones,!spring-snapshots

当然你也可以,使用其他的仓库配置方式。

pom的完整配置

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
  <!--	spring-ai-openai-spring-boot-starter	-->
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
		</dependency>

	</dependencies>
	<dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>3.4.2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.ai</groupId>
                    <artifactId>spring-ai-bom</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
	</dependencyManagement>
	<repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                        <enabled>false</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <releases>
                        <enabled>false</enabled>
                </releases>
            </repository>
	</repositories>

	<build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                            <source>9</source>
                            <target>9</target>
                    </configuration>
                </plugin>
            </plugins>
	</build>

配置文件

application.yml

server:
  port: 8080

spring:
  ai:
    openai:
      api-key: "sk-xxxxxxxxxxxxxxx"
      base-url: "https://api.deepseek.com"
      chat:
        options:
          model: "deepseek-reasoner"
          temperature: 0.7
      # The DeepSeek API doesn't support embeddings, so we need to disable it.
      embedding.enabled: false

配置的意义参考官网:docs.spring.io/spring-ai/r…

api-key的申请:platform.deepseek.com/api_keys

代码调用

官网示例

@RestController
public class ChatController {

    private final OpenAiChatModel chatModel;

    @Autowired
    public ChatController(OpenAiChatModel 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));
    }

    @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);
    }
}

效果

难道这就是各大厂商宣布的接入了DeepSeek R1的方式??