Large Model Web Search and Visual Reasoning Example
Description
-
A large model is connected to voice conversation to implement a voice interaction system that includes recording, speech transcription, visual reasoning, web search, conversation generation, and voice playback.
-
Example code path:
~/kuavo_ros_application/src/kuavo_large_model/kuavo_kimi_model/kimi_python3_demo.py
Program Logic
- Import modules and define global variables
- Purpose: import the required Python standard libraries and third-party libraries, and define global parameters for recording and audio processing.
- RosAudioPublisher class
-
Purpose: initialize the ROS node, create an audio data publisher, transmit the data stream to the lower-level controller, and play audio through the speaker device
-
publish_pcm_audio_data function: publish the PCM file to the audio_data topic in int16 format.
- recorder function
-
Purpose: implement the recording function, automatically starting and stopping recording based on the volume threshold and silence duration.
-
Key logic:
-
Use pyaudio to open an audio stream and read data.
-
Calculate the audio volume via audioop.rms.
-
If the volume exceeds the threshold, start recording; if the silence duration exceeds the set value, stop recording.
-
Save the recorded data as a PCM file.
-
- kimi_vision_chat function
-
Purpose: use the Kimi API (via the https://api.moonshot.cn/v1 endpoint) to call the moonshot-v1-8k-vision-preview vision model for conversation, and convert the reply to speech for playback via TTS.
-
Key logic:
-
Capture one frame from the camera and save it as picture.jpg.
-
Build the request body and send it to the Kimi API.
-
Parse the reply content returned by the API.
-
Call tts_xunfei to convert the reply to speech and play it.
-
- kimi_network_chat function
-
Purpose: use the Kimi API (via the https://api.moonshot.cn/v1 endpoint) to call the moonshot-v1-128k language model for conversation, and convert the reply to speech for playback via TTS.
-
Key logic:
-
Build the request body and send it to the Kimi API.
-
Call the search_impl function to perform a web search.
-
Continuously call the chat function in a loop to obtain the reply content.
-
Parse the reply content returned by the API.
-
Call tts_xunfei to convert the reply to speech and play it.
-
- Client class
-
Purpose: implement WebSocket communication with the iFlytek Real-time Speech Transcription Standard Edition (RTASR) service.
-
Key logic:
-
Initialize the WebSocket connection and generate the signature.
-
Send audio data to the RTASR service.
-
Receive and parse the transcription results returned by RTASR.
-
- Main program
-
Purpose: main program logic, looping through recording, transcription, conversation, and playback.
-
Key logic:
-
Initialize the video capture object.
-
Call recorder to record audio.
-
Use the Client class to send the recording to the RTASR service.
-
Define regular expression keywords.
-
If the user input contains a keyword, call kimi_network_chat for conversation and web search and play the reply.
-
If the user input does not contain a keyword, call kimi_vision_chat for conversation and visual reasoning and play the reply.
-
Environment Setup
Installing Dependencies on the Upper-Level Computer
- Run the one-click deployment script
cd ~/kuavo_ros_application/src/ros_audio/kuavo_audio_player/scripts
chmod +x deploy_autostart_h12pro.sh
./deploy_autostart_h12pro.sh- Install additional dependencies
# 安装音频和摄像头相关系统库
sudo apt install -y portaudio19-dev python3-pyaudio v4l-utils
# 安装Python依赖(指定版本)
python3 -m pip install pyaudio==0.2.11 websocket-client==1.6.1 opencv-python==4.8.0.76
python3 -m pip install numpy==1.22.2 requests==2.31.0 openai==1.0.0Notes
⚠️ Note: This example uses iFlytek's RTASR and TTS models, as well as Beijing Moonshot's moonshot-v1-8k-vision-preview vision model and moonshot-v1-128k language model. All four models are paid models. You need to create accounts, top up, and obtain API Keys yourself, then copy the obtained API Keys into the corresponding places in the program. When in use, the robot's upper-level computer must be connected to the external network (with internet access).
-
The speech-to-text model used in this example is iFlytek's model: https://www.xfyun.cn/
-
iFlytek Real-time Speech Transcription Standard Edition (RTASR)
- Visit the iFlytek platform, select Speech Recognition - Real-time Speech Transcription Standard Edition: https://console.xfyun.cn/services/rta, and obtain the app_id and api_key.
- Replace the app_id and api_key on lines 425 and 426 of the program
~/kuavo_ros_application/src/kuavo_large_model/kuavo_kimi_model/kimi_python3_demo.pywith the ones you obtained.
-
iFlytek Speech Synthesis (TTS) model
- Visit the iFlytek platform, select Speech Synthesis - Online Speech Synthesis: https://console.xfyun.cn/services/tts, and obtain the APPID, APISecret, and APIKey.
- Replace the APPID, APISecret, and APIKey on lines 139 and 140 of the program
~/kuavo_ros_application/src/kuavo_large_model/kuavo_kimi_model/tts_ws_python3_demo.pywith the ones you obtained.
-
-
The large models used in this example are the moonshot-v1-8k-vision-preview vision model and the moonshot-v1-128k language model released by Beijing Moonshot: https://platform.moonshot.cn/
- Obtain a Kimi API Key:
- Go to https://platform.moonshot.cn/ and click User Center.
- Top up, then create and obtain an API Key.
- Replace the api-key on lines 224 and 278 of the program
~/kuavo_ros_application/src/kuavo_large_model/kuavo_kimi_model/kimi_python3_demo.pywith the one you obtained.
- Obtain a Kimi API Key:
Execution
⚠️ Note: Make sure ROS master-slave communication between the upper-level computer and the lower-level controller is working properly.
-
Lower-level controller
cd kuavo-ros-opensource #进入下位机工作空间 sudo su catkin build kuavo_audio_player source devel/setup.bash roslaunch kuavo_audio_player play_music.launch -
Result

-
Upper-level computer
sudo modprobe -r uvcvideo && sudo modprobe uvcvideo cd kuavo_ros_application # 进入上位机工作空间(根据实际部署目录切换) source /opt/ros/noetic/setup.bash source devel/setup.bash python3 src/kuavo_large_model/kuavo_kimi_model/kimi_python3_demo.py