0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TRTCとChatGPT(OpenAI API)を連携して音声AIチャットを構築する手順

0
Posted at

🎯 概要

このドキュメントでは、Tencent Cloud TRTC SDKとOpenAI ChatGPT APIを連携し、リアルタイムの音声AIチャット機能をFlutterアプリに実装する方法を紹介します。

  • ユーザーの音声をTRTCで収音し、ASRでテキスト化
  • ChatGPT(gpt-3.5-turbo)で対話処理
  • TTS(音声合成)してTRTC経由でユーザーに応答

✅ 1. OpenAI APIの準備

OpenAI APIキーの取得

  1. OpenAI Platformにログイン
  2. API Keyを生成 → sk-xxxxx... を取得して保存

🔧 2. Flutter(TRTC SDK)で音声送受信

final trtc = TRTCCloud.sharedInstance();

Future<void> startVoiceSession() async {
  await trtc.startLocalAudio(true); // マイクON
  await trtc.enableAudioVolumeEvaluation(300);
  await trtc.enableAudioASR(true); // 音声認識ON

  trtc.registerListener((type, params) {
    if (type == TRTCCloudListener.onASRMessage) {
      String text = params["text"];
      sendTextToServer(text); // ChatGPTへ送信
    } else if (type == TRTCCloudListener.onRecvCustomCmdMsg) {
      final cmdData = jsonDecode(params["data"]);
      handleAIMessage(cmdData);
    }
  });
}

🧠 3. ChatGPTとの連携(Pythonサーバー)

import openai

openai.api_key = 'sk-xxxxx...'

def call_chatgpt(user_input):
    messages = [
        {"role": "system", "content": "あなたは優秀な日本語AIアシスタントです。"},
        {"role": "user", "content": user_input}
    ]
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    return response['choices'][0]['message']['content']

🔊 4. GPTの応答を音声に変換(TTS)

from openai import OpenAI

client = OpenAI(api_key="sk-xxxxx...")

def synthesize_tts(text):
    response = client.audio.speech.create(
        model="tts-1",
        voice="nova",
        input=text
    )
    return response.content  # 音声バイナリ

🔁 5. サーバー → クライアントへ応答を返す(gRPC or HTTP + TRTC)

def reply_to_trtc(session_id, reply_text):
    audio_data = synthesize_tts(reply_text)
    trtc_stream.send_audio(session_id, audio_data)  # TRTCへ音声返送
    trtc_stream.send_custom_msg(session_id, {
        "type": "ai_reply",
        "text": reply_text
    })

✅ 6. Flutterで応答を受信・再生

void handleAIMessage(Map data) {
  if (data["type"] == "ai_reply") {
    showTextOnScreen(data["text"]);
  }
}

void playAudio(Uint8List audioData) {
  trtc.playAudio(audioData); // バイナリ音声再生
}

📝 まとめ

項目 内容
音声認識(ASR) TRTC SDK(ローカル)
対話処理 ChatGPT API(gpt-3.5)
音声合成 OpenAI tts-1(nova音声)
音声伝送 TRTCクラウド音声送信

この構成により、音声→GPT応答→音声出力というAI会話体験を、Flutter + Python + TRTC + OpenAI で実現可能です。

🔄 全体構成イメージ(TRTC + GPT連携フロー)

[ユーザーの音声]
      ↓
TRTC SDK によるASR(音声→テキスト)
      ↓
ChatGPT(GPT-3.5/4)にテキスト送信
      ↓
GPTの応答を取得(テキスト)
      ↓
TTSエンジンで音声に変換
      ↓
TRTCで音声を再生(自動返答)

今後の改善アイデア:

  • 会話履歴をセッション管理
  • ユーザー別のキャラクター切り替え(プロンプト制御)
  • マルチ言語対応(ASR+TTS切り替え)

🔗 参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?