chatgpt介面開發筆記3: 語音識別介面 1.文本轉語音 1、瞭解介面參數 介面地址: POST https://api.openai.com/v1/audio/speech 下麵是介面文檔描述內容: 參數: { "model": "tts-1", "input": "你好,我是饒坤,我是ter ...
chatgpt介面開發筆記3: 語音識別介面
1.文本轉語音
1、瞭解介面參數
介面地址:
POST https://api.openai.com/v1/audio/speech
下麵是介面文檔描述內容:
參數:
{
"model": "tts-1",
"input": "你好,我是饒坤,我是terramours gpt的開發者",
"voice": "alloy"
}
- model 模型
- input 需要轉換的文字
- voice 語音風格
2.postman測試
3.結果:
2.語音轉文本
1、瞭解介面參數
介面地址:
POST https://api.openai.com/v1/audio/transcriptions
文檔:
參數
curl https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F file="@/path/to/file/audio.mp3" \
-F model="whisper-1"
- file 需要解析的語言文件
- model 模型類型
2.postman測試
3.結果:
{
"text": "你好,我是饒坤,我是 Terramers GPT 的開發者。"
}
SDK開發
對應的語音介面我會加入到SDK中,使用C#開發者可以在nuget中搜索AllInAI.Sharp.API.
SDK為開源項目,代碼地址:https://github.com/raokun/AllInAI.Sharp.API
首先在項目中安裝sdk
Install-Package AllInAI.Sharp.API
1.Speech
1.OpenAI
public async Task OpenAISpeechTest() {
try {
AuthOption authOption = new AuthOption() { Key = "sk-**", BaseUrl = "https://api.openai.com", AIType = Enums.AITypeEnum.OpenAi };
AudioService audioService = new AudioService(authOption);
AudioSpeechReq req = new AudioSpeechReq() { Model = "tts-1", Input = "你好,我是饒坤,我是AllInAI.Sharp.API的開發者", Voice = "alloy" };
var res = await audioService.Speech<Stream>(req);
if(res.Data != null) {
var filePath = $"D:/test/{Guid.NewGuid()}.mp3";
using (FileStream fileStream = File.Create(filePath)) {
res.Data.CopyTo(fileStream);
}
}
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
1.Transcriptions
1.OpenAI
public async Task OpenAITranscriptionsTest() {
try {
AuthOption authOption = new AuthOption() { Key = "sk-**", BaseUrl = "https://api.openai.com", AIType = Enums.AITypeEnum.OpenAi };
// 讀取音頻文件的二進位內容
byte[] audioData = File.ReadAllBytes("C:/Users/Administrator/Desktop/response.mp3");
AudioService audioService = new AudioService(authOption) ;
AudioCreateTranscriptionReq req = new AudioCreateTranscriptionReq() { File=audioData,FileName= "response.mp3",Model= "whisper-1" ,Language="zh"};
AudioTranscriptionRes res = await audioService.Transcriptions(req);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
閱讀如遇樣式問題,請前往個人博客瀏覽: [https://www.raokun.top](chatgpt介面開發筆記3: 語音識別介面)
擁抱ChatGPT:https://first.terramours.site
SDK應用開源項目:https://github.com/TerraMours/TerraMours_Gpt_Web