Success! Integrating Xiaomi AI Speaker with ChatGPT

xiaogptxiaomi ai speakerchatgpt integrationchatglmdocker setup
Published·Modified·

xiaoz learned about the xiaogpt project through a friend's recommendation. xiaogpt allows you to connect your home Xiaomi AI speaker to large language models like ChatGPT and ChatGLM, making the speaker smarter.

1264da806ff51a87.png

xiaoz has successfully connected their first-generation Xiaomi AI speaker to ChatGPT. You can check the effect via the Bilibili link below. If you find it useful, please remember to like, share, and subscribe.

Before You Start

This tutorial is slightly complex and not recommended for beginners. It is recommended for IT professionals with some development experience. Before starting, you need to prepare:

  • A Xiaomi AI speaker with Bluetooth enabled
  • A tool for accessing the internet
  • ChatGPT or ChatGLM API SK
  • A long-running intranet server, such as a NAS device
  • An intranet Windows computer
  • Familiarity with basic Python programming

Get the Xiaomi Speaker DID

xiaogpt relies on the Xiaomi speaker DID for control and communication. We first need to use the miservice_fork tool, which is developed in Python. Therefore, your computer needs to have Python 3 installed. xiaoz operated on their Windows computer (any intranet computer device will do). For how to install Python 3, it is recommended to search online.

# Install miservice_fork
pip install miservice_fork
# Set Xiaomi username
set MI_USER=xxxx
# Set Xiaomi account password
set MI_PASS=xxx
# Get DID
micli list

The result is shown in the image below. Find the DID number of your Xiaomi speaker and record it.

74355bf72401c977.png

Install xiaogpt with Docker

Before installation, we need to create the xiaogpt configuration file config.json. xiaoz's configuration file is as follows:

{
        "openai_key":"sk-xxx",
    "api_base":"https://xxx.com/v1",
        "hardware":"S12A",
        "mute_xiaoai": true,
        "account": "xxx",
        "password":"xxx.",
        "mi_did":"xxx",
        "bot": "chatgptapi",
    "tts": "mi",
        "prompt": "Your name is xxx, you are 18 years old, and you are an AI assistant. Your master is xiaoz. Your answers should be concise, and the total number of characters should not exceed 150.",
        "keyword": [
                "Please"
        ],
        "stream": true
}

The meaning of the configuration parameters is as follows:

  • openai_key: The SK provided by OpenAI. Since the OpenAI API cannot be directly accessed from within China, you can try using a third-party OpenAI API relay service.
  • api_base: Since xiaoz uses a third-party OpenAI API relay service, the interface domain name here needs to be changed to the third-party domain name, and v1 must be added at the end.
  • hardware: The model number is on the back of the Xiaomi speaker. Enter it here. If you cannot find the model number on the back or the model number is incorrect, you can use micli mina to find the model number.
  • mute_xiaoai: Enable the interrupt service, so that the speaker's own answer is not finished, and then let ChatGPT take over. If not enabled, you have to wait for the speaker to finish answering every time.
  • account: The username of the Xiaomi account.
  • password: The password of the Xiaomi account.
  • mi_did: The DID obtained above.
  • bot: Which API interface to use.
  • tts: Default mi is the native Xiaomi speaker voice. You can also use Edge TTS, but xiaoz tested that Edge TTS is very unstable.
  • prompt: The prompt, which you can modify yourself.
  • keyword: The trigger keyword.
  • stream: When enabled, efficiency is better.

About getting the hardware

xiaoz uses the first-generation Xiaomi speaker, which shows MDZ-25-DA on the bottom. This model number cannot be recognized by xiaogpt.

60a483db0c73b50c.png

You may need to change it to S12A/S12/L07A. In short, try modifying them one by one.

Then continue to use Docker Compose for installation. It is recommended to install it on a NAS. xiaoz's complete docker-compose.yaml content is as follows:

version: '3.8'
services:
 xiaogpt:
   image: yihong0618/xiaogpt
   volumes:
     - ./config:/config
   ports:
     - "9527:9527"
   restart: always
   command: --config=/config/config.json
   container_name: xiaogpt

Change ./config to your configuration file directory. For example, if your configuration file is located at ./config/config.json, then fill in ./config. Then enter docker-compose up -d to start.

If everything goes smoothly, try waking up Xiao Ai and adding the word "Please" to trigger ChatGPT, as shown in the video above.

Connect to ChatGLM

If you cannot solve the OpenAI access problem and have not found a suitable OpenAI relay service, you can try connecting to the domestic large model ChatGLM: https://open.bigmodel.cn/. After obtaining the SK in the Zhipu AI backend, fill it into the configuration file. The configuration reference for using ChatGLM is as follows:

{
        "glm_key":"xxx",
        "hardware":"S12A",
        "mute_xiaoai": true,
        "account": "xxx",
        "password":"xxx.",
        "mi_did":"xxx",
        "bot": "glm",
        "tts": "mi",
        "use_glm": true,
        "prompt": "Answer should not exceed 200 characters",
        "keyword": [
                "Please"
         ],
         "stream": true
}

Troubleshooting

If adding "Please" after triggering Xiao Ai has no effect, you can use docker logs xiaogpt to view the container logs. If an error like Exception: we have no hardware: S12A please check appears, it may not be that the hardware is written incorrectly, but could be other configuration parameter errors. In any case, check each parameter one by one.

Actual Experience

Currently, connecting the Xiaomi speaker to ChatGPT via xiaogpt is not perfect, but it has expanded the gameplay of the Xiaomi speaker, making it more interesting and intelligent. Friends with conditions are still worth trying.

After xiaoz's actual experience, the following problems were found:

  • In each conversation, Xiao Ai will answer first, and then ChatGPT will take over.
  • If Edge TTS is enabled, it will be very unstable, often failing to output audio or having slow response times. It is recommended to use the built-in voice by default.
  • Continuous dialogue has bugs, which may lead to an inability to fully exit continuous dialogue.

Summary

When using xiaogpt to connect the Xiaomi speaker to ChatGPT, pay attention to the following issues:

  • The Xiaomi speaker needs to have Bluetooth enabled.
  • Install miservice_fork to get the correct DID number.
  • Get the hardware number from the bottom of the speaker. If it is a first-generation device, you can try filling in: S12A/S12/L07A.
  • The domestic network cannot correctly access the OpenAI API. You can consider a third-party OpenAI relay service or connect to ChatGLM.

xiaogpt project address: https://github.com/yihong0618/xiaogpt, thanks to the author for the contribution.

Additionally, xiaoz has created a NAS user group chat. If you are interested, you can scan the QR code to join.

4d46cac8d56d7b26.png