feat: 新增 playingCommand

This commit is contained in:
WJG 2024-06-02 19:38:06 +08:00
parent cfb63fe6f9
commit c82619f239
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
5 changed files with 114 additions and 63 deletions

View File

@ -22,16 +22,12 @@ export default {
profile: masterProfile,
},
speaker: {
// 小米账号
userId: process.env.MI_USER,
// 小米 ID
userId: "944123456", // 注意:不是手机号或邮箱,请在「个人信息」-「小米 ID」查看
// 账号密码
password: process.env.MI_PASS,
// 小爱音箱 ID 或名称
did: process.env.MI_DID,
// TTS 指令
ttsCommand: [5, 1],
// 设备唤醒指令
wakeUpCommand: [5, 3],
password: "123456",
// 小爱音箱 ID 或在米家中设置的名称
did: "小爱音箱Pro",
// 当消息以下面的关键词开头时,会调用 AI 来回复消息
callAIKeywords: ["请", "你", "傻妞"],
// 当消息以下面的关键词开头时,会进入 AI 唤醒状态
@ -50,5 +46,11 @@ export default {
onAIError: ["啊哦,出错了,请稍后再试吧!"],
// 无响应一段时间后,多久自动退出唤醒模式(默认 30 秒)
exitKeepAliveAfter: 30,
// TTS 指令,请到 https://home.miot-spec.com 查询具体指令
ttsCommand: [5, 1],
// 设备唤醒指令,请到 https://home.miot-spec.com 查询具体指令
wakeUpCommand: [5, 3],
// 查询是否在播放中指令,请到 https://home.miot-spec.com 查询具体指令
// playingCommand: [3, 1, 1],
},
};

105
README.md
View File

