DashScope
在本章节中,我们将学习如何使用 Spring AI Alibaba 接入阿里云 DashScope 系列模型。在开始学习之前,请确保您已经了解相关概念。
DashScope 平台
灵积通 过灵活、易用的模型 API 服务,让各种模态模型的能力,都能方便的为 AI 开发者所用。通过灵积 API,开发者不仅可以直接集成大模型的强大能力,也可以对模型进行训练微调,实现模型定制化。
Prerequisites
您需要使用阿里云 DashScope 创建 API Key 才能访问 DashScope 模型。
在 阿里云 DashScope 控制台 创建账户,并在 API Keys 页面 生成 API Key。
Spring AI Alibaba 项目定义了一个名为 spring.ai.dashscope.api-key 的配置属性,您应将其设置为从 DashScope 控制台获得的 API Key 值。
您可以在 application.properties 文件中设置此配置属性:
spring.ai.dashscope.api-key=<your-dashscope-api-key>
为了在处理敏感信息(如 API keys)时增强安全性,您可以使用 Spring Expression Language (SpEL) 来引用自定义环境变量:
# In application.yml
spring:
ai:
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY}
# In your environment or .env file
export AI_DASHSCOPE_API_KEY=<your-dashscope-api-key>
您也可以在应用程序代码中以编程方式设置此配置:
// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("AI_DASHSCOPE_API_KEY");
Add Repositories and BOM
Spring AI Alibaba artifacts 发布在 Maven Central 仓库中。 请参阅 Artifact Repositories 部分,将这些仓库添加到您的构建系统中。
为了帮助进行依赖管理,Spring AI Alibaba 提供了 BOM (bill of materials),以确保在整个项目中使用一致版本的 Spring AI Alibaba。请参阅 Dependency Management 部分,将 Spring AI Alibaba BOM 添加到您的构建系统中。
Auto-configuration
Spring AI Alibaba 为 DashScope Chat Client 提供 Spring Boot auto-configuration。
要启用它,请将以下依赖项添加到项目的 Maven pom.xml 或 Gradle build.gradle 构建文件中:
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
提示: 请参阅 Dependency Management 部分,将 Spring AI Alibaba BOM 添加到您的构建文件中。
Chat Properties
Retry Properties
前缀 spring.ai.retry 是用于配置 DashScope chat model 的 retry 机制的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.retry.max-attempts | 最大重试次数。 | 10 |
| spring.ai.retry.backoff.initial-interval | 指数退避策略的初始睡眠持续时间。 | 2 sec. |
| spring.ai.retry.backoff.multiplier | 退避间隔乘数。 | 5 |
| spring.ai.retry.backoff.max-interval | 最大退避持续时间。 | 3 min. |
| spring.ai.retry.on-client-errors | 如果为 false,抛出 NonTransientAiException,并且不对 4xx 客户端错误代码尝试重试 | false |
| spring.ai.retry.exclude-on-http-codes | 不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)。 | empty |
| spring.ai.retry.on-http-codes | 应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。 | empty |
Connection Properties
前缀 spring.ai.dashscope 是用于连接到 DashScope 的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.dashscope.base-url | 要连接到的 URL | https://dashscope.aliyuncs.com |
| spring.ai.dashscope.api-key | API Key | - |
| spring.ai.dashscope.work-space-id | 可选地,您可以指定用于 API 请求的工作空 间 ID。 | - |
提示: 对于属于多个工作空间的用户,您可以可选地指定用于 API 请求的工作空间 ID。 这些 API 请求的使用量将计入指定工作空间的使用量。
Configuration Properties
注意: 现在通过前缀为
spring.ai.model.chat的顶级属性来配置 chat auto-configurations 的启用和禁用。要启用,spring.ai.model.chat=dashscope(默认启用)
要禁用,spring.ai.model.chat=none(或任何与 dashscope 不匹配的值)
此更改是为了允许配置多个模型。
前缀 spring.ai.dashscope.chat 是用于配置 DashScope 的 chat model 实现的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.model.chat | 启用 DashScope chat model。 | dashscope |
| spring.ai.dashscope.chat.base-url | 可选覆盖 spring.ai.dashscope.base-url 属性以提供 chat 特定的 URL。 | - |
| spring.ai.dashscope.chat.completions-path | 要附加到 base URL 的路径。 | /api/v1/services/aigc/text-generation/generation |
| spring.ai.dashscope.chat.api-key | 可选覆盖 spring.ai.dashscope.api-key 以提供 chat 特定的 API Key。 | - |
| spring.ai.dashscope.chat.work-space-id | 可选地,您可以指定用于 API 请求的工作空间 ID。 | - |
| spring.ai.dashscope.chat.options.model | 要使用的 DashScope chat model 名称。您可以在以下模型之间选择:qwen-plus |