更新 SpringAI/0_使用SpringAI接入AI模型.md

This commit is contained in:
8ga 2025-03-16 14:02:30 +08:00
parent e647cbbf18
commit 981599bd21

View File

@ -78,3 +78,102 @@ Spring AI 的核心是解决企业如何集成 AI 模型。
- [Chat Conversation Memory](https://docs.spring.io/spring-ai/reference/api/chatclient.html#_chat_memory)在聊天机器人或对话系统中用于存储和管理对话历史记录的功能或组件。这个概念对于创建连贯且上下文相关的对话体验至关重要。具体来说Chat Conversation Memory能够记住用户与系统之间的多轮对话内容并在后续交互中使用这些信息来维持对话的连续性。例如如果用户在一段对话中提到了某个特定的信息如他们的名字或者他们感兴趣的产品系统可以通过记忆这一信息在之后的对话中正确引用从而提供更加个性化和流畅的用户体验。这种记忆机制可以实现于多种层面包括但不限于 **短期记忆**:仅保留最近几轮对话的信息,适合处理即时的、短暂的会话需求。**长期记忆**:能够持久化用户的偏好、个人信息等长期有效的数据,支持更深层次的个性化服务。**全局记忆**跨越多个会话保存用户数据允许跨会话追踪用户的行为和偏好。通过有效利用Chat Conversation Memory可以构建出更加智能和人性化的对话应用。
- [Retrieval Augmented GenerationRAG](https://docs.spring.io/spring-ai/reference/api/chatclient.html#_retrieval_augmented_generation)一种结合了信息检索和文本生成的技术框架旨在增强生成模型的能力。将检索组件、生成组件结合使得生成的文本不仅基于预训练模型中的知识还能动态地从文档、其他数据源中检索最新的或特定领域的信息来辅助生成过程。例如在问答系统中可以根据最新的资料提供答案而不受限于模型训练时的知识库。总的来说RAG为解决传统生成模型面临的知识限制问题提供了有效的解决方案尤其是在需要引用具体事实或最新信息的任务上表现尤为突出。
- 适用于所有AI模型和向量存储的Spring Boot自动配置和启动器使用 https://start.spring.io 选择您想要的模型或向量存储。
## Quick Start
Spring AI 支持 spring boot 3.2.x 及更高版本对JDK的最低要求是JDK17+。建议采用 Open JDK Zulu 或 Open JDK Temurin 的最新版本。在 pom.xml 中引入 Spring AI 物料清单,使用 BOM 可以避免依赖冲突,并且是经过测试的推荐版本。
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
```
添加 Milestone 和 Snapshot 存储库,因为 Spring AI 还没有 Release所以没有上传到Maven中央仓库。
```xml
<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>
</repositories>
```
以千问举例引入阿里灵积DashScope平台的starter这官方适配Spring AI的依赖包版本和 Spring AI 的版本是一致的。
```
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
```
申请灵积平台的 API Keyhttps://dashscope.console.aliyun.com/apiKey添加相关配置
```yml
spring:
ai:
dash-scope:
api-key: xxx
chat:
options:
model: qwen-max # 使用的模型
```
### 使用ChatClient发送消息示例
1. 引入ChatModel可以切换不同的AI模型
```java
private final ChatModel chatModel;
```
2. 非流式问答
```java
@GetMapping("chat")
public String chat(@RequestParam String prompt) {// 用户输入的prompt
return ChatClient.create(chatModel)
.prompt()
.user(prompt)
.call() // 阻塞等待返回结果可以是ChatResponse、JaveBean、String
.content();
}
```
3. 流式问答
```java
@GetMapping(value = "chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<String>> chatStream(@RequestParam String prompt) {
return ChatClient.create(chatModel)
.prompt()
.user(prompt)
.stream()
.chatResponse()
// ServerSendEventSSE是一个轻量级的服务端向客户端单向推送的流式传输工具
.map(chatResponse -> ServerSentEvent.builder(JSONUtil.toJsonStr(chatResponse)).event("message").build());
}
```