LLM 模块

MicroPython 应用示例:

from module import LlmModule

def on_keyword_detected():
    print("[Keyword] detected")

def on_asr_data_input(data: str, finish: bool, index: int):
    print(f"[ASR data] {data}")

def on_llm_data_input(data: str, finish: bool, index: int):
    print(f"[LLM data] {data}")

module_llm = LlmModule()
module_llm.begin_voice_assistant()

module_llm.set_voice_assistant_on_keyword_detected_callback(on_keyword_detected)
module_llm.set_voice_assistant_on_asr_data_input_callback(on_asr_data_input)
module_llm.set_voice_assistant_on_llm_data_input_callback(on_llm_data_input)

while True:
    module_llm.update()

UiFlow2 应用示例:

example.png

llm_voice_assistant.m5f2

class LlmModule

构造函数

class LlmModule

根据板卡类型初始化 LlmModule,并设置 UART 通信。

UIFLOW2:

init.png

Methods

LlmModule.update() None

更新 ModuleLLM,接收响应消息。

UIFLOW2:

update.png

LlmModule.check_connection() bool

检查模块连接是否正常。

返回:

如果模块连接正常则为 True,否则为 False。

返回类型:

bool

UIFLOW2:

check_connection.png

LlmModule.get_response_msg_list() list

获取模块响应消息列表。

返回:

响应消息列表(以字典形式)。

返回类型:

list

UIFLOW2:

get_response_msg_list.png

LlmModule.clear_response_msg_list() None

清除模块的响应消息列表。

UIFLOW2:

clear_response_msg_list.png

LlmModule.sys_ping() int

向系统发送 ping,并获取响应代码。

UIFLOW2:

sys_ping.png

LlmModule.sys_reset(wait_reset_finish=True) int

重置系统。

参数:

wait_reset_finish (bool) – 是否等待复位完成。

返回:

复位命令的结果。

返回类型:

int

UIFLOW2:

sys_reset.png

LlmModule.sys_reboot() int

重启系统。

返回:

重启命令的结果。

返回类型:

int

UIFLOW2:

sys_reboot.png

LlmModule.llm_setup(prompt='', model='qwen2.5-0.5b', response_format='llm.utf-8.stream', input='llm.utf-8.stream', enoutput=True, enkws=True, max_token_len=127, request_id='llm_setup') str

设置 LLM 模块。

参数:
  • prompt (str) – 提示文本。

  • model (str) – 模型名称。

  • response_format (str) – 响应格式。

  • input (str) – 输入格式。

  • enoutput (bool) – 启用输出。

  • enkws (bool) – 启用关键词唤醒。

  • max_token_len (int) – 最大 token 长度。

  • request_id (str) – Request ID。

返回:

setup 命令的结果。

返回类型:

str

UIFLOW2:

llm_setup.png

LlmModule.llm_inference(work_id, input_data, request_id='llm_inference') str

使用 LLM 模块执行推理。

参数:
  • work_id – 工作 ID。

  • input_data – 输入数据。

  • request_id (str) – Request ID。

返回:

推理命令的结果。

返回类型:

str

UIFLOW2:

llm_inference.png

LlmModule.audio_setup(capcard=0, capdevice=0, cap_volume=0.5, playcard=0, playdevice=1, play_volume=0.15, request_id='audio_setup') str

设置音频模块。

参数:
  • capcard (int) – 采集卡索引。

  • capdevice (int) – 捕获设备索引。

  • cap_volume (float) – 采集音量。

  • playcard (int) – 播放卡片索引。

  • playdevice (int) – 播放设备索引。

  • play_volume (float) – 播放音量。

  • request_id (str) – Request ID。

返回:

setup 命令的结果。

返回类型:

str

UIFLOW2:

audio_setup.png

LlmModule.tts_setup(model='single_speaker_english_fast', response_format='tts.base64.wav', input='tts.utf-8.stream', enoutput=True, enkws=True, request_id='tts_setup') str

设置 TTS 模块。

参数:
  • model (str) – TTS 模型名称。

  • response_format (str) – 响应格式。

  • input (str) – 输入格式。

  • enoutput (bool) – 启用输出。

  • enkws (bool) – 启用关键词唤醒。

  • request_id (str) – Request ID。

