2025-03-15 14:27:06 +08:00
## 引言
2025-03-15 18:14:35 +08:00
自从 Open AI 发布 ChatGPT 后, 获得了全球大量的关注。生成式AI的强大能力, 改变了许多人的生活方式。在编程语言的社区中, 正积极地建设生成式AI的能力。在Java语言为主的Spring社区, 发布了 Spring AI 1.0.0-SNAPSHAT 版本。下文将介绍如何安装并部署AI模型, 重点探讨如何通过 Spring AI 框架构建AI服务。
2025-03-15 14:27:06 +08:00
2025-03-15 17:37:27 +08:00
## 什么是Hugging Face?
2025-03-15 15:30:02 +08:00
2025-03-15 17:37:27 +08:00
Hugging Face是一个公开的AI模型社区, 托管着来自世界各地AI领域的开发者、企业、组织上传的模型、数据集等内容。方便那些对AI感兴趣的爱好者分享并下载。它就像一个AI模型的超市一样, AI模型的生产制造商会把生产出来的模型上架到这个“超市”, 消费者在这里可以挑选自己感兴趣的模型下载、试用。官网地址( 需要魔法) : www.huggingface.co
2025-03-15 14:27:06 +08:00
2025-03-15 17:37:27 +08:00
## 什么是Ollama?
前面已经提到了从哪里获取AI模型, 但是这个模型和应用软件是不一样的, windows、linux、macos等操作系统无法直接运行。Ollama就是一个AI模型的安装和管理工具, Ollama可能不是最好的AI模型管理工具, 但它的兼容性很强, 这是它的优势。我们可以在windows、linux、macos系统里安装Ollama, 再通过Ollama获取并安装Hugging Face里的AI模型。
2025-03-15 17:42:14 +08:00
可以在 https://ollama.com/download 网站下载并安装Ollama。安装好以后可以通过命令行拉取模型了, 以deepseek-r1举例:
2025-03-15 17:37:27 +08:00
```
ollama run hf.co/deepseek-ai/deepseek-r1:7b
```
命令的格式:` ollama run hf.co/用户名/模型名:参数量级`, 上面的7b就是指R1的70亿参数模型。没有魔法的话, `hf.co`可能访问不了,可以换成镜像站`hf-mirror.com`:
```
ollama run hf-mirror.com/deepseek-ai/deepseek-r1:7b
```
关于Ollama更多的能力这里不再继续展开, 感兴趣的话可以在网上找视频继续学习。
## 什么是SpringAI?
2025-03-15 21:37:38 +08:00
- 官方文档地址: https://spring.io/projects/spring-ai
- Github仓库地址: https://github.com/spring-projects/spring-ai
官方的定位:
2025-03-15 17:37:27 +08:00
> Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain.
2025-03-15 17:38:20 +08:00
翻译: Spring AI 是一个用于 AI 工程的应用程序框架。 其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI 领域,并将使用 POJO 构建应用程序推广到 AI 领域。
2025-03-15 17:37:27 +08:00
< img src = 'https://images.ctfassets.net/mnrwi97vnhts/4mda205vy509Dx3vGkMwFr/af520e66dc79fb80cd1bc129a11d6d23/spring-ai-integration-diagram-3.svg' >
2025-03-15 21:37:38 +08:00
Spring AI 的核心是解决企业如何集成 AI 模型。
## Spring AI的功能
- 对 AI 模型供应商的支持, 例如: DeepSeek、Qwen( Alibaba) 、qianfan( Baidu) 、Anthropic、OpenAI、Microsoft、Amazon、Google、Ollama。支持的模型类型有:
+ [Chat Completion ](https://docs.spring.io/spring-ai/reference/api/chatmodel.html ):聊天模型
+ [Embedding ](https://docs.spring.io/spring-ai/reference/api/embeddings.html ):嵌入模型
+ [Text to Image ](https://docs.spring.io/spring-ai/reference/api/imageclient.html ):文生图
+ [Audio Transcription ](https://docs.spring.io/spring-ai/reference/api/audio/transcriptions.html ):音频转换
+ [Text to Speech ](https://docs.spring.io/spring-ai/reference/api/audio/speech.html ):文字转语音
+ [Moderation ](https://docs.spring.io/spring-ai/reference/api/index.html#api/moderation ):内容审核
- [结构化输出 ](https://docs.spring.io/spring-ai/reference/api/structured-output-converter.html ): 就像是传统应用的ORM一样, 把AI模型的输出内容映射到POJO。
- 对向量数据库的支持以及跨向量存储的便携式API, 包括一种新颖的类似SQL的 Metadata Filter API, 对向量数据库的支持包括:
+ Apache Cassandra
+ Azure Cosmos DB
+ Azure Vector Search
+ Chroma
+ Elasticsearch
+ GemFire
+ MariaDB
+ Milvus
+ MongoDB Atlas
+ Neo4j
+ OpenSearch
+ Oracle
+ PostgreSQL/PGVector
+ PineCone
+ Qdrant
+ Redis
+ SAP Hana
+ Typesense
+ Weaviate
- [Tools/Function Calling}(https://docs.spring.io/spring-ai/reference/api/functions.html):允许模型请求执行客户端工具和函数,从而按需访问必要的实时信息。
- [Observability ](https://docs.spring.io/spring-ai/reference/observability/index.html ): 提供对AI相关操作的可观测性。
- 用于数据工程的文档注入 [ETL 框架 ](https://docs.spring.io/spring-ai/reference/api/etl-pipeline.html )
- [AI 模型评估 ](https://docs.spring.io/spring-ai/reference/api/testing.html ):帮助评估生成的内容并防止幻觉响应的实用程序。
- [ChatClient ](https://docs.spring.io/spring-ai/reference/api/chatclient.html ):用于与 AI 聊天模型通信的链式调用API, 类似于 WebClient 和 RestClient。
- [Advisors ](https://docs.spring.io/spring-ai/reference/api/advisors.html ): 封装了常见的生成式AI使用模式, 能够转换发送至语言模型( LLMs) 及从模型接收的数据, 并确保在不同模型和应用场景间的兼容性和可移植性。
2025-03-15 21:48:49 +08:00
- [Chat Conversation Memory ](https://docs.spring.io/spring-ai/reference/api/chatclient.html#_chat_memory ): 在聊天机器人或对话系统中用于存储和管理对话历史记录的功能或组件。这个概念对于创建连贯且上下文相关的对话体验至关重要。具体来说, Chat Conversation Memory能够记住用户与系统之间的多轮对话内容, 并在后续交互中使用这些信息来维持对话的连续性。例如, 如果用户在一段对话中提到了某个特定的信息( 如他们的名字或者他们感兴趣的产品) , 系统可以通过记忆这一信息, 在之后的对话中正确引用, 从而提供更加个性化和流畅的用户体验。这种记忆机制可以实现于多种层面, 包括但不限于 **短期记忆** :仅保留最近几轮对话的信息,适合处理即时的、短暂的会话需求。**长期记忆**:能够持久化用户的偏好、个人信息等长期有效的数据,支持更深层次的个性化服务。**全局记忆**: 跨越多个会话保存用户数据, 允许跨会话追踪用户的行为和偏好。通过有效利用Chat Conversation Memory, 可以构建出更加智能和人性化的对话应用。
- [Retrieval Augmented Generation( RAG) ](https://docs.spring.io/spring-ai/reference/api/chatclient.html#_retrieval_augmented_generation ): 一种结合了信息检索和文本生成的技术框架, 旨在增强生成模型的能力。将检索组件、生成组件结合, 使得生成的文本不仅基于预训练模型中的知识, 还能动态地从文档、其他数据源中检索最新的或特定领域的信息来辅助生成过程。例如在问答系统中, 可以根据最新的资料提供答案, 而不受限于模型训练时的知识库。总的来说, RAG为解决传统生成模型面临的知识限制问题, 提供了有效的解决方案, 尤其是在需要引用具体事实或最新信息的任务上表现尤为突出。
- 适用于所有AI模型和向量存储的Spring Boot自动配置和启动器, 使用 https://start.spring.io 选择您想要的模型或向量存储。