Java接入文心一言

文章目录

文心一言应用创建

首先需要先申请文心千帆大模型,申请地址:文心一言 (baidu.com),点击加入体验,等通过审核之后就可以进入文心千帆大模型后台进行应用管理了。

在百度智能云首页即可看到文心千帆大模型平台

然后进入后台管理之后,点击应用接入,创建应用即可

创建完应应用之后,便可以调用文心一言的http开发接口进行交互了。

接口对接

接口文档

首先要看一下接口文档:API调用指南 - 文心千帆WENXINWORKSHOP | 百度智能云文档 (baidu.com)

这里我用的是ERNIE-Bot-turbo API,主要是由于它响应更快。

下面介绍一下具体接入的代码

代码示例

依赖

依赖安装

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">
    <parent>
        <artifactId>baidu</artifactId>
        <groupId>com.walter</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <version>1.0</version>
    <artifactId>baidumodel</artifactId>
    <description>百度大模型</description>

    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
	    <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-http</artifactId>
            <version>5.8.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-json</artifactId>
            <version>5.8.11</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
	    <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-sse</artifactId>
	    <version>3.14.9</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
	    <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
    </dependencies>
</project>

常量类

ApiConstant.java

java 复制代码
@Slf4j
public class ApiConstant {

    /**
     * ERNIE_BOT_TURBO 发起会话接口
     */
    public static final String ERNIE_BOT_TURBO_INSTANT = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=";

    public static String getToken(String appKey, String secretKey) {
        String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + appKey + "&client_secret=" + secretKey;
        String s = HttpUtil.get(url);
        Token bean = JSONUtil.toBean(s, Token.class);
        return bean.getAccess_token();
    }
}

实体类

BaiduChatMessage.java

java 复制代码
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BaiduChatMessage implements Serializable {
    private String role;
    private String content;
}

ErnieBotTurboResponse.java

java 复制代码
@Data
public class ErnieBotTurboResponse implements Serializable {
    private String id;
    private String object;
    private Integer created;

    private String sentence_id;
    private Boolean is_end;
    private Boolean is_truncated;
    private String result;
    private Boolean need_clear_history;

    private Usage usage;

    @Data
    public static class Usage implements Serializable {
        private Integer prompt_tokens;
        private Integer completion_tokens;
        private Integer total_tokens;

    }
}

ErnieBotTurboStreamParam.java

java 复制代码
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ErnieBotTurboStreamParam implements Serializable {

    private List<BaiduChatMessage> messages;
    private Boolean stream;
    private String user_id;

    public boolean isStream() {
        return Objects.equals(this.stream, true);
    }
}

Token.java

java 复制代码
@Data
public class Token implements Serializable {
    private String access_token;
    private Integer expires_in;
    private String error;
    private String error_description;
}

BaiduEventSourceListener.java

java 复制代码
// 这个类主要是为了与文心一言API建立流式连接,实现数据的实时返回,而不是等完整的数据生成之后才将数据返回
// 可以减少用户等待时间,实现更好的交互体验
@Slf4j
public class BaiduEventSourceListener extends EventSourceListener {

    @Override
    public void onOpen(EventSource eventSource, Response response) {
        log.info("baidu建立sse连接...");
    }

    @Override
    public void onEvent(EventSource eventSource, String id, String type, String data) {
        log.info("baidu返回数据:{}", data);
    }

    @Override
    public void onClosed(EventSource eventSource) {
        log.info("baidu关闭sse连接...");
    }

    @SneakyThrows
    @Override
    public void onFailure(EventSource eventSource, Throwable t, Response response) {
        if(Objects.isNull(response)){
            log.error("baidu  sse连接异常:{}", t);
            eventSource.cancel();
            return;
        }
        ResponseBody body = response.body();
        if (Objects.nonNull(body)) {
            log.error("baidu  sse连接异常data:{},异常:{}", body.string(), t);
        } else {
            log.error("baidu  sse连接异常data:{},异常:{}", response, t);
        }
        eventSource.cancel();
    }
}

BaiduService.java

java 复制代码
// 该类主要是处理接口请求,处理接口响应逻辑
@Slf4j
@Data
public class BaiduService {

