Files
Vetti-Service-new/vetti-common/src/main/java/com/vetti/common/ai/WhisperClient.java
wangxiangshun 5cc31cfbbe 代码初始化
2025-10-02 17:19:01 +08:00

113 lines
4.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<String, String> 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<String, String> 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<String, String> 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());
}
}
}