AI大模型实用(一)SpringAI接入deepseek示例

一、SpringAI接入deepseek所需环境

  • JDK17+(JDK8无法支持)
  • SpringBoot 3.x
  • maven 3.6+

官网地址

https://docs.spring.io/spring-ai/reference/api/chatclient.html

SpringAI接入deepseek方式有很多。 下面演示SpringAI原生方式接入deepseek.

注:Spring AI调用Ollama+DeepSeek

二、pom依赖

1、 SpringBoot修改版本

复制代码
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

2、SpringAI通过deepseek提供的openapi接入deepseek

复制代码
<!--       springAI提供的openapi-->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>

三、相关配置

复制代码
spring.ai.openai.api-key=xxx(修改成自己的密匙)
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat

四、代码

java 复制代码
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SpringAIConfig {
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder) {
        return builder.defaultSystem("你是一名资深开发工程师,你的名称叫siri").build();
    }
}

示例1:定义controller

java 复制代码
package com.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/ChatController")
public class ChatController {
    private final ChatClient chatClient;

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

    @GetMapping("/ai")
    String generation(String question) {
        return this.chatClient.prompt()
                .user(question)
                .call()
                .content();
    }


}

访问: http://127.0.0.1:8886/ChatController/ai?question=%E4%BB%8B%E7%BB%8D%E4%B8%80%E4%B8%8B%E4%BD%A0%E8%87%AA%E5%B7%B1

运行结果:

访问:

运行结果:http://127.0.0.1:8886/ChatController/chattest?question=%E4%BD%A0%E6%98%AF%E8%B0%81

示例2:定义controller(流式输出)

java 复制代码
package com.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/SpringAIController")
public class SpringAIController {
    @Autowired
    private ChatClient chatClient;

//    private final ChatClient chatClient;
    //ChatClient底层是使用ChatModel作为属性的,在初始化ChatClient的时候可以指定ChatModel
    @Autowired
    private ChatModel chatModel;

    public SpringAIController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.defaultSystem("你是一个AI智能应用").build();

    }

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "msg",defaultValue = "介绍一下杜甫")
                        String message) {
        //prompt:提示词
        return this.chatClient.prompt()
                //用户输入的信息
                .user(message)
                //请求大模型
                .call()
                //返回文本
                .content();
    }
   //流式响应
    @GetMapping(value = "/chatstream",produces="text/html;charset=UTF-8")
    public Flux<String> chatStream(@RequestParam(value = "message") String message) {
        return chatClient.prompt().user(message).stream().content();
    }
}

访问:

http://127.0.0.1:8886/SpringAIController/chatstream?message=%E4%BB%8B%E7%BB%8D%E4%B8%80%E4%B8%8B%E4%BD%A0%E8%87%AA%E5%B7%B1

注意: 不使用流式输出时,访问http://127.0.0.1:8886/SpringAIController/chat?question=%E4%BD%A0%E6%98%AF%E8%B0%81

结果出现超时: TimeoutException: Total timeout 10000 ms elapsed

相关推荐
AC赳赳老秦9 小时前
代码生成超越 GPT-4:DeepSeek-V4 编程任务实战与 2026 开发者效率提升指南
数据库·数据仓库·人工智能·科技·rabbitmq·memcache·deepseek
小白狮ww12 小时前
要给 OCR 装个脑子吗?DeepSeek-OCR 2 让文档不再只是扫描
人工智能·深度学习·机器学习·ocr·cpu·gpu·deepseek
realhuizhu21 小时前
为什么程序员配出的颜色像"斑斓的灰"?因为你还在靠直觉
前端开发·ai工具·ui设计·deepseek·程序员提升
逐梦苍穹1 天前
速通DeepSeek论文mHC:给大模型装上物理阀门的架构革命
人工智能·deepseek·mhc
落霞的思绪1 天前
Spring AI Alibaba 集成 Redis 向量数据库实现 RAG 与记忆功能
java·spring·rag·springai
realhuizhu3 天前
你有多少次对着设计稿说"感觉不对,但说不上来"?
提示词工程·设计效率·deepseek·ai设计工具·品牌logo
AC赳赳老秦4 天前
DeepSeek一体机部署:中小企业本地化算力成本控制方案
服务器·数据库·人工智能·zookeeper·时序数据库·terraform·deepseek
Elwin Wong4 天前
浅析DeepSeek-OCR v1&v2
人工智能·大模型·llm·ocr·deepseek
AI刀刀4 天前
千问 文心 元宝 Kimi公式乱码
ai·pdf·豆包·deepseek·ds随心转
aihuangwu4 天前
deepseek图表怎么导出
人工智能·ai·deepseek·ds随心转