Skip to content

Commit

Permalink
Fixes baiducloud bug and partial code formatting (Sourcery refactored) (
Browse files Browse the repository at this point in the history
#984)

* Fixes baiducloud bug and partial code formatting

* 'Refactored by Sourcery'

---------

Co-authored-by: Matt Gideon <117586514+Haibersut@users.noreply.github.com>
Co-authored-by: Sourcery AI <>
  • Loading branch information
sourcery-ai[bot] and Haibersut authored Jun 25, 2023
1 parent e6e9f59 commit 71e9647
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 97 deletions.
9 changes: 5 additions & 4 deletions adapter/google/bard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Generator

from adapter.botservice import BotAdapter
import json
from urllib.parse import quote
from exceptions import BotOperationNotSupportedException
from config import BardCookiePath
from constants import botManager
from exceptions import BotOperationNotSupportedException
from loguru import logger
import json
import httpx
from urllib.parse import quote


hashu = lambda word: ctypes.c_uint64(hash(word)).value

Expand Down Expand Up @@ -77,7 +78,7 @@ async def ask(self, prompt: str) -> Generator[str, None, None]:
data = json.loads(json.loads(lines)[0][2])
result = data[0][0]
self.bard_session_id = data[1][0]
self.r = data[1][1] # 用于下一次请求, 这个位置是固定的
self.r = data[1][1] # 用于下一次请求, 这个位置是固定的
# self.rc = data[4][1][0]
for check in data:
if not check:
Expand Down
2 changes: 1 addition & 1 deletion adapter/ms/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Generator, Union, List

import aiohttp
import re
import asyncio
from PIL import Image

Expand All @@ -15,7 +16,6 @@
from drawing import DrawingAPI
from exceptions import BotOperationNotSupportedException
from loguru import logger
import re
from ImageGen import ImageGenAsync
from graia.ariadne.message.element import Image as GraiaImage

Expand Down
11 changes: 8 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import sys

sys.path.append(os.getcwd())
import creart
from asyncio import AbstractEventLoop
import asyncio
Expand All @@ -10,6 +8,8 @@
from constants import config, botManager
from utils.edge_tts import load_edge_tts_voices

sys.path.append(os.getcwd())

hook()

loop = creart.create(AbstractEventLoop)
Expand All @@ -21,27 +21,33 @@
if config.mirai:
logger.info("检测到 mirai 配置,将启动 mirai 模式……")
from platforms.ariadne_bot import start_task

bots.append(loop.create_task(start_task()))

if config.onebot:
logger.info("检测到 Onebot 配置,将启动 Onebot 模式……")
from platforms.onebot_bot import start_task

bots.append(loop.create_task(start_task()))
if config.telegram:
logger.info("检测到 telegram 配置,将启动 telegram bot 模式……")
from platforms.telegram_bot import start_task

bots.append(loop.create_task(start_task()))
if config.discord:
logger.info("检测到 discord 配置,将启动 discord bot 模式……")
from platforms.discord_bot import start_task

bots.append(loop.create_task(start_task()))
if config.http:
logger.info("检测到 http 配置,将启动 http service 模式……")
from platforms.http_service import start_task

bots.append(loop.create_task(start_task()))
if config.wecom:
logger.info("检测到 Wecom 配置,将启动 Wecom Bot 模式……")
from platforms.wecom_bot import start_task

bots.append(loop.create_task(start_task()))
try:
logger.info("[Edge TTS] 读取 Edge TTS 可用音色列表……")
Expand All @@ -53,4 +59,3 @@

loop.run_until_complete(asyncio.gather(*bots))
loop.run_forever()

149 changes: 77 additions & 72 deletions manager/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
import asyncio
import hashlib
import itertools
import os
Expand Down Expand Up @@ -90,6 +90,39 @@ def __init__(self, config: Config) -> None:
pass
self.cache_db = TinyDB('data/login_caches.json')

async def handle_openai(self):
# 考虑到有人会写错全局配置
for account in self.config.openai.accounts:
account = account.dict()
if 'browserless_endpoint' in account:
logger.warning("警告: browserless_endpoint 配置位置有误,正在将其调整为全局配置")
self.config.openai.browserless_endpoint = account['browserless_endpoint']
if 'api_endpoint' in account:
logger.warning("警告: api_endpoint 配置位置有误,正在将其调整为全局配置")
self.config.openai.api_endpoint = account['api_endpoint']

# 应用 browserless_endpoint 配置
if self.config.openai.browserless_endpoint:
V1.BASE_URL = self.config.openai.browserless_endpoint or V1.BASE_URL
logger.info(f"当前的 browserless_endpoint 为:{V1.BASE_URL}")

# 历史遗留问题 1
if V1.BASE_URL == 'https://bypass.duti.tech/api/':
logger.error("检测到你还在使用旧的 browserless_endpoint,已为您切换。")
V1.BASE_URL = "https://bypass.churchless.tech/api/"
# 历史遗留问题 2
if not V1.BASE_URL.endswith("api/"):
logger.warning(
f"提示:你可能要将 browserless_endpoint 修改为 \"{self.config.openai.browserless_endpoint}api/\"")

# 应用 api_endpoint 配置
if self.config.openai.api_endpoint:
openai.api_base = self.config.openai.api_endpoint or openai.api_base
if openai.api_base.endswith("/"):
openai.api_base.removesuffix("/")
logger.info(f"当前的 api_endpoint 为:{openai.api_base}")
await self.login_openai()

async def login(self):
self.bots = {
"chatgpt-web": [],
Expand All @@ -102,86 +135,58 @@ async def login(self):
"chatglm-api": [],
"slack-accesstoken": [],
}

self.__setup_system_proxy()
if len(self.bing) > 0:
self.login_bing()
if len(self.poe) > 0:
self.login_poe()
if len(self.bard) > 0:
self.login_bard()
if len(self.slack) > 0:
self.login_slack()
if len(self.xinghuo) > 0:
self.login_xinghuo()
if len(self.openai) > 0:
# 考虑到有人会写错全局配置
for account in self.config.openai.accounts:
account = account.dict()
if 'browserless_endpoint' in account:
logger.warning("警告: browserless_endpoint 配置位置有误,正在将其调整为全局配置")
self.config.openai.browserless_endpoint = account['browserless_endpoint']
if 'api_endpoint' in account:
logger.warning("警告: api_endpoint 配置位置有误,正在将其调整为全局配置")
self.config.openai.api_endpoint = account['api_endpoint']

# 应用 browserless_endpoint 配置
if self.config.openai.browserless_endpoint:
V1.BASE_URL = self.config.openai.browserless_endpoint or V1.BASE_URL
logger.info(f"当前的 browserless_endpoint 为:{V1.BASE_URL}")

# 历史遗留问题 1
if V1.BASE_URL == 'https://bypass.duti.tech/api/':
logger.error("检测到你还在使用旧的 browserless_endpoint,已为您切换。")
V1.BASE_URL = "https://bypass.churchless.tech/api/"
# 历史遗留问题 2
if not V1.BASE_URL.endswith("api/"):
logger.warning(
f"提示:你可能要将 browserless_endpoint 修改为 \"{self.config.openai.browserless_endpoint}api/\"")

# 应用 api_endpoint 配置
if self.config.openai.api_endpoint:
openai.api_base = self.config.openai.api_endpoint or openai.api_base
if openai.api_base.endswith("/"):
openai.api_base.removesuffix("/")
logger.info(f"当前的 api_endpoint 为:{openai.api_base}")

await self.login_openai()
if len(self.yiyan) > 0:
self.login_yiyan()
if len(self.chatglm) > 0:
self.login_chatglm()

login_funcs = {
'bing': self.login_bing,
'poe': self.login_poe,
'bard': self.login_bard,
'slack': self.login_slack,
'xinghuo': self.login_xinghuo,
'openai': self.handle_openai,
'yiyan': self.login_yiyan,
'chatglm': self.login_chatglm
}

for key, login_func in login_funcs.items():
if hasattr(self, key) and len(getattr(self, key)) > 0:
if asyncio.iscoroutinefunction(login_func):
await login_func()
else:
login_func()

count = sum(len(v) for v in self.bots.values())

if count < 1:
logger.error("没有登录成功的账号,程序无法启动!")
exit(-2)
else:
# 输出登录状况
for k, v in self.bots.items():
logger.info(f"AI 类型:{k} - 可用账号: {len(v)} 个")
# 自动推测默认 AI

if not self.config.response.default_ai:
if len(self.bots['poe-web']) > 0:
self.config.response.default_ai = 'poe-chatgpt'
elif len(self.bots['slack-accesstoken']) > 0:
self.config.response.default_ai = 'slack-claude'
elif len(self.bots['chatgpt-web']) > 0:
self.config.response.default_ai = 'chatgpt-web'
elif len(self.bots['openai-api']) > 0:
self.config.response.default_ai = 'chatgpt-api'
elif len(self.bots['bing-cookie']) > 0:
self.config.response.default_ai = 'bing'
elif len(self.bots['bard-cookie']) > 0:
self.config.response.default_ai = 'bard'
elif len(self.bots['yiyan-cookie']) > 0:
self.config.response.default_ai = 'yiyan'
elif len(self.bots['chatglm-api']) > 0:
self.config.response.default_ai = 'chatglm-api'
elif len(self.bots['xinghuo-cookie']) > 0:
self.config.response.default_ai = 'xinghuo'
elif len(self.bots['slack-accesstoken']) > 0:
self.config.response.default_ai = 'slack-claude'
else:
self.config.response.default_ai = 'chatgpt-web'
# 自动推测默认 AI
default_ai_mappings = {
"poe-web": "poe-chatgpt",
"slack-accesstoken": "slack-claude",
"chatgpt-web": "chatgpt-web",
"openai-api": "chatgpt-api",
"bing-cookie": "bing",
"bard-cookie": "bard",
"yiyan-cookie": "yiyan",
"chatglm-api": "chatglm-api",
"xinghuo-cookie": "xinghuo",
}

self.config.response.default_ai = next(
(
default_ai
for key, default_ai in default_ai_mappings.items()
if len(self.bots[key]) > 0
),
'chatgpt-web',
)

def reset_bot(self, bot):
from adapter.quora.poe import PoeClientWrapper
Expand Down
18 changes: 10 additions & 8 deletions middlewares/baiducloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ async def handle_respond(self, session_id: str, prompt: str, rendered: str, resp
conclusion = f"{config.baiducloud.prompt_message}\n原因:{msg}"
return await action(session_id, prompt, conclusion, respond)

except aiohttp.ClientError as e:
logger.error(f"HTTP error occurred: {e}")

await respond("[百度云文本审核] 判定出错\n以下是原消息:")
except Exception as e:
respond_message = "[百度云文本审核] 判定出错\n以下是原消息:"
if isinstance(e, aiohttp.ClientError):
error_message = f"[百度云文本审核] HTTP错误: {e}"
elif isinstance(e, json.JSONDecodeError):
error_message = f"[百度云文本审核] JSON解码错误: {e}"
else:
error_message = f"[百度云文本审核] 其他错误:{e}"
logger.error(error_message)
await respond(respond_message)
should_pass = True

except json.JSONDecodeError as e:
logger.error(f"[百度云文本审核] JSON decode error occurred: {e}")
except StopIteration as e:
logger.error(f"[百度云文本审核] StopIteration exception occurred: {e}")
if should_pass:
return await action(session_id, prompt, rendered, respond)
16 changes: 8 additions & 8 deletions utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

def is_open(ip, port):
"""Check if a host and port is open"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
try:
# True if open, False if not
is_open = s.connect_ex((ip, int(port))) == 0
if is_open:
s.shutdown(socket.SHUT_RDWR)
is_port_open = sock.connect_ex((ip, int(port))) == 0
if is_port_open:
sock.shutdown(socket.SHUT_RDWR)
except Exception:
is_open = False
s.close()
return is_open
is_port_open = False
sock.close()
return is_port_open
2 changes: 1 addition & 1 deletion utils/text_to_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Optional

import aiohttp
import unicodedata
import asyncio
import imgkit
from pydantic import BaseModel
Expand All @@ -20,7 +21,6 @@

import markdown
import qrcode
import unicodedata
from PIL import Image
from PIL import ImageDraw, ImageFont
from charset_normalizer import from_bytes
Expand Down

0 comments on commit 71e9647

Please sign in to comment.