欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 探索AI创新的前沿——从零开始学习和运用SpringAI

探索AI创新的前沿——从零开始学习和运用SpringAI

2024/10/24 5:20:59 来源:https://blog.csdn.net/qq_51447436/article/details/139725212  浏览:    关键词:探索AI创新的前沿——从零开始学习和运用SpringAI

1.SpringAI介绍

SpringAI是AI工程师的一个应用框架,它提供了一个友好的API和开发AI应用的抽象,旨在简化AI应用的开发工序。
目标是将可移植性和模块化设计等设计原则应用于AI领域的Spring生态系统,并将POJO作为应用程序的构建块推广到AI领域。
跨AI提供商的便携API支持聊天、文本到图像和嵌入模型。同时支持同步和流API选项。还支持各种定制的功能。
总的来说就是:Spring出了一个AI框架,帮助我们快速调用AI,从而实现各种功能场景。SpringAI官网链接
在这里插入图片描述

2.创建SpringBoot项目

  1. 打开IDEA新建一个项目或者模块;

  2. 在新出的弹框中选择Spring Boot;这里需要注意类型选择Maven、JDK需要选择17版本,打包方式选择jar
    在这里插入图片描述

  3. 选择需要添加的依赖项目,分别是SpringWeb和OpneAI;
    在这里插入图片描述

  4. 最后点击创建即可。

3.修改仓库地址

项目创建完毕后,如果你的项目仓库地址是阿里云镜像,则有可能导致openai依赖无法正常下载,因此需要修改pom.xml中的仓库地址。具体如下所示:

<repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>

4.修改配置文件

spring.application.name=SpringAI-demo# 添加openai配置文件
spring.ai.openai.api-key=
spring.ai.openai.base-url=
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7

这些配置项是用于Spring AI框架与OpenAI API集成的配置参数。以下是对每个配置项的详细解释:

1. spring.ai.openai.api-key

  • 解释:这个配置项用于指定与OpenAI API通信的API密钥。API密钥是一个私密的字符串,OpenAI使用它来识别和授权您的应用程序。
  • 示例
    spring.ai.openai.api-key=sk-XXXXXXXXXXXXXX
    

2. spring.ai.openai.base-url

  • 解释:这个配置项用于指定OpenAI API的基础URL。通常,您可以使用默认的OpenAI URL,但在某些情况下,您可能需要指定一个自定义URL(例如,使用代理或自托管的OpenAI实例)。
  • 示例
    spring.ai.openai.base-url=https://api.openai.com/v1
    

3. spring.ai.openai.chat.options.model

  • 解释:这个配置项用于指定要使用的OpenAI模型的名称。OpenAI提供了多种模型,每种模型具有不同的性能和用途。例如,gpt-3.5-turbo是一个性能良好且性价比高的模型,适合大多数对话和生成任务。
  • 示例
    spring.ai.openai.chat.options.model=gpt-3.5-turbo
    

4. spring.ai.openai.chat.options.temperature

  • 解释:这个配置项用于设置生成文本时的“温度”参数。温度控制生成文本的随机性。较高的温度值(如0.9)会使输出更随机和多样化,而较低的温度值(如0.2)会使输出更确定和集中。0.7是一个常用的平衡值。
  • 示例
    spring.ai.openai.chat.options.temperature=0.7
    

5.编写控制层代码

1.Chat Client AI

package com.xing.springaidemo.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ChatController {private final ChatClient chatClient;public ChatController(ChatClient.Builder chatClientBuilder) {this.chatClient = chatClientBuilder.build();}@GetMapping("/hello")public String hello() {return "Hello SpringAI";}@GetMapping("/ai")String generation(String userInput) {return this.chatClient.prompt().user(userInput).call().content();}
}

2.Chat Model AI

package com.xing.springaidemo.controller;import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
public class ChatController {private final OpenAiChatModel model;@Autowiredpublic ChatController(OpenAiChatModel model) {this.model = model;}@GetMapping("/hello")public String hello() {return "Hello SpringAI";}@GetMapping("/ai/generate")public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return Map.of("generation", model.call(message));}
}

6.查看效果

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com