实验:利用spring ai alibaba通过三种方式调用deepseek

1 调用ollama本地部署的 deepseek R1 大模型

坑全被我踩光了,wc;

我用的是1.0.0-M6 版本

一开始是 ChatClient 怎么都注入不到 controller 里;ChatClient 这个 Bean 根本就没有自动配置好装入容器;

后面使用chatModel 注入成功了;发现使用时要么缺 getMessagee 方法要么缺少getContent 方法;还需要手动构建 ChatClient 的 ;

MD; 不搞了,

2 DashScope 方式 ;调用 云端大模型 通义千文

模型都是自己选择的。

引入

</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
</dependency>
<!-- 引入spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring:
  ai:
    dashscope:
      api-key: sk-1af6101b78b747ada71e7d932a86663a       //刚刚复制的api key
      chat:
        options:
          model: qwen-plus         //刚刚复制的code 
package com.AI;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

// 同步调用
    @GetMapping("/chat")
    public String chat(@RequestParam(value = "input") String input) {

        return this.chatClient.prompt()
                .user(input)
                .call()
                .content();
    }
// 流式调用
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(String input) {
        return this.chatClient.prompt()
                .user(input)
                .stream()
                .content();
    }
}

3 open AI

这个就不折腾了感兴趣的自己去捣鼓

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

例如硅基流动的配置如下

spring:
  ai:
    openai:
      api-key: ${AI_SILICONFLOW_API_KEY}
      base-url: https://api.siliconflow.cn
      chat:
        options:
          model: Qwen/Qwen2.5-72B-Instruct
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