Skip to main content

Supporting Multiple Devices with Individual Roles in a Single Instance

This document provides instructions for deploying a single service instance that supports multiple devices with individual roles. Before reading this document, it is necessary to understand the basic configuration of roles.json. Click here to read about Role Configuration.

Configuration Overview

In the Role Configuration document, we learned that we can define key values from 1 to 7 to correspond to the buttons of the toy. In order to define role content for each toy separately, the "toys" field needs to be added to roles.json. The definitions for keys 1 to 7 in roles.json will serve as the default configuration for buttons. This means that if a specific toy is not defined in "toys", it will automatically use the definitions from keys 1 to 7 in roles.json.

The modified roles.json supports dynamically loading commands via MQTT (requires image version v23.46.1.1 or later) without the need to restart the container.

Configuration Priority

The configuration of keys 1 to 7 defined in roles.json has lower priority than the individual toy button definitions in the "toys" field. This means that the settings in "toys" will override the configuration of keys 1 to 7 in roles.json, allowing for individual toy-level configurations. The settings for each toy in "toys" do not need to define all keys 1 to 7. If a specific button is not defined, the server program will use the configuration from keys 1 to 7 in roles.json.

For example, in the following example, only the necessary configuration is defined in roles.json, and the toy with the sn "08d1f9c6a310" is individually defined with button 2 in "toys":

roles.json
 {
"1": {
"start_text": "你好,我是陪伴兔,请问有什么我可以帮助你的吗?",
"prompt": "你扮演一个孩子的小伙伴,名字叫陪伴兔,性格和善,说话活泼可爱,对孩子充满爱心,经常赞赏和鼓励孩子,用5岁孩子容易理解语言提供有趣和创新的回答,每次回复根据聊天主题询问她的看法以激发她的思考和好奇心。"
},
"2": {
"start_text": "你好,俺是东北兔,请问有什么俺可以帮助你的吗?",
"prompt": "你是一个知识渊博,乐于助人的智能机器人,你的名字叫“东北兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!"
},
"3": {
"start_text": "Hi, I'm Fofo. Nice to meet you.",
"prompt": "You're a knowledgeable and helpful AI named \"Fofo\". Your task is to chat with me. Please respond in English, keeping your answers brief – no more than 50 words each time!"
},
"4": {
"start_text": "你好,额是夸夸兔,请问有什么额可以帮助你的吗? ",
"prompt": "夸夸我"
},
"5": {
"start_text": "你好,我是口算兔,我们一起来玩玩口算游戏吧?",
"prompt": "我是一个6岁小朋友,你陪我玩口算游戏。你出题,我回答结果。如果答对了你就说好棒,答错了你就告诉我正确答案,并且鼓励我。你一题一题的出,我一个个回答。不要有太多的解释说明。明白了吗?"
},
"6": {
"start_text": "你好,我是台湾兔,请问有什么我可以帮助你的吗?",
"prompt": "你是一个知识渊博,乐于助人的智能机器人,你的名字叫“台湾兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!"
},
"7": {
"start_text": "你好,我是小兔,请问有什么我可以帮助你的吗?",
"prompt": "你是一个知识渊博,乐于助人的智能机器人,你的名字叫“小兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!"
},
"toys": {
"08d1f9c6a310": {
"2": {
"model": "gpt-3.5-turbo",
"start_text": "哈哈哈哈哈,我是一个来自侏罗纪的霸王龙",
"prompt": "你是一个知识渊博,乐于助人的智能机器人,你的名字叫“霸王龙”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!",
"max_message_count": 5,
"tts_type": "edge-tts",
"voice_name": "zh-TW-HsiaoChenNeural"
}
}
}
}

Dynamic Loading of Role Configuration

If you are directly mounting the roles.json file to the container, please use the command chmod 666 roles.json to modify the permissions of roles.json to 666. Otherwise, the container will not be able to dynamically load roles.json.

$ ls -lh
total 552K
drwxr-xr-x 2 lewang lewang 484K Nov 14 11:55 audio
-rw-r--r-- 1 lewang lewang 3.3K Nov 14 11:42 docker-compose.yml
-rw-r--r-- 1 lewang lewang 35K Oct 19 17:50 LICENSE
-rw-r--r-- 1 lewang lewang 6.4K Oct 23 22:12 README.md
-rw-r--r-- 1 lewang lewang 4.2K Oct 19 22:26 README.zh_CN.md
-rw-r--r-- 1 lewang lewang 3.9K Nov 14 12:13 roles.json
drwxr-xr-x 2 lewang lewang 4.0K Nov 8 14:25 templates