返回:

setup 命令的结果。

返回类型:

str

UIFLOW2:

tts_setup.png

LlmModule.kws_setup(kws='HELLO', model='sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01', response_format='kws.bool', input='sys.pcm', enoutput=True, request_id='kws_setup') str

设置 KWS 模块。

参数:
  • kws (str) – 要检测的关键字。

  • model (str) – KWS 模型名称。

  • response_format (str) – 响应格式。

  • input (str) – 输入格式。

  • enoutput (bool) – 启用输出。

  • request_id (str) – Request ID。

返回:

setup 命令的结果。

返回类型:

str

UIFLOW2:

kws_setup.png

LlmModule.asr_setup(model='sherpa-ncnn-streaming-zipformer-20M-2023-02-17', response_format='asr.utf-8.stream', input='sys.pcm', enoutput=True, enkws=True, rule1=2.4, rule2=1.2, rule3=30.0, request_id='asr_setup') str

设置 ASR 模块。

参数:
  • model (str) – ASR 模型名称。

  • response_format (str) – 响应格式。

  • input (str) – 输入格式。

  • enoutput (bool) – 启用输出。

  • enkws (bool) – 启用关键词唤醒。

  • rule1 (float) – 通用要求

  • rule2 (float) – 规则 2 的值。

  • rule3 (float) – 规则 3 的取值。

  • request_id (str) – Request ID。

返回:

setup 命令的结果。

返回类型:

str

UIFLOW2:

asr_setup.png

LlmModule.get_latest_llm_work_id() str

获取最新的 LLM module 工作 ID。

返回:

最新的 LLM 模块工作 ID。

返回类型:

str

UIFLOW2:

get_latest_llm_work_id.png

LlmModule.get_latest_audio_work_id() str

获取最新的 Audio 模块工作 id。

返回:

最新的 Audio 模块工作 ID。

返回类型:

str

UIFLOW2:

get_latest_audio_work_id.png

LlmModule.get_latest_tts_work_id() str

获取最新的 TTS module 工作 ID。

返回:

最新的 TTS 模块工作 ID。

返回类型:

str

UIFLOW2:

get_latest_tts_work_id.png

LlmModule.get_latest_kws_work_id() str

获取最新的 KWS module work id。

返回:

最新的 KWS 模块工作 id。

返回类型:

str

UIFLOW2:

get_latest_kws_work_id.png

LlmModule.get_latest_asr_work_id() str

获取最新的 ASR 模块工作 id。

返回:

最新的 ASR 模块工作 ID。

返回类型:

str

UIFLOW2:

get_latest_asr_work_id.png

LlmModule.get_latest_error_code() int

获取最新的 ModuleLLM 响应错误码。

返回:

最新的 ModuleLLM 响应错误码。

返回类型:

int

UIFLOW2:

get_latest_error_code.png

LlmModule.begin_voice_assistant(wake_up_keyword='HELLO', prompt='') bool

开始语音助手。

参数:
  • wake_up_keyword (str) – 唤醒关键词。

  • prompt (str) – The assistant prompt。

返回:

如果语音助手启动成功则为 True,否则为 False。

返回类型:

bool

UIFLOW2:

begin_voice_assistant.png

LlmModule.set_voice_assistant_on_keyword_detected_callback(on_keyword_detected) None

设置当检测到唤醒关键词时的回调函数。

参数:

on_keyword_detected – 在检测到关键字时要执行的回调函数。

UIFLOW2:

set_voice_assistant_on_keyword_detected_callback.png

LlmModule.set_voice_assistant_on_asr_data_input_callback(on_asr_data_input) None

设置当 ASR 数据输入时的回调函数。

参数:

on_asr_data_input – 用于处理 ASR 数据输入的回调函数。

UIFLOW2:

set_voice_assistant_on_asr_data_input_callback.png

LlmModule.set_voice_assistant_on_llm_data_input_callback(on_llm_data_input) None

设置当输入 LLM 数据时的回调函数。

参数:

on_llm_data_input – 用于处理 LLM 数据输入的回调函数。

UIFLOW2:

set_voice_assistant_on_llm_data_input_callback.png