欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

柚子快報(bào)激活碼778899分享:開(kāi)發(fā)語(yǔ)言 Java接入文心一言

柚子快報(bào)激活碼778899分享:開(kāi)發(fā)語(yǔ)言 Java接入文心一言

http://yzkb.51969.com/

文章目錄

文心一言應(yīng)用創(chuàng)建接口對(duì)接接口文檔代碼示例依賴

常量類實(shí)體類

結(jié)束語(yǔ)

文心一言應(yīng)用創(chuàng)建

首先需要先申請(qǐng)文心千帆大模型,申請(qǐng)地址:文心一言 (baidu.com),點(diǎn)擊加入體驗(yàn),等通過(guò)審核之后就可以進(jìn)入文心千帆大模型后臺(tái)進(jìn)行應(yīng)用管理了。

在百度智能云首頁(yè)即可看到文心千帆大模型平臺(tái)

然后進(jìn)入后臺(tái)管理之后,點(diǎn)擊應(yīng)用接入,創(chuàng)建應(yīng)用即可

創(chuàng)建完應(yīng)應(yīng)用之后,便可以調(diào)用文心一言的http開(kāi)發(fā)接口進(jìn)行交互了。

接口對(duì)接

接口文檔

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

這里我用的是ERNIE-Bot-turbo API,主要是由于它響應(yīng)更快。

下面介紹一下具體接入的代碼

代碼示例

依賴

依賴安裝

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">

baidu

com.walter

1.0

4.0.0

1.0

baidumodel

百度大模型

public

aliyun nexus

http://maven.aliyun.com/nexus/content/groups/public/

true

false

com.fasterxml.jackson.core

jackson-databind

2.11.1

cn.hutool

hutool-http

5.8.11

junit

junit

test

cn.hutool

hutool-json

5.8.11

org.projectlombok

lombok

1.18.12

com.squareup.okhttp3

okhttp-sse

3.14.9

org.slf4j

slf4j-log4j12

1.7.30

org.slf4j

slf4j-api

1.7.30

常量類

ApiConstant.java

@Slf4j

public class ApiConstant {

/**

* ERNIE_BOT_TURBO 發(fā)起會(huì)話接口

*/

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();

}

}

實(shí)體類

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;

@Data

public 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 List 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

// 這個(gè)類主要是為了與文心一言API建立流式連接,實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)返回,而不是等完整的數(shù)據(jù)生成之后才將數(shù)據(jù)返回

// 可以減少用戶等待時(shí)間,實(shí)現(xiàn)更好的交互體驗(yàn)

@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返回?cái)?shù)據(jù):{}", data);

}

@Override

public void onClosed(EventSource eventSource) {

log.info("baidu關(guān)閉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

// 該類主要是處理接口請(qǐng)求,處理接口響應(yīng)邏輯

@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();

}

// 該方法是同步請(qǐng)求API,會(huì)等大模型將數(shù)據(jù)完全生成之后,返回響應(yīng)結(jié)果,可能需要等待較長(zhǎng)時(shí)間,視生成文本長(zhǎng)度而定

public ErnieBotTurboResponse ernieBotTurbo(ErnieBotTurboStreamParam param) {

if (param == null) {

log.error("參數(shù)異常:param不能為空");

throw new RuntimeException("參數(shù)異常: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);

}

// 該方法是通過(guò)流的方式請(qǐng)求API,大模型每生成一些字符,就會(huì)通過(guò)流的方式相應(yīng)給客戶端,

// 我們是在 BaiduEventSourceListener.java 的 onEvent 方法中獲取大模型響應(yīng)的數(shù)據(jù),其中data就是具體的數(shù)據(jù),

// 我們獲取到數(shù)據(jù)之后,就可以通過(guò) SSE/webscocket 的方式實(shí)時(shí)相應(yīng)給前端頁(yè)面展示

public void ernieBotTurboStream(ErnieBotTurboStreamParam param, EventSourceListener eventSourceListener) {

if (Objects.isNull(eventSourceListener)) {

log.error("參數(shù)異常:EventSourceListener不能為空");

throw new RuntimeException("參數(shù)異常:EventSourceListener不能為空");

}

if (param == null) {

log.error("參數(shù)異常:param不能為空");

throw new RuntimeException("參數(shù)異常: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();

//創(chuàng)建事件

EventSource eventSource = factory.newEventSource(request, eventSourceListener);

} catch (JsonProcessingException e) {

log.error("請(qǐng)求參數(shù)解析是失敗!", e);

throw new RuntimeException("請(qǐng)求參數(shù)解析是失??!", e);

}

}

}

結(jié)束語(yǔ)

以上就是通過(guò)文心一言的OpenAPI與大模型交互的整體邏輯,等代碼功能再做完善之后,改代碼會(huì)以SDK的方式開(kāi)源到Gitee,歡迎一起探討。

柚子快報(bào)激活碼778899分享:開(kāi)發(fā)語(yǔ)言 Java接入文心一言

http://yzkb.51969.com/

好文推薦

評(píng)論可見(jiàn),查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。

轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/18136590.html

發(fā)布評(píng)論

您暫未設(shè)置收款碼

請(qǐng)?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問(wèn)

文章目錄