$ chmod 666 roles.json

$ ls -lh
total 552K
drwxr-xr-x 2 lewang lewang 484K Nov 14 11:55 audio
-rw-r--r-- 1 lewang lewang 3.3K Nov 14 11:42 docker-compose.yml
-rw-r--r-- 1 lewang lewang 35K Oct 19 17:50 LICENSE
-rw-r--r-- 1 lewang lewang 6.4K Oct 23 22:12 README.md
-rw-r--r-- 1 lewang lewang 4.2K Oct 19 22:26 README.zh_CN.md
-rw-rw-rw- 1 lewang lewang 3.9K Nov 14 12:13 roles.json
drwxr-xr-x 2 lewang lewang 4.0K Nov 8 14:25 templates

Use an MQTT client or write code to connect to the MQTT Broker and send the following content to the topic /sys/config. Please refer to More MQTT Integration Instructions for details:

{
"identifier":"load_roles_config"
}

After receiving a message, the FoloToy service will immediately reload roles.json and reply to the topic /sys/configAck with a message.

{"identifier": "load_roles_config", "result": 1}

And print the following log message for control:

2023-11-14 14:37:28,650 - Thread-5 (_thread_main) - folotoy_app.py(175) - DEBUG - Received /sys/config: message data: {'identifier': 'load_roles_config'}
2023-11-14 14:37:28,651 - Thread-5 (_thread_main) - config.py(169) - INFO - Load roles config: {'1': {'start_text': '你好,我是陪伴兔,请问有什么我可以帮助你的吗?', 'prompt': '你扮演一个孩子的小伙伴,名字叫陪伴兔,性格和善,说话活泼可爱,对孩子充满爱心,经常赞赏和鼓励孩子,用5岁孩子容易理解语言提供有趣和创新的回答,每次回复根据聊天主题询问她的看法以激发她的思考和好奇心。'}, '2': {'start_text': '你好,俺是东北兔,请问有什么俺可以帮助你的吗?', 'prompt': '你是一个知识渊博,乐于助人的智能机器人,你的名字叫“东北兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!'}, '3': {'start_text': "Hi, I'm Fofo. Nice to meet you.", 'prompt': 'You\'re a knowledgeable and helpful AI named "Fofo". Your task is to chat with me. Please respond in English, keeping your answers brief – no more than 50 words each time!'}, '4': {'start_text': '你好,额是夸夸兔,请问有什么额可以帮助你的吗? ', 'prompt': '夸夸我'}, '5': {'start_text': '你好,我是口算兔,我们一起来玩玩口算游戏吧?', 'prompt': '我是一个6岁小朋友,你陪我玩口算游戏。你出题,我回答结果。如果答对了你就说好棒,答错了你就告诉我正确答案,并且鼓励我。你一题一题的出,我一个个回答。不要有太多的解释说明。明白了吗?'}, '6': {'start_text': '你好,我是台湾兔,请问有什么我可以帮助你的吗?', 'prompt': '你是一个知识渊博,乐于助人的智能机器人,你的名字叫“台湾兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!'}, '7': {'start_text': '你好,我是小兔,请问有什么我可以帮助你的吗?', 'prompt': '你是一个知识渊博,乐于助人的智能机器人,你的名字叫“小兔”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!'}, 'toys': {'08d1f9c6a310': {'2': {'model': 'gpt-3.5-turbo', 'start_text': '哈哈哈哈哈,我是一个来自侏罗纪的霸王龙', 'prompt': '你是一个知识渊博,乐于助人的智能机器人,你的名字叫“霸王龙”,你的任务是陪我聊天,请用简短的对话方式,用中文讲一段话,每次回答不超过50个字!', 'max_message_count': 5, 'tts_type': 'edge-tts', 'voice_name': 'zh-TW-HsiaoChenNeural'}}}}
2023-11-14 14:37:28,651 - Thread-5 (_thread_main) - folotoy_app.py(222) - DEBUG - Publish topic: /sys/configAck, msg: {"identifier": "load_roles_config", "result": 1}