微信扫码
与创始人交个朋友
我要投稿
Spring AI
项目旨在简化包含人工智能功能的应用程序的开发,避免不必要的复杂性。该项目从著名的 Python 项目(例如 LangChain 和 LlamaIndex)中汲取灵感,但 Spring AI 并不是这些项目的直接移植。该项目的成立相信下一波生成式人工智能应用程序不仅适用于 Python 开发人员,而且将在许多编程语言中普遍存在。
该项目从著名的 Python 项目(例如 LangChain 和 LlamaIndex)中汲取灵感,但 Spring AI 并不是这些项目的直接移植。该项目的成立相信下一波生成式人工智能应用程序不仅适用于 Python 开发人员,而且将在许多编程语言中普遍存在。
Spring AI 提供以下功能:
• 支持所有主要模型提供商,例如 OpenAI、Microsoft、Amazon、Google 和 Huggingface
• 支持的模型类型包括“聊天”和“文本到图像”,还有更多模型类型正在开发中
• 支持的模型类型包括“聊天”和“文本到图像”,还有更多模型类型正在开发中
• AI 模型输出到 POJO 的映射
• 支持所有主要矢量数据库提供商,例如 Azure 矢量搜索、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate
• 跨 Vector Store 提供商的可移植 API,包括同样可移植的新颖的类似 SQL 的元数据过滤器 API
• Function calling 函数调用
• AI 模型和向量存储的 Spring Boot 自动配置和启动器
• 数据工程的 ETL 框架
此功能集可让您实现常见用例,例如“对文档进行问答”或“聊天”。
- SpringBoot 3.2.4
- JDK 21
- Gradle 8.7
- OpenAI API Key
- Docker Compose
- Git
如果不想从零开始,可直接克隆我已经完成的源码:
git clone https://github.com/amuguelove/eric-spring-ai-rag-demo
Spring Cli
# MacOS按照spring-cli工具
brew tap spring-cli-projects/spring-cli
brew install spring-cli
spring boot new --from ai --name eric-spring-ai-rag-demo
Spring Initializr
这里使用IDEA创建,也可使用网页版:https://start.spring.io/,具体步骤查看下图:
需要替换成自己的 OpenAI API key
和 代理URL
,以下只是样例数据。
export OPENAI_API_KEY=sk-1234567890abcdef1234567890abcdef
export OPENAI_BASE_URL=https://api.openai.com/
spring:
application:
name: spring-ai-rag-demo
datasource:
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: postgres
ai:
openai:
api-key: ${OPENAI_API_KEY}
# Note:OpenAiApi will splice:"/v1/chat/completions"
base-url: ${OPENAI_BASE_URL}
chat:
options:
model: gpt-3.5-turbo
max-tokens: 1024
temperature: 0.7
embedding:
options:
model: 'text-embedding-ada-002'
vectorstore:
pgvector:
index-type: hnsw
distance-type: cosine_distance
dimensions: 1536
启动项目后,通过代码仓库中的 request.http
文件直接运行,不需要使用浏览器和Postman等工具。
### Test OpenAI connectivity
GET http://localhost:8080/ai/generate
### Clean Data
DELETE http://localhost:8080/data/delete
### Load Data
POST http://localhost:8080/data/load
### Count Data
GET http://localhost:8080/data/count
### RAG Test
GET http://localhost:8080/rag
### RAG Test TWO
GET http://localhost:8080/rag?question=开源大模型食用指南主要讲了什么?
### Without RAG
GET http://localhost:8080/rag?rag=false
./gradlw bootRun
GET http://localhost:8080/ai/generate
{
"question": "Tell me a joke",
"answer": "Why did the scarecrow win an award? Because he was outstanding in his field!"
}
执行脚本:POST http://localhost:8080/data/load
,以下是日志输出:
2024-04-10T22:55:52.459+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] t.f.ai.dataloader.DataLoaderService : Parsing PDF document
2024-04-10T22:55:52.969+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] t.f.ai.dataloader.DataLoaderService : Parsing document, splitting, creating embeddings and storing in vector store... this will take a while.
2024-04-10T22:55:53.009+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 1
2024-04-10T22:55:53.156+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 4
2024-04-10T22:55:53.184+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 7
2024-04-10T22:55:53.188+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 10
2024-04-10T22:55:53.278+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 13
2024-04-10T22:55:53.330+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 16
2024-04-10T22:55:53.370+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 19
2024-04-10T22:55:53.397+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 22
2024-04-10T22:55:53.429+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 25
2024-04-10T22:55:53.455+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing PDF page: 28
2024-04-10T22:55:53.581+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.ai.reader.pdf.PagePdfDocumentReader : Processing 37 pages
2024-04-10T22:55:53.606+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.612+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.617+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.623+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.625+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.631+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.636+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.645+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.650+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.656+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.664+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.669+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.676+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.682+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.687+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.693+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.699+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.704+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.710+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.713+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.717+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.722+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.729+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.736+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.741+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.751+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:55:53.757+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 3 chunks.
2024-04-10T22:55:53.762+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] o.s.a.transformer.splitter.TextSplitter : Splitting up document into 2 chunks.
2024-04-10T22:56:52.916+08:00 INFO 18778 --- [spring-ai-rag-demo] [nio-8080-exec-3] t.f.ai.dataloader.DataLoaderService : Done parsing document, splitting, creating embeddings and storing in vector store
查看PG Vector
数据库,验证向量数据是否成功插入:
# 默认启用RAG进行问答
GET http://localhost:8080/rag
{
"question": "What is the largest trend of 2023?",
"answer": "The largest trend of 2023 is the rise of Generative AI, powered by Large Language Models (LLMs) such as GPT-3 and GPT-4. This trend has gained significant prominence in the AI and ML industry, with widespread adoption being driven by technologies like ChatGPT."
}
GET http://localhost:8080/rag?question=开源大模型食用指南主讲了什么?
{
"question": "开源大模型食用指南主讲了什么?",
"answer": "开源大模型食用指南主要介绍了围绕开源大模型、针对国内初学者的教程。其中包括环境配置、本地部署、高效微调等技能的全流程指导,旨在简化开源大模型的部署、使用和应用流程,让更多普通学生、研究者更好地使用开源大模型。具体内容包括基于 AutoDL 平台的开源 LLM 环境配置指南,不同开源 LLM 的部署使用教程,开源 LLM 的部署应用指导,以及开源 LLM 的全量微调、高效微调方法等。这些内容旨在帮助学生和未来的从业者了解和熟悉开源大模型的食用方法。"
}
# 关闭RAG问答
GET http://localhost:8080/rag?rag=false
{
"question": "What is the largest trend of 2023?",
"answer": "I'm sorry, but I don't have information on future trends as they are subject to change and evolve over time."
}
Spring AI[1]
RAG and Spring AI — Querying Your Own Documents with Open AI)[2]
以下是官方的一些设计图,感兴趣的可以看下:
允许开发人员在启动应用程序时使用特定于模型的选项,然后在运行时使用提示请求覆盖它们:
Spring AI Chat Completion API 构建在 Spring AI Generic Model API
之上,提供聊天特定的抽象和实现。
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-08-13
2024-03-30
2024-05-28
2024-05-10
2024-04-26
2024-04-12
2024-04-25
2024-07-25
2024-05-06
2024-05-14