探索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;
    @Autowired
    public 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.查看效果

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

散一世繁华,颠半世琉璃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值