跳转至

SpringAI

约 109 个字 45 行代码 1 张图片 预计阅读时间 1 分钟

创建SpringBoot项目并引入SpringAI需要的依赖

本次使用的SpringBoot的版本如下:

XML
1
2
3
4
5
6
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

本次使用Deepseek的API,DeepSeek的API做了针对OpenAI的兼容,所以可以直接使用OpenAI的依赖,下面的pom配置是亲测可用的配置(不考虑流式输出):

XML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<dependencies>
  <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-starter-model-openai</artifactId>
      <version>1.0.2</version> <!-- 版本根据Maven仓库任意指定即可 -->
  </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>

根据官方文档的介绍进行如下的配置:

YAML
1
2
3
4
5
6
7
8
9
spring:
  ai:
    openai:
      api-key: xxxx
      base-url: https://api.deepseek.com
      chat:
        options:
          model: deepseek-chat
          temperature: 0.7

测试

使用下面的DeepSeekController可以得到测试结果:

Java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@RequestMapping("/ds")
@RestController
public class DeepSeekController {
  @Autowired 
  private OpenAiChatModel openAiChatModel; // 使用ChatModel而不是ChatClient

  @RequestMapping("/chat")
  public String chat(String message) {
    return openAiChatModel.call(message);
  }
}