前言
SpringAI整合了全球(主要是国外)的大多数大模型,而且对于大模型开发的三种技术架构都有比较好的封装和支持,开发起来非常方便。
不同的模型能够接收的输入类型、输出类型不一定相同。SpringAI根据模型的输入和输出类型不同对模型进行了分类:
大模型应用开发大多数情况下使用的都是基于对话模型(Chat Model),也就是输出结果为自然语言或代码的模型。
目前SpringAI支持的大约19种对话模型,以下是一些功能对比:
Provider
Multimodality
Tools/Functions
Streaming
Retry
Built-in JSON
Local
OpenAI API Compatible
Anthropic Claude
text, pdf, image
✔
✔
✔
❌
❌
❌
Azure OpenAI
text, image
✔
✔
✔
✔
❌
✔
DeepSeek (OpenAI-proxy)
text
❌
✔
✔
✔
✔
✔
Google VertexAI Gemini
text, pdf, image, audio, video
✔
✔
✔
✔
❌
✔
Groq (OpenAI-proxy)
text, image
✔
✔
✔
❌
❌
✔
HuggingFace
text
❌
❌
❌
❌
❌
❌
Mistral AI
text, image
✔
✔
✔
✔
❌
✔
MiniMax
text
✔
✔
✔
❌
❌
❌
Moonshot AI
text
✔
✔
✔
❌
❌
❌
NVIDIA (OpenAI-proxy)
text, image
✔
✔
✔
❌
❌
✔
OCI GenAI/Cohere
text
❌
❌
❌
❌
❌
❌
Ollama
text, image
✔
✔
✔
✔
✔
✔
OpenAI
In: text, image, audio Out: text, audio
✔
✔
✔
✔
❌
✔
Perplexity (OpenAI-proxy)
text
❌
✔
✔
❌
❌
✔
QianFan
text
❌
✔
✔
❌
❌
❌
ZhiPu AI
text
✔
✔
✔
❌
❌
❌
Watsonx.AI
text
❌
✔
❌
❌
❌
❌
Amazon Bedrock Converse
text, image, video, docs (pdf, html, md, docx …)
✔
✔
✔
❌
❌
❌
其中功能最完整的就是OpenAI和Ollama平台的模型了。
接下来,我们就以这两个平台为例给大家讲解SpringAI的应用。
1.SpringAI入门(对话机器人)
接下来,我们就利用SpringAI发起与大模型的第一次对话。
1.1.快速入门
1.1.1.创建工程
创建一个新的SpringBoot工程,勾选Web、MySQL驱动即可:
工程结构如图:
原始pom.xml如下:
XML
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.4.3
com.itheima
ai-demo
0.0.1-SNAPSHOT
ai-demo
ai-demo
17
org.springframework.boot
spring-boot-starter-web
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
org.springframework.boot
spring-boot-maven-plugin
1.1.2.引入依赖
SpringAI完全适配了SpringBoot的自动装配功能,而且给不同的大模型提供了不同的starter,比如:
模型/平台
starter
Anthropic
XML
复制代码
1
2
3
4
org.springframework.ai
spring-ai-anthropic-spring-boot-starter
Azure OpenAI
XML
复制代码
1
2
3
4
org.springframework.ai
spring-ai-azure-openai-spring-boot-starter
DeepSeek
XML
复制代码
1
2
3
4
org.springframework.ai
spring-ai-openai-spring-boot-starter
Hugging Face
XML
复制代码
1
2
3
4
org.springframework.ai
spring-ai-huggingface-spring-boot-starter
Ollama
XML
复制代码
1
2
3
4
org.springframework.ai
spring-ai-ollama-spring-boot-starter
OpenAI