Java毕设课设接入AI 百度千帆AppBuilder 最小依赖、最少代码

Java毕设课设接入AI 百度千帆AppBuilder 最小依赖、最少代码

  • 我不允许你的毕设还不会用AI!

百度千帆AppBuilder,拥有多轮对话功能、最简单的请求接口,我将源码全部放在下面。

注册千帆账号,获取APPID和密钥

网址:https://qianfan.cloud.baidu.com/ 先注册百度账号
在这里插入图片描述
创建一个属于你的智能Ai机器人
在这里插入图片描述
创建后在我的个人空间有显示,记住这个应用ID! 也就是APP_ID
在这里插入图片描述
然后,在密钥管理里面再新增一个密钥
在这里插入图片描述

就可以拿着APP_ID和密钥,开始创建Maven项目测试了!

创建Maven项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codeying</groupId>
    <artifactId>baiduAi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <!--springboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--文心一言-->
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.5.RELEASE</version>
            </plugin>
        </plugins>
    </build>


</project>

消息体实体类

package com.codeying.baiduapp;

import lombok.Data;

/**
 * 请求体  用于请求百度千帆
 */
@Data
public class BaiduAppRequest {

    private String app_id;

    private String query;

    private boolean stream;

    private String conversation_id;

    public BaiduAppRequest(String app_id, String query, String conversation_id) {
        this.app_id = app_id;
        this.query = query;
        this.conversation_id = conversation_id;
    }
}

package com.codeying.baiduapp;

import lombok.Data;

import java.util.List;


/**
 * 响应体 接收消息用
 */
@Data
public class BaiduAppResponse {

    private String request_id;

    private String date;

    private String answer;

    private String conversation_id;

    private String message_id;

    private Boolean is_completion;

    private List<Object> content;

}

请求发送测试、和main方法测试

package com.codeying.baiduapp;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 百度千帆AppBuilder
 * 这个不需要自己存储对话记录,只需要传入对话ID,百度云端自动存储之前的对话记录,非常方便
 */
@Slf4j
public class BaiduApp{

    private String APP_KEY;
    private String APP_ID;

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder()
            .callTimeout(160, TimeUnit.SECONDS) // 设置调用超时时间
            .readTimeout(160, TimeUnit.SECONDS) // 设置读取超时时间
            .writeTimeout(160, TimeUnit.SECONDS) // 设置写超时时间
            .build();
    static final ObjectMapper objectMapper = new ObjectMapper();

    //本次对话ID
    private String conversation_id;

    //测试
    public static void main(String []args) throws Exception {
        BaiduApp app = new BaiduApp();
        app.setAPP_ID("你的APPID");
        app.setAPP_KEY("你创建的密钥");
        app.qaq("帮我写电商系统的绪论50字");
        app.qaq("还有意义50字");
    }

    /**
     * 提问且获取消息
     */
    public BaiduAppResponse qaq(String question)  throws Exception {
        //没有创建回话则创建。
        if(conversation_id == null || "".equals(conversation_id)){
            createConversation();
        }
        //提问
        log.info("提问千帆App:"+question);
        MediaType mediaType = MediaType.parse("application/json");
        //消息体
        String json = objectMapper.writeValueAsString(new BaiduAppRequest(APP_ID,question,conversation_id));
        RequestBody body = RequestBody.create(mediaType, json);
        //请求
        Request request = new Request.Builder()
                .url("https://qianfan.baidubce.com/v2/app/conversation/runs")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Appbuilder-Authorization", "Bearer "+APP_KEY)
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        //获取消息
        String res = response.body().string();
        BaiduAppResponse baiduAppResponse = objectMapper.readValue(res,BaiduAppResponse.class);
        log.info("千帆App回复:"+baiduAppResponse.getAnswer());
        return baiduAppResponse;
    }

    /**
     * 创建和百度千帆AppBuilder的对话
     */
    private void createConversation() throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"app_id\":\""+APP_ID+"\"}");
        Request request = new Request.Builder()
                .url("https://qianfan.baidubce.com/v2/app/conversation")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Appbuilder-Authorization", "Bearer "+APP_KEY)
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String res = response.body().string();
        log.info("百度千帆AppBuilder 创建对话响应:"+res);
        conversation_id = objectMapper.readTree(res).get("conversation_id").asText();
    }

    public void setAPP_KEY(String APP_KEY) {
        this.APP_KEY = APP_KEY;
    }

    public void setAPP_ID(String APP_ID) {
        this.APP_ID = APP_ID;
    }

}

在上面的例子中,我用到的测试为:

 //测试
    public static void main(String []args) throws Exception {
        BaiduApp app = new BaiduApp();
       app.setAPP_ID("你的APPID");
        app.setAPP_KEY("你创建的密钥");
        app.qaq("帮我写电商系统的绪论50字");
        app.qaq("还有意义50字");
    }

测试结果

在这里插入图片描述
毕设课设,就找学长敲代码!!!👇👇👇👇👇点我小卡片添加vx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学长敲代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值