Doubao Real-Time Voice Large Model Interaction Example
Description
-
Integrated with the Doubao real-time voice large model to implement a ROS-based voice interaction system, including microphone audio capture, real-time voice interaction, and audio playback.
-
Example code path:
~/kuavo-ros-opensource/src/kuavo_doubao_model/llm_doubao.py
Program Logic
- Import modules and define global variables
- Purpose: import the required Python standard libraries, third-party libraries, and ROS-related modules, and define the core interaction class.
ROSDialogSessionclass
-
Purpose: handle WebSocket communication with the Doubao real-time voice large model, and manage audio data reception, processing, and ROS publishing.
-
Key logic:
-
Initialize the session ID and WebSocket client, and establish a connection with the Doubao large model
-
Process audio chunks in a dedicated thread to implement buffering and processing of audio data
-
Convert audio byte data into a ROS-compatible integer list format
-
Implement audio resampling (from 24kHz to 16kHz) to adapt to the ROS audio system
-
Publish the processed audio data to a ROS topic
-
RobotLLMDoubaoCoreclass
-
Purpose: serve as the system core, managing microphone audio capture, session lifecycle, and the overall interaction flow.
-
Key logic:
-
Initialize the microphone interface and the ROS audio interface
-
Configure WebSocket connection parameters, including authentication information
-
Verify the connection to the Doubao large model service
-
Start and stop the voice interaction system
-
Run the asynchronous session in a separate thread, handling microphone data and server communication
-
- Main program code path:
~/kuavo-ros-opensource/src/kuavo_doubao_model/start_communication.py
-
Purpose: the main entry point of the program, responsible for initializing the ROS node, establishing the Doubao voice connection, starting voice interaction, and handling exit logic.
-
Key logic:
-
Initialize the ROS node voice_conversation
-
Create a RobotSpeech instance to manage voice interaction
-
Call the establish_doubao_speech_connection method to establish a connection with the Doubao large model using the provided app_id and access_key
-
Once the connection succeeds, call the start_speech method to start voice interaction
-
Enter a loop that detects keyboard input in a non-blocking manner
-
When user input 'c' is detected or the ROS node is shut down, call the stop_speech method to stop voice interaction
-
Notes
⚠️ Note: This example uses Volcano Engine's Doubao end-to-end real-time voice large model. This model is a paid model, so you need to create an account, top up, and obtain the APP ID and Access Token, then copy the obtained APP ID and Access Token into the corresponding places in the program. When using it, the robot's lower-level controller must be connected to the external network (able to access the Internet).
-
The Doubao end-to-end real-time voice large model used in this example
- After logging in, click Large Model - Doubao Real-Time Voice Model - Use Now to obtain the APP ID and Access Token
- Replace the app_id and access_key on lines 11 and 12 of the program
~/kuavo-ros-opensource/src/kuavo_doubao_model/start_communication.pywith the ones you obtained
Execution
-
To run this example, you need to connect a USB microphone to the lower-level controller, preferably a microphone with a one-touch mute/unmute function.
-
It is recommended to first test whether the microphone works in the system settings of the lower-level controller.
-
Startup
cd ~/kuavo-ros-opensource # Enter the workspace (switch according to the actual deployment directory) sudo su source devel/setup.bash roslaunch kuavo_audio_receiver receive_voice.launch # If the terminal prompts that no device matching the keyword ['USB Composite Device'] is found, press ctrl+c to close the program, run sudo modprobe snd-usb-audio in the terminal, and then restart the program- Open another terminal and enter
rosnode list | grep /audio_stream_player_nodeto check whetheraudio_stream_player_nodeexists; if it exists, skip runningplay_music.launch
cd ~/kuavo-ros-opensource # Open another terminal and enter the lower-level controller workspace (switch according to the actual deployment directory) sudo su source devel/setup.bash roslaunch kuavo_audio_player play_music.launch- Run the example
cd ~/kuavo-ros-opensource # Open another terminal and enter the lower-level controller workspace (switch according to the actual deployment directory) sudo su source devel/setup.bash python3 src/demo/kuavo_doubao_model/start_communication.py - Open another terminal and enter
-
Result
