Spring AI Alibaba 学习day01

AI应用开发框架,spring ai alibaba 与阿里云百炼和通义千问有深度集成

Spring AI Alibaba 基于 Spring AI 构建,因此 SAA 继承了 SpringAI 的所有原子能力抽象并在此基础上扩充丰富了模型、向量存储、记忆、RAG 等核心组件适配,让其能够接入阿里云的 AI 生态

使用步骤:

1.获取api key

2.获取模型名称

3.获取baseUrl

4.父工程pom

复制代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.leot</groupId>
        <artifactId>SSA-leot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.leot</groupId>
    <artifactId>SSA-OLLAMA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SSA-OLLAMA</name>
    <description>SSA-OLLAMA</description>

    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-starter-model-ollama</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Ollama 使用

1.依赖引入

复制代码
<dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-ollama</artifactId>
            <version>1.0.0</version>
        </dependency>

2.yaml

复制代码
spring:
  application:
    name: SSA-OLLAMA
  ai:
    dashscope:
        api-key: youapikey
    ollama:
      base-url: http://localhost:11434
      chat:
        model: deepseek-r1:7b
server:
  port: 8080
  servlet:
    encoding:
      charset: UTF-8
      force: true
      enabled: true

3.使用

复制代码
   @Resource
    @Qualifier(value = "ollamaChatModel")
    private ChatModel chatModel;

    @GetMapping("/hello/chatmodel")
    public String hello(@RequestParam(name = "msg",defaultValue = "你是谁") String msg) {
        return chatModel.call(msg) ;
    }
    @GetMapping("/hello/streamodel")
    public Flux<String> helloa(@RequestParam(name = "msg",defaultValue = "你是谁") String msg) {
        return chatModel.stream(msg) ;
    }

chatModel 和chatClient

ChatClient是高级封装,基于ChatModel构建,适合快速构建标准化复杂AI服务,支持同步和流式交互,集成多种高级功能。

ChatClient 不支持依赖注入 ,而chatmodel是支持依赖注入的

复制代码
@RestController
public class ChatClientDemo {
    private final ChatClient dashScopechatClient;

    public ChatClientDemo(@Qualifier("dashscopeChatModel") ChatModel dashScopechatModel) {
        this.dashScopechatClient = ChatClient
                .builder(dashScopechatModel)
                .build();
    }

    @GetMapping("/hello/chatclient")
    public ChatResponse hello(@RequestParam(name = "msg",defaultValue = "你是谁") String msg) {
        return dashScopechatClient.prompt()
                .user(msg)
                .call()
                .chatResponse();
    }
}

SSE流式输出

服务器发送事件

持续推送数据片段到前端

SSE 的核心思想是:客户端发起一个请求,服务器保持这个连接打开并在有新数据时,通过这个连接将数据发送给客户端。这与传统的请求 - 响应模式(客户端请求一次,服务器响应一次,连接关闭)有本质区别。

|--------|------------------------------|---------------------------|
| 特性 | Server-Sent Events (SSE) | WebSocket |
| 通信方向 | 单向(服务器 -> 客户端) | 双向 |
| 协议 | 基于 HTTP | 独立的 ws://wss:// 协议 |
| 数据类型 | 文本 | 文本和二进制 |
| 复杂性 | 简单 | 相对复杂 |
| 连接开销 | 较低 | 较高 |
| 自动重连 | 是 | 需要手动实现 |
| 浏览器支持 | 广泛支持(除 IE) | 广泛支持 |

多模型共存

复制代码
@Configuration
public class SseConfig {

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



    private String DEEPSEEK_MODEL="deepseek-v3.2";
    private String QWEN_MODEL="qwen3-max";

    @Bean(name = "deepseek")
    public ChatModel DeepSeekChatModel( ){
        return DashScopeChatModel.builder()
                .dashScopeApi(DashScopeApi.builder().apiKey(apiKey).build())
                .defaultOptions(DashScopeChatOptions.builder().withModel(DEEPSEEK_MODEL).build())
                .build();
    }
    @Bean(name = "qwen")
    public ChatModel QwenChatModel( ){
        return DashScopeChatModel.builder()
                .dashScopeApi(DashScopeApi.builder().apiKey(apiKey).build())
                .defaultOptions(DashScopeChatOptions.builder().withModel(QWEN_MODEL).build())
                .build();
    }
     @Bean(name = "deepSeekChatClient")
    public ChatClient chatClientdeepseek(@Qualifier("deepseek") ChatModel deepseekChatModel){
        return ChatClient.builder(deepseekChatModel) .build();
    }
    @Bean(name = "qwenChatClient")
    public ChatClient chatClientqwen(@Qualifier("qwen") ChatModel qwenChatModel){
        return ChatClient.builder(qwenChatModel) .build();
    }
相关推荐
重学一遍1 小时前
Spring Security + JWT + Redis 的认证授权系统
java·redis·spring
ghie90901 小时前
基于粒子滤波的多目标检测前跟踪(TBD)MATLAB实现
人工智能·目标检测·matlab
分布式存储与RustFS2 小时前
RustFS在AI场景下的实测:从GPU到存储的完整加速方案
开发语言·人工智能·rust·对象存储·企业存储·rustfs·minio国产化替代
Deepoch2 小时前
Deepoc具身模型开发板:半导体制造智能化的技术引擎
人工智能·开发板·半导体·具身模型·deepoc
凤希AI伴侣2 小时前
凤希AI提出FXPA2P:下一代点对点AI服务架构-2026年1月14日
人工智能·架构·凤希ai伴侣
科技与数码2 小时前
中小企业AI知识权威构建:北京鲲鹏伟业的GEO赋能之道——GEO公司助力企业数字化转型
人工智能
阿湯哥2 小时前
Workflow or Agent+Skill:AI 工作流的进化抉择
人工智能
阿坤带你走近大数据2 小时前
如何解决农业数据的碎片化问题
大数据·人工智能·rag·大模型应用
Modeler·X2 小时前
关系型与非关系型数据库终极对决
数据库·人工智能