Connected! Setting up XiaoAi Speaker with ChatGPT

Publish: 2024-04-12 | Modify: 2024-04-12

xiaogpt is a project that xiaoz learned about through a friend's recommendation, and it allows the Xiaomi AI speaker to be connected to large models such as ChatGPT and ChatGLM, making the AI speaker smarter.

Currently, xiaoz has successfully connected the first-generation Xiaomi AI speaker to ChatGPT. You can see the effect through the Bilibili link below. If it's useful, don't forget to give it a thumbs up.

Before You Start

This tutorial is slightly complicated and not suitable for beginners. It is recommended for IT professionals with a certain development foundation to try. Before you start, you need to prepare:

  • Xiaomi AI speaker with Bluetooth enabled
  • Magic Internet access tool
  • ChatGPT or ChatGLM API SK
  • A long-running local network server, such as a NAS device
  • A Windows computer on the local network
  • Familiarity with basic Python programming language

Obtaining Xiaomi Speaker DID

xiaogpt depends on the Xiaomi speaker DID to control and communicate. We need to use the miservice_fork tool to obtain the DID. This tool is developed using Python, so your computer needs to have Python 3 installed. xiaoz is operating on a Windows computer (any local network device can be used). It is recommended to search the Internet for instructions on how to install Python 3.

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

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

Docker Installation of xiaogpt

Before installation, we need to create the xiaogpt configuration file config.json. The configuration file used by xiaoz 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, 18 years old, an AI assistant. Your owner is xiaoz. Please keep your answers concise and to the point, and the total number of responses should not exceed 150 words.",
        "keyword": [
                "Please"
        ],
        "stream": true
}

The configuration parameters are as follows:

  • openai_key: SK provided by OpenAI. Due to the inability to directly access the OpenAI API in China, consider using a third-party OpenAI API intermediary service.
  • api_base: Since xiaoz uses a third-party OpenAI API intermediary service, the interface domain here needs to be changed to the domain of the third party, and v1 needs to be appended at the end.
  • hardware: The model number on the bottom of the Xiaomi AI speaker. If the model number is not found or is incorrect, use micli mina to find the model number.
  • mute_xiaoai: Enable interrupt service, which means not allowing Xiaoai to finish the answer herself and then let ChatGPT take over. If not enabled, you have to wait for Xiaoai to finish answering each time.
  • account: Xiaomi account username
  • password: Xiaomi account password
  • mi_did: The DID obtained above
  • bot: Which API interface to use
  • tts: Default mi is the original voice of Xiaoai's response. You can also use Edge TTS, but xiaoz found Edge TTS to be very unstable in testing.
  • prompt: Prompt words, can be modified
  • keyword: Trigger keywords
  • stream: Better efficiency when enabled

Obtaining hardware information

xiaoz uses the first-generation Xiaomi AI speaker, and the model number displayed on the bottom is MDZ-25-DA, which xiaogpt does not recognize.

You may need to modify it to S12A/S12/L07A and try each one.

Then continue to use Docker Compose for installation, and it is recommended to install it on a NAS. The complete docker-compose.yaml content used by xiaoz 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 the directory of your configuration file. For example, if your configuration file is located at ./config/config.json, then enter ./config, and then enter docker-compose up -d to start.

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

Connecting to ChatGLM

If you cannot solve the issue of accessing OpenAI and cannot find a suitable OpenAI intermediary service, consider connecting to the domestic large model ChatGLM: https://open.bigmodel.cn/. Obtain the SK in the Zhidu AI backend and fill it into the configuration file. The configuration 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": "The response cannot exceed 200 words",
        "keyword": [
                "Please"
         ],
         "stream": true
}

Troubleshooting

If adding the word "Please" after triggering Xiaoai does not work, you can use docker logs xiaogpt to view the container logs. If you encounter an error such as Exception: we have no hardware: S12A please check, it may not be due to a hardware error, but could be due to other configuration parameter errors. Check each parameter.

Real Experience

At present, using xiaogpt to connect the Xiaomi AI speaker to ChatGPT is not perfect, but it has expanded the ways to use the AI speaker, making it more interesting and intelligent. It is still worth trying for friends who have the conditions.

After actual experience, xiaoz found the following issues:

  • Each time there is a conversation, Xiaoai will preemptively answer part of it, and then ChatGPT takes over.
  • If Edge TTS is enabled, it will be very unstable, often unable to output audio or respond slowly. It is recommended to use the default Xiaoai built-in voice.
  • There are bugs in continuous conversations, which may cause the conversation to be unable to exit completely.

Summary

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

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

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

In addition, xiaoz has set up a NAS user exchange group, and those interested can scan the QR code to join.

4d46cac8d56d7b26.png


Comments