香橙派Orangepi3B制作AI大模型语音对话盒子(三)
剑事
2023年10月26日 10:17
收录于文集
共3篇
AI教程~

开始动手

唤醒入口唤醒入口支持多个唤醒词,触发某个唤醒词会执行回调 detector.start(detected_callback=snowboydecoder.play_audio_file, interrupt_check=interrupt_callback, sleep_time=0.03)

demo.py 中这段代码 detected_callback 就是回调,回传一个 index

触发回调后面就是进行大模型对话部分

大模型调用

因为是语音交互,所以要先把声音转成文字

代码比较简单,直接通过 麦克风识别

speech_recognizer = speechsdk.SpeechRecognizer(speech_config=tts_config)

speech_recognition_result = speech_recognizer.recognize_once_async().get()

speech_recognition_result.text 返回的文本

api_key 根据文档设置

下一步,把识别好的文本内容传给大模型接口

from dashscope import Generation gen = Generation() response = gen.call( Generation.Models.qwen_turbo, messages=message, result_format='message', # set the result is message format. stream=True ) message 是数组,可以设置上下文,根据自己的需求设置数量 获得大模型返回内容后,tts 转语音,主要代码如下 speech_config.speech_synthesis_voice_name = 指定嗓音 audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True) speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) result = speech_synthesizer.speak_text_async(text).get()


到这里对话交互就实现完了,对话交互是整个功能里最简单的了

优化和填坑是比较耗时的部分