xiaogpt 1.42__py3-none-any.whl → 1.44__py3-none-any.whl

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.
xiaogpt/bot/__init__.py CHANGED
@@ -1,5 +1,23 @@
1
+ from __future__ import annotations
2
+
3
+ from xiaogpt.bot.base_bot import BaseBot
1
4
  from xiaogpt.bot.chatgptapi_bot import ChatGPTBot
2
5
  from xiaogpt.bot.gpt3_bot import GPT3Bot
3
6
  from xiaogpt.bot.newbing_bot import NewBingBot
7
+ from xiaogpt.config import Config
8
+
9
+ BOTS: dict[str, type[BaseBot]] = {
10
+ "gpt3": GPT3Bot,
11
+ "newbing": NewBingBot,
12
+ "chatgptapi": ChatGPTBot,
13
+ }
14
+
15
+
16
+ def get_bot(config: Config) -> BaseBot:
17
+ try:
18
+ return BOTS[config.bot].from_config(config)
19
+ except KeyError:
20
+ raise ValueError(f"Unsupported bot {config.bot}, must be one of {list(BOTS)}")
21
+
4
22
 
5
- __all__ = ["GPT3Bot", "ChatGPTBot", "NewBingBot"]
23
+ __all__ = ["GPT3Bot", "ChatGPTBot", "NewBingBot", "get_bot"]
xiaogpt/bot/base_bot.py CHANGED
@@ -1,11 +1,25 @@
1
+ from __future__ import annotations
2
+
1
3
  from abc import ABC, abstractmethod
4
+ from typing import Any, AsyncGenerator, TypeVar
5
+
6
+ from xiaogpt.config import Config
7
+
8
+ T = TypeVar("T", bound="BaseBot")
2
9
 
3
10
 
4
11
  class BaseBot(ABC):
12
+ history: list
13
+
14
+ @abstractmethod
15
+ async def ask(self, query: str, **options: Any) -> str:
16
+ pass
17
+
5
18
  @abstractmethod
6
- async def ask(self, query, **options):
19
+ async def ask_stream(self, query: str, **options: Any) -> AsyncGenerator[str, None]:
7
20
  pass
8
21
 
22
+ @classmethod
9
23
  @abstractmethod
10
- async def ask_stream(self, query, **options):
24
+ def from_config(cls: type[T], config: Config) -> T:
11
25
  pass
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  import openai
2
4
  from rich import print
3
5
 
@@ -8,7 +10,13 @@ from xiaogpt.utils import split_sentences
8
10
  class ChatGPTBot(BaseBot):
9
11
  default_options = {"model": "gpt-3.5-turbo"}
10
12
 
11
- def __init__(self, openai_key, api_base=None, proxy=None, deployment_id=None):
13
+ def __init__(
14
+ self,
15
+ openai_key: str,
16
+ api_base: str | None = None,
17
+ proxy: str | None = None,
18
+ deployment_id: str | None = None,
19
+ ) -> None:
12
20
  self.history = []
13
21
  openai.api_key = openai_key
14
22
  if api_base:
@@ -24,6 +32,15 @@ class ChatGPTBot(BaseBot):
24
32
  if proxy:
25
33
  openai.proxy = proxy
26
34
 
35
+ @classmethod
36
+ def from_config(cls, config):
37
+ return cls(
38
+ openai_key=config.openai_key,
39
+ api_base=config.api_base,
40
+ proxy=config.proxy,
41
+ deployment_id=config.deployment_id,
42
+ )
43
+
27
44
  async def ask(self, query, **options):
28
45
  ms = []
29
46
  for h in self.history:
xiaogpt/bot/gpt3_bot.py CHANGED
@@ -1,10 +1,11 @@
1
1
  import openai
2
2
  from rich import print
3
3
 
4
+ from xiaogpt.bot.base_bot import BaseBot
4
5
  from xiaogpt.utils import split_sentences
