public class BigModelNew extends WebSocketListener {
声明一个类 BigModelNew
,继承自 WebSocketListener
。
public static final String hostUrl = "https://spark-api.xf-yun.com/v2.1/chat";
public static final String appid = "";
public static final String apiSecret = "";
public static final String apiKey = "";
定义了一些常量,包括 hostUrl
、appid
、apiSecret
、apiKey
,分别表示目标服务器地址、应用ID、API密钥和API密钥。
public static List<RoleContent> historyList=new ArrayList<>();
声明一个静态的 List
对象 historyList
,用于存储对话的历史记录。
public static String totalAnswer="";
声明一个静态的字符串 totalAnswer
,用于汇总大模型的答案。
public static String NewQuestion = "";
声明一个静态的字符串 NewQuestion
,表示用户的最新问题。
public static final Gson gson = new Gson();
创建一个 Gson 对象,用于处理 JSON 数据的序列化和反序列化。
private String userId;
private Boolean wsCloseFlag;
声明私有属性 userId
和 wsCloseFlag
,分别表示用户ID和WebSocket关闭标志。
private static Boolean totalFlag=true;
声明一个静态的布尔值 totalFlag
,用于控制是否提示用户输入。
public BigModelNew(String userId, Boolean wsCloseFlag) {
this.userId = userId;
this.wsCloseFlag = wsCloseFlag;
}
构造函数,接受用户ID和WebSocket关闭标志作为参数,并进行初始化。
public static void main(String[] args) throws Exception {
while (true){
if(totalFlag){
Scanner scanner=new Scanner(System.in);
System.out.print("我:");
totalFlag=false;
NewQuestion=scanner.nextLine();
// 构建鉴权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 BigModelNew(i + "",
false));
}
}else{
Thread.sleep(200);
}
}
}
主函数,通过循环等待用户输入问题,然后构建鉴权URL,创建WebSocket请求。
public static boolean canAddHistory(){
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;
}
}
定义一个方法 canAddHistory
,用于判断是否可以将新的对话历史加入集合中,控制历史记录的最大长度。
class MyThread extends Thread {
private WebSocket webSocket;
public MyThread(WebSocket webSocket) {
this.webSocket = webSocket;
}
public void run() {
try {
// 构建 JSON 请求数据
JSONObject requestJson=new JSONObject();
JSONObject header=new JSONObject();
// 省略部分代码...
webSocket.send(requestJson.toString());
// 等待服务端返回完毕后关闭
while (true) {
Thread.sleep(200);
if (wsCloseFlag) {
break;
}
}
webSocket.close(1000, "");
} catch (Exception e) {
e.printStackTrace();
}
}
}
定义一个内部类 MyThread
继承自 Thread
,用于创建并执行WebSocket线程。
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
System.out.print("大模型:");
MyThread myThread = new MyThread(webSocket);
myThread.start();
}
WebSocket 打开时的回调函数,启动 MyThread
线程。
@Override
public void onMessage(WebSocket webSocket, String text) {
// 省略部分代码...
if (myJsonParse.header.status == 2) {
// 省略部分代码...
wsCloseFlag = true;
totalFlag=true;
}
}
WebSocket 接收到消息时的回调函数,处理接收到的消息,并设置标志以关闭WebSocket。
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
// 省略部分代码...
}
WebSocket 失败时的回调函数,处理连接失败的情况。
public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
// 省略部分代码...
return httpUrl.toString();
}
定义一个静态方法 getAuthUrl
,用于生成鉴权URL。
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;
}
定义一些内部类,用于解析 JSON 数据。
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;
}
}
定义一个内部类 RoleContent
,用于表示对话中的角色和内容。