package qiwu.xfllmdemo.utils; import com.google.gson.Gson; import okhttp3.*; import org.jetbrains.annotations.NotNull; import qiwu.xfllmdemo.pojo.LLMResponseVO; import java.io.IOException; import java.util.*; public class BigModelNew extends WebSocketListener { // 地址与鉴权信息 public static final String hostUrl = "https://spark-api.xf-yun.com/v1.1/chat"; //ws(s)://spark-api.xf-yun.com/v2.1/chat public static final String appid = "76f57fca"; public static final String apiSecret = "ZGFiZDAxMWVkM2I5ZDg0YzJhMzVjMjc0"; public static final String apiKey = "cbd03b0dc6862d38b24c5c25ba8b0f02"; // 环境治理的重要性 环保 人口老龄化 我爱我的祖国 public static final String NewQuestion = "你知道什么是兽文化吗"; public static final Gson gson = new Gson(); // 个性化参数 private String userId; private Boolean wsCloseFlag; // 构造函数 public BigModelNew(String userId, Boolean wsCloseFlag) { this.userId = userId; this.wsCloseFlag = wsCloseFlag; } // 主函数 public static void main(String[] args) throws Exception { // 构建鉴权url String authUrl = AuthURLGenerator.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++) { WebSocket webSocket = client.newWebSocket(request, new BigModelNew(i + "", false)); // client. } } // 线程来发送音频与参数 class MyThread extends Thread { private WebSocket webSocket; public MyThread(WebSocket webSocket) { this.webSocket = webSocket; } public void run() { String requestJson; //请求参数json串 try { requestJson = "{\n" + " \"header\": {\n" + " \"app_id\": \"" + appid + "\",\n" + " \"uid\": \"" + UUID.randomUUID().toString().substring(0, 10) + "\"\n" + " },\n" + " \"parameter\": {\n" + " \"chat\": {\n" + " \"domain\": \"general\",\n" + " \"temperature\": 0.5,\n" + " \"max_tokens\": 1024\n" + " }\n" + " },\n" + " \"payload\": {\n" + " \"message\": {\n" + " \"text\": [\n" + " {\n" + " \"role\": \"user\",\n" + " \"content\": \"中国第一个皇帝是谁?\"\n" + " },\n" + " {\n" + " \"role\": \"assistant\",\n" + " \"content\": \"秦始皇\"\n" + " },\n" + " {\n" + " \"role\": \"user\",\n" + " \"content\": \"秦始皇修的长城吗\"\n" + " },\n" + " {\n" + " \"role\": \"assistant\",\n" + " \"content\": \"是的\"\n" + " },\n" + " {\n" + " \"role\": \"user\",\n" + " \"content\": \"" + NewQuestion + "\"\n" + " }\n" + " ]\n" + " }\n" + " }\n" + "}"; webSocket.send(requestJson); // System.err.println(requestJson); // 等待服务端返回完毕后关闭 do { // System.err.println(wsCloseFlag + "---"); Thread.sleep(200); } while (!wsCloseFlag); webSocket.close(1000, ""); } catch (Exception e) { e.printStackTrace(); } } } @Override public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) { super.onOpen(webSocket, response); MyThread myThread = new MyThread(webSocket); myThread.start(); } @Override public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) { // System.out.println(userId + "用来区分那个用户的结果" + text); LLMResponseVO myJsonParse = gson.fromJson(text, LLMResponseVO.class); if (myJsonParse.header.code != 0) { System.out.println("发生错误,错误码为:" + myJsonParse.header.code); System.out.println("本次请求的sid为:" + myJsonParse.header.sid); webSocket.close(1000, ""); } List textList = myJsonParse.payload.choices.text; for (LLMResponseVO.Payload.Choices.Text temp : textList) { System.out.print(temp.content); } if (myJsonParse.header.status == 2) { // 输出已结束,可以关闭连接,释放资源 wsCloseFlag = true; } } @Override public void onFailure(@NotNull WebSocket webSocket, @NotNull 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) { e.printStackTrace(); } } }