Welcome to Raspberry Pi 5 repository
10. AI large model offline voice assistant
AI Large Model Offline Voice Assistant
AI Large Model Offline Voice Assistant1. Offline Voice Configuration2. Create Auto-Start Service (Systemd)2.1 Create a Docker Container Startup Script2.2 If using Yahboom's AI large model factory image or SSD factory image, you don't need to recreate the script. Just run the following command to start the auto-start service:2.3 Related Commands:3. CSI Camera Auto-Start ServiceIf using Yahboom's AI large model factory image or SSD factory image, you don't need to recreate the script. Just run the following command to restart the auto-start service:
Note: Raspberry Pi 5 1GB has no large model package and cannot run it. 2GB and 4GB versions cannot run offline due to performance limitations. You can refer to the online large model tutorial.
1. Offline Voice Configuration
Before setting up auto-start, we must ensure that the program itself can work independently in offline mode. This requires modifying the configuration file.
Locate the configuration file: In your project code, find and open the configuration file:
config/yahboom.yamlModify configuration parameters: Please check the following parameters in the file and ensure their values match what's shown below. If a parameter doesn't exist, add it.
xasr#Voice node parametersros__parametersVAD_MODE2 #VAD sensitivitysample_rate16000 #ASR recording audio sample rateframe_duration_ms30 #VAD frame size in msuse_oline_asrFalse #Whether to use online ASR recognition (True for online, False for offline)mic_serial_port"/dev/ttyUSB0" #Microphone serial port aliasmic_index0 #Microphone indexlanguage'zh' #ASR languageregional_setting "China" #international: International version, China: China versionmodel_service#Model server node parametersros__parameterslanguage'zh' #Large model interface languageuseolinettsFalse #Whether to use online TTS (True for online, False for offline)# Large model configuration# llm_platform: 'ollama' # Optional platforms: 'ollama', 'tongyi', 'spark', 'qianfan', 'openrouter'llm_platform'ollama' # Currently selected large model platformregional_setting "China"use_oline_asranduseolinettsmust be set toFalse.llm_platformmust be set toollama.With these settings, everything will be offline.
Save the file and rebuild the project to apply changes:
xxxxxxxxxxcd ~/yahboom_wscolcon buildsource install/setup.bash
After completing this step, the program is now a pure offline voice service.
2. Create Auto-Start Service (Systemd)
Now, we will create a systemd service to make largemodel_control.launch.py run automatically at system startup.
Note: If you are using Yahboom's AI large model factory image or SSD factory image, these scripts have already been created. You only need to restart the auto-start container.
2.1 Create a Docker Container Startup Script
Create the script file: In the home directory (
~), create a file namedros2_docker_autostart.sh.xxxxxxxxxxvim ~/ros2_docker_autostart.shWrite script content: Copy and paste the following content into the script file.
xxxxxxxxxxset -eCN=${CONTAINER_NAME:-ros_humble_ai_service}IMG=${IMAGE_NAME:-ros_humble_ai:v1.0}WS=${HOST_WS_DIR:-"$HOME/yahboom_ws"}ROS=${ROS_DISTRO:-humble}CMD=${LAUNCH_CMD:-"ros2 launch largemodel largemodel_control.launch.py"}AUID=${AUDIO_UID:-$(id -u)}XAUTH=${XAUTHORITY:-"$HOME/.Xauthority"}mkdir -p "$WS"x=()if [ -d /tmp/.X11-unix ]; thencommand -v xhost >/dev/null 2>&1 && xhost +local:root || truex+=( -e DISPLAY=${DISPLAY:-:0} -e QT_X11_NO_MITSHM=1 -v /tmp/.X11-unix:/tmp/.X11-unix )[ -f "$XAUTH" ] && x+=( -v "$XAUTH:/root/.Xauthority" -e XAUTHORITY=/root/.Xauthority )fia=()if [ -S "/run/user/$AUID/pulse/native" ]; thena+=( -e PULSE_SERVER=unix:/run/user/$AUID/pulse/native -v "/run/user/$AUID/pulse:/run/user/$AUID/pulse:ro" )[ -d "$HOME/.config/pulse" ] && a+=( -v "$HOME/.config/pulse:/root/.config/pulse:ro" )fid=()[ -e /dev/video0 ] && d+=( --device=/dev/video0 )[ -d /dev/bus/usb ] && d+=( --device=/dev/bus/usb )[ -d /dev/snd ] && d+=( --device=/dev/snd )[ -e /dev/mic ] && d+=( -v /dev/mic:/dev/mic )if docker ps -a --format '{{.Names}}' | grep -qx "$CN"; thendocker ps --format '{{.Names}}' | grep -qx "$CN" || docker start "$CN" >/dev/nullelsedocker run -d \--name "$CN" --net=host --privileged --restart unless-stopped \--security-opt apparmor:unconfined \-v "$WS:/root/yahboom_ws:rw" \"${d[@]}" "${x[@]}" "${a[@]}" \"$IMG" \bash -lc "source /opt/ros/$ROS/setup.bash; [ -f /root/yahboom_ws/install/setup.bash ] && source /root/yahboom_ws/install/setup.bash; $CMD"fiecho "OK: $CN"Save and exit
Enable Docker to start on boot:
xxxxxxxxxxsudo systemctl enable --now dockerGrant script execution permission, then run:
xxxxxxxxxxchmod +x ~/ros2_docker_autostart.sh./ros2_docker_autostart.shVerify:
xxxxxxxxxxdocker inspect -f '{{.HostConfig.RestartPolicy.Name}}' ros_humble_ai_serviceThe output should be
unless-stopped. After this, Docker will automatically pull up the container each time you boot, achieving the effect of auto-starting the voice assistant.
2.2 If using Yahboom's AI large model factory image or SSD factory image, you don't need to recreate the script. Just run the following command to start the auto-start service:
xxxxxxxxxx~/ros2_docker_autostart.sh
2.3 Related Commands:
Immediately stop the container, which will also stop auto-start:
xxxxxxxxxxdocker stop ros_humble_ai_service
View Docker container running logs:
xxxxxxxxxxdocker logs -f ros_humble_ai_service
3. CSI Camera Auto-Start Service
To call CSI video in Docker, you need to start host_stream.py on the host machine. Therefore, if you need to call CSI camera video during auto-start, you also need to add this file to the Systemd auto-start service.
Note: If you are using Yahboom's AI large model factory image or SSD factory image, these scripts have already been created. You only need to restart the auto-start service.
Enter the command to write the file:
xxxxxxxxxxsudo vim /etc/systemd/system/host-stream.service
Copy the following content into the file:
xxxxxxxxxx[Unit]Description=Host stream (runs ~/host_stream.py)After=network.target[Service]Type=simpleUser=piWorkingDirectory=/home/piExecStart=/usr/bin/python3 /home/pi/host_stream.pyRestart=on-failureRestartSec=5StandardOutput=append:/home/pi/host_stream.logStandardError=append:/home/pi/host_stream.log[Install]WantedBy=multi-user.target
Save and exit
Reload and start the service:
xxxxxxxxxxsudo systemctl daemon-reloadsudo systemctl enable host-stream.servicesudo systemctl start host-stream.service
Check service status:
xxxxxxxxxxsudo systemctl status host-stream.service
If you see `Active: active (running)`, the service has started successfully!When you don't need the CSI camera auto-start service, run the following command to stop:
xxxxxxxxxxsudo systemctl stop host-stream.servicesudo systemctl disable host-stream.service
If using Yahboom's AI large model factory image or SSD factory image, you don't need to recreate the script. Just run the following command to restart the auto-start service:
sudo systemctl enable host-stream.servicesudo systemctl start host-stream.service