SpringBoot实现简单AI问答(百度千帆)

第一步:注册并登录百度智能云,创建应用并获取自己的APIKey与SecretKey,参考网址:

点击去百度智能云
在这里插入图片描述

第二步:引入千帆的pom依赖

		<dependency>
            <groupId>com.baidubce</groupId>
            <artifactId>qianfan</artifactId>
            <version>0.0.9</version>
        </dependency>

第三步:创建前端需要的controller

import com.baidubce.qianfan.core.auth.Auth;
import com.lx.vue.common.resp.ResultData;
import com.lx.vue.common.resp.ReturnCodeEnum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.builder.ChatBuilder;
import com.baidubce.qianfan.model.chat.ChatResponse;

@RestController
public class QianFanController {

    private static final String APIKey = "你的APIKey";
    private static final String SecretKey = "你的SecretKey";

    private static Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH,APIKey, SecretKey);


    @PostMapping("/ai/sendMsg")
    public ResultData sendMsg(@RequestBody String problem) {
        String result = null;
        try {
            result = chat(problem);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(ReturnCodeEnum.RC500.getCode(),"服务暂不可用",null);
        }
        return new ResultData(ReturnCodeEnum.RC200.getCode(),ReturnCodeEnum.RC200.getMessage(),result);
    }

    private static String chat(String problem) {
        ChatBuilder bulder = qianfan.chatCompletion()
                .model("ERNIE-Speed-8K");//你要使用的大模型款式,最好和我一样,其他的很有可能是收费的
            bulder.addMessage("user",problem);//你的问题  
        ChatResponse response = bulder.execute();
        return response.getResult();
    }
}

第四步:前端进行调用,并动态将自己的问题与AI的回答填入Vue页面

在这里插入图片描述

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中使用百度AI的车牌识别功能,可以通过调用百度AI提供的RESTful API来实现。下面是一个示例代码,可以帮助你完成在Spring Boot项目中调用百度AI车牌识别的功能: 1. 引入依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.15.3</version> </dependency> ``` 2. 创建配置类 在Spring Boot项目中创建一个配置类,用于初始化百度AI的相关配置信息,如API Key、Secret Key等。代码如下: ```java @Configuration public class BaiduAIConfig { @Value("${baidu.ai.app-id}") private String appId; @Value("${baidu.ai.api-key}") private String apiKey; @Value("${baidu.ai.secret-key}") private String secretKey; @Bean public AipOcr aipOcr() { // 初始化AipOcr AipOcr aipOcr = new AipOcr(appId, apiKey, secretKey); // 设置连接超时时间和读取超时时间 aipOcr.setConnectionTimeoutInMillis(2000); aipOcr.setSocketTimeoutInMillis(60000); return aipOcr; } } ``` 在上面的代码中,我们使用了@Configuration注解来将该类声明为Spring的配置类,使用@Value注解来注入配置文件中的API Key、Secret Key等信息。我们创建了一个名为aipOcr的Bean,用于初始化AipOcr对象。使用setConnectionTimeoutInMillis()和setSocketTimeoutInMillis()方法设置了连接超时时间和读取超时时间。 3. 创建Controller 在Spring Boot项目中创建一个Controller,用于接收上传的图片,并调用百度AI的车牌识别API进行识别。代码如下: ```java @RestController @RequestMapping("/car") public class CarPlateController { @Autowired private AipOcr aipOcr; @PostMapping("/plate") public String carPlateRecognition(@RequestParam("image") MultipartFile image) throws Exception { // 车牌识别 byte[] bytes = image.getBytes(); HashMap<String, String> options = new HashMap<>(); JSONObject result = aipOcr.licensePlate(bytes, options); if (result != null && result.has("words_result")) { JSONObject wordsResult = result.getJSONObject("words_result"); String number = wordsResult.getString("number"); return "车牌号码:" + number; } else { return "识别失败"; } } } ``` 在上面的代码中,我们使用了@RestController注解来声明该类为一个RESTful风格的Controller,使用@Autowired注解来注入AipOcr对象。我们创建了一个名为carPlateRecognition的方法,用于接收上传的图片,并调用百度AI的车牌识别API进行识别。我们使用MultipartFile对象来接收上传的图片,使用licensePlate()方法调用百度AI的车牌识别API,最后解析结果得到车牌号码。 4. 测试 在浏览器或Postman等工具中发送POST请求,请求路径为/car/plate,上传一张包含车牌的图片,即可测试百度AI的车牌识别功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值