Welcome to RDK S100(P) Board repository
3.AI large model voice interaction
3. AI Large Model Voice Interaction
3. AI Large Model Voice Interaction1. Concept Introduction1.1 What is "AI Large Model Voice Interaction"?1.2 Implementation Principle Overview2. Code analysisKey code1. Voice input node (largemodel/largemodel/asr.py)2. AI service and speech output node (largemodel/largemodel/model_service.py)Code Analysis3. Practical Operation3.1 Configuring Offline Voice Interaction3.2 Startup and testing functionality
1. Concept Introduction
1.1 What is "AI Large Model Voice Interaction"?
In the largemodel project, AI Large Model Voice Interaction connects the previously introduced offline ASR and offline TTS with the core of the Large Language Model (LLM), forming a complete dialogue system that can hear, speak, and think.
This is no longer an isolated function, but a prototype of a true voice assistant. Users can engage in natural language conversations with the robot via voice, and the robot can understand questions, think about answers, and respond with voice. The entire process is completed locally, without a network.
The core of this function is the model_service ROS2 node. It acts as the brain and neural center, subscribing to the ASR recognition results, calling the LLM for thinking, and then publishing the LLM's text response to the TTS node for speech synthesis.
1.2 Implementation Principle Overview
The implementation principle of this function is a classic data flow pipeline:
- Audio -> Text (ASR): The
asrnode continuously listens for ambient sound. Once it detects a user uttering a sentence, it converts it into text and publishes it to the/asr_texttopic. - Text -> Thought -> Text (LLM): The
model_servicenode subscribes to the/asr_texttopic. When it receives text from the ASR, it sends it as a prompt to the locally deployed large language model (such asQwenrunning viaOllama). The LLM generates a response text based on the context. - Text -> Audio (TTS): After receiving the LLM's response, the
model_servicenode publishes it to the/tts_texttopic. - Audio Playback: The
tts_onlynode subscribes to the/tts_texttopic. Upon receiving text, it immediately calls the offline TTS model to synthesize it into audio and plays it through the speaker.
This process forms a complete closed loop of speech input -> text processing -> text output -> speech output.
2. Code analysis
Key code
1. Voice input node (largemodel/largemodel/asr.py)
x
# From largemodel/largemodel/asr.pyclass ASRNode(Node): def __init__(self): # ... self.asr_pub = self.create_publisher(String, "asr", 5) # ... def kws_handler(self)->None: if self.listen_for_speech(self.mic_index): asr_text = self.ASR_conversion(self.user_speechdir) if asr_text != 'error': self.asr_pub_result(asr_text) def asr_pub_result(self,asr_result:str)->None: msg=String(data=asr_result) self.asr_pub.publish(msg)2. AI service and speech output node (largemodel/largemodel/model_service.py)
x
# From largemodel/largemodel/model_service.pyclass LargeModelService(Node): def __init__(self): # ... self.asrsub = self.create_subscription(String,'asr', self.asr_callback,1) # ... def asr_callback(self,msg): """Callback function for handling ASR messages. """ # ... result = self.model_client.infer_with_text(msg.data, message=messages_to_use) self.process_model_result(result) def process_model_result(self, result, from_seewhat=False): """Process the result returned by the model. """ # ... user_friendly_response = response_json.get("response", "我正在处理...") # ... self._safe_play_audio(user_friendly_response) # ... self.execute_tools(tools_list) def _safe_play_audio(self, text_to_speak: str): """ Synthesizes and plays all non-empty messages only in non-text chat mode. """ if not self.text_chat_mode and text_to_speak: try: self.model_client.voice_synthesis(text_to_speak, self.tts_out_path) self.play_audio_async(self.tts_out_path) except Exception as e: self.get_logger().error(f"Safe audio playback failed: {e}")Code Analysis
The voice interaction functionality of the large AI model is collaboratively implemented by two independent ROS nodes, asr.py and model_service.py. They communicate via the ROS topic /asr, forming a complete processing loop.
- Voice Input and Publication (
asr.py):
- The
ASRNodenode handles voice input. Thekws_handlerfunction performs recording and text conversion. - After successful conversion, the
asr_pub_resultfunction is called. This function places the obtained text string into astd_msgs.msg.Stringmessage and then publishes the message to the/asrtopic via theself.asr_pubpublisher.
- Subscription and Response (
model_service.py):
- During initialization, the
LargeModelServicenode subscribes to the/asrtopic using thecreate_subscriptionmethod and specifiesasr_callbackas its callback function. - When
ASRNodepublishes a message on the/asrtopic, the ROS system automatically calls theasr_callbackfunction ofLargeModelService, passing the message as the parametermsg.
- Interaction Loop:
- After receiving the message, the
asr_callbackfunction extracts the text data (msg.data) and passes it to the large model for inference (self.model_client.infer_with_text). - After inference is complete, the
process_model_resultfunction is called. This function extracts the text for the reply from the model results. - Next, the
_safe_play_audiofunction is called, which uses TTS (self.model_client.voice_synthesis) to synthesize the reply text into audio and play it. - At this point, a complete interaction loop from "receiving user voice" to "responding to the user with voice" is completed. The two nodes are decoupled using ROS's publish-subscribe pattern;
asr.pyhandles the input, andmodel_service.pyhandles the processing and output.
3. Practical Operation
3.1 Configuring Offline Voice Interaction
To implement a fully offline voice interaction system, you need to ensure that ASR, TTS, and LLM are all configured in offline mode.
- Open the main configuration file
yahboom.yaml:
xxxxxxxxxxgedit ~/yahboom_ws/src/largemodel/config/yahboom.yaml- Modify/confirm the following key configurations:
x
asr ros__parameters use_oline_asrFalse # Key: Set to False to enable offline ASRmodel_service ros__parameters useolinettsFalse # Key: Set to False to enable offline TTS llm_platform'ollama' # Key: Set to 'ollama' to enable offline LLM- Open the model interface configuration file
large_model_interface.yaml:
xxxxxxxxxxgedit ~/yahboom_ws/src/largemodel/config/large_model_interface.yaml- Confirm all offline model paths are correct:
xxxxxxxxxx# large_model_interface.yaml## Offline large modelollama_model"qwen2.5:7b" # Ensure this model has been downloaded via ollama pull## Offline speech recognitionlocal_asr_model"/path/to/your/SenseVoiceSmall" # Ensure the ASR model path is correct## Offline speech synthesiszh_tts_model"/path/to/your/zh_CN-huayan-medium.onnx" # Ensure the TTS model path is correct# ...Recompile
xxxxxxxxxxcd ~/yahboom_ws/colcon buildsource install/setup.bash
3.2 Startup and testing functionality
- Start the
largemodelmain program:
Run the following command to enable voice interaction:
xxxxxxxxxxros2 launch largemodel largemodel_control.launch.py- Testing:
- Wake-up: Say into the microphone, "Hello, yahboom."
- Dialogue: Speaker responseAfter that, you can speak your question.
- Observe the log: In the terminal running the
launchfile, you should see:
- The ASR node recognizes your question and prints it out.
- The
model_servicenode receives the text, calls the LLM, and prints the LLM's response.
- Listen to the answer: Later, you should be able to hear the robot answer your question in synthesized speech through the speaker.