接入科大迅飞的语音识别:
private void printResult(RecognizerResult results) {
String text = JsonParser2.parseIatResult(results.getResultString());
String sn = null;
// 读取json结果中的sn字段
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}
mIatResults.put(sn, text);
StringBuffer resultBuffer = new StringBuffer();
for (String key : mIatResults.keySet()) {
resultBuffer.append(mIatResults.get(key));
}
String s = mEditTextMessage.getText().toString();
mEditTextMessage.setText(s + resultBuffer.toString());
mEditTextMessage.setSelection(mEditTextMessage.length());
}
/**
* 听写UI监听器
*/
private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
// 返回结果
public void onResult(RecognizerResult results, boolean isLast) {
Log.i("jiaABC", "results"+results.toString());
printResult(results);
}
// 识别回调错误
public void onError(SpeechError error) {
//showTip(error.getPlainDescription(true));
Log.i("jiaABC", "error"+error.toString());
}
};
//科大迅飞语音识别
@NeedsPermission(Manifest.permission.RECORD_AUDIO)
public void startDialog(){
buffer.setLength(0);
//mEditTextMessage.setText(null);// 清空显示内容
mIatResults.clear();
// 显示听写对话框
// 初始化识别无UI识别对象
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
//mIat = SpeechRecognizer.createRecognizer(HomeActivity.this, mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
RecognizerDialog mIatDialog = new RecognizerDialog(HomeActivity.this, mInitListener);
//mSharedPreferences = getSharedPreferences(PREFER_NAME, Activity.MODE_PRIVATE);
mIatDialog.setListener(mRecognizerDialogListener);
mIatDialog.show();
}private void printResult(RecognizerResult results) {
String text = JsonParser2.parseIatResult(results.getResultString());
String sn = null;
// 读取json结果中的sn字段
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}
mIatResults.put(sn, text);
StringBuffer resultBuffer = new StringBuffer();
for (String key : mIatResults.keySet()) {
resultBuffer.append(mIatResults.get(key));
}
String s = mEditTextMessage.getText().toString();
mEditTextMessage.setText(s + resultBuffer.toString());
mEditTextMessage.setSelection(mEditTextMessage.length());
}
/**
* 听写UI监听器
*/
private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
// 返回结果
public void onResult(RecognizerResult results, boolean isLast) {
Log.i("jiaABC", "results"+results.toString());
printResult(results);
}
// 识别回调错误
public void onError(SpeechError error) {
//showTip(error.getPlainDescription(true));
Log.i("jiaABC", "error"+error.toString());
}
};
//科大迅飞语音识别
@NeedsPermission(Manifest.permission.RECORD_AUDIO)
public void startDialog(){
buffer.setLength(0);
//mEditTextMessage.setText(null);// 清空显示内容
mIatResults.clear();
// 显示听写对话框
// 初始化识别无UI识别对象
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
//mIat = SpeechRecognizer.createRecognizer(HomeActivity.this, mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
RecognizerDialog mIatDialog = new RecognizerDialog(HomeActivity.this, mInitListener);
//mSharedPreferences = getSharedPreferences(PREFER_NAME, Activity.MODE_PRIVATE);
mIatDialog.setListener(mRecognizerDialogListener);
mIatDialog.show();
}
rerofit调用百度的大模型,yi_34b_chat 可以免费接入,玩玩还是不错的,rxjava请求token可以自动刷新token,而且能合并请求一步到位
@Headers({"Content-type:application/json;charset=UTF-8"})
@POST("rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat")
Call<Baidu> getBaidu(@Query("access_token") String access_token, @Body RequestBody route);
//oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]'
@GET("oauth/2.0/token")
Call<BaiduTocken> getBaiduToken(@Query("grant_type") String grant_type, @Query("client_id") String client_id,@Query("client_secret") String client_secret);
@GET("oauth/2.0/token")
Observable<BaiduTocken> getBaiduToken2(@Query("grant_type") String grant_type, @Query("client_id") String client_id,@Query("client_secret") String client_secret);
retrofit初始化固定的,自己照着搞一个就行。
public static ApiService getApiService() {
//星火
//return RetrofitClient.getSingleClient("https://spark-api-open.xf-yun.com/").create(ApiService.class);
//百度
return RetrofitClient.getSingleClient("https://aip.baidubce.com/").create(ApiService.class);
}
public static void getBaidu(String access_token, RequestBody rb, Callback<Baidu> callback) {
getApiService().getBaidu(access_token, rb).enqueue(callback);
}
public static void getBaiduToken(String client_id, String client_secret, Callback<BaiduTocken> callback) {
getApiService().getBaiduToken("client_credentials", client_id, client_secret).enqueue(callback);
}
测试:
getBaidu("token", body, new Callback<Baidu>() {
@Override
public void onResponse(Call<Baidu> call, Response<Baidu> response) {
System.out.println("getGPT--------------Success"+call.request().toString());
System.out.println("getGPT--------------Success"+response.code());
System.out.println("getGPT--------------Success"+response.message());
System.out.println("getGPT--------------Success"+response.raw());
System.out.println("getGPT--------------Success"+response.body());
}
@Override
public void onFailure(Call<Baidu> call, Throwable t) {
System.out.println("getGPT--------------onFailure"+call.request().toString());
System.out.println("getGPT------------onFailure"+t.toString());
}
});
getBaiduToken("API kEY", "Secret key", new Callback<BaiduTocken>() {
@Override
public void onResponse(Call<BaiduTocken> call, Response<BaiduTocken> response) {
System.out.println("getGPT--------------Success"+call.request().toString());
System.out.println("getGPT--------------Success"+response.code());
System.out.println("getGPT--------------Success"+response.message());
System.out.println("getGPT--------------Success"+response.raw());
System.out.println("getGPT--------------Success"+response.body());
}
@Override
public void onFailure(Call<BaiduTocken> call, Throwable t) {
System.out.println("getGPT--------------onFailure"+call.request().toString());
System.out.println("getGPT------------onFailure"+t.toString());
}
});
大模型用到的请求和接收model
public class Msg {
private List<MessagesBean> messages;
public static class MessagesBean {
private String role;
private String content;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "MessagesBean{" +
"role='" + role + '\'' +
", content='" + content + '\'' +
'}';
}
}
public List<MessagesBean> getMessages() {
return messages;
}
public void setMessages(List<MessagesBean> messages) {
this.messages = messages;
}
@Override
public String toString() {
return "Msg{" +
"messages=" + messages +
'}';
}
}
public class Baidu {
private String id;
private String object;
private Integer created;
private String result;
private Boolean is_truncated;
private Boolean need_clear_history;
private UsageBean usage;
public static class UsageBean {
private Integer prompt_tokens;
private Integer completion_tokens;
private Integer total_tokens;
public Integer getPrompt_tokens() {
return prompt_tokens;
}
public void setPrompt_tokens(Integer prompt_tokens) {
this.prompt_tokens = prompt_tokens;
}
public Integer getCompletion_tokens() {
return completion_tokens;
}
public void setCompletion_tokens(Integer completion_tokens) {
this.completion_tokens = completion_tokens;
}
public Integer getTotal_tokens() {
return total_tokens;
}
public void setTotal_tokens(Integer total_tokens) {
this.total_tokens = total_tokens;
}
@Override
public String toString() {
return "UsageBean{" +
"prompt_tokens=" + prompt_tokens +
", completion_tokens=" + completion_tokens +
", total_tokens=" + total_tokens +
'}';
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getObject() {
return object;
}
public void setObject(String object) {
this.object = object;
}
public Integer getCreated() {
return created;
}
public void setCreated(Integer created) {
this.created = created;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public Boolean getIs_truncated() {
return is_truncated;
}
public void setIs_truncated(Boolean is_truncated) {
this.is_truncated = is_truncated;
}
public Boolean getNeed_clear_history() {
return need_clear_history;
}
public void setNeed_clear_history(Boolean need_clear_history) {
this.need_clear_history = need_clear_history;
}
public UsageBean getUsage() {
return usage;
}
public void setUsage(UsageBean usage) {
this.usage = usage;
}
@Override
public String toString() {
return "Baidu{" +
"id='" + id + '\'' +
", object='" + object + '\'' +
", created=" + created +
", result='" + result + '\'' +
", is_truncated=" + is_truncated +
", need_clear_history=" + need_clear_history +
", usage=" + usage +
'}';
}
}
public class BaiduTocken {
private String refresh_token;
private Integer expires_in;
private String session_key;
private String access_token;
private String scope;
private String session_secret;
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public Integer getExpires_in() {
return expires_in;
}
public void setExpires_in(Integer expires_in) {
this.expires_in = expires_in;
}
public String getSession_key() {
return session_key;
}
public void setSession_key(String session_key) {
this.session_key = session_key;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getSession_secret() {
return session_secret;
}
public void setSession_secret(String session_secret) {
this.session_secret = session_secret;
}
@Override
public String toString() {
return "BaiduTocken{" +
"refresh_token='" + refresh_token + '\'' +
", expires_in=" + expires_in +
", session_key='" + session_key + '\'' +
", access_token='" + access_token + '\'' +
", scope='" + scope + '\'' +
", session_secret='" + session_secret + '\'' +
'}';
}
}
唉,我的Arduino nano接了lcd屏显示速度很慢,也不知道哪里出了问题,唉,估计要买四条腿的,8条腿带不动