xiaogpt 1.50__tar.gz → 1.51__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xiaogpt
3
- Version: 1.50
3
+ Version: 1.51
4
4
  Summary: Play ChatGPT with xiaomi AI speaker
5
5
  Author-Email: yihong0618 <zouzou0208@gmail.com>
6
6
  License: MIT
@@ -13,6 +13,7 @@ Requires-Dist: miservice_fork
13
13
  Requires-Dist: openai
14
14
  Requires-Dist: aiohttp
15
15
  Requires-Dist: rich
16
+ Requires-Dist: zhipuai
16
17
  Requires-Dist: edge-tts>=6.1.3
17
18
  Requires-Dist: EdgeGPT==0.1.26
18
19
  Description-Content-Type: text/markdown
@@ -36,6 +37,7 @@ Play ChatGPT with Xiaomi AI Speaker
36
37
  - GPT3
37
38
  - ChatGPT
38
39
  - New Bing
40
+ - [ChatGLM](http://open.bigmodel.cn/)
39
41
 
40
42
  ## Windows 获取小米音响DID
41
43
 
@@ -110,6 +112,9 @@ python3 xiaogpt.py --hardware LX06 --mute_xiaoai --stream
110
112
  # 如果你想使用 gpt3 ai
111
113
  export OPENAI_API_KEY=${your_api_key}
112
114
  python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gpt3
115
+
116
+ # 如果你想使用 ChatGLM api
117
+ python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_glm --glm_key ${glm_key}
113
118
  ```
114
119
 
115
120
  ## config.json
@@ -141,6 +146,7 @@ python3 xiaogpt.py
141
146
  ```
142
147
 
143
148
  具体参数作用请参考 [Open AI API 文档](https://platform.openai.com/docs/api-reference/chat/create)。
149
+ ChatGLM [文档](http://open.bigmodel.cn/doc/api#chatglm_130b)
144
150
  ## 配置项说明
145
151
 
146
152
  | 参数 | 说明 | 默认值 |
@@ -149,6 +155,7 @@ python3 xiaogpt.py
149
155
  | account | 小爱账户 | |
150
156
  | password | 小爱账户密码 | |
151
157
  | openai_key | openai的apikey | |
158
+ | glm_key | chatglm 的 apikey | |
152
159
  | cookie | 小爱账户cookie (如果用上面密码登录可以不填) | |
153
160
  | mi_did | 设备did | |
154
161
  | use_command | 使用 MI command 与小爱交互 | `false` |
@@ -17,6 +17,7 @@ Play ChatGPT with Xiaomi AI Speaker
17
17
  - GPT3
18
18
  - ChatGPT
19
19
  - New Bing
20
+ - [ChatGLM](http://open.bigmodel.cn/)
20
21
 
21
22
  ## Windows 获取小米音响DID
22
23
 
@@ -91,6 +92,9 @@ python3 xiaogpt.py --hardware LX06 --mute_xiaoai --stream
91
92
  # 如果你想使用 gpt3 ai
92
93
  export OPENAI_API_KEY=${your_api_key}
93
94
  python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gpt3
95
+
96
+ # 如果你想使用 ChatGLM api
97
+ python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_glm --glm_key ${glm_key}
94
98
  ```
95
99
 
96
100
  ## config.json
@@ -122,6 +126,7 @@ python3 xiaogpt.py
122
126
  ```
123
127
 
124
128
  具体参数作用请参考 [Open AI API 文档](https://platform.openai.com/docs/api-reference/chat/create)。
129
+ ChatGLM [文档](http://open.bigmodel.cn/doc/api#chatglm_130b)
125
130
  ## 配置项说明
126
131
 
127
132
  | 参数 | 说明 | 默认值 |
@@ -130,6 +135,7 @@ python3 xiaogpt.py
130
135
  | account | 小爱账户 | |
131
136
  | password | 小爱账户密码 | |
132
137
  | openai_key | openai的apikey | |
138
+ | glm_key | chatglm 的 apikey | |
133
139
  | cookie | 小爱账户cookie (如果用上面密码登录可以不填) | |
134
140
  | mi_did | 设备did | |
135
141
  | use_command | 使用 MI command 与小爱交互 | `false` |
@@ -16,11 +16,12 @@ dependencies = [
16
16
  "openai",
17
17
  "aiohttp",
18
18
  "rich",
19
+ "zhipuai",
19
20
  "edge-tts>=6.1.3",
20
21
  "EdgeGPT==0.1.26",
21
22
  ]
22
23
  dynamic = []
23
- version = "1.50"
24
+ version = "1.51"
24
25
 
25
26
  [project.license]
26
27
  text = "MIT"
@@ -4,12 +4,14 @@ from xiaogpt.bot.base_bot import BaseBot
4
4
  from xiaogpt.bot.chatgptapi_bot import ChatGPTBot
5
5
  from xiaogpt.bot.gpt3_bot import GPT3Bot
6
6
  from xiaogpt.bot.newbing_bot import NewBingBot
7
+ from xiaogpt.bot.glm_bot import GLMBot
7
8
  from xiaogpt.config import Config
8
9
 
9
10
  BOTS: dict[str, type[BaseBot]] = {
10
11
  "gpt3": GPT3Bot,
11
12
  "newbing": NewBingBot,
12
13
  "chatgptapi": ChatGPTBot,
14
+ "glm": GLMBot,
13
15
  }
14
16
 
15
17
 
@@ -20,4 +22,4 @@ def get_bot(config: Config) -> BaseBot:
20
22
  raise ValueError(f"Unsupported bot {config.bot}, must be one of {list(BOTS)}")
21
23
 
22
24
 
23
- __all__ = ["GPT3Bot", "ChatGPTBot", "NewBingBot", "get_bot"]
25
+ __all__ = ["GPT3Bot", "ChatGPTBot", "NewBingBot", "GLMBot", "get_bot"]
@@ -0,0 +1,46 @@
1
+ """ChatGLM bot"""
2
+ from __future__ import annotations
3
+ from typing import Any, AsyncGenerator
4
+
5
+ import zhipuai
6
+ from rich import print
7
+
8
+ from xiaogpt.bot.base_bot import BaseBot
9
+
10
+
11
+ class GLMBot(BaseBot):
12
+ default_options = {"model": "chatglm_130b"}
13
+
14
+ def __init__(
15
+ self,
16
+ glm_key: str,
17
+ ) -> None:
18
+ self.history = []
19
+ zhipuai.api_key = glm_key
20
+
21
+ @classmethod
22
+ def from_config(cls, config):
23
+ return cls(glm_key=config.glm_key)
24
+
25
+ def ask(self, query, **options):
26
+ ms = []
27
+ for h in self.history:
28
+ ms.append({"role": "user", "content": h[0]})
29
+ ms.append({"role": "assistant", "content": h[1]})
30
+ kwargs = {**self.default_options, **options}
31
+ kwargs["prompt"] = ms
32
+ ms.append({"role": "user", "content": f"{query}"})
33
+ r = zhipuai.model_api.sse_invoke(**kwargs)
34
+ message = ""
35
+ for i in r.events():
36
+ message += str(i.data)
37
+
38
+ self.history.append([f"{query}", message])
39
+ # only keep 5 history
40
+ first_history = self.history.pop(0)
41
+ self.history = [first_history] + self.history[-5:]
42
+ print(message)
43
+ return message
44
+
45
+ def ask_stream(self, query: str, **options: Any):
46
+ pass
@@ -47,8 +47,9 @@ class GPT3Bot(BaseBot):
47
47
 
48
48
  async def text_gen():
49
49
  async for event in completion:
50
- print(event["text"], end="")
51
- yield event["text"]
50
+ text = event["choices"][0]["text"]
51
+ print(text, end="")
52
+ yield text
52
53
 
53
54
  try:
54
55
  async for sentence in split_sentences(text_gen()):
@@ -27,6 +27,11 @@ def main():
27
27
  dest="openai_key",
28
28
  help="openai api key",
29
29
  )
30
+ parser.add_argument(
31
+ "--glm_key",
32
+ dest="glm_key",
33
+ help="chatglm api key",
34
+ )
30
35
  parser.add_argument(
31
36
  "--proxy",
32
37
  dest="proxy",
@@ -94,13 +99,23 @@ def main():
94
99
  const="newbing",
95
100
  help="if use newbing",
96
101
  )
102
+ group.add_argument(
103
+ "--use_glm",
104
+ dest="bot",
105
+ action="store_const",
106
+ const="glm",
107
+ help="if use chatglm",
108
+ )
97
109
  parser.add_argument(
98
110
  "--bing_cookie_path",
99
111
  dest="bing_cookie_path",
100
112
  help="new bing cookies path if use new bing",
101
113
  )
102
114
  group.add_argument(
103
- "--bot", dest="bot", help="bot type", choices=["gpt3", "chatgptapi", "newbing"]
115
+ "--bot",
116
+ dest="bot",
117
+ help="bot type",
118
+ choices=["gpt3", "chatgptapi", "newbing", "glm"],
104
119
  )
105
120
  parser.add_argument(
106
121
  "--config",
@@ -129,6 +144,8 @@ def main():
129
144
  )
130
145
 
131
146
  options = parser.parse_args()
147
+ if options.bot == "glm" and options.stream:
148
+ raise Exception("For now ChatGLM do not support stream")
132
149
  config = Config.from_options(options)
133
150
 
134
151
  miboy = MiGPT(config)
@@ -60,6 +60,7 @@ class Config:
60
60
  account: str = os.getenv("MI_USER", "")
61
61
  password: str = os.getenv("MI_PASS", "")
62
62
  openai_key: str = os.getenv("OPENAI_API_KEY", "")
63
+ glm_key: str = os.getenv("CHATGLM_KEY", "")
63
64
  proxy: str | None = None
64
65
  mi_did: str = os.getenv("MI_DID", "")
65
66
  keyword: Iterable[str] = KEY_WORD
@@ -356,7 +356,10 @@ class MiGPT:
356
356
  if not self.config.stream:
357
357
  async with ClientSession(trust_env=True) as session:
358
358
  openai.aiosession.set(session)
359
- answer = await self.chatbot.ask(query, **self.config.gpt_options)
359
+ if self.config.bot == "glm":
360
+ answer = self._chatbot.ask(query, **self.config.gpt_options)
361
+ else:
362
+ answer = await self.chatbot.ask(query, **self.config.gpt_options)
360
363
  message = self._normalize(answer) if answer else ""
361
364
  yield message
362
365
  return
File without changes
File without changes
File without changes
File without changes
File without changes