ベスト プラクティス | Tencent Cloud Intelligent Voice を使用してインテリジェントな会話ロボットを構築する

AI テクノロジーの推進により、インテリジェントな会話ロボットは徐々に私たちの仕事や生活、さらにはパートナーにとって重要な効率ツールとなり、特に「コスト削減と効率向上」の最も独創的で直感的な実装を企業にもたらしています。

開発者として、音声テクノロジーに基づいたインテリジェントな会話ロボットの構築について考えたことはありますか?

この記事では、技術的な実装の詳細を段階的に説明します。

まず、インテリジェントな会話ロボットに必要なものを分析してみましょう。

1. 音声入力: インテリジェントな対話を行いたい場合は、音声入出力が必ず必要です。

2. 音声認識: 音声をテキストに認識します。

3. インテリジェントな質問と回答サービス: 音声認識結果をサービスに入力し、結果を取得します。

4. 音声合成: インテリジェントな質疑応答サービスの回答から音声を生成します。

5. 音声ブロードキャスト: インテリジェント質疑応答サービスによって回答された質問は、音声の形でブロードキャストされます。

フローチャート:

ボイスコレクション:

1. Tencent Cloud Speech Recognition が提供する SDK を使用します (Android、IOS、WeChat アプレット)

2. ハードウェア録音機器を使用して自分で音声を収集できます

3.端末(IOS、Androidなど)に録音デバイスを設定して音声を収集します

 

技術的プロセス:

1. まず音声を収集します

2. 音声ストリーム データを使用して Tencent クラウド音声認識 (ASR) を呼び出します。

3. 音声認識テキストデータをインテリジェントな質疑応答サービスに呼び出します。

4. インテリジェントな質問と回答サービスからの回答を使用して、Tencent Cloud Speech Synthesis (TTS) に電話します。

5. 最後に、音声合成で生成した音声を最後まで戻して再生します

1. 準備作業

1.1 音声認識サービスを有効にする

著者は Tencent の音声認識を使用しています。まずサービスをアクティブ化します。Tencent  Cloud 音声認識コンソールでここをクリックし 、[今すぐアクティブ化] をクリックしてサービスをアクティブ化します。

ここをクリックして初心者エクスペリエンス リソース パッケージを受け取ることができます: 音声認識_リアルタイム音声認識_録音ファイル認識_音声からテキストへのサービス-Tencent Cloud

1.2 サービスを呼び出すためのAPIキーを取得する

Tencent Cloud サービスにアクセスするには秘密キーが必要です。Tencent Cloud Access Management のAPI キー管理ページで、新しい秘密キーを作成できます。この秘密キーは適切に保管する必要があり、漏洩してはなりません。そうしないと、他人に盗まれてしまいます。秘密キーは後で使用します。

1.3 音声認識・音声合成SDKの入手

音声認識 SDK の取得:音声認識 リアルタイム音声認識 (WebSocket) - API ドキュメント - ドキュメント センター - Tencent Cloud

音声合成 SDK を入手します:音声合成の基本音声合成-API ドキュメント-ドキュメント センター-Tencent Cloud

クライアント SDK を取得します。

1.IOS:ログイン - Tencent クラウド

2. Android:ログイン - Tencent Cloud

3. WeChat ミニ プログラム: Tencent Cloud Intelligent Voice | ミニ プログラム プラグイン | WeChat パブリック プラットフォーム

1.4. インテリジェントな質問と回答サービスにアクセスする 

WeLM:- WeLM

ここでは、ChatGPT などの他のインテリジェントな質問と回答サービスを使用することもできます。

2. コード開発

ロジックには次のものが含まれます。

1. ASR リアルタイム ID を要求する

2. インテリジェントな質疑応答サービスをリクエストする

3. TTS音声合成をリクエストして音声を取得する

コードのコンパイル:

1. コマンドを実行して go.mod 環境を生成する go mod init Demon

2.go ビルド コンパイル

3. ./demo -e 16k_zh -f test audio address-format 1 を実行します。

注: このコードにはサーバー部分のみが含まれています。自分で SDK に接続し、音声をサーバーにストリーミングして認識することができます。

package main

import (
	"encoding/base64"
	"flag"
	"fmt"
	ttsCommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	tts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tts/v20190823"
	"github.com/tencentcloud/tencentcloud-speech-sdk-go/asr"
	"github.com/tencentcloud/tencentcloud-speech-sdk-go/common"
	"os"
	"sync"
	"time"
)

var (
	AppID           = "输入appid"
	SecretID        = "输入密钥ID"
	SecretKey       = "输入密钥key"
	EngineModelType = "16k_zh"
	SliceSize       = 16000
)

// MySpeechRecognitionListener implementation of SpeechRecognitionListener
type MySpeechRecognitionListener struct {
	ID int
}

// OnRecognitionStart implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnRecognitionStart(response *asr.SpeechRecognitionResponse) {
}

// OnSentenceBegin implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnSentenceBegin(response *asr.SpeechRecognitionResponse) {
}

// OnRecognitionResultChange implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnRecognitionResultChange(response *asr.SpeechRecognitionResponse) {
}

// OnSentenceEnd implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnSentenceEnd(response *asr.SpeechRecognitionResponse) {
	fmt.Printf("语音识别结果: %s \n", response.Result.VoiceTextStr)
	ConversationalRobot(response.Result.VoiceTextStr)
}

