spring-ai-alibaba 是Spring AI生态里与阿里巴巴相关的组件,借助它能够实现接入各类大模型。以下为你详细介绍如何使用 spring-ai-alibaba 接入不同大模型:

接入open ai

项目环境准备

        首先要创建一个Spring Boot项目,并且在 pom.xml 里添加必要的依赖。接入openapi为例(我这里使用硅基流动和openapi一样)官网:SiliconFlow

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sqz</groupId>
    <artifactId>spring-ai-deepseek</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-ai-deepseek</name>
    <description>spring-ai-deepseek</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--接入openapi-->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>

       <!--接入ollama-->
        <!--
          <dependency>
                  <groupId>org.springframework.ai</groupId>
                  <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
                  <version>1.0.0-M6</version>
          </dependency>
          -->
            <!--接入 通义千问-->
            <!--   <dependency>
                    <groupId>com.alibaba.cloud.ai</groupId>
                  <artifactId>spring-ai-alibaba-starter</artifactId>
                   <version>1.0.0-M6.1</version>
               </dependency>
             -->


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>aliyunmaven</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

配置认证信息

在 application.properties 或者 application.yml 中配置硅基流动认证信息,例如:

spring:
  application:
    name: alibaba-ai-demo
  ai:
    openai:
      api-key: your-api-key  #apiKey到硅基流动官网获取免费的
      base-url: https://api.siliconflow.cn
      chat:
        options:
          model: Qwen/QwQ-32B  #使用通义千问模型

这里的 your-api-key 要替换成自己获取的API Key。

 编写代码接入大模型

下面是一个简单的Java代码示例,展示了如何使用 spring-ai-alibaba 接入openapi模型:

package com.sqz.springaideepseek.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
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
                .defaultSystem("你是一个万能助手,你可以回答任何问题。")
                .build();
    }

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "input") String input) {

        return this.chatClient.prompt()
                .user(input)
                .call()
                .content();
    }

    /**
     * ChatClient 流式响应
     */
    @GetMapping(value = "/stream/response", produces =  "text/event-stream;charset=UTF-8")
    public Flux<ServerSentEvent<String>> simpleChat(@RequestParam String message) {
        return chatClient.prompt()
                .user(message)
                .stream()
                .content()
                .map(content -> ServerSentEvent.<String>builder()
                        .data(content)
                        .build());
    }
}

测试:http://localhost:8080/chat?input=%E4%BD%A0%E6%98%AF%E8%B0%81

接入ollama 

将配置文件改为ollama配置:

spring:
  application:
    name: alibaba-ai-demo
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        model: deepseek-r1:1.5b

pom文件注释掉openapi,依赖 放开ollama注释

        <!--接入openapi-->
        <!--
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>
        -->
       <!--接入ollama-->

      <dependency>
              <groupId>org.springframework.ai</groupId>
              <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
              <version>1.0.0-M6</version>
      </dependency>

接入阿里云大模型

pom文件注释掉openapi和ollama,打开alibaba

<dependency>
    <groupId>com.alibaba.cloud.ai</groupId>
    <artifactId>spring-ai-alibaba-starter</artifactId>
    <version>1.0.0-M6.1</version>
</dependency>

修改配置文件

spring:
  application:
    name: alibaba-ai-demo
  ai:
    dashscope:
      api-key: your-api-key

这里的 your-api-key 要替换成你自己在阿里云获取的API Key。就可以测试了

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