重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
spring-ai-alibaba by teachingai/full-stack-skills
npx skills add https://github.com/teachingai/full-stack-skills --skill spring-ai-alibabaSpring AI Alibaba 提供了与阿里云 DashScope(通义千问)的集成,支持使用阿里云的大语言模型服务。
依赖 :
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-starter-model-aliyun-dashscope</artifactId>
</dependency>
或使用 Gradle :
dependencies {
implementation 'com.alibaba.cloud.ai:spring-ai-starter-model-aliyun-dashscope'
}
application.yml :
spring:
ai:
alibaba:
dashscope:
api-key: ${DASHSCOPE_API_KEY}
chat:
options:
model: qwen-turbo
temperature: 0.7
max-tokens: 2000
application.properties :
spring.ai.alibaba.dashscope.api-key=${DASHSCOPE_API_KEY}
spring.ai.alibaba.dashscope.chat.options.model=qwen-turbo
spring.ai.alibaba.dashscope.chat.options.temperature=0.7
spring.ai.alibaba.dashscope.chat.options.max-tokens=2000
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用 ChatClient :
@Service
public class ChatService {
private final ChatClient chatClient;
public ChatService(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chat(String message) {
return chatClient.call(message);
}
public String chatWithPrompt(String userMessage) {
Prompt prompt = new Prompt(new UserMessage(userMessage));
ChatResponse response = chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}
}
流式响应 :
@Service
public class ChatService {
private final StreamingChatClient streamingChatClient;
public ChatService(StreamingChatClient streamingChatClient) {
this.streamingChatClient = streamingChatClient;
}
public Flux<String> streamChat(String message) {
return streamingChatClient.stream(message)
.map(response -> response.getResult().getOutput().getContent());
}
}
支持的模型 :
qwen-turbo - 通义千问 Turbo 模型(快速响应)qwen-plus - 通义千问 Plus 模型(平衡性能)qwen-max - 通义千问 Max 模型(最强性能)配置不同模型 :
spring:
ai:
alibaba:
dashscope:
chat:
options:
model: qwen-max # 使用最强模型
temperature: 0.7
max-tokens: 2000
定义模板 :
@Service
public class PromptService {
private final PromptTemplate promptTemplate;
public PromptService() {
this.promptTemplate = new PromptTemplate(
"请用{style}风格回答以下问题:{question}"
);
}
public String generatePrompt(String style, String question) {
Map<String, Object> variables = Map.of(
"style", style,
"question", question
);
return promptTemplate.render(variables);
}
}
使用 ChatClient :
@Service
public class ChatService {
private final ChatClient chatClient;
private final PromptTemplate promptTemplate;
public ChatService(ChatClient chatClient) {
this.chatClient = chatClient;
this.promptTemplate = new PromptTemplate(
"请用{style}风格回答以下问题:{question}"
);
}
public String chatWithStyle(String style, String question) {
Prompt prompt = promptTemplate.create(Map.of(
"style", style,
"question", question
));
ChatResponse response = chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}
}
配置 :
spring:
ai:
alibaba:
dashscope:
embedding:
options:
model: text-embedding-V1
使用 EmbeddingClient :
@Service
public class EmbeddingService {
private final EmbeddingClient embeddingClient;
public EmbeddingService(EmbeddingClient embeddingClient) {
this.embeddingClient = embeddingClient;
}
public List<Double> embed(String text) {
EmbeddingResponse response = embeddingClient.embedForResponse(
List.of(text)
);
return response.getResult().getOutput();
}
public List<List<Double>> embedBatch(List<String> texts) {
EmbeddingResponse response = embeddingClient.embedForResponse(texts);
return response.getResult().getOutput();
}
}
维护对话上下文 :
@Service
public class ConversationService {
private final ChatClient chatClient;
private final List<Message> conversationHistory = new ArrayList<>();
public ConversationService(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chat(String userMessage) {
conversationHistory.add(new UserMessage(userMessage));
Prompt prompt = new Prompt(conversationHistory);
ChatResponse response = chatClient.call(prompt);
String assistantMessage = response.getResult().getOutput().getContent();
conversationHistory.add(new AssistantMessage(assistantMessage));
return assistantMessage;
}
public void clearHistory() {
conversationHistory.clear();
}
}
@Service
public class ChatService {
private final ChatClient chatClient;
public String chat(String message) {
try {
return chatClient.call(message);
} catch (Exception e) {
// 处理错误
log.error("Chat error", e);
return "抱歉,处理请求时出现错误";
}
}
}
通义千问对中文支持较好,可以:
<!-- Spring AI Alibaba DashScope -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-starter-model-aliyun-dashscope</artifactId>
</dependency>
<!-- Spring Boot Web (可选,用于 REST API) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
完整配置 :
spring:
ai:
alibaba:
dashscope:
api-key: ${DASHSCOPE_API_KEY}
chat:
options:
model: qwen-turbo
temperature: 0.7
max-tokens: 2000
top-p: 0.9
embedding:
options:
model: text-embedding-V1
每周安装次数
57
代码仓库
GitHub 星标数
248
首次出现
2026年1月24日
安全审计
安装于
opencode48
gemini-cli47
codex46
github-copilot44
cursor41
amp40
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
123,700 周安装