SpringAI大模型应用开发
·
一、认识AI和大模型
①AI发展历程

②大语言模型



二、AIGC
AIGC(AI Generated Content):是AI领域的一个应用分支,专注于利用AI技术自动生成内容
三、提示工程
提示工程(Prompt Engineering)也被称为上下文提示,用于设计和优化输入文本(Prompt)来引导AI模型生成预期的输出
①prompt的组成
- 角色:给 AI 定义一个最匹配任务的角色,比如:「你是一位专业的博客作者」
- 指示:对任务进行描述,比如:「撰写一篇关于最新AI技术发展的文章」
- 上下文:给出与任务相关的其它背景信息
- 例子:必要时给出举例,[实践证明其对输出正确性有帮助」
- 输入:任务的输入信息;在提示词中明确的标识出输入
- 输出:输出的格式描述,以便后继模块自动解析模型的输出结果,比如(JSON、Java)
四、大模型应用开发
①模型部署

云部署:
本地部署:
②调用大模型

③什么是大模型应用
大模型应用是基于大模型的推理、分析、生成能力,结合传统编程能力,开发出的各种应用


④大模型应用开发技术方案

(1)纯Prompt问答

(2)Function Calling

(3)RAG

五、SpringAI
①快速入门
(1)引入依赖

参考代码:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.8</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lmai</groupId> <artifactId>lmai</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lmai</name> <description>lmai</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> <spring-ai.version>1.0.1</spring-ai.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-ollama</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-openai</artifactId> </dependency> <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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.38</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>${spring-ai.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)配置模型
spring:
application:
name: lmai
ai:
openai:
base-url: https://dashscope.aliyuncs.com/compatible-mode
api-key: your-api-key
chat:
options:
model: qwen-max
temperature: 0.8
server:
port: 8888
#连接的是阿里云的通义千问(Qwen) 大模型中的 qwen-max 版本。
#配置中明确指定了 model: qwen-max,
#而 base-url: https://dashscope.aliyuncs.com/compatible-mode
#指向的是阿里云 DashScope 服务的兼容模式接口,
#DashScope 是阿里云官方的大模型服务平台,
#其中就包含通义千问(Qwen)系列模型,
#qwen-max 是该系列中性能较强的一个模型版本
(3)配置客户端
package com.lmai.ai.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CommonConfiguration {
@Bean
public ChatClient chatClient(OpenAiChatModel model) {
return ChatClient
.builder(model)
.defaultSystem("你是企业级智能助手,致力于为用户提供专业、高效的业务支持。主要职责包括:解答业务流程疑问、提供系统操作指导、协助处理日常工作任务、回应基础信息查询等。\n" +"回应需遵循以下原则:\n" +
"1. 语言简洁明了,避免冗余表述;\n" +
"2. 信息准确可靠,基于已知内容作答,不确定时需明确告知;\n" +
"3. 态度友好专业,符合职场沟通规范;\n" +
"4. 严格遵守数据安全规定,不泄露任何敏感信息。")
.build();
}
}
package com.lmai.ai.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class CommonController {
private final ChatClient chatClient;
@RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
public Flux<String> chat(String prompt){
return chatClient.prompt()
.user(prompt)
.stream()
.content();
}
}
②会话日志

修改客户端和配置文件:
package com.lmai.ai.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CommonConfiguration {
@Bean
public ChatClient chatClient(OpenAiChatModel model) {
return ChatClient
.builder(model)
.defaultSystem("你是企业级智能助手,致力于为用户提供专业、高效的业务支持。主要职责包括:解答业务流程疑问、提供系统操作指导、协助处理日常工作任务、回应基础信息查询等。\n" +"回应需遵循以下原则:\n" +
"1. 语言简洁明了,避免冗余表述;\n" +
"2. 信息准确可靠,基于已知内容作答,不确定时需明确告知;\n" +
"3. 态度友好专业,符合职场沟通规范;\n" +
"4. 严格遵守数据安全规定,不泄露任何敏感信息。")
.defaultAdvisors(new SimpleLoggerAdvisor())//新增
.build();
}
}
spring:
application:
name: lmai
ai:
openai:
base-url: https://dashscope.aliyuncs.com/compatible-mode
api-key: your-api-key
chat:
options:
model: qwen-max
temperature: 0.8
#连接的是阿里云的通义千问(Qwen) 大模型中的 qwen-max 版本。
#配置中明确指定了 model: qwen-max,
#而 base-url: https://dashscope.aliyuncs.com/compatible-mode
#指向的是阿里云 DashScope 服务的兼容模式接口,
#DashScope 是阿里云官方的大模型服务平台,
#其中就包含通义千问(Qwen)系列模型,
#qwen-max 是该系列中性能较强的一个模型版本
server:
port: 8888
logging:
level:
org.springframework.ai.chat.client.advisor: debug
con.lmai.ai: debug
更多推荐



所有评论(0)