Welcome to Jetson AGX Orin 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 Principle2. Code AnalysisKey Code1. Voice Input Node (largemodel/largemodel/asr.py)2. AI service and voice output node (largemodel/largemodel/model_service.py)Code Analysis3. Practical Operations3.1 Configuring Offline Voice Interaction3.2 Starting and Testing the Function
1. Concept Introduction
1.1 What is "AI Large Model Voice Interaction"?
In the largemodel project, AI Large Model Voice Interaction combines the offline ASR and offline TTS described above with the large language model (LLM) core to form a complete conversational system that listens, speaks, and thinks.
This is no longer an isolated function, but rather the prototype of a true voice assistant. Users can engage in natural language conversations with the robot through voice, and the robot can understand questions, think about answers, and respond with voice. This entire process is completed locally, without the need for a network.
The core of this function is the model_service` ROS2 node. It acts as the brain and neural center, subscribing to ASR recognition results, invoking the LLM for thinking, and then publishing the LLM's text responses to the TTS node for speech synthesis.
1.2 Implementation Principle
This feature is implemented using a classic data flow pipeline:
- Audio -> Text (ASR): The
asrnode continuously listens to ambient sound. Once it detects a user speaking 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. Upon receiving the text from the ASR, it passes it as a prompt to a locally deployed large language model (such asQwenrunning throughOllama). The LLM generates a text response 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 invokes the offline TTS model to synthesize it into audio and plays it through the speaker.
This process forms a complete closed loop: 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 voice output node (largemodel/largemodel/model_service.py)
xxxxxxxxxx# 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. / 处理ASR消息的回调函数。""" # ... 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 implemented by two independent ROS nodes, asr.py and model_service.py, which communicate via the ROS topic /asr, forming a complete processing loop.
- Voice Input and Publishing (
asr.py):
- The
ASRNodenode processes voice input. In thekws_handlerfunction, it performs audio recording and text conversion. - After successful conversion, the
asr_pub_resultfunction is called. This function places the resulting text string into astd_msgs.msg.Stringmessage and then publishes it 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 themsgparameter.
- 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 reply text from the model result. - Next, the
_safe_play_audiofunction is called, which uses the TTS function (self.model_client.voice_synthesis) to synthesize the reply text into audio and play it back. - 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 the ROS publish-subscribe model.
asr.pyhandles input, whilemodel_service.pyhandles processing and output.
3. Practical Operations
3.1 Configuring Offline Voice Interaction
To implement a fully offline voice interaction system, you need to ensure that all three components, ASR, TTS, and LLM, are configured for offline mode.
Open the main configuration file
yahboom.yaml:xxxxxxxxxxgedit ~/yahboom_ws/src/largemodel/config/yahboom.yamlModify/confirm the following key configurations:
xxxxxxxxxxasrros__parametersuse_oline_asrFalse # Key: Set to False to enable offline ASRregional_setting "international"model_serviceros__parametersuseolinettsFalse # Key: Set to False to enable offline TTSllm_platform'ollama' # Key: Set to 'ollama' to enable offline LLMregional_setting "international"Open the model interface configuration file
large_model_interface.yaml:xxxxxxxxxxgedit ~/yahboom_ws/src/largemodel/config/large_model_interface.yamlConfirm that all offline model paths are correct:
xxxxxxxxxx# large_model_interface.yaml## Offline large modelollama_model"qwen2.5:7b" # Make sure the model has been downloaded via ollama pull## Offline speech recognitionlocal_asr_model"/path/to/your/SenseVoiceSmall" # Make sure the ASR model path is correct## Offline speech synthesiszh_tts_model"/path/to/your/zh_CN-huayan-medium.onnx" # Make sure the TTS model path is correct# ...
If you have modified any configuration files, please run the following command to compile the ROS workspace for the configuration to take effect.
xxxxxxxxxxcd ~/yahboom_wscolcon build
3.2 Starting and Testing the Function
Start
largemodelmain program: Run the following command to enable voice interaction:xxxxxxxxxxros2 launch largemodel largemodel_control.launch.pyTest:
- Wake up: Say "Hi, yahboom" into the microphone.
- Dialogue: After the speaker responds, you can speak your questions.
- Observe the log: In the terminal running the
launchfile, you should see the following:
- The ASR node recognizes your question and prints it.
- The
model_servicenode receives the text, calls the LLM, and prints the LLM's response.
- Listen to the answer: After a while, you should hear the robot's synthesized voice answer your question through the speaker.