From 7052b5fedb74335d9aca96192b69f723c80ec403 Mon Sep 17 00:00:00 2001 From: widyhu2020 <444633706@qq.com> Date: Sat, 25 Mar 2023 18:52:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E5=85=B3=E9=94=AE=E5=AD=97=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=8E=BBchatgpt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaogpt.py | 77 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/xiaogpt.py b/xiaogpt.py index a577c0cf..2b536aa3 100755 --- a/xiaogpt.py +++ b/xiaogpt.py @@ -34,8 +34,8 @@ MI_USER = "" MI_PASS = "" OPENAI_API_KEY = "" -KEY_WORD = "帮我" PROMPT = "请用100字以内回答" +KEY_WORD = ["请问", "帮我", "给我", "你觉得", "告诉我"] # simulate the response from xiaoai server by type the input. CLI_INTERACTIVE_MODE = False @@ -304,7 +304,9 @@ async def ask_chatgpt(self, query): return "" async def get_if_xiaoai_is_playing(self): + # 这里有bug,后面的库并没有这个方法 playing_info = await self.mina_service.player_get_status(self.device_id) + print(playing_info) # WTF xiaomi api is_playing = ( json.loads(playing_info.get("data", {}).get("info", "{}")).get("status", -1) @@ -313,13 +315,50 @@ async def get_if_xiaoai_is_playing(self): return is_playing async def stop_if_xiaoai_is_playing(self): + # 这里有bug,后面的库并没有这个方法 is_playing = await self.get_if_xiaoai_is_playing() if is_playing: # stop it await self.mina_service.player_pause(self.device_id) + async def do_chat_with_xiaoai(self, last_record, query, key): + if query.find(key) == 0: + # only mute when your clause start's with the keyword + if self.this_mute_xiaoai: + await self.stop_if_xiaoai_is_playing() + self.this_mute_xiaoai = False + # drop 帮我回答 + # query = query.replace(KEY_WORD, "") + query = f"{query},{PROMPT}" + # waiting for xiaoai speaker done + if not self.mute_xiaoai: + await asyncio.sleep(4) + await self.do_tts("正在问GPT请耐心等待") + try: + print( + "以下是小爱的回答: ", + last_record.get("answers")[0] + .get("tts", {}) + .get("text"), + ) + except Exception as e: + print(e) + print("小爱没回") + message = await self.ask_gpt(query) + # tts to xiaoai with ChatGPT answer + print("以下是GPT的回答: " + message) + await self.do_tts(message) + if self.mute_xiaoai: + while 1: + is_playing = await self.get_if_xiaoai_is_playing() + time.sleep(2) + if not is_playing: + break + self.this_mute_xiaoai = True + + async def run_forever(self): - print(f"Running xiaogpt now, 用`{KEY_WORD}`开头来提问") + print(f"Running xiaogpt now, 用`请问`开头来提问") async with ClientSession() as session: await self.init_all_data(session) while 1: @@ -343,38 +382,8 @@ async def run_forever(self): if new_timestamp > self.last_timestamp: self.last_timestamp = new_timestamp query = last_record.get("query", "") - if query.find(KEY_WORD) != -1: - # only mute when your clause start's with the keyword - if self.this_mute_xiaoai: - await self.stop_if_xiaoai_is_playing() - self.this_mute_xiaoai = False - # drop 帮我回答 - query = query.replace(KEY_WORD, "") - query = f"{query},{PROMPT}" - # waiting for xiaoai speaker done - if not self.mute_xiaoai: - await asyncio.sleep(4) - await self.do_tts("正在问GPT请耐心等待") - try: - print( - "以下是小爱的回答: ", - last_record.get("answers")[0] - .get("tts", {}) - .get("text"), - ) - except: - print("小爱没回") - message = await self.ask_gpt(query) - # tts to xiaoai with ChatGPT answer - print("以下是GPT的回答: " + message) - await self.do_tts(message) - if self.mute_xiaoai: - while 1: - is_playing = await self.get_if_xiaoai_is_playing() - time.sleep(2) - if not is_playing: - break - self.this_mute_xiaoai = True + for key in KEY_WORD: + await self.do_chat_with_xiaoai(last_record, query, key) else: if self.verbose: print("No new xiao ai record") From d74a0c491d3c72d86c4fdd78b2efde047b471405 Mon Sep 17 00:00:00 2001 From: widyhu2020 <444633706@qq.com> Date: Sat, 25 Mar 2023 18:55:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=AF=B4=E6=98=8E=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8fe8caa..d5703da2 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Play ChatGPT with Xiaomi AI Speaker 7. 因为现在必须指定 conversation_id 和 parent_id 来持续对话,会自动建一个新的 conversation 8. 如果上面不可用,可以尝试用手机抓包,https://userprofile.mina.mi.com/device_profile/v2/conversation 找到 cookie 利用 --cookie '${cookie}' cookie 别忘了用单引号包裹 9. 默认用目前 ubus, 如果你的设备不支持 ubus 可以使用 --use_command 来使用 command 来 tts -10. 使用 --mute_xiaoai 选项,可以让小爱不回答,但会频繁请求,玩一下可以使用,不建议一直用 +10. 使用 --mute_xiaoai 选项,可以让小爱不回答,但会频繁请求,玩一下可以使用,不建议一直用(该参数有bug,无法使用) 11. 使用 --account ‘${account}’ --password ‘${password}’ 可以不进行步骤 2 12. 如果有能力可以自行替换唤醒词,也可以去掉唤醒词,源码在 https://github.com/yihong0618/xiaogpt/blob/main/xiaogpt.py#L32 13. 可以使用 gpt-3 的 api 那样可以更流畅的对话,速度快, 请 google 如何用 openai api, 命令 --use_gpt3 @@ -46,9 +46,9 @@ python3 xiaogpt.py --hardware LX06 --conversation_id="xxxxxxxx"; python3 xiaogpt.py --hardware LX06 --cookie ${cookie}; # 如果你想直接输入账号密码 python3 xiaogpt.py --hardware LX06 --account ${your_xiaomi_account} --password ${your_password}; -# 如果你想 mute 小米的回答 +# 如果你想 mute 小米的回答(--mute_xiaoai该参数不能使用) python3 xiaogpt.py --hardware LX06 --mute_xiaoai -# 如果你想使用 gpt3 ai +# 如果你想使用 gpt3 ai (--mute_xiaoai该参数不能使用) export OPENAI_API_KEY=${your_api_key} python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gpt3 # 如果你想用 chatgpt api