Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
themeEmacs
public async Task<string> GetChatCompletion(string message, List<string> assist)
{
    var completionResult = await _client.CompleteChatAsync(new ChatMessage[]
    {
        ChatMessage.CreateUserMessage(message),
        ChatMessage.CreateAssistantMessage(string.Join("\n", assist))
    });
    string aiResponse = completionResult.Value.Content.FirstOrDefault()?.Text ?? string.Empty;

    return aiResponse;
}


/// <summary>
/// voice alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, , shimme
/// </summary>
/// <param name="text"></param>
/// <param name="voice"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public async Task<float[]> ConvertTextToVoiceAsync(string text, string voice = "alloy")
{
    var requestBody = new
    {
        model = "gpt-4o-mini-tts",  // TTS 모델 이름
        input = text,               // 변환할 텍스트
        voice                       // 음성 스타일
    };
    
    var ttsTask = _httpClient.PostAsJsonAsync("audio/speech", requestBody);
    
    await Task.WhenAll(ttsTask); // 두 작업이 완료될작업을 동시실행하고 , 동시완료될 때까지 기다림

    var response = await ttsTask;

    if (!response.IsSuccessStatusCode)
    {
        throw new InvalidOperationException($"TTS API 호출 실패: {response.ReasonPhrase}");
    }

    // MP3 데이터를 byte 배열로 변환하여 반환
    var audioBytes = await response.Content.ReadAsByteArrayAsync();

    // MP3 데이터를 PCM 데이터로 변환
    return ConvertMp3ToFloatArray(audioBytes);
}

...