一、Spring AI简介:Spring AI致力于简化AI项目的开发与部署流程,使Java开发者能够更高效地将AI技术集成到业务系统中。它提供了开箱即用的工具和接口,方便集成和管理各种AI模型。


目前,Spring AI 支持的 AI 平台包括:OpenAI (ChatGPT)、Azure OpenAI、Hugging Face、Google、DeepSeek等。


在最新的Spring AI 版本中,DeepSeek 大模型被正式加入进来,我可以用 Spring AI 直接访问 DeepSeek 的大模型服务。

二、完整代码如下

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.3.8</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>spring.ai.deepseek.example</groupId>
	<artifactId>spring-ai-deepseek-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-ai-deepseek-demo</name>
	<description>Spring AI , getting started example, using Open AI</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
		<spring-ai.version>1.0.0-M5</spring-ai.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.ai</groupId>
				<artifactId>spring-ai-bom</artifactId>
				<version>${spring-ai.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.properties配置文件:

server.port=8899
spring.application.name=spring-ai-deepseek-demo
 
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

启动类:

@SpringBootApplication
public class SpringAiDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringAiDemoApplication.class, args);
	}
	
	/*@Bean
	public CommandLineRunner runner(ChatClient.Builder builder) {
	    return args -> {
	        ChatClient chatClient = builder.build();
	        String response = chatClient.prompt("Tell me a joke").call().content();							
	        System.out.println(response);
	    };
	}*/	
	
}

Controller类:

@RestController
public class ChatDeepSeekController {

    private final OpenAiChatModel chatModel;

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

访问地址:http://127.0.0.1:8899/ai/generate?message=Tell me a joke

三、参考文档:
https://spring.io/projects/spring-ai
https://docs.spring.io/spring-ai/reference/api/chat/deepseek-chat.html

完整源码项目:完整源码地址

注意事项:

SpingBoot 3.3 需要java17 版本

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