    private static final long TIME_OUT = 30;

    private OkHttpClient okHttpClient;
    private String appKey;
    private String secretKey;

    public BaiduService(@NonNull String appKey, @NonNull String secretKey) {
        this.appKey = appKey;
        this.secretKey = secretKey;
        this.okHttpClient(30, 30, 30, null);
    }

    public BaiduService(@NonNull String appKey, @NonNull String secretKey, long connectTimeout, long writeTimeout, long readTimeout, Proxy proxy) {
        this.appKey = appKey;
        this.secretKey = secretKey;
        this.okHttpClient(connectTimeout, writeTimeout, readTimeout, proxy);
    }


    private void okHttpClient(long connectTimeout, long writeTimeout, long readTimeout, Proxy proxy) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.connectTimeout(connectTimeout, TimeUnit.SECONDS);
        client.writeTimeout(writeTimeout, TimeUnit.SECONDS);
        client.readTimeout(readTimeout, TimeUnit.SECONDS);
        if (Objects.nonNull(proxy)) {
            client.proxy(proxy);
        }
        this.okHttpClient = client.build();
    }

    // 该方法是同步请求API,会等大模型将数据完全生成之后,返回响应结果,可能需要等待较长时间,视生成文本长度而定
    public ErnieBotTurboResponse ernieBotTurbo(ErnieBotTurboStreamParam param) {
        if (param == null) {
            log.error("参数异常:param不能为空");
            throw new RuntimeException("参数异常:param不能为空");
        }
        if (param.isStream()) {
            param.setStream(false);
        }
        String post = HttpUtil.post(ApiConstant.ERNIE_BOT_TURBO_INSTANT + ApiConstant.getToken(appKey, secretKey), JSONUtil.toJsonStr(param));
        return JSONUtil.toBean(post, ErnieBotTurboResponse.class);
    }

    // 该方法是通过流的方式请求API,大模型每生成一些字符,就会通过流的方式相应给客户端,
    // 我们是在 BaiduEventSourceListener.java 的 onEvent 方法中获取大模型响应的数据,其中data就是具体的数据,
    // 我们获取到数据之后,就可以通过 SSE/webscocket 的方式实时相应给前端页面展示
    public void ernieBotTurboStream(ErnieBotTurboStreamParam param, EventSourceListener eventSourceListener) {
        if (Objects.isNull(eventSourceListener)) {
            log.error("参数异常:EventSourceListener不能为空");
            throw new RuntimeException("参数异常:EventSourceListener不能为空");
        }
        if (param == null) {
            log.error("参数异常:param不能为空");
            throw new RuntimeException("参数异常:param不能为空");
        }
        if (!param.isStream()) {
            param.setStream(true);
        }
        try {
            EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
            ObjectMapper mapper = new ObjectMapper();
            String requestBody = mapper.writeValueAsString(param);
            Request request = new Request.Builder()
                    .url(ApiConstant.ERNIE_BOT_TURBO_INSTANT + ApiConstant.getToken(appKey, secretKey))
                    .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
                    .build();
            //创建事件
            EventSource eventSource = factory.newEventSource(request, eventSourceListener);
        } catch (JsonProcessingException e) {
            log.error("请求参数解析是失败!", e);
            throw new RuntimeException("请求参数解析是失败!", e);
        }

    }
}

结束语

以上就是通过文心一言的OpenAPI与大模型交互的整体逻辑,等代码功能再做完善之后,改代码会以SDK的方式开源到Gitee,欢迎一起探讨。

相关推荐
Amarantine、沐风倩✨15 分钟前
设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)
java·物联网·音视频·webrtc·html5·视频编解码·七牛云存储
Kisorge41 分钟前
【C语言】指针数组、数组指针、函数指针、指针函数、函数指针数组、回调函数
c语言·开发语言
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
轻口味2 小时前
命名空间与模块化概述
开发语言·前端·javascript
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
晓纪同学3 小时前
QT-简单视觉框架代码
开发语言·qt
威桑3 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
Hello.Reader3 小时前
深入解析 Apache APISIX
java·apache
飞飞-躺着更舒服3 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt