简介:本文详细介绍了如何通过Spring AI框架接入OpenAI的API,实现文字转语音(TTS)和语音转文字(ASR)功能,包括环境配置、API调用、错误处理及优化建议。
随着人工智能技术的快速发展,语音交互已成为人机交互的重要方式。Spring AI作为一款基于Spring生态的AI开发框架,为开发者提供了便捷的AI能力集成方案。结合OpenAI强大的语音处理API,开发者可以快速实现文字转语音(TTS)和语音转文字(ASR)功能。本文将详细介绍如何通过Spring AI接入OpenAI,实现高效的语音交互。
在Maven项目的pom.xml中添加以下依赖:
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai</artifactId><version>0.8.0</version> <!-- 使用最新版本 --></dependency>
在application.properties或application.yml中配置:
spring.ai.openai.api-key=your_openai_api_keyspring.ai.openai.base-url=https://api.openai.com/v1
OpenAI提供了高质量的TTS服务,支持多种语音风格和语言。主要参数包括:
model: 指定TTS模型(如tts-1或tts-1-hd)input: 要转换的文本voice: 语音风格(如alloy、echo、fable等)response_format: 输出格式(如mp3、opus等)
import org.springframework.ai.openai.api.OpenAiTtsClient;import org.springframework.ai.openai.api.model.TtsRequest;import org.springframework.ai.openai.api.model.TtsResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class TextToSpeechService {@Autowiredprivate OpenAiTtsClient ttsClient;public byte[] convertTextToSpeech(String text, String voice) {TtsRequest request = TtsRequest.builder().model("tts-1") // 或使用"tts-1-hd"获取高清版本.input(text).voice(voice).responseFormat("mp3").build();TtsResponse response = ttsClient.generateSpeech(request);return response.getAudio();}}
alloy: 中性、专业的语音风格echo: 友好、自然的语音风格fable: 富有表现力的语音风格onyx: 正式、权威的语音风格nova: 年轻、活力的语音风格OpenAI的语音转文字服务支持多种音频格式(如mp3、wav等),并提供高精度的转录结果。主要参数包括:
model: 指定ASR模型(如whisper-1)file: 音频文件内容language: 指定语言(可选)response_format: 输出格式(如json、text等)
import org.springframework.ai.openai.api.OpenAiWhisperClient;import org.springframework.ai.openai.api.model.WhisperRequest;import org.springframework.ai.openai.api.model.WhisperResponse;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;@Servicepublic class SpeechToTextService {@Autowiredprivate OpenAiWhisperClient whisperClient;public String convertSpeechToText(MultipartFile audioFile) {try {WhisperRequest request = WhisperRequest.builder().model("whisper-1").file(audioFile.getBytes()).responseFormat("text") // 或"json"获取更详细结果.build();WhisperResponse response = whisperClient.transcribe(request);return response.getText();} catch (Exception e) {throw new RuntimeException("语音转文字失败", e);}}}
language参数指定
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;@RestController@RequestMapping("/api/audio")public class AudioApiController {@Autowiredprivate TextToSpeechService ttsService;@Autowiredprivate SpeechToTextService sttService;@PostMapping("/text-to-speech")public ResponseEntity<byte[]> textToSpeech(@RequestParam String text,@RequestParam(defaultValue = "alloy") String voice) {byte[] audio = ttsService.convertTextToSpeech(text, voice);return ResponseEntity.ok().header("Content-Type", "audio/mpeg").body(audio);}@PostMapping("/speech-to-text")public ResponseEntity<String> speechToText(@RequestParam("file") MultipartFile audioFile) {String text = sttService.convertSpeechToText(audioFile);return ResponseEntity.ok(text);}}
import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.mock.web.MockMultipartFile;import org.springframework.util.StreamUtils;import java.io.IOException;import java.io.InputStream;import java.nio.charset.StandardCharsets;import static org.junit.jupiter.api.Assertions.*;@SpringBootTestpublic class AudioServiceTests {@Autowiredprivate TextToSpeechService ttsService;@Autowiredprivate SpeechToTextService sttService;@Testpublic void testTextToSpeech() {String text = "Hello, this is a test of Spring AI with OpenAI TTS.";byte[] audio = ttsService.convertTextToSpeech(text, "alloy");assertNotNull(audio);assertTrue(audio.length > 0);}@Testpublic void testSpeechToText() throws IOException {// 准备测试音频文件(实际测试时应使用真实音频)String sampleText = "This is a sample audio for testing speech to text.";InputStream is = new ByteArrayInputStream(sampleText.getBytes(StandardCharsets.UTF_8));MockMultipartFile audioFile = new MockMultipartFile("file", "test.wav", "audio/wav", is);String result = sttService.convertSpeechToText(audioFile);assertNotNull(result);assertTrue(result.length() > 0);}}
通过Spring AI框架接入OpenAI的语音处理API,开发者可以快速构建高效的语音交互应用。本文详细介绍了文字转语音和语音转文字的实现方法,包括环境配置、API调用、错误处理和性能优化等方面。
未来发展方向:
Spring AI与OpenAI的结合为开发者提供了强大的语音处理能力,有助于构建更加自然、高效的人机交互应用。随着AI技术的不断进步,语音交互将在更多场景中得到应用,为开发者带来更多创新机会。