https://github.com/spring-projects-experimental/spring-ai

2:國(guó)內(nèi)以科大訊飛為例。

首先在官網(wǎng)https://xinghuo.xfyun.cn/注冊(cè)賬號(hào)和開(kāi)通應(yīng)用,獲取服務(wù)接口認(rèn)證信息。

個(gè)人用戶有200萬(wàn)的token數(shù),1tokens 約等于 1.5 個(gè)中文漢字 或者 0.8 個(gè)英文單詞,問(wèn)題和模型回復(fù)的字符串內(nèi)容,都是消耗token數(shù)的。


構(gòu)建demo工程,start.spring.io 初始化springboot項(xiàng)目

集成科大訊飛的java示例,將上面下載的zip工程解壓,pom.xml添加如下引用聲明:

  <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.40</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-sse</artifactId>
<version>3.14.9</version>
</dependency>

運(yùn)行BigModelDemo

package com.hfitech.ai.llm.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import okhttp3.*;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

public class BigModelDemo extends WebSocketListener {
// 地址與鑒權(quán)信息 https://spark-api.xf-yun.com/v1.1/chat 1.5地址 domain參數(shù)為general
// 地址與鑒權(quán)信息 https://spark-api.xf-yun.com/v2.1/chat 2.0地址 domain參數(shù)為generalv2
public static final String hostUrl = "https://spark-api.xf-yun.com/v2.1/chat";
// 替換為自己的應(yīng)用授權(quán)信息
public static final String appid = "xxxxxxx";
public static final String apiSecret = "xxxxxxxx";
public static final String apiKey = "xxxxxxxx";

public static List<RoleContent> historyList=new ArrayList<>(); // 對(duì)話歷史存儲(chǔ)集合

public static String totalAnswer=""; // 大模型的答案匯總

// 環(huán)境治理的重要性 環(huán)保 人口老齡化 我愛(ài)我的祖國(guó)
public static String NewQuestion = "";

public static final Gson gson = new Gson();

// 個(gè)性化參數(shù)
private String userId;
private Boolean wsCloseFlag;

private static Boolean totalFlag=true; // 控制提示用戶是否輸入
// 構(gòu)造函數(shù)
public BigModelDemo(String userId, Boolean wsCloseFlag) {
this.userId = userId;
this.wsCloseFlag = wsCloseFlag;
}

// 主函數(shù)
public static void main(String[] args) throws Exception {
// 個(gè)性化參數(shù)入口,如果是并發(fā)使用,可以在這里模擬
while (true){
if(totalFlag){
Scanner scanner=new Scanner(System.in);
System.out.print("我:");
totalFlag=false;
NewQuestion=scanner.nextLine();
// System.out.println(NewQuestion);
// 構(gòu)建鑒權(quán)url
String authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);
OkHttpClient client = new OkHttpClient.Builder().build();
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build();
for (int i = 0; i < 1; i++) {
totalAnswer="";
WebSocket webSocket = client.newWebSocket(request, new BigModelDemo(i + "",
false));
}
}else{
Thread.sleep(200);
}
}
}

public static boolean canAddHistory(){ // 由于歷史記錄最大上線1.2W左右,需要判斷是能能加入歷史
int history_length=0;
for(RoleContent temp:historyList){
history_length=history_length+temp.content.length();
}
if(history_length>12000){
historyList.remove(0);
historyList.remove(1);
historyList.remove(2);
historyList.remove(3);
historyList.remove(4);
return false;
}else{
return true;
}
}