5
6
 
6
7
 
7
- class GPT3Bot:
8
+ class GPT3Bot(BaseBot):
8
9
  def __init__(self, openai_key, api_base=None, proxy=None):
9
10
  openai.api_key = openai_key
10
11
  if api_base:
@@ -13,6 +14,12 @@ class GPT3Bot:
13
14
  openai.proxy = proxy
14
15
  self.history = []
15
16
 
17
+ @classmethod
18
+ def from_config(cls, config):
19
+ return cls(
20
+ openai_key=config.openai_key, api_base=config.api_base, proxy=config.proxy
21
+ )
22
+
16
23
  async def ask(self, query, **options):
17
24
  data = {
18
25
  "prompt": query,
@@ -4,12 +4,13 @@ import re
4
4
 
5
5
  from EdgeGPT import Chatbot, ConversationStyle
6
6
 
7
+ from xiaogpt.bot.base_bot import BaseBot
7
8
  from xiaogpt.utils import split_sentences
8
9
 
9
10
  _reference_link_re = re.compile(r"\[\d+\]: .+?\n+")
10
11
 
11
12
 
12
- class NewBingBot:
13
+ class NewBingBot(BaseBot):
13
14
  def __init__(
14
15
  self,
15
16
  bing_cookie_path: str = "",
@@ -21,6 +22,14 @@ class NewBingBot:
21
22
  cookiePath=bing_cookie_path, cookies=bing_cookies, proxy=proxy
22
23
  )
23
24
 
25
+ @classmethod
26
+ def from_config(cls, config):
27
+ return cls(
28
+ bing_cookie_path=config.bing_cookie_path,
29
+ bing_cookies=config.bing_cookies,
30
+ proxy=config.proxy,
31
+ )
32
+
24
33
  @staticmethod
25
34
  def clean_text(s):
26
35
  s = s.replace("**", "")
xiaogpt/utils.py CHANGED
@@ -1,8 +1,11 @@
1
1
  #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
2
4
  import os
3
5
  import re
4
6
  import socket
5
7
  from http.cookies import SimpleCookie
8
+ from typing import AsyncIterator
6
9
  from urllib.parse import urlparse
7
10
 
8
11
  from requests.utils import cookiejar_from_dict
@@ -12,18 +15,14 @@ from requests.utils import cookiejar_from_dict
12
15
  def parse_cookie_string(cookie_string):
13
16
  cookie = SimpleCookie()
14
17
  cookie.load(cookie_string)
15
- cookies_dict = {}
16
- cookiejar = None
17
- for k, m in cookie.items():
18
- cookies_dict[k] = m.value
19
- cookiejar = cookiejar_from_dict(cookies_dict, cookiejar=None, overwrite=True)
20
- return cookiejar
18
+ cookies_dict = {k: m.value for k, m in cookie.items()}
19
+ return cookiejar_from_dict(cookies_dict, cookiejar=None, overwrite=True)
21
20
 
22
21
 
23
22
  _no_elapse_chars = re.compile(r"([「」『』《》“”'\"()()]|(?<!-)-(?!-))", re.UNICODE)
24
23
 
25
24
 
26
- def calculate_tts_elapse(text):
25
+ def calculate_tts_elapse(text: str) -> float:
27
26
  # for simplicity, we use a fixed speed
28
27
  speed = 4.5 # this value is picked by trial and error
29
28
  # Exclude quotes and brackets that do not affect the total elapsed time
@@ -33,7 +32,7 @@ def calculate_tts_elapse(text):
33
32
  _ending_punctuations = ("。", "?", "!", ";", ".", "?", "!", ";")
34
33
 
35
34
 
36
- async def split_sentences(text_stream):
35
+ async def split_sentences(text_stream: AsyncIterator[str]) -> AsyncIterator[str]:
37
36
  cur = ""
38
37
  async for text in text_stream:
39
38
  cur += text
@@ -45,7 +44,7 @@ async def split_sentences(text_stream):
45
44
 
46
45
 
47
46
  ### for edge-tts utils ###
48
- def find_key_by_partial_string(dictionary, partial_key):
47
+ def find_key_by_partial_string(dictionary: dict[str, str], partial_key: str) -> str:
49
48
  for key, value in dictionary.items():
50
49
  if key in partial_key:
51
50
  return value
@@ -63,7 +62,7 @@ def validate_proxy(proxy_str: str) -> bool:
63
62
  return True
64
63
 
65
64
 
66
- def get_hostname():
65
+ def get_hostname() -> str:
67
66
  if "XIAOGPT_HOSTNAME" in os.environ:
68
67
  return os.environ["XIAOGPT_HOSTNAME"]
69
68
 
xiaogpt/xiaogpt.py CHANGED
@@ -23,7 +23,7 @@ from miservice import MiAccount, MiIOService, MiNAService, miio_command
23
23
  from rich import print
24
24
  from rich.logging import RichHandler
25
25
 
26
- from xiaogpt.bot import ChatGPTBot, GPT3Bot, NewBingBot
26
+ from xiaogpt.bot import get_bot
27
27
  from xiaogpt.config import (
28
28
  COOKIE_TEMPLATE,
29
29
  EDGE_TTS_DICT,
@@ -183,25 +183,7 @@ class MiGPT:
183
183
  @property
184
184
  def chatbot(self):
185
185
  if self._chatbot is None:
186
- if self.config.bot == "gpt3":
187
- self._chatbot = GPT3Bot(
188
- self.config.openai_key, self.config.api_base, self.config.proxy
189
- )
190
- elif self.config.bot == "chatgptapi":
191
- self._chatbot = ChatGPTBot(
192
- self.config.openai_key,
193
- self.config.api_base,
194
- self.config.proxy,
195
- self.config.deployment_id,
196
- )
197
- elif self.config.bot == "newbing":
198
- self._chatbot = NewBingBot(
199
- bing_cookie_path=self.config.bing_cookie_path,
200
- bing_cookies=self.config.bing_cookies,
201
- proxy=self.config.proxy,
202
- )
203
- else:
204
- raise Exception(f"Do not support {self.config.bot}")
186
+ self._chatbot = get_bot(self.config)
205
187
  return self._chatbot
206
188
 
207
189
  async def simulate_xiaoai_question(self):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xiaogpt
3
- Version: 1.42
3
+ Version: 1.44
4
4
  Summary: Play ChatGPT with xiaomi AI speaker
5
5
  Author-Email: yihong0618 <zouzou0208@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,16 @@
1
+ xiaogpt-1.44.dist-info/METADATA,sha256=sczs5fZ8JgDLlt2SThUV8CzeyRTTSbCaI7hmkbo71Dk,11696
2
+ xiaogpt-1.44.dist-info/WHEEL,sha256=7dGFtUmOf30dPBLpGD2z643cxg89joO7p3JHBAwDv6E,90
3
+ xiaogpt-1.44.dist-info/entry_points.txt,sha256=zLFzA72qQ_eWBepdA2YU5vdXFqORH8wXhv2Ox1vnYP8,46
4
+ xiaogpt-1.44.dist-info/licenses/LICENSE,sha256=XdClh516MvlnOf9749JZHCxSB7y6_fyXcWmLDz6IkZY,1063
5
+ xiaogpt/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ xiaogpt/__main__.py,sha256=MSmt_5Xg84uHqzTN38JwgseJK8rsJn_11A8WD99VtEo,61
7
+ xiaogpt/bot/__init__.py,sha256=ggkvJF_CgTJkYGvklIfKQalM5MyshZHShl5vugLZTiY,639
8
+ xiaogpt/bot/base_bot.py,sha256=kzbUw1eQrgFqkE_eZZrWmcbPdwFrQezJBLm7cJAM8qw,554
9
+ xiaogpt/bot/chatgptapi_bot.py,sha256=AzsB8SoEzDRRAhoDCxU3a7gyiiTj_hvY1tfjeNJS-Kk,3275
10
+ xiaogpt/bot/gpt3_bot.py,sha256=D9F_8ZGKp264ro6aTCzV3PGZ3ZSdj__-Idi5_xmkga0,1605
11
+ xiaogpt/bot/newbing_bot.py,sha256=OBTZYWEqd3sfPl_0BrQxXZC79l8-l-2ikYFIhc4plqI,1996
12
+ xiaogpt/cli.py,sha256=YP8OcSN0Xqw7FRhp0VRb6SSBND-czYVi2BhKITHnki8,3415
13
+ xiaogpt/config.py,sha256=WkskKyZJ8BJ0Ou9x8_rlLwx8V_oVIaaNi7oNApZOJbc,5263
14
+ xiaogpt/utils.py,sha256=kJ8nOBkilEoo1i2uUSp9tkUWV4pWrd5_EzEicZXzNjY,2071
15
+ xiaogpt/xiaogpt.py,sha256=OByDLqV2AmDZ-QsJIgCnR2b8A0b2O-d7nqMAF-0z2M8,18548
16
+ xiaogpt-1.44.dist-info/RECORD,,
@@ -1,16 +0,0 @@
1
- xiaogpt-1.42.dist-info/METADATA,sha256=hD94oa_XXf7OP8WyoWPHPDqClOnLSa879XBS-H3Joqw,11696
2
- xiaogpt-1.42.dist-info/WHEEL,sha256=7dGFtUmOf30dPBLpGD2z643cxg89joO7p3JHBAwDv6E,90
3
- xiaogpt-1.42.dist-info/entry_points.txt,sha256=zLFzA72qQ_eWBepdA2YU5vdXFqORH8wXhv2Ox1vnYP8,46
4
- xiaogpt-1.42.dist-info/licenses/LICENSE,sha256=XdClh516MvlnOf9749JZHCxSB7y6_fyXcWmLDz6IkZY,1063
5
- xiaogpt/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- xiaogpt/__main__.py,sha256=MSmt_5Xg84uHqzTN38JwgseJK8rsJn_11A8WD99VtEo,61
7
- xiaogpt/bot/__init__.py,sha256=bbp6GwmXZvMyo9b9OxvonVTMGrHgruWvjHMb3mHY8s4,189
8
- xiaogpt/bot/base_bot.py,sha256=t6pg0w0vjzKfYVmdNYjQoIzgu4zFU8cW_TlLOCAHyQw,218
9
- xiaogpt/bot/chatgptapi_bot.py,sha256=1hgGSfGvS9YuPM0zT2G_SQxlgO6Ksb64PCNDHT0_ENo,2895
10
- xiaogpt/bot/gpt3_bot.py,sha256=9PNhaxM_efZL2i087eUsh_G8clEB-a6Zp0f97yqjmjM,1386
11
- xiaogpt/bot/newbing_bot.py,sha256=HX2HBp6pxX6vooPWO34zo_KgvXJMpe80rRQFTqqCHEM,1732
12
- xiaogpt/cli.py,sha256=YP8OcSN0Xqw7FRhp0VRb6SSBND-czYVi2BhKITHnki8,3415
13
- xiaogpt/config.py,sha256=WkskKyZJ8BJ0Ou9x8_rlLwx8V_oVIaaNi7oNApZOJbc,5263
14
- xiaogpt/utils.py,sha256=Vjn4kt9_Bvi08a55H8ajoWiKlISFiQrdKGtFAr0dQ9M,1991
15
- xiaogpt/xiaogpt.py,sha256=XU163xFv3ve0TNI5YRHrnxxAGaKDEHK7yPXdlLoaoiw,19359
16
- xiaogpt-1.42.dist-info/RECORD,,
File without changes