原文链接
前言
前段时间由于种种原因我的AI BOT https://chat.jylt.top网站停运了数天,后来申请了百度的文心一言和阿里的通义千问开放接口,文心一言的接口很快就通过了,但是文心一言至今杳无音讯。文心一言通过审之后,很快将AI BOT的AI能力接入了文心一言,这里记录一下具体的接入方案。欢迎前往网站体验。
温馨大模型SDK现已开源:https://gitee.com/gitwcx/baidu-model-java-sdk
文心一言应用创建
首先需要先申请文心千帆大模型,申请地址:文心一言 (baidu.com),点击加入体验,等通过审核之后就可以进入文心千帆大模型后台进行应用管理了。
在百度智能云首页即可看到文心千帆大模型平台
然后进入后台管理之后,点击应用接入,创建应用即可
创建完应应用之后,便可以调用文心一言的http开发接口进行交互了。
接口对接
接口文档
首先要看一下接口文档:API调用指南 - 文心千帆WENXINWORKSHOP | 百度智能云文档 (baidu.com)
这里我用的是ERNIE-Bot-turbo API,主要是由于它响应更快。
下面介绍一下具体接入的代码
代码示例
依赖
依赖安装<?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@Slf4jpublic 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@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,欢迎一起探讨