Part05 中州养老项目实训 ☀️

YangeIT大约 6 分钟汉口学院MysqlRedisSwaggerHTTPGETPOST

Part05 中州养老项目实训 ☀️

1 拓展-SpringAI+DeepSeek 额外拓展

前言

开发一个和Deepseek聊天的页面,使用SpringAI+Deepseek image

代码操作

SpringAI定义
SpringAI定义

官网:https://spring.io/projects/spring-aiopen in new window

1. 创建一个Maven项目,并且导入以下依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.yangeit</groupId>
    <artifactId>ai-deepseek</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>
        <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>
        <!-- lombok的依赖,能免除get和set方法 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
    </dependencies>
</project>

2. 创建配置文件application.yml,并添加以下内容(含有API Key)并创建启动类

#服务端口

server:
  port: 8089
# 服务名
spring:
  ai:
    openai:
      base-url: https://api.deepseek.com
      # DeepSeek的OpenAI式端点
      api-key: sk-7用自己的
      chat.options:
        model: deepseek-chat  # 指定DeepSeek的模型名称 或者chat reasoner


@SpringBootApplication
public class DeepSeekApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeepSeekApplication.class,args);
        System.out.println("启动成功");
    }
}

3. 创建一个AIConfig配置配类,给AI设定角色

@Configuration
public class AiConfig {

    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder
                .defaultSystem("你现在不是 deepseek 了," +
                        "你是一名学识渊博的诗人,擅长唐诗宋词")
                .build();

    }

}

4. 创建一个Controller类,并添加一个GET请求处理方法,用于接收用户输入并调用DeepSeek API

@RestController
@RequestMapping("/ai")
@Slf4j
public class DeepSeekController {

	@Autowired
	ChatClient chatClient;

	@GetMapping("/chat")
	public String generate(@RequestParam(value = "message") String message) {
		log.info("Generating response");
		  // 调用 ChatClient 的 prompt 方法生成响应
		  // 1. prompt(message): 创建一个包含用户输入消息的 Prompt 对象
		  // 2. call(): 调用 ChatClient 与 AI 模型交互以获取响应
		  // 3. content(): 提取响应的内容部分
		return chatClient.prompt().user(message).call().content();
	}

	/**
	 * @description: 流式响应
	 **/
	@GetMapping(value = "/chat2",
			produces = "application/json;charset=utf-8")
	public Flux<String> generation02(@RequestParam String message){
		Flux<String> output = chatClient.prompt()
				.user(message)
				.stream()
				.content();
		return output;
	}
}

总结

课堂作业

  1. SpringAI至少需要JDK版本是多少? 🎤
  2. SpringAI是大模型吗? 他的作用是什么?