Skip to content

Commit

Permalink
add --conversation_id, normalize method, do_action
Browse files Browse the repository at this point in the history
  • Loading branch information
pjq committed Feb 20, 2023
1 parent 898fc9d commit b38d5b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ play chatgpt with xiaomi ai speaker

e.g.
```shell
python3 xiaogpt.py --hardware LX06
python3 xiaogpt.py --hardware LX06 --conversation_id="xxxxxxxx";
```

## QA
Expand Down
33 changes: 27 additions & 6 deletions xiaogpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse_cookie_string(cookie_string):


class MiGPT:
def __init__(self, hardware):
def __init__(self, hardware, conversation_id=""):
self.mi_token_home = Path.home() / ".mi.token"
self.hardware = hardware
self.cookie_string = ""
Expand All @@ -44,6 +44,7 @@ def __init__(self, hardware):
self.device_id = ""
self.service_token = ""
self.tts_command = HARDWARE_COMMAND_DICT.get(hardware, "5-1")
self.conversation_id=conversation_id

def _init_all_data(self):
# Step 1 make sure we init the ai api and servive token
Expand Down Expand Up @@ -86,7 +87,7 @@ def _init_cookie(self):
def _init_first_data_and_chatbot(self):
data = self.get_latest_ask_from_xiaoai()
self.last_timestamp, self.last_record = self.get_last_timestamp_and_record(data)
self.chatbot = Chatbot(configure())
self.chatbot = Chatbot(configure(), conversation_id=self.conversation_id)

def get_latest_ask_from_xiaoai(self):
r = self.s.get(
Expand All @@ -105,6 +106,18 @@ def get_last_timestamp_and_record(self, data):
else:
return 0, None

def do_action(self, command, value):
# print(f"MiService: do_action {command}:{value}")
result = subprocess.run(["micli", command, value])
print(f"MiService: do_action {command}: done, {result}")

def normalize(self, message):
message = message.replace(" ", ",")
message = message.replace("\n", ",")
message = message.replace("\"", ",")

return message

def run_forever(self):
self._init_all_data()
while 1:
Expand All @@ -128,11 +141,11 @@ def run_forever(self):
print("Running chatgpt ask maybe a little slow we do not pay")
# waiting for xiaoai speaker done
time.sleep(8)
subprocess.run(["micli", self.tts_command, "正在问GPT我们不是会员还用的API有点慢"])
self.do_action(self.tts_command, "正在问GPT我们不是会员还用的API有点慢")
data = list(self.chatbot.ask(query))[-1]
if message := data.get("message", ""):
# xiaoai tts did not support space
message = message.replace(" ", ",")
message = self.normalize(message)
message = "以下是GPT的回答:" + message
print(message)
try:
Expand All @@ -147,7 +160,7 @@ def run_forever(self):
# 5-1 for xiaoai pro tts
# TODO more data to chunk
try:
subprocess.run(["micli", self.tts_command, message])
self.do_action(self.tts_command, message)
time.sleep(1)
except Exception as e:
print("Something is wrong: ", str(e))
Expand All @@ -165,7 +178,15 @@ def run_forever(self):
default="LX06",
help="小爱 hardware",
)

parser.add_argument(
"--conversation_id",
dest="conversation_id",
type=str,
default="",
help="ChatGPT conversation_id",
)
options = parser.parse_args()
miboy = MiGPT(options.hardware)
miboy = MiGPT(options.hardware, options.conversation_id)
miboy._init_all_data()
miboy.run_forever()

0 comments on commit b38d5b9

Please sign in to comment.