Spring AI Alibaba 项目接入阿里云百炼平台大模型

1 依赖

jdk 21 + springboot 3.4.5 + spring-ai-alibaba-starter-dashscope 1.0.0.2

<properties>
	<java.version>21</java.version>
	<spring-ai.version>1.0.0</spring-ai.version>
	<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
	<spring-boot.version>3.4.5</spring-boot.version>
</properties>


<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.alibaba.cloud.ai</groupId>
			<artifactId>spring-ai-alibaba-bom</artifactId>
			<version>${spring-ai-alibaba.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>${spring-boot.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-bom</artifactId>
			<version>${spring-ai.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>

	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>com.alibaba.cloud.ai</groupId>
		<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
	</dependency>

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

<build>
	<plugins>
		<!-- Maven 编译器插件 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.11.0</version>
			<configuration>
				<source>21</source>
				<target>21</target>
				<parameters>true</parameters> <!-- 支持参数名保留 -->
				<compilerArgs>
					<arg>-Xlint:unchecked</arg>
					<arg>-Xlint:deprecation</arg>
				</compilerArgs>
			</configuration>
		</plugin>

		<!-- Spring Boot Maven 插件 -->
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>${spring-boot.version}</version>
		</plugin>

		<!-- 资源处理插件 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>3.3.1</version>
			<configuration>
				<encoding>UTF-8</encoding>
			</configuration>
		</plugin>
	</plugins>
</build>

2 配置文件

接入阿里云百炼平台大模型的配置如下所示。

# 阿里云百炼平台模型秘钥
spring.ai.dashscope.api-key=${ALI_AI_DASHSCOPE_API_KEY}
# 模型名称(如 qwen-plus)
spring.ai.dashscope.chat.options.model=${ALI_AI_DASHSCOPE_CHAT_MODEL}
# 使用SDK调用时需配置的base_url
spring.ai.dashscope.base-url=https://dashscope.aliyuncs.com/compatible-mode/v1

3 demo

spring ai alibaba框架会自动读取配置文件构建阿里云百炼平台模型的 ChatClient,只需注入使用即可。以下为使用 ChatClient进行简单调用的案例。

@Slf4j
@RestController
@RequestMapping("/test")
public class DashScopeChatClientController {

    private static final String DEFAULT_PROMPT = "你好,介绍下你自己!";

    /**
     * 阿里云百炼平台模型的 ChatClient
     */
    private final ChatClient dashScopeChatClient;

    // 使用如下的方式注入阿里云百炼平台模型的 ChatClient
    public DashScopeChatClientController(ChatClient.Builder chatClientBuilder) {
        this.dashScopeChatClient = chatClientBuilder.build();
    }

    /**
     * ChatClient 简单调用
     */
    @GetMapping("/simple/chat")
    public String simpleChat(String prompt) {
        if (StringUtils.isBlank(prompt)) {
            prompt = DEFAULT_PROMPT;
        }
        String content;
        try {
            content = dashScopeChatClient.prompt(prompt).call().content();
        } catch (Exception e) {
            log.error("simpleChat error", e);
            throw new RuntimeException(e.getMessage());
        }
        log.info("simpleChat --> \n prompt ={}, \n content = {}", prompt, content);
        return content;
    }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ability Liao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值