临沂建设工程招聘信息网站,国内优秀企业网站设计,网络游戏中心,seo网络优化师Spring AI Alibaba 介绍和功能演示
背景
Spring AI Alibaba 开源项目基于 Spring AI 构建#xff0c;是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践#xff0c;提供高层次的 AI API 抽象与云原生基础设施集成方案#xff0c;帮助开发者快速构建 AI 应用。…Spring AI Alibaba 介绍和功能演示
背景
Spring AI Alibaba 开源项目基于 Spring AI 构建是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践提供高层次的 AI API 抽象与云原生基础设施集成方案帮助开发者快速构建 AI 应用。
Spring AI Alibaba 生态图如下 演示
在此节中将演示如何使用 Spring AI Alibaba 提供的接口功能完成和 LLMs 的交互。
框架搭建
pom.xml
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion3.3.4/version/dependency!-- 最新版本 Spring AI Alibaba --dependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-starter/artifactIdversion1.0.0-M3.1/version/dependency/dependencies!-- 添加仓库配置否则报错如果添加之后仍然报错刷新 mvn 或者清楚 IDEA 缓存 --repositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repository/repositories!-- 解决报错 Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the -parameters flag --buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.11.0/versionconfigurationparameterstrue/parameters/configuration/plugin/plugins/build/projectapplication.yml
spring:ai:dashscope:api-key: ${AI_DASHSCOPE_API_KEY}启动类
SpringBootApplication
public class AIApplication {public static void main(String[] args) {SpringApplication.run(AIApplication.class, args);}}到此为止我们已经搭建好了一个基本的 AI 应用雏形现在开始和大模型交互 只演示朴素请求流式 API 不演示 Chat 功能
AIChatController.java
RestController
RequestMapping(/ai)
public class AIChatController {// 使用高级 Client API也可以使用低级 ChatModel APIprivate final ChatClient chatClient;public AIChatController(ChatClient.Builder builder) {this.chatClient builder.build();}GetMapping(/chat/{prompt})public String chatWithChatMemory(PathVariable String prompt) {return chatClient.prompt().user(prompt).call().chatResponse().getResult().getOutput().getContent();}}如果一切顺利请求 http://localhost:8080/ai/chat/你好接口将得到以下输出
你好有什么我可以帮助你的吗从代码中可以看到使用 Spring AI Alibaba 之后和模型交互变得非常简单容易。
但是大模型是无状态的怎么能让他变得有记忆Spring AI 提供了 ChatMemory 的接口只需要调用接口即可源码将在后续文章中分析
RestController
RequestMapping(/ai)
public class AIChatController {private final ChatClient chatClient;public AIChatController(ChatModel chatModel) {this.chatClient ChatClient.builder(chatModel).defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory())).build();}GetMapping(/chat/{chatId}/{prompt})public String chatWithChatMemory(PathVariable String chatId,PathVariable String prompt) {return chatClient.prompt().user(prompt).advisors(a - a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)).call().chatResponse().getResult().getOutput().getContent();}}我们像这样请求接口
# 1
input: http://localhost:8080/ai/chat/10001/你好我是牧生
output你好牧生很高兴认识你。你可以叫我Qwen我是阿里云推出的一种超大规模语言模型。我有强大的语言生成和理解能力可以进行自然流畅的对话还能写故事、写公文、写邮件、写剧本等等也能表达观点玩游戏等。有什么我可以帮助你的吗# 2
inputhttp://localhost:8080/ai/chat/10001/我是谁
output你刚才提到你的名字是牧生。如果你有任何问题或需要进一步的帮助随时告诉我我很乐意为你服务# 当切换 chatId 时
inputhttp://localhost:8080/ai/chat/10000/我叫什么名字
output您还没有告诉我您的名字呢。如果您愿意可以告诉我您希望被称为什么或者您想如何介绍自己。能看到借助 Spring AI 的 ChatMemory 接口已经使大模型变得聪明了起来。 PSSpring AI Alibaba 的 Chat Memory 功能已经在规划中了 Image 功能
本节我们将展示如何使用 Spring AI Alibaba 提供的 Image API 完成和大模型的图像交互功能包含文生图多模态等功能。
AIImageController.java
RestController
RequestMapping(/ai)
public class AIImageController {private final ImageModel imageModel;public AIImageController(ImageModel imageModel) {this.imageModel imageModel;}GetMapping(/image/{input})public void image(PathVariable(input) String input, HttpServletResponse response) {String imageUrl imageModel.call(new ImagePrompt(input)).getResult().getOutput().getUrl();try {URL url URI.create(imageUrl).toURL();InputStream in url.openStream();response.setHeader(Content-Type, MediaType.IMAGE_PNG_VALUE);response.getOutputStream().write(in.readAllBytes());response.getOutputStream().flush();} catch (IOException e) {response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);}}}请求接口 http://localhost:8080/ai/image/给我一张AI图片将会得到以下输出 多模态
MultiModelController.java 多模态还支持视频解析和 流式 API。 RestController
RequestMapping(/ai)
public class AIImageController {private final ImageModel imageModel;// 加入 chatClientprivate final ChatClient client;public AIImageController(ImageModel imageModel, ChatClient.Builder builder) {this.imageModel imageModel;this.client builder.build();}GetMapping(/image/{input})public void image(PathVariable(input) String input, HttpServletResponse response) {ImageOptions options ImageOptionsBuilder.builder().withModel(wanx-v1).build();String imageUrl imageModel.call(new ImagePrompt(input, options)).getResult().getOutput().getUrl();try {URL url URI.create(imageUrl).toURL();InputStream in url.openStream();response.setHeader(Content-Type, MediaType.IMAGE_PNG_VALUE);response.getOutputStream().write(in.readAllBytes());response.getOutputStream().flush();} catch (IOException e) {response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);}}GetMapping(/image)public String image(RequestParam(value prompt, required false, defaultValue 图片里是什么)String prompt) throws Exception {// 图片资源 同时支持读取本地文件作为输入ListMedia mediaList List.of(new Media(MimeTypeUtils.IMAGE_PNG,new URI(https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg).toURL()));UserMessage message new UserMessage(prompt, mediaList);message.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.IMAGE);ChatResponse response client.prompt(new Prompt(message,DashScopeChatOptions.builder().withModel(qwen-vl-max-latest).withMultiModel(true).build())).call().chatResponse();return response.getResult().getOutput().getContent();}}请求 http://localhost:8080/ai/image 接口将得到以下输出
这张图片展示了一位女士和一只狗在海滩上互动的温馨场景。女士坐在沙滩上面带微笑与狗握手。狗戴着项圈显得非常温顺和友好。背景是广阔的海洋和天空阳光洒在沙滩上营造出一种温暖和谐的氛围。Audio 功能
本节我们将展示如何使用 Spring AI Alibaba 提供的 Audio API 完成和大模型的音频交互功能包含文生语音语音转文字等功能。 截止文章发布期间Spring AI Alibaba 的语音转录接口还没有发版有关 stt 和 tts 的更多使用参考官方 example。 https://github.com/alibaba/spring-ai-alibaba/tree/main/spring-ai-alibaba-examples/audio-example/src/main/java/com/alibaba/cloud/ai/example/audio AIAudioController.java
RestController
RequestMapping(/ai)
public class AIAudioController implements ApplicationRunner {private final SpeechSynthesisModel speechSynthesisModel;private static final String TEXT 白日依山尽黄河入海流。;private static final String FILE_PATH src/main/resources/gen/tts/;private AIAudioController(SpeechSynthesisModel speechSynthesisModel) {this.speechSynthesisModel speechSynthesisModel;}GetMapping(/tts)public void tts() throws IOException {SpeechSynthesisResponse response speechSynthesisModel.call(new SpeechSynthesisPrompt(TEXT));File file new File(FILE_PATH output.mp3);try (FileOutputStream fos new FileOutputStream(file)) {ByteBuffer byteBuffer response.getResult().getOutput().getAudio();fos.write(byteBuffer.array());}catch (IOException e) {throw new IOException(e.getMessage());}}Overridepublic void run(ApplicationArguments args) {File file new File(FILE_PATH);if (!file.exists()) {file.mkdirs();}}PreDestroypublic void destroy() throws IOException {FileUtils.deleteDirectory(new File(FILE_PATH));}}请求接口将得到一个语音文件的输出。
函数调用
函数调用是为了弥补大模型的训练数据落后的问题用外部的 API 来补充 LLMs 的知识给用户最合理的回答。
天气函数注册 MockWeatherService.java
public class MockWeatherService implements FunctionMockWeatherService.Request, Response {Overridepublic Response apply(Request request) {if (request.city().contains(杭州)) {return new Response(String.format(%s%s晴转多云, 气温32摄氏度。, request.date(), request.city()));}else if (request.city().contains(上海)) {return new Response(String.format(%s%s多云转阴, 气温31摄氏度。, request.date(), request.city()));}else {return new Response(String.format(暂时无法查询%s的天气状况。, request.city()));}}JsonInclude(JsonInclude.Include.NON_NULL)JsonClassDescription(根据日期和城市查询天气)public record Request(JsonProperty(required true, value city) JsonPropertyDescription(城市, 比如杭州) String city,JsonProperty(required true, value date) JsonPropertyDescription(日期, 比如2024-08-22) String date) {}}紧接着我们在 AIChatController 中加入函数调用的代码
RestController
RequestMapping(/ai)
public class AIChatController {private final ChatClient chatClient;public AIChatController(ChatModel chatModel) {this.chatClient ChatClient.builder(chatModel).defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory())).build();}GetMapping(/chat/{chatId}/{prompt})public String chatWithChatMemory(PathVariable String chatId,PathVariable String prompt) {return chatClient.prompt().user(prompt).advisors(a - a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)).call().chatResponse().getResult().getOutput().getContent();}// 函数调用GetMapping(/weather-service/{city})public String weatherService(PathVariable String city) {return chatClient.prompt().function(getWeather, 根据城市查询天气, new MockWeatherService()).user(city).call().content();}}请求 http://localhost:8080/ai/weather-service/杭州2024年11月29日天气怎么样 接口将得到如下响应
2024年11月29日杭州的天气预报为晴转多云气温为32摄氏度。请根据天气情况做好相应的准备。如果您有其他问题欢迎随时询问RAG
本节中我们将使用 ES 作为 RAG 的实现演示 Spring AI Alibaba RAG 的实现。
在 resource 目录下准备一个 system-qa.st
Context information is below.
---------------------
{question_answer_context}
---------------------
Given the context and provided history information and not prior knowledge,
reply to the user comment. If the answer is not in the context, inform
the user that you cant answer the question.之后准备一个 df 文件点击这里下载https://github.com/alibaba/spring-ai-alibaba/blob/main/spring-ai-alibaba-examples/rag-example/src/main/resources/data/spring_ai_alibaba_quickstart.pdf
使用 docker compose up -d 启动一个 es
准备配置文件
config/es.yaml
cluster.name: docker-es
node.name: es-node-1
network.host: 0.0.0.0
network.publish_host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: *
bootstrap.memory_lock: true# 关闭认证授权 es 8.x 默认开启
# 如果不关闭spring boot 连接会 connection closed
xpack.security.enabled: falsedocker-compose.yaml
version: 3.3services:elasticsearch:image: docker.elastic.co/elasticsearch/elasticsearch:8.16.1container_name: elasticsearchprivileged: trueenvironment:- cluster.nameelasticsearch- discovery.typesingle-node- ES_JAVA_OPTS-Xms512m -Xmx1096m- bootstrap.memory_locktruevolumes:- ./config/es.yaml:/usr/share/elasticsearch/config/elasticsearch.ymlports:- 9200:9200- 9300:9300deploy:resources:limits:cpus: 2memory: 1000Mreservations:memory: 200Mapplication.yml 中加入 rag 相关配置
server:port: 9097spring:ai:dashscope:api-key: ${AI_DASHSCOPE_API_KEY}vectorstore:elasticsearch:index-name: spring-ai-alibaba-indexsimilarity: cosinedimensions: 1536initialize-schema: truepom.xml 中加入如下配置
dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-pdf-document-reader/artifactIdversion1.0.0-M3/version
/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-elasticsearch-store-spring-boot-starter/artifactIdversion1.0.0-M3/version
/dependencyAIRagController.java
package indi.yuluo.controller;import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.DenseVectorProperty;
import co.elastic.clients.elasticsearch._types.mapping.KeywordProperty;
import co.elastic.clients.elasticsearch._types.mapping.ObjectProperty;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.IndexSettings;
import com.alibaba.cloud.ai.advisor.RetrievalRerankAdvisor;
import com.alibaba.cloud.ai.model.RerankModel;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;import org.springframework.ai.autoconfigure.vectorstore.elasticsearch.ElasticsearchVectorStoreProperties;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** author yuluo* author a hrefmailto:yuluo08290126gmail.comyuluo/a*/RestController
RequestMapping(/ai)
public class AIRagController implements ApplicationRunner {private static final Logger logger LoggerFactory.getLogger(AIRagController.class);Value(classpath:/data/spring_ai_alibaba_quickstart.pdf)private Resource PdfResource;Value(classpath:/prompts/system-qa.st)private Resource systemResource;private static final String textField content;private static final String vectorField embedding;private final ChatModel chatModel;private final VectorStore vectorStore;private final RerankModel rerankModel;private final ElasticsearchClient elasticsearchClient;private final ElasticsearchVectorStoreProperties options;public AIRagController(ChatModel chatModel,VectorStore vectorStore,RerankModel rerankModel,ElasticsearchClient elasticsearchClient,ElasticsearchVectorStoreProperties options) {this.chatModel chatModel;this.vectorStore vectorStore;this.rerankModel rerankModel;this.elasticsearchClient elasticsearchClient;this.options options;}GetMapping(/rag)public FluxString generate(RequestParam(value message, defaultValue how to get start with spring ai alibaba?)String message,HttpServletResponse response) throws IOException {// 不设置返回值会乱码response.setCharacterEncoding(StandardCharsets.UTF_8.name());return this.retrieve(message).map(x - x.getResult().getOutput().getContent());}private FluxChatResponse retrieve(String message) throws IOException {// Enable hybrid search, both embedding and full text searchSearchRequest searchRequest SearchRequest.defaults().withFilterExpression(new FilterExpressionBuilder().eq(textField, message).build());// Step3 - Retrieve and llm generateString promptTemplate systemResource.getContentAsString(StandardCharsets.UTF_8);;ChatClient chatClient ChatClient.builder(chatModel).defaultAdvisors(new RetrievalRerankAdvisor(vectorStore,rerankModel,searchRequest,promptTemplate,0.1)).build();return chatClient.prompt().user(message).stream().chatResponse();}Overridepublic void run(ApplicationArguments args) throws Exception {// 1. parse documentDocumentReader reader new PagePdfDocumentReader(PdfResource);ListDocument documents reader.get();logger.info({} documents loaded, documents.size());// 2. split trunksListDocument splitDocuments new TokenTextSplitter().apply(documents);logger.info({} documents split, splitDocuments.size());// 3. create embedding and store to vector storelogger.info(create embedding and save to vector store);createIndexIfNotExists();vectorStore.add(splitDocuments);}private void createIndexIfNotExists() {try {String indexName options.getIndexName();Integer dimsLength options.getDimensions();if (Objects.isNull(indexName) || indexName.isEmpty()) {throw new IllegalArgumentException(Elastic search index name must be provided);}boolean exists elasticsearchClient.indices().exists(idx - idx.index(indexName)).value();if (exists) {logger.debug(Index {} already exists. Skipping creation., indexName);return;}String similarityAlgo options.getSimilarity().name();IndexSettings indexSettings IndexSettings.of(settings - settings.numberOfShards(String.valueOf(1)).numberOfReplicas(String.valueOf(1)));MapString, Property properties new HashMap();properties.put(vectorField, Property.of(property - property.denseVector(DenseVectorProperty.of(dense - dense.index(true).dims(dimsLength).similarity(similarityAlgo)))));properties.put(textField, Property.of(property - property.text(TextProperty.of(t - t))));MapString, Property metadata new HashMap();metadata.put(ref_doc_id, Property.of(property - property.keyword(KeywordProperty.of(k - k))));properties.put(metadata,Property.of(property - property.object(ObjectProperty.of(op - op.properties(metadata)))));CreateIndexResponse indexResponse elasticsearchClient.indices().create(createIndexBuilder - createIndexBuilder.index(indexName).settings(indexSettings).mappings(TypeMapping.of(mappings - mappings.properties(properties))));if (!indexResponse.acknowledged()) {throw new RuntimeException(failed to create index);}logger.info(create elasticsearch index {} successfully, indexName);}catch (IOException e) {logger.error(failed to create index, e);throw new RuntimeException(e);}}}之后请求 http://localhost:8080/ai/rag 接口将得到如下响应
根据提供的上下文信息以下是开始使用 Spring AI Alibaba 的步骤 ### 概述 Spring AI Alibaba 实现了与阿里云通义模型的完整适配。下面将介绍如何使用 Spring AI Alibaba 开发一个基于通义模型服务的智能聊天应用。 ### 快速体验示例 #### 注意事项 - **JDK 版本**因为 Spring AI Alibaba 基于 Spring Boot 3.x 开发所以本地 JDK 版本要求为 17 及以上。 #### 步骤 1. **下载项目** - 运行以下命令下载源码并进入 helloworld 示例目录 sh git clone --depth1 https://github.com/alibaba/spring-ai-alibaba.git cd spring-ai-alibaba/spring-ai-alibaba-examples/helloworld-example 2. **运行项目** - 首先需要获取一个合法的 API-KEY 并设置 AI_DASHSCOPE_API_KEY 环境变量。你可以跳转到 [阿里云百炼平台](https://sca.aliyun.com/) 了解如何获取 API-KEY。 sh export AI_DASHSCOPE_API_KEY${REPLACE-WITH-VALID-API-KEY} - 启动示例应用 sh ./mvnw compile exec:java -Dexec.mainClasscom.alibaba.cloud.ai.example.helloworld.HelloWorldExample 3. **访问应用** - 打开浏览器访问 http://localhost:8080/ai/chat?input给我讲一个笑话吧向通义模型提问并得到回答。 希望这些步骤能帮助你快速上手 Spring AI Alibaba如果有任何问题可以随时提问。总结
Spring AI Alibaba 基于 Spring AI 开发并在上层提供更多高级的抽象 API。帮助开发者构建 Java LLMs 应用。