Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --conversation_id, normalize method, do_action #7

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ play chatgpt with xiaomi ai speaker
3. 参考 [revChatGPT](https://github.com/acheong08/ChatGPT) 项目 README 配置 chatGPT 的 config
4. run `python xiaogpt.py --hardware ${your_hardware}` hardware 你看小爱屁股上有型号,输入进来
5. 跑起来之后就可以问小爱同学问题了,“帮我回答开头的问题” 会发送一份给 ChatGPT 然后小爱同学用 tts 回答
6. 如果你想用 conversation_id 来持续对话,可以加上 --conversation_id="xxxxxxxx"

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

```

## QA
Expand Down
36 changes: 30 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 @@ -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,14 @@ 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有点慢"])
data = list(self.chatbot.ask(query))[-1]
self.do_action(self.tts_command, "正在问GPT我们不是会员还用的API有点慢")
if self.conversation_id:
data = list(self.chatbot.ask(query))[-1]
else:
data = list(self.chatbot.ask(query, conversation_id=self.conversation_id))[-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 +163,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 +181,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()