注意:
1、本文大模型以deepseek为例,需提前到DeepSeek官网申请到key。
一、下载依赖
如果maven使用的是阿里云镜像地址可能会下载不到下面的依赖:
spring-ai-openai-spring-boot-starter
可以在maven中指定仓库,如下所示
<!--下载spring-ai相关包需要用到的仓库地址-->
<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>
附pom.xml中完整配置
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springai-deepseek-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!--引入lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</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>
<!--下载spring-ai相关包需要用到的仓库地址-->
<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>
</project>
二、配置springai的api地址和key
server.port=9899
spring.application.name=spring-ai-deepseek-demo
spring.ai.openai.api-key=sk-c1efe9e2c2874f7cae1c7******
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
注意:
1.api-key需替换为自己在deepseek上申请到的key;
2.关于temperature参数说明,请参考下文和项目需要进行配置。
temperature值越高,生成的文本越多样化,但也可能包含更多的随机性和不可预测的内容。
值越低,生成的文本越接近于确定性的结果,即生成的文本会更加一致和可预测。
三、创建controller,开发api
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/deepseek")
public class DeepSeekController {
private final OpenAiChatModel chatModel;
@GetMapping("/chat")
public String chat(@RequestParam String message) {
String response = chatModel.call(message);
System.out.println(response);
return response;
}
}
然后启动项目,开始测试,当传入的参数message的值为hello时测试效果如下:

当message传入的值为Java时

126

被折叠的 条评论
为什么被折叠?