// OnRecognitionComplete implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnRecognitionComplete(response *asr.SpeechRecognitionResponse) {
}

// OnFail implementation of SpeechRecognitionListener
func (listener *MySpeechRecognitionListener) OnFail(response *asr.SpeechRecognitionResponse, err error) {
	fmt.Printf("%s|%s|OnFail: %v\n", time.Now().Format("2006-01-02 15:04:05"), response.VoiceID, err)
}

var proxyURL string
var VoiceFormat *int
var e *string

func main() {
	var f = flag.String("f", "test.pcm", "audio file")
	var p = flag.String("p", "", "proxy url")
	VoiceFormat = flag.Int("format", 0, "voice format")
	e = flag.String("e", "", "engine_type")
	fmt.Println("input-", *e, "-input")
	flag.Parse()
	if *e == "" {
		panic("please input engine_type")
	}
	if *VoiceFormat == 0 {
		panic("please input voice format")
	}

	proxyURL = *p
	var wg sync.WaitGroup
	wg.Add(1)
	go processOnce(1, &wg, *f)
	fmt.Println("Main: Waiting for workers to finish")
	wg.Wait()
	fmt.Println("Main: Completed")

}

func processOnce(id int, wg *sync.WaitGroup, file string) {
	defer wg.Done()
	process(id, file)
}

func process(id int, file string) {
	audio, err := os.Open(file)
	defer audio.Close()
	if err != nil {
		fmt.Printf("open file error: %v\n", err)
		return
	}

	listener := &MySpeechRecognitionListener{
		ID: id,
	}
	credential := common.NewCredential(SecretID, SecretKey)
	EngineModelType = *e
	fmt.Println("engine_type:", EngineModelType)
	recognizer := asr.NewSpeechRecognizer(AppID, credential, EngineModelType, listener)
	recognizer.ProxyURL = proxyURL
	recognizer.VoiceFormat = *VoiceFormat
	err = recognizer.Start()
	if err != nil {
		fmt.Printf("%s|recognizer start failed, error: %v\n", time.Now().Format("2006-01-02 15:04:05"), err)
		return
	}

	data := make([]byte, SliceSize)
	//这里的data可以换成实时端上传输过来的音频流
	for n, err := audio.Read(data); n > 0; n, err = audio.Read(data) {
		if err != nil {
			if err.Error() == "EOF" {
				break
			}
			fmt.Printf("read file error: %v\n", err)
			break
		}
		//一句话识别结束会回调上面OnSentenceEnd方法
		err = recognizer.Write(data[0:n])
		if err != nil {
			break
		}
		time.Sleep(20 * time.Millisecond)
	}
	recognizer.Stop()
}

func ConversationalRobot(text string) {
	//调用智能问答服务,获取回答
	Result := SendToGPTService(text)
	//把智能问答服务的文案转成音频文件
	audioData := TextToVoice(Result)
	//将音频文件返回给端上播放
	ResponseAudioData(audioData)
}

func ResponseAudioData(audioData []byte) {
	//把音频数据audioData推到端上播放
}

func SendToGPTService(text string) string {
	// API 调用智能问答服务
	// 获取智能问答服务返回结果
	result := "智能问答服务返回结果"
	fmt.Println("智能问答服务 API调用")
	return result
}

func TextToVoice(text string) []byte {
	fmt.Println("语音合成调用")
	// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
	// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
	// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
	credential := ttsCommon.NewCredential(
		SecretID,
		SecretKey,
	)
	// 实例化一个client选项,可选的,没有特殊需求可以跳过
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "tts.tencentcloudapi.com"
	// 实例化要请求产品的client对象,clientProfile是可选的
	client, _ := tts.NewClient(credential, "ap-beijing", cpf)

	// 实例化一个请求对象,每个接口都会对应一个request对象
	request := tts.NewTextToVoiceRequest()

	request.Text = ttsCommon.StringPtr(text)
	request.SessionId = ttsCommon.StringPtr("f435g34d23a24y546g")

	// 返回的resp是一个TextToVoiceResponse的实例,与请求对象对应
	response, err := client.TextToVoice(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Printf("An API error has returned: %s", err)
		return nil
	}
	if err != nil {
		panic(err)
	}
	// 输出json格式的字符串回包
	audioData, _ := base64.StdEncoding.DecodeString(*response.Response.Audio)
	fmt.Println("语音合成调用结束")
	return audioData
}

上記は知的音声対話ロボットの実装に関する技術的な詳細ですが、興味のある学生は開発の実践や拡張も可能です。

現在、知能型会話ロボットは、顧客との接触、マーケティング業務、窓口サービス、人間とコンピュータの対話などの経済生産活動において大規模に導入される段階に入っており、AI技術の継続的な革新により、知能型会話ロボットもまた、より高いレベルの製品を導出する、よりスマートなモード。

Tencent Cloud Intelligence は、企業顧客や開発者向けにワンストップの音声テクノロジー サービスも提供しており、製品の詳細については、Tencent Cloud の公式 Web サイトをご覧ください。

Tencent Cloud インテリジェント音声認識:音声認識_リアルタイム音声認識_録音ファイル認識_音声テキスト化サービス-Tencent Cloud

Tencent Cloud インテリジェント音声合成:音声合成_音声カスタマイズ_Text-to-Speech サービス-Tencent Cloud

おすすめ

転載: blog.csdn.net/tencentAI/article/details/129831795