返回顶部
首页 > 资讯 > 操作系统 >百度文心一言接入教程-Java版
  • 377
分享到

百度文心一言接入教程-Java版

百度文心一言java 2023-08-30 15:08:58 377人浏览 独家记忆
摘要

原文链接 前言 前段时间由于种种原因我的AI BOT网站停运了数天,后来申请了百度的文心一言和阿里的通义千问开放接口,文心一言的接口很快就通过了,但是文心一言至今杳无音讯。文心一言通过审之后,很快将ai BOT的AI能力接入了文心一言,这里

原文链接

前言

前段时间由于种种原因我的AI BOT网站停运了数天,后来申请了百度的文心一言和阿里的通义千问开放接口,文心一言的接口很快就通过了,但是文心一言至今杳无音讯。文心一言通过审之后,很快将ai BOT的AI能力接入了文心一言,这里记录一下具体的接入方案。

文心一言应用创建

首先需要先申请文心千帆大模型,申请地址:文心一言 (baidu.com),点击加入体验,等通过审核之后就可以进入文心千帆大模型后台进行应用管理了。https://oss.jylt.cc/img/content/32ef85ebfc3b3eb8ef83903ee3757cc0/fa3c3e55-07d1-48cc-9d36-65e90140db95.png

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

https://oss.jylt.cc/img/content/32ef85ebfc3b3eb8ef83903ee3757cc0/8353247f-7e02-4317-ab1b-2122fcb957fa.png

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

https://oss.jylt.cc/img/content/32ef85ebfc3b3eb8ef83903ee3757cc0/05d7b488-6683-42bb-ab93-872b652b9aa8.png

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

接口对接

接口文档

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

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

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

代码示例

依赖

  • 依赖安装
<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>baiduartifactId>        <groupId>com.waltergroupId>        <version>1.0version>    parent>    <modelVersion>4.0.0modelVersion>    <version>1.0version>    <artifactId>baidumodelartifactId>    <description>百度大模型description>    <repositories>        <repository>            <id>publicid>            <name>aliyun nexusname>            <url>http://maven.aliyun.com/nexus/content/groups/public/url>            <releases>                <enabled>trueenabled>            releases>            <snapshots>                <enabled>falseenabled>            snapshots>        repository>    repositories>    <dependencies>        <dependency>            <groupId>com.fasterxml.jackson.coregroupId>            <artifactId>jackson-databindartifactId>    <version>2.11.1version>        dependency>        <dependency>            <groupId>cn.hutoolgroupId>            <artifactId>hutool-httpartifactId>            <version>5.8.11version>        dependency>        <dependency>            <groupId>junitgroupId>            <artifactId>junitartifactId>            <scope>testscope>        dependency>        <dependency>            <groupId>cn.hutoolgroupId>            <artifactId>hutool-JSONartifactId>            <version>5.8.11version>        dependency>        <dependency>            <groupId>org.projectlombokgroupId>            <artifactId>lombokartifactId>    <version>1.18.12version>        dependency>        <dependency>            <groupId>com.squareup.okhttp3groupId>            <artifactId>okhttp-sseartifactId>    <version>3.14.9version>        dependency>        <dependency>            <groupId>org.slf4jgroupId>            <artifactId>slf4j-log4j12artifactId>    <version>1.7.30version>        dependency>        <dependency>            <groupId>org.slf4jgroupId>            <artifactId>slf4j-apiartifactId>            <version>1.7.30version>        dependency>    dependencies>project>

常量类

  • ApiConstant.java
@Slf4jpublic class ApiConstant {        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
@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic class BaiduChatMessage implements Serializable {    private String role;    private String content;}
  • ErnieBotTurboResponse.java
@Datapublic 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
@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic 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
@Datapublic class Token implements Serializable {    private String access_token;    private Integer expires_in;    private String error;    private String error_description;}
  • BaiduEventSourceListener.java
// 这个类主要是为了与文心一言API建立流式连接,实现数据的实时返回,而不是等完整的数据生成之后才将数据返回// 可以减少用户等待时间,实现更好的交互体验@Slf4jpublic 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
// 该类主要是处理接口请求,处理接口响应逻辑@Slf4j@Datapublic 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,欢迎一起探讨

来源地址:https://blog.csdn.net/wuchenxiwalter/article/details/131989886

--结束END--

本文标题: 百度文心一言接入教程-Java版

本文链接: https://lsjlt.com/news/383080.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作