Spring-Ai-Alibaba [04] 04-llm-platform-custom-demo

Spring-Ai-Alibaba 04 04-llm-platform-custom-demo

概述

本文是 Spring AI Alibaba 框架学习系列第四篇,

介绍 如何调用其他平台的大模型,

当前 Demo 中,同时使用 DeepSeek 平台大模型、官网地址:https://api-docs.deepseek.com/zh-cn/

代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo

开发环境

  • 基础框架: Spring Boot 3.5.14
  • AI 框架: Spring AI 1.1.2 + Spring AI Alibaba 1.1.2.2
  • 大模型: 阿里云通义千问 (qwen-plus)
  • 构建工具: Maven 3.9.11
  • JDK 版本: 21.0.10

项目结构

pom.xml 文件

xml 复制代码
<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>com.junjiu.spring.ai.alibaba.demo</groupId>
        <artifactId>Spring-AI-Alibaba-Demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>04-llm-platform-custom-demo</artifactId>
    <packaging>jar</packaging>
    <description>
        Demo01 ~ Demo03 的LLM 选用的一直是百炼平台,
        当前 Demo04 选用 DeepSeek:https://api-docs.deepseek.com/zh-cn/
    </description>

    <name>04-llm-platform-custom-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-deepseek</artifactId>
        </dependency>
    </dependencies>
</project>

config 配置类

java 复制代码
package com.junjiu.spring.ai.alibaba.demo.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.ai.deepseek.api.DeepSeekApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * program: Spring-AI-Alibaba-Demo
 * ClassName: LLMConfig
 * description:
 *
 * @author: 君九
 * @create: 2026-05-22 22:39
 * @version: 1.0
 **/
@Configuration
public class LLMConfig {

    // 从配置文件读取
    @Value("${spring.ai.dashscope.base-url}")
    private String baseUrl;

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    @Value("${spring.ai.dashscope.chat.options.model}")
    private String model;


    @Bean
    public DeepSeekChatModel deepSeekChatModel() {
        return DeepSeekChatModel.builder()
                .deepSeekApi(DeepSeekApi.builder()
                        .baseUrl(baseUrl)
                        .apiKey(apiKey)
                        .build())
                .defaultOptions(
                        DeepSeekChatOptions.builder()
                                .model(model)
                                .build()
                )
                .build();
    }
    @Bean(name = "deepSeekChatClient")
    public ChatClient deepSeekChatClient() {
        return ChatClient.create(deepSeekChatModel());
    }
}

控制层

java 复制代码
package com.junjiu.spring.ai.alibaba.demo.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * program: Spring-AI-Alibaba-Demo
 * ClassName: DeepSeekController
 * description:
 *
 * @author: 君九
 * @create: 2026-05-22 22:50
 * @version: 1.0
 **/
@RestController
@RequestMapping("/deepseek")
public class DeepSeekController {

    @Resource(name = "deepSeekChatClient")
    ChatClient deepSeekChatClient;

    /**
     * 聊天对话
     * @param message
     * @return
     */
    @RequestMapping("/chat")
    public Flux<String> chat(@RequestParam(name = "message", defaultValue = "你是谁?") String message) {
        return deepSeekChatClient.prompt()
                .user(message)
                .stream()
                .content();
    }
}

启动类

java 复制代码
package com.junjiu.spring.ai.alibaba.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

/**
 * program: Spring-AI-Alibaba-Demo
 * ClassName: LlmPlatformApplication
 * description:
 *
 * @author: 君九
 * @create: 2026-05-22 22:35
 * @version: 1.0
 **/
@SpringBootApplication
public class LlmPlatformApplication {

    public static void main(String[] args) {
        SpringApplication.run(LlmPlatformApplication.class, args);
    }

    public ApplicationListener<ApplicationReadyEvent> readyEventApplicationListener(Environment env) {
        return event -> {
            System.out.println("\n🎉========================================🎉");
            System.out.println("✅ Application is ready!");
            System.out.println("AIALI_API_KEY=" + System.getenv("AIALI_API_KEY"));
            System.out.println("🎉========================================🎉\n");
        };
    }
}

验证

打开浏览器访问:http://localhost:5826/deepseek/chat

代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo

若有转载,请标明出处:https://blog.csdn.net/CharlesYuangc/article/details/161344688

相关推荐
余俊晖2 分钟前
多模态文档解析开源新进展:Unlimited OCR技术方案
人工智能·ocr·多模态
触底反弹6 分钟前
🔥 50 行代码,我手写了一个 MCP Server 给 Claude 用!
javascript·人工智能·程序员
梦帮科技8 分钟前
基于EVM架构的Web3音频确权与RNS Token智能合约设计深度解析
人工智能·python·架构·web3·区块链·音视频·智能合约
要开心吖ZSH10 分钟前
处方物流信息同步优化:从 36 秒到亚秒级的踩坑记录
java·数据库·mysql·性能优化
无糖可可果11 分钟前
LLM 如何预测下一个词?—— 深入 Transformer 内部
人工智能
chaowwwww12 分钟前
从0开始实现个人最简harness项目
人工智能·agent
Wang's Blog12 分钟前
JavaWeb快速入门: MyBatis入门与实战
java·mybatis
项目治理之道12 分钟前
项目管理中的“附件黑洞”:VisualProject如何让附件从“信息孤岛”蜕变为核心资产
人工智能·项目管理工具·项目管理软件·金融科技管理
东风破_15 分钟前
Tool Use 的核心:模型只负责判断,Runtime 才真正执行
人工智能
To_OC15 分钟前
我写了个 10 行的加法函数,终于搞懂了什么是 Harness Engineering
人工智能·llm·claude