package com.vetti.common.ai; import cn.hutool.json.JSONObject; import okhttp3.*; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class WhisperClient { private static final String API_URL = "https://api.openai.com/v1/audio/transcriptions"; private static final String MODEL = "whisper-1"; private final String apiKey; private final OkHttpClient client; // 构造函数,传入API密钥 public WhisperClient(String apiKey) { this.apiKey = apiKey; this.client = new OkHttpClient(); } /** * 将音频文件转换为文本 * @param audioFile 音频文件 * @param options 可选参数 (language, response_format等) * @return 转换后的文本 * @throws IOException 网络或文件操作异常 * @throws WhisperException API返回错误 */ public String transcribe(File audioFile, Map options) throws IOException, WhisperException { // 验证文件是否存在 if (!audioFile.exists()) { throw new IllegalArgumentException("音频文件不存在: " + audioFile.getAbsolutePath()); } // 构建请求体 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("model", MODEL) .addFormDataPart("file", audioFile.getName(), RequestBody.create(audioFile, MediaType.parse("audio/*"))); // 添加可选参数 if (options != null) { for (Map.Entry entry : options.entrySet()) { bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue()); } } // 构建请求 Request request = new Request.Builder() .url(API_URL) .header("Authorization", "Bearer " + apiKey) .post(bodyBuilder.build()) .build(); // 发送请求并处理响应 try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); if (!response.isSuccessful()) { throw new WhisperException( "API请求失败: " + response.code() + ", 详情: " + responseBody ); } // 解析JSON响应 JSONObject json = new JSONObject(responseBody); return ""; } } /** * 简化的转写方法,使用默认参数 */ public String transcribe(File audioFile) throws IOException, WhisperException { return transcribe(audioFile, null); } // 自定义异常类,用于处理Whisper API相关错误 public static class WhisperException extends Exception { public WhisperException(String message) { super(message); } } // 使用示例 public static void main(String[] args) { // 替换为你的API密钥 String apiKey = "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"; WhisperClient client = new WhisperClient(apiKey); // 音频文件路径 File audioFile = new File("/Users/wangxiangshun/Public/project-aio/vetti/vetti-service/output.mp3"); try { // 添加可选参数,例如指定语言为中文 Map options = new HashMap<>(); options.put("language", "zh"); String result = client.transcribe(audioFile, options); System.out.println("转写结果: " + result); } catch (IOException e) { System.err.println("IO错误: " + e.getMessage()); e.printStackTrace(); } catch (WhisperException e) { System.err.println("Whisper API错误: " + e.getMessage()); } catch (IllegalArgumentException e) { System.err.println("参数错误: " + e.getMessage()); } } }