来宾网站建设公司做网站管理系统
2026/4/18 4:17:00 网站建设 项目流程
来宾网站建设公司,做网站管理系统,南宁网站建公司吗,昆明网站建设哪家比较好文章目录 1 AI框架 1.1 Spring AI 简介1.2 Spring AI 使用 1.2.1 pom.xml1.2.2 可实现的功能 1.3 Spring Cloud Alibaba AI1.4 Spring Cloud Alibaba AI 实践操作 1.4.1 pom.xml1.4.2 配置文件1.4.3 对接文本模型1.4.4 文生图模型1.4.5 语音合成模型 1 AI框架 1.1 Spring …文章目录1 AI框架1.1 Spring AI 简介1.2 Spring AI 使用1.2.1 pom.xml1.2.2 可实现的功能1.3 Spring Cloud Alibaba AI1.4 Spring Cloud Alibaba AI 实践操作1.4.1 pom.xml1.4.2 配置文件1.4.3 对接文本模型1.4.4 文生图模型1.4.5 语音合成模型1 AI框架1.1 Spring AI 简介在软件开发的世界中Java一直是企业级应用的主力军。而Spring框架尤其是Spring Boot以其生态系统的丰富性为开发者提供了无与伦比的便利。现在Spring Boot正迈向一个新的纪元——人工智-能的时代。Spring AI项目的推出不仅标志着Spring生态的进一步扩展也为广大Java开发者开启了一个全新的编程领域。Spring AI是从著名的Python项目LangChain和LlamaIndex中汲取灵感它不是这些项目的直接移植它的成立信念是下一波生成式人工智能应用程序将不仅适用于 Python 开发人员而且将在许多编程语言中无处不在。Spring AI功能可以看Spring推出的官方文档https://spring.io/projects/spring-ai我们可以从Spring AI的官网描述中总结出Spring AI的几个核心的关键词提供抽象能力简化AI应用的开发模型与向量支持AI集成与自动配置Spring AI简化了我们构建大型复杂的AI应用的过程当然如果你的项目仅仅是需要调用一个AI接口那其实直接调用官方SDK反而更方便。Spring AI提供的功能如下SQL类过滤器API 提供类似SQL的元数据过滤器API实现跨供应商的一致性。Spring Boot集成 专为Spring Boot设计的自动配置和启动器让AI集成变得轻而易举。API可移植性支持所有主要的模型提供商如OpenAIMicrosoftAmazonGoogle和Huggingface。支持的模型类型包括聊天和文本到图像。跨 AI 提供商的可移植 API用于聊天和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。将 AI 模型输出映射到 POJO。支持所有主要的向量数据库例如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。跨 Vector Store 提供程序的可移植 API包括新颖的类似 SQL 的元数据过滤器 API该 API 也是可移植的。AI 模型和矢量存储的 Spring Boot stater。用于数据工程的 ETL 框架1.2 Spring AI 使用1.2.1 pom.xml添加Maven存储库: 在项目的pom.xml中添加Spring Milestone和Snapshot存储库。repositories !-- Spring Milestone Repository for milestones -- repository idspring-milestones/id urlhttps://repo.spring.io/milestone/url /repository !-- Spring Snapshot Repository for snapshots -- repository idspring-snapshots/id urlhttps://repo.spring.io/snapshot/url /repository /repositories注意在集成 Spring AI 或其他较新的 Spring 生态系统组件时添加Spring Milestone和Snapshot存储库是为了确保能够访问到最新的开发版本、里程碑版本和快照版本。这些版本可能尚未发布到Maven Central这样的稳定仓库但包含了最新的特性、修复和改进导入Spring AI BOM: 使用Spring AI BOM定义可以确保你使用的是测试过的、兼容的库版本。dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version0.8.1-SNAPSHOT/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement添加AI功能: 根据你的需求添加相关的AI模块依赖项到pom.xml。dependencies !-- 示例添加OpenAI的支持 -- dependency groupIdorg.springframework.experimental.ai/groupId artifactIdspring-ai-openai/artifactId /dependency /dependencies1.2.2 可实现的功能可实现的功能生成式AI利用Spring AI你可以通过简单的API调用实现文本的生成、翻译、摘要等功能。矢量数据库当你需要对文本数据进行语义搜索时Spring AI提供的矢量数据库支持使得相关操作变得简单高-效。AI绘画对于需要将文本转换为图像的应用场景Spring AI的绘画功能可以无缝集成到你的应用中。1.3 Spring Cloud Alibaba AI原始的Spring AI并没有国内相关大模型的接入对国内开发者不太友好。总的来说Spring Cloud Alibaba AI目前基于Spring AI 0.8.1版本API完成通义系列大模型的接入。在当前最新版本中Spring Cloud Alibaba AI主要完成了几种常见生成式模型的适配包括对话、文生图、文生语音等开发者可以使用Spring Cloud Alibaba AI开发基于通义的聊天、图片或语音生成 AI 应用框架还提供 OutParser、Prompt Template、Stuff 等实用能力。Spring Cloud Alibaba AI官方还提供了包括聊天对话、文生图、文生语音等多种应用的开发示例具体可以前往官网查看https://sca.aliyun.com1.4 Spring Cloud Alibaba AI 实践操作首先新建一个Maven项目JDK选的是17版本。1.4.1 pom.xmlMaven文件需要引入spring-cloud-alibaba-dependencies和spring-cloud-starter-alibaba-ai两个依赖。dependencyManagement dependencies dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-alibaba-dependencies/artifactId version2023.0.1.0/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement dependencies dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-ai/artifactId /dependency /dependencies1.4.2 配置文件配置阿里云通义千问的Api-Key没有的读者可以从官网上申请。server: port: 8080 spring: application: name: alibaba-spring-ai-demo cloud: ai: tongyi: api-key: 你的api-key主要配置阿里云申请的api-keyhttps://dashscope.console.aliyun.com/model1.4.3 对接文本模型我们首先测试如何对接文本大模型。新建一个控制器类新建/simple接口用来测试基本QA。RestController RequestMapping(/ai) CrossOrigin public class TongYiController { Autowired Qualifier(tongYiSimpleServiceImpl) private TongYiService tongYiSimpleService; GetMapping(/simple) public String completion( RequestParam(value message, defaultValue AI时代下Java开发者该何去何从) String message ) { return tongYiSimpleService.completion(message); } }新建一个TongyiService服务类public interface TongYiService { /** * 基本问答 */ String completion(String message); /** * 文生图 */ ImageResponse genImg(String imgPrompt); /** * 语音合成 */ String genAudio(String text); }具体的实现类如下由Spring AI自动注入ChatClient、StreamingChatClientChatClient屏蔽底层通义大模型交互细节后者用于流式调用。对于QA而言仅仅通过client.call(prompt)一行代码就可以完成对模型的调用。Service Slf4j public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl { /** * 自动注入ChatClient、StreamingChatClient屏蔽模型调用细节 */ private final ChatClient chatClient; private final StreamingChatClient streamingChatClient; Autowired public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) { this.chatClient chatClient; this.streamingChatClient streamingChatClient; } /** * 具体实现 */ Override public String completion(String message) { Prompt prompt new Prompt(new UserMessage(message)); return chatClient.call(prompt).getResult().getOutput().getContent(); } }我们发送一个请求prompt是AI时代下Java开发者该何去何从测试结果如下1.4.4 文生图模型这里只给出service的代码其它代码同上面的文本问答。可以看到只需要实例化一个imagePrompt再调用模型即可。Slf4j Service public class TongYiImagesServiceImpl extends AbstractTongYiServiceImpl { private static final Logger logger LoggerFactory.getLogger(TongYiService.class); private final ImageClient imageClient; Autowired public TongYiImagesServiceImpl(ImageClient client) { this.imageClient client; } Override public ImageResponse genImg(String imgPrompt) { var prompt new ImagePrompt(imgPrompt); return imageClient.call(prompt); } }测试的prompt是Painting a boy coding in front of the desk, with his dog.测试结果如下效果还是很不错的1.4.5 语音合成模型Slf4j Service public class TongYiAudioSimpleServiceImpl extends AbstractTongYiServiceImpl { private static final Logger logger LoggerFactory.getLogger(TongYiService.class); private final SpeechClient speechClient; Autowired public TongYiAudioSimpleServiceImpl(SpeechClient client) { this.speechClient client; } Override public String genAudio(String text) { logger.info(gen audio prompt is: {}, text); var resWAV speechClient.call(text); // save的代码省略就是将音频保存到本地而已 return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue()); } }测试结果也是成功的

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询