@ -16,7 +16,7 @@
这些独立的智能体,也可以彼此感知,彼此配合,构成一个更强大的协作网络。
而小爱音箱就像是你的智能家居专属管家,全心全意为你服务,释放智能家居真正的潜力。
而小爱音箱就像是你的智能家居专属管家,全心全意为你服务,释放智能家居真正的潜力。
## ✨ 项目亮点
@ -37,7 +37,7 @@
对于电脑小白或者不想自己配置代码运行环境Node的同学可以使用 Docker 启动方式。
请先按照[「配置参数」](#%EF%B8%8F-配置参数)章节,配置好你的 `.env``.migpt.js` 文件然后使用以下命令启动 docker
请先按照[「配置参数」](#%EF%B8%8F-配置参数)章节,配置好你的 `.env``.migpt.js` 文件然后使用以下命令启动 docker
```shell
docker run -d --env-file $(pwd)/.env \
@ -76,6 +76,41 @@ main();
## ⚙️ 配置参数
### .migpt.js
重命名本项目根目录下的 `.migpt.example.js` 文件为 `.migpt.js`
然后,将里面的配置参数修改成你自己的,参数含义如下:
| 参数名称 | 描述 | 示例 |
| ---------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------- |
| **bot** | | |
| `name` | 对方名称(小爱音箱) | `"傻妞"` |
| `profile` | 对方的个人简介/人设 | `"性别女,性格乖巧可爱,喜欢搞怪,爱吃醋。"` |
| **master** | | |
| `name` | 主人名称(我自己) | `"陆小千"` |
| `profile` | 主人的个人简介/人设 | `"性别男,善良正直,总是舍己为人,是傻妞的主人。"` |
| **room** | | |
| `name` | 会话群名称 | `"魔幻手机"` |
| `description` | 会话群简介 | `"傻妞和陆小千的私聊"` |
| **speaker** | | |
| `userId` | [小米 ID](https://account.xiaomi.com/fe/service/account/profile)(注意:不是手机号或邮箱) | `"944123456"` |
| `password` | 账户密码 | `"123456"` |
| `did` | 小爱音箱 ID 或名称 | `"小爱音箱 Pro"` |
| `ttsCommand` | 小爱音箱 TTS 指令([可在此查询](https://home.miot-spec.com) | `[5, 1]` |
| `wakeUpCommand` | 小爱音箱唤醒指令([可在此查询](https://home.miot-spec.com) | `[5, 3]` |
| `playingCommand` | 小爱音箱是否在播放中指令([可在此查询](https://home.miot-spec.com) | `[3, 1, 1]` |
| **speaker 其他参数(可选)** | | |
| `callAIKeywords` | 当消息以关键词开头时,会调用 AI 来响应用户消息 | `["请", "傻妞"]` |
| `wakeUpKeywords` | 当消息以关键词开头时,会进入 AI 唤醒状态 | `["召唤傻妞", "打开傻妞"]` |
| `exitKeywords` | 当消息以关键词开头时,会退出 AI 唤醒状态 | `["退出傻妞", "关闭傻妞"]` |
| `onEnterAI` | 进入 AI 模式的欢迎语 | `["你好,我是傻妞,很高兴认识你"]` |
| `onExitAI` | 退出 AI 模式的提示语 | `["傻妞已退出"]` |
| `onAIAsking` | AI 开始回答时的提示语 | `["让我先想想", "请稍等"]` |
| `onAIReplied` | AI 结束回答时的提示语 | `["我说完了", "还有其他问题吗"]` |
| `onAIError` | AI 回答异常时的提示语 | `["出错了,请稍后再试吧!"]` |
| `exitKeepAliveAfter` | 无响应一段时间后,多久自动退出唤醒模式(单位秒,默认 30 秒) | `30` |
### 环境变量
重命名本项目根目录下的 `.env.example` 文件为 `.env`
@ -84,10 +119,6 @@ main();
| 环境变量名称 | 描述 | 示例 |
| ---------------------- | ------------------------------------------------------------------------------------------- | ------------------------------------ |
| **小米服务** | | |
| `MI_USER` | [小米 ID](https://account.xiaomi.com/fe/service/account/profile)(注意:不是手机号或邮箱) | `"944123456"` |
| `MI_PASS` | 账户密码 | `"123456"` |
| `MI_DID` | 小爱音箱 ID 或名称 | `"小爱音箱 Pro"` |
| **OpenAI** | | |
| `OPENAI_API_KEY` | OpenAI API 密钥 | `abc123` |
| `OPENAI_MODEL` | 使用的 OpenAI 模型 | `gpt-4o` |
@ -102,40 +133,6 @@ main();
| `TTS_DOUBAO` | 豆包 TTS 接口 | `"https://example.com/tts.wav"` |
| `SPEAKERS_DOUBAO` | 豆包 TTS 音色列表接口 | `"https://example.com/tts-speakers"` |
### .migpt.js
重命名本项目根目录下的 `.migpt.example.js` 文件为 `.migpt.js`
然后,将里面的配置参数修改成你自己的,参数含义如下:
| 参数名称 | 描述 | 示例 |
| ---------------------------- | ------------------------------------------------------------- | -------------------------------------------------- |
| **bot** | | |
| `name` | 对方名称(小爱音箱) | `"傻妞"` |
| `profile` | 对方的个人简介/人设 | `"性别女,性格乖巧可爱,喜欢搞怪,爱吃醋。"` |
| **master** | | |
| `name` | 主人名称(我自己) | `"陆小千"` |
| `profile` | 主人的个人简介/人设 | `"性别男,善良正直,总是舍己为人,是傻妞的主人。"` |
| **room** | | |
| `name` | 会话群名称 | `"魔幻手机"` |
| `description` | 会话群简介 | `"傻妞和陆小千的私聊"` |
| **speaker** | | |
| `userId` | 小米账户 | `"12345678901"` |
| `password` | 账户密码 | `"123456"` |
| `did` | 小爱音箱 ID 或名称 | `"小爱音箱 Pro"` |
| `ttsCommand` | 小米音箱 TTS 指令([可在此查询](https://home.miot-spec.com) | `[5, 1]` |
| `wakeUpCommand` | 小米音箱唤醒指令([可在此查询](https://home.miot-spec.com) | `[5, 3]` |
| **speaker 其他参数(可选)** | | |
| `callAIKeywords` | 当消息以关键词开头时,会调用 AI 来响应用户消息 | `["请", "傻妞"]` |
| `wakeUpKeywords` | 当消息以关键词开头时,会进入 AI 唤醒状态 | `["召唤傻妞", "打开傻妞"]` |
| `exitKeywords` | 当消息以关键词开头时,会退出 AI 唤醒状态 | `["退出傻妞", "关闭傻妞"]` |
| `onEnterAI` | 进入 AI 模式的欢迎语 | `["你好,我是傻妞,很高兴认识你"]` |
| `onExitAI` | 退出 AI 模式的提示语 | `["傻妞已退出"]` |
| `onAIAsking` | AI 开始回答时的提示语 | `["让我先想想", "请稍等"]` |
| `onAIReplied` | AI 结束回答时的提示语 | `["我说完了", "还有其他问题吗"]` |
| `onAIError` | AI 回答异常时的提示语 | `["出错了,请稍后再试吧!"]` |
| `exitKeepAliveAfter` | 无响应一段时间后,多久自动退出唤醒模式(单位秒,默认 30 秒) | `30` |
## 💬 常见问题
**Q除了 OpenAI 还支持哪些模型,如何设置?**
@ -157,13 +154,15 @@ OPENAI_API_KEY=$MOONSHOT_API_KEY
**Q问小爱音箱问题 AI 没有回复,控制台有打印收到的新消息**
小爱音箱收到提问,默认不会调用 AI 进行回复。只有以唤醒词(`callAIKeywords`),比如:“请”、“你”等开头的消息,或在唤醒模式下才会调用 AI 进行回复。
小爱音箱收到提问,默认不会调用 AI 进行回复。
只有以唤醒词(`callAIKeywords`)开头的消息才会回复,比如:“请问 xxx”、“你 xxx” 等。
**Q小爱音箱没有播放 AI 的回复,控制台有打印 AI 的回复**
不同型号的小爱音箱 TTS 指令不同: [https://github.com/idootop/mi-gpt/issues/5](https://github.com/idootop/mi-gpt/issues/5#issuecomment-2122881495)
可以到 <https://home.miot-spec.com> 查询具体的 TTS 指令,然后修改配置文件中的指令参数。
请到 <https://home.miot-spec.com> 查询具体指令,并修改配置文件中的 `ttsCommand` 参数。
<details>
<summary>👉 查看教程</summary>
@ -173,21 +172,33 @@ OPENAI_API_KEY=$MOONSHOT_API_KEY
</details>
**Q回答过长的情况下小爱音箱没有读完整前面的句子**
**Q回答过长的情况下小爱音箱只读了最后一句,前面的句子没有读(完整)**
通过小米接口获取到的设备播放状态不是最新的,参考 [issue#14](https://github.com/idootop/mi-gpt/issues/14)
部分小爱音箱型号不支持通过 Mina 获取设备播放状态,只能通过 MiOT 指令查询。
请到 <https://home.miot-spec.com> 查询具体指令,并修改配置文件中的 `playingCommand` 参数。
<details>
<summary>👉 查看教程</summary>
![](https://raw.githubusercontent.com/idootop/mi-gpt/main/assets/playing.png)
</details>
**Q怎样使用豆包的音色**
很遗憾,豆包上的音色需要豆包 TTS 接口支持,本项目暂不对外提供此服务。
此功能需要豆包 TTS 接口支持,本项目暂不对外提供此服务。
**Q我还有其他问题**
请在此处提交 [issue](https://github.com/idootop/mi-gpt/issues),有时间我会查看并解答。
请在此处提交 [issue](https://github.com/idootop/mi-gpt/issues) 反馈,并提供详细的问题描述和相关错误截图
## 🚨 免责声明
本项目旨在分享学习大型语言模型LLM智能对话及智能家居自动化相关知识严禁用于任何商业目的或违反所在地区的法律法规。使用者须知悉本项目代码可能存在未知的缺陷或风险因使用本项目引起的任何形式的损失或损害包括但不限于设备故障、账户封禁等使用者需自行承担全部责任。
本项目仅供学习和研究目的,不得用于任何商业活动。用户在使用本项目时应遵守所在地区的法律法规,对于违法使用所导致的后果,本项目及作者不承担任何责任。
本项目可能存在未知的缺陷和风险(包括但不限于设备损坏和账号封禁等),使用者应自行承担使用本项目所产生的所有风险及责任。
作者不保证本项目的准确性、完整性、及时性、可靠性,也不承担任何因使用本项目而产生的任何损失或损害责任。
使用本项目即表示您已阅读并同意本免责声明的全部内容。
## ❤️ 鸣谢

BIN
assets/playing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

View File

@ -19,6 +19,7 @@ type Speaker = {
};
type ActionCommand = [number, number];
type PropertyCommand = [number, number, number];
export type BaseSpeakerConfig = MiServiceConfig & {
debug?: boolean;
@ -27,7 +28,7 @@ export type BaseSpeakerConfig = MiServiceConfig & {
*/
tts?: TTSProvider;
/**
* TTS command
* TTS
*
* Prolx06 -> [5, 1]
*
@ -35,13 +36,21 @@ export type BaseSpeakerConfig = MiServiceConfig & {
*/
ttsCommand?: ActionCommand;
/**
* command
*
*
* Prolx06 -> [5, 3]
*
* https://home.miot-spec.com
*/
wakeUpCommand?: ActionCommand;
/**
*
*
* Playlx05 -> [3, 1, 1]
*
* https://home.miot-spec.com
*/
playingCommand?: PropertyCommand;
/**
* 500 1
*/
@ -61,6 +70,7 @@ export class BaseSpeaker {
tts: TTSProvider;
ttsCommand: ActionCommand;
wakeUpCommand: ActionCommand;
playingCommand?: PropertyCommand;
config: MiServiceConfig;
constructor(config: BaseSpeakerConfig) {
this.config = config;
@ -68,6 +78,7 @@ export class BaseSpeaker {
debug = false,
checkInterval = 1000,
tts = "xiaoai",
playingCommand,
ttsCommand = [5, 1],
wakeUpCommand = [5, 3],
audioBeep = process.env.audioBeep,
@ -78,6 +89,7 @@ export class BaseSpeaker {
this.tts = tts;
this.ttsCommand = ttsCommand;
this.wakeUpCommand = wakeUpCommand;
this.playingCommand = playingCommand;
}
async initMiServices() {
@ -226,18 +238,33 @@ export class BaseSpeaker {
await sleep(3000);
// 等待回答播放完毕
while (true) {
let playing: any = { status: "idle" };
if (this.playingCommand) {
const res = await this.MiIOT!.getProperty(
this.playingCommand[0],
this.playingCommand[1]
);
if (this.debug) {
this.logger.debug(jsonEncode({ playState: res ?? "undefined" }));
}
if (res === this.playingCommand[2]) {
playing = { status: "playing" };
}
} else {
const res = await this.MiNA!.getStatus();
if (this.debug) {
this.logger.debug(jsonEncode(res));
this.logger.debug(jsonEncode({ playState: res ?? "undefined" }));
}
playing = { ...playing, ...res };
}
if (
!this.responding || // 有新消息
(res?.status === "playing" && res?.media_type) // 小爱自己开始播放音乐
(playing.status === "playing" && playing.media_type) // 小爱自己开始播放音乐
) {
// 响应被中断
return "break";
}
if (res && res.status !== "playing") {
if (playing.status !== "playing") {
break;
}
await sleep(this.checkInterval);

View File

@ -11,6 +11,7 @@ export async function testSpeaker() {
debug: true,
});
await speaker.initMiServices();
await testAISpeakerStatus(speaker);
// await testSpeakerResponse(speaker);
await testSpeakerStreamResponse(speaker);
// await testSpeakerGetMessages(speaker);
@ -19,6 +20,16 @@ export async function testSpeaker() {
// await testAISpeaker(speaker);
}
async function testAISpeakerStatus(speaker: AISpeaker) {
const playingCommand = [5, 3, 1];
const res1 = await speaker.MiIOT!.getProperty(
playingCommand[0],
playingCommand[1]
);
const res2 = await speaker.MiNA!.getStatus();
console.log("finished");
}
async function testAISpeaker(speaker: AISpeaker) {
speaker.askAI = async (msg) => {
return { text: "你说:" + msg.text };