// 線程來(lái)發(fā)送音頻與參數(shù)
class MyThread extends Thread {
private WebSocket webSocket;

public MyThread(WebSocket webSocket) {
this.webSocket = webSocket;
}

public void run() {
try {
JSONObject requestJson=new JSONObject();

JSONObject header=new JSONObject(); // header參數(shù)
header.put("app_id",appid);
header.put("uid",UUID.randomUUID().toString().substring(0, 10));

JSONObject parameter=new JSONObject(); // parameter參數(shù)
JSONObject chat=new JSONObject();
chat.put("domain","generalv2");
chat.put("temperature",0.5);
chat.put("max_tokens",4096);
parameter.put("chat",chat);

JSONObject payload=new JSONObject(); // payload參數(shù)
JSONObject message=new JSONObject();
JSONArray text=new JSONArray();

// 歷史問(wèn)題獲取
if(historyList.size()>0){
for(RoleContent tempRoleContent:historyList){
text.add(JSON.toJSON(tempRoleContent));
}
}

// 最新問(wèn)題
RoleContent roleContent=new RoleContent();
roleContent.role="user";
roleContent.content=NewQuestion;
text.add(JSON.toJSON(roleContent));
historyList.add(roleContent);

message.put("text",text);
payload.put("message",message);

requestJson.put("header",header);
requestJson.put("parameter",parameter);
requestJson.put("payload",payload);
// System.err.println(requestJson); // 可以打印看每次的傳參明細(xì)
webSocket.send(requestJson.toString());
// 等待服務(wù)端返回完畢后關(guān)閉
while (true) {
// System.err.println(wsCloseFlag + "---");
Thread.sleep(200);
if (wsCloseFlag) {
break;
}
}
webSocket.close(1000, "");
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
System.out.print("大模型:");
MyThread myThread = new MyThread(webSocket);
myThread.start();
}

@Override
public void onMessage(WebSocket webSocket, String text) {
// System.out.println(userId + "用來(lái)區(qū)分那個(gè)用戶的結(jié)果" + text);
JsonParse myJsonParse = gson.fromJson(text, JsonParse.class);
if (myJsonParse.header.code != 0) {
System.out.println("發(fā)生錯(cuò)誤,錯(cuò)誤碼為:" + myJsonParse.header.code);
System.out.println("本次請(qǐng)求的sid為:" + myJsonParse.header.sid);
webSocket.close(1000, "");
}
List<Text> textList = myJsonParse.payload.choices.text;
for (Text temp : textList) {
System.out.print(temp.content);
totalAnswer=totalAnswer+temp.content;
}
if (myJsonParse.header.status == 2) {
// 可以關(guān)閉連接,釋放資源
System.out.println();
System.out.println("*************************************************************************************");
if(canAddHistory()){
RoleContent roleContent=new RoleContent();
roleContent.setRole("assistant");
roleContent.setContent(totalAnswer);
historyList.add(roleContent);
}else{
historyList.remove(0);
RoleContent roleContent=new RoleContent();
roleContent.setRole("assistant");
roleContent.setContent(totalAnswer);
historyList.add(roleContent);
}
wsCloseFlag = true;
totalFlag=true;
}
}

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
try {
if (null != response) {
int code = response.code();
System.out.println("onFailure code:" + code);
System.out.println("onFailure body:" + response.body().string());
if (101 != code) {
System.out.println("connection failed");
System.exit(0);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 鑒權(quán)方法
public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
URL url = new URL(hostUrl);
// 時(shí)間
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = format.format(new Date());
// 拼接
String preStr = "host: " + url.getHost() + "\n" +
"date: " + date + "\n" +
"GET " + url.getPath() + " HTTP/1.1";
// System.err.println(preStr);
// SHA256加密
Mac mac = Mac.getInstance("hmacsha256");
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "hmacsha256");
mac.init(spec);

byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
// Base64加密
String sha = Base64.getEncoder().encodeToString(hexDigits);
// System.err.println(sha);
// 拼接
String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);
// 拼接地址
HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().//
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).//
addQueryParameter("date", date).//
addQueryParameter("host", url.getHost()).//
build();

// System.err.println(httpUrl.toString());
return httpUrl.toString();
}

//返回的json結(jié)果拆解
class JsonParse {
Header header;
Payload payload;
}

class Header {
int code;
int status;
String sid;
}

class Payload {
Choices choices;
}

class Choices {
List<Text> text;
}

class Text {
String role;
String content;
}
class RoleContent{
String role;
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;
}
}
}

在控制臺(tái)可以實(shí)現(xiàn)和大模型的對(duì)話聊天。

文章轉(zhuǎn)自微信公眾號(hào)@大語(yǔ)言模型

上一篇:

如何速成RAG+Agent框架大模型應(yīng)用搭建

下一篇:

Gorilla?LLM:連接海量API的大型語(yǔ)言模型
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)