(QwenLiveConnectionParam param, String backendModel, Path input, Path output,
long idleMs, long timeoutMs)
| 44 | } |
| 45 | |
| 46 | static void run(QwenLiveConnectionParam param, String backendModel, Path input, Path output, |
| 47 | long idleMs, long timeoutMs) throws Exception { |
| 48 | if (idleMs <= 0 || timeoutMs <= idleMs) throw new IllegalArgumentException("Require 0 < idle < timeout."); |
| 49 | QwenLiveSessionConfig config = QwenLiveSessionConfig.builder() |
| 50 | .model("qwen-audio-realtime").instructions("用简洁中文回答用户的语音问题") |
| 51 | .inputAudioFormat("pcm").outputAudioFormat("pcm") |
| 52 | .context(QwenLiveContext.builder().compaction( |
| 53 | QwenLiveContextCompaction.builder().enabled(false).mode("local").build()).build()) |
| 54 | .delegation(QwenLiveDelegation.builder().type(QwenLiveDelegationType.RESPONSES) |
| 55 | .responses(QwenLiveResponsesConfig.builder().model(backendModel) |
| 56 | .instructions("直接回答用户问题,不使用外部工具").build()).build()).build(); |
| 57 | config.getConfig(); |
| 58 | CountDownLatch configured = new CountDownLatch(1); |
| 59 | AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 60 | AtomicLong lastAudio = new AtomicLong(Long.MIN_VALUE); |
| 61 | AtomicBoolean accepting = new AtomicBoolean(true); |
| 62 | try (OutputStream audioFile = new BufferedOutputStream( |
| 63 | Files.newOutputStream(output, StandardOpenOption.CREATE_NEW))) { |
| 64 | QwenLiveConversation conversation = new QwenLiveConversation(param, new QwenLiveCallback() { |
| 65 | @Override public void onEvent(JsonObject event) { |
| 66 | synchronized (audioFile) { |
| 67 | if (!accepting.get()) return; |
| 68 | try { |
| 69 | String type = event.has(FIELD_TYPE) ? event.get(FIELD_TYPE).getAsString() : ""; |
| 70 | if (EVENT_SESSION_UPDATED.equals(type)) configured.countDown(); |
| 71 | if (EVENT_SESSION_CLOSED.equals(type)) { |
| 72 | onError(new IOException("Session ended before audio idle window.")); |
| 73 | } |
| 74 | if (EVENT_SESSION_OUTPUT_AUDIO_DELTA.equals(type)) { |
| 75 | JsonElement audio = event.get(FIELD_AUDIO); |
| 76 | if (audio == null || audio.isJsonNull()) audio = event.get(FIELD_DELTA); |
| 77 | if (audio == null || audio.isJsonNull()) throw new IOException("Audio event has no audio/delta data."); |
| 78 | byte[] bytes = Base64.getDecoder().decode(audio.getAsString()); |
| 79 | if (bytes.length > 0) { |
| 80 | audioFile.write(bytes); |
| 81 | lastAudio.set(System.nanoTime()); |
| 82 | } |
| 83 | } |
| 84 | } catch (IOException | RuntimeException e) { onError(e); } |
| 85 | audioFile.notifyAll(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override public void onClose(int code, String reason) { |
| 90 | onError(new IOException("WebSocket closed before output settled: " + code)); |
| 91 | } |
| 92 | |
| 93 | @Override public void onError(Throwable cause) { |
| 94 | synchronized (audioFile) { |
| 95 | if (!accepting.get()) return; |
| 96 | failure.compareAndSet(null, cause); |
| 97 | configured.countDown(); |
| 98 | audioFile.notifyAll(); |
| 99 | } |
| 100 | } |
| 101 | }); |
| 102 | try { |
| 103 | conversation.connect(); |
no test coverage detected