商业网站建设平台,室内装修设计软件推荐,办公室装修风格效果图,法律咨询网站开发原文链接
前言
前段时间由于种种原因我的AI BOT网站停运了数天#xff0c;后来申请了百度的文心一言和阿里的通义千问开放接口#xff0c;文心一言的接口很快就通过了#xff0c;但是文心一言至今杳无音讯。文心一言通过审之后#xff0c;很快将AI BOT的AI能力接入了文心…原文链接
前言
前段时间由于种种原因我的AI BOT网站停运了数天后来申请了百度的文心一言和阿里的通义千问开放接口文心一言的接口很快就通过了但是文心一言至今杳无音讯。文心一言通过审之后很快将AI BOT的AI能力接入了文心一言这里记录一下具体的接入方案。
文心一言应用创建
首先需要先申请文心千帆大模型申请地址文心一言 (baidu.com)点击加入体验等通过审核之后就可以进入文心千帆大模型后台进行应用管理了。
在百度智能云首页即可看到文心千帆大模型平台 然后进入后台管理之后点击应用接入创建应用即可 创建完应应用之后便可以调用文心一言的http开发接口进行交互了。
接口对接
接口文档
首先要看一下接口文档API调用指南 - 文心千帆WENXINWORKSHOP | 百度智能云文档 (baidu.com)
这里我用的是ERNIE-Bot-turbo API主要是由于它响应更快。
下面介绍一下具体接入的代码
代码示例
依赖
依赖安装
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdbaidu/artifactIdgroupIdcom.walter/groupIdversion1.0/version/parentmodelVersion4.0.0/modelVersionversion1.0/versionartifactIdbaidumodel/artifactIddescription百度大模型/descriptionrepositoriesrepositoryidpublic/idnamealiyun nexus/nameurlhttp://maven.aliyun.com/nexus/content/groups/public//urlreleasesenabledtrue/enabled/releasessnapshotsenabledfalse/enabled/snapshots/repository/repositoriesdependenciesdependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.11.1/version/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-http/artifactIdversion5.8.11/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdscopetest/scope/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-json/artifactIdversion5.8.11/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.12/version/dependencydependencygroupIdcom.squareup.okhttp3/groupIdartifactIdokhttp-sse/artifactIdversion3.14.9/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.30/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-api/artifactIdversion1.7.30/version/dependency/dependencies
/project
常量类
ApiConstant.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_typeclient_credentialsclient_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
AllArgsConstructor
public class BaiduChatMessage implements Serializable {private String role;private String content;
}ErnieBotTurboResponse.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;Datapublic static class Usage implements Serializable {private Integer prompt_tokens;private Integer completion_tokens;private Integer total_tokens;}
}ErnieBotTurboStreamParam.java
Data
Builder
NoArgsConstructor
AllArgsConstructor
public class ErnieBotTurboStreamParam implements Serializable {private ListBaiduChatMessage messages;private Boolean stream;private String user_id;public boolean isStream() {return Objects.equals(this.stream, true);}
}
Token.java
Data
public class Token implements Serializable {private String access_token;private Integer expires_in;private String error;private String error_description;
}BaiduEventSourceListener.java
// 这个类主要是为了与文心一言API建立流式连接实现数据的实时返回而不是等完整的数据生成之后才将数据返回
// 可以减少用户等待时间实现更好的交互体验
Slf4j
public class BaiduEventSourceListener extends EventSourceListener {Overridepublic void onOpen(EventSource eventSource, Response response) {log.info(baidu建立sse连接...);}Overridepublic void onEvent(EventSource eventSource, String id, String type, String data) {log.info(baidu返回数据{}, data);}Overridepublic void onClosed(EventSource eventSource) {log.info(baidu关闭sse连接...);}SneakyThrowsOverridepublic 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
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欢迎一起探讨