Skip to content

Commit

Permalink
Merge pull request #78 from djkcyl/dev
Browse files Browse the repository at this point in the history
更新了下bilireq,解决不了那就不管了
  • Loading branch information
djkcyl authored Oct 5, 2023
2 parents 71f6ce5 + 944d3e8 commit abfb942
Show file tree
Hide file tree
Showing 7 changed files with 1,311 additions and 1,831 deletions.
51 changes: 15 additions & 36 deletions aunly_bbot/function/command/content_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@


@channel.use(ListenerSchema(listening_events=[GroupMessage], decorators=[Permission.require()]))
async def main(
app: Ariadne, group: Group, member: Member, message: MessageChain, source: Source
):
async def main(app: Ariadne, group: Group, member: Member, message: MessageChain, source: Source):
if member.id not in BotConfig.admins and not BotConfig.Bilibili.content_resolve:
return

bili_number = await extract_bilibili_info(message)
if not bili_number:
return
Expand Down Expand Up @@ -76,10 +77,7 @@ async def main(
subtitle = await get_subtitle(aid, cid)
archive_data.content = json.dumps(subtitle, ensure_ascii=False)

if (
len(subtitle) < 10
or video_info.arc.duration < BotConfig.Bilibili.asr_length_threshold
):
if len(subtitle) < 10 or video_info.arc.duration < BotConfig.Bilibili.asr_length_threshold:
raise AbortError("字幕内容过少且视频时长过短,跳过总结请求")

chatgpt_thinks = True
Expand All @@ -95,12 +93,9 @@ async def openai_summarization():
try:
if (
BotConfig.Bilibili.openai_whitelist_users
and member.id
not in BotConfig.Bilibili.openai_whitelist_users
and member.id not in BotConfig.Bilibili.openai_whitelist_users
):
await Interval.manual(
member, BotConfig.Bilibili.openai_cooldown
)
await Interval.manual(member, BotConfig.Bilibili.openai_cooldown)
except ExecutionStop:
msg = f"{member.id} 在 10 分钟内已经请求过总结,跳过本次请求"
logger.info(msg)
Expand Down Expand Up @@ -141,12 +136,8 @@ async def wordcloud():
word_frequencies = dict(json.loads(archive_data.jieba))
else:
logger.info(f"{aid} 分词不存在,正在尝试分词......")
word_frequencies = await asyncio.to_thread(
get_frequencies, subtitle
)
archive_data.jieba = json.dumps(
word_frequencies, ensure_ascii=False
)
word_frequencies = await asyncio.to_thread(get_frequencies, subtitle)
archive_data.jieba = json.dumps(word_frequencies, ensure_ascii=False)

wordcloud = await get_worldcloud_image(word_frequencies)
if wordcloud:
Expand Down Expand Up @@ -176,15 +167,11 @@ async def wordcloud():

except TimeoutException:
logger.exception(f"视频 {aid} 信息生成超时")
await app.send_group_message(
group, MessageChain(f"{bili_number} 视频信息生成超时,请稍后再试。"), quote=source
)
await app.send_group_message(group, MessageChain(f"{bili_number} 视频信息生成超时,请稍后再试。"), quote=source)
except Exception as e:
capture_exception()
logger.exception("视频解析 API 调用出错")
await app.send_group_message(
group, MessageChain(f"视频解析 API 调用出错:{e}"), quote=source
)
await app.send_group_message(group, MessageChain(f"视频解析 API 调用出错:{e}"), quote=source)

elif bili_number[:2] == "cv":
if BotConfig.Bilibili.openai_summarization or BotConfig.Bilibili.use_wordcloud:
Expand Down Expand Up @@ -216,9 +203,7 @@ async def openai_summarization():
else:
image = await rich_text2image(summarise)
if image:
await app.send_group_message(
group, MessageChain(Image(data_bytes=image)), quote=source
)
await app.send_group_message(group, MessageChain(Image(data_bytes=image)), quote=source)
except AbortError as e:
logger.warning(f"专栏 {column_id} 总结被终止:{e}")
except Exception:
Expand All @@ -232,18 +217,12 @@ async def wordcloud():
if archive_data.jieba:
word_frequencies = dict(json.loads(archive_data.jieba))
else:
word_frequencies = await asyncio.to_thread(
get_frequencies, [cv_title, cv_text]
)
archive_data.jieba = json.dumps(
word_frequencies, ensure_ascii=False
)
word_frequencies = await asyncio.to_thread(get_frequencies, [cv_title, cv_text])
archive_data.jieba = json.dumps(word_frequencies, ensure_ascii=False)

wordcloud = await get_worldcloud_image(word_frequencies)
if wordcloud:
await app.send_group_message(
group, MessageChain(Image(data_bytes=wordcloud)), quote=source
)
await app.send_group_message(group, MessageChain(Image(data_bytes=wordcloud)), quote=source)
except Exception:
capture_exception()
logger.exception(f"专栏 {column_id} 词云出错")
Expand Down
12 changes: 3 additions & 9 deletions aunly_bbot/function/scheduler/refresh_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,18 @@ async def main(app: Ariadne):
if BotConfig.Bilibili.username and BotConfig.Bilibili.password and Bili_Auth.tokens:
BOT_Status.set_status(Status.INITIALIZED, False)

while not BOT_Status.check_status(Status.LIVE_IDLE) or not BOT_Status.check_status(
Status.DYNAMIC_IDLE
):
while not BOT_Status.is_all_statuses_true(Status.LIVE_IDLE, Status.DYNAMIC_IDLE):
await asyncio.sleep(0.1)

try:
resp = await refresh_token(auth=Bili_Auth)
Bili_Auth.update(resp)
logger.debug(await Bili_Auth.get_info())
login_cache_file.write_text(
json.dumps(dict(Bili_Auth), indent=2, ensure_ascii=False)
)
login_cache_file.write_text(json.dumps(dict(Bili_Auth), indent=2, ensure_ascii=False))
logger.success(f"[BiliBili推送] 刷新 token 成功,token:{resp['token_info']}")
await app.send_friend_message(
BotConfig.master,
MessageChain(
f"[BiliBili推送] 刷新 token 成功\n{json.dumps(resp['token_info'], indent=2)}"
),
MessageChain(f"[BiliBili推送] 刷新 token 成功\n{json.dumps(resp['token_info'], indent=2)}"),
)
except ResponseCodeError as e:
logger.error(f"[BiliBili推送] 刷新 token 失败,{e}")
Expand Down
15 changes: 6 additions & 9 deletions aunly_bbot/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ class _Bilibili(BaseModel, extra=Extra.ignore):
use_wordcloud: bool = False
use_bcut_asr: bool = False
asr_length_threshold: int = 60
captcha_address: Optional[AnyHttpUrl] = AnyHttpUrl(
"https://captcha-cd.ngworks.cn", scheme="https"
)
captcha_address: Optional[AnyHttpUrl] = AnyHttpUrl("https://captcha-cd.ngworks.cn", scheme="https")
content_resolve: bool = True

# 验证是否可以登录
@validator("use_login", always=True)
Expand Down Expand Up @@ -178,7 +177,7 @@ class _BotConfig(BaseModel, extra=Extra.ignore):
log_level: str = "INFO"
name: str = "BBot"
master: int = 123
admins: Optional[list[int]]
admins: list[int] = []
max_subsubscribe: int = 4
access_control: bool = True
update_check: bool = True
Expand All @@ -187,10 +186,10 @@ class _BotConfig(BaseModel, extra=Extra.ignore):
# 验证 admins 列表
@validator("admins")
def verify_admins(cls, admins, values):
if type(admins) == int:
if isinstance(admins, int):
click.secho("admins 格式为 int, 已重置为 list[admins]", fg="bright_yellow")
admins = [admins]
elif type(admins) != list or not admins:
elif not isinstance(admins, list) or not admins:
if "master" not in values:
raise ValueError("未查询到合法的 master")
click.secho("admins 为空或格式不为 list, 已重置为 list[master]", fg="bright_yellow")
Expand Down Expand Up @@ -222,9 +221,7 @@ def _read_file(file: Path = DEFUALT_CONFIG_PATH):
# ValueError解析
@staticmethod
def valueerror_parser(e: ValidationError):
return {
".".join([str(x) for x in err["loc"]]): err["msg"] for err in json.loads(e.json())
}
return {".".join([str(x) for x in err["loc"]]): err["msg"] for err in json.loads(e.json())}

# 从配置文件中加载配置
@classmethod
Expand Down
1 change: 1 addition & 0 deletions aunly_bbot/static/bot_config.exp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bilibili:
use_bcut_asr: true # 是否使用 BCut 接口进行 AI 语音识别
asr_length_threshold: 60 # 调用语音识别的最小长度阈值(秒)
captcha_address: "https://captcha-cd.ngworks.cn" # 验证码识别服务地址
content_resolve: true # 是否解析消息内的 BiliBili 链接
Event:
mute: true # 是否向管理员发送被禁言的事件提醒。
permchange: true # 是否向管理员发送权限变更的事件提醒。
Expand Down
21 changes: 9 additions & 12 deletions aunly_bbot/utils/openai.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import httpx
import random
import asyncio
Expand All @@ -19,9 +20,11 @@

if BotConfig.Bilibili.openai_summarization:
logger.info("正在加载 OpenAI Token 计算模型")
tiktoken_enc = asyncio.run(
tiktoken_async.encoding_for_model(BotConfig.Bilibili.openai_model)
)
try:
tiktoken_enc = asyncio.run(tiktoken_async.encoding_for_model(BotConfig.Bilibili.openai_model))
except Exception as e:
logger.warning(f"加载 OpenAI Token 计算模型失败,请重新启动,{e}")
sys.exit(1)
logger.info(f"{tiktoken_enc.name} 加载成功")


Expand All @@ -41,9 +44,7 @@ def get_summarise_prompt(title: str, transcript: str) -> list[dict[str, str]]:
"Or if there is very little content that cannot be well summarized, "
"then you can simply output the two words 'no meaning'. Remember, not to output anything else."
)
return get_full_prompt(
f'Title: "{title}"\nTranscript: "{transcript}"', sys_prompt, language
)
return get_full_prompt(f'Title: "{title}"\nTranscript: "{transcript}"', sys_prompt, language)
return get_full_prompt(
prompt=(
"使用以下Markdown模板为我总结视频字幕数据,除非字幕中的内容无意义,或者内容较少无法总结,或者未提供字幕数据,或者无有效内容,你就不使用模板回复,只回复“无意义”:"
Expand Down Expand Up @@ -88,9 +89,7 @@ def get_small_size_transcripts(text_data: list[str], token_limit: int = LIMIT_CO
return " ".join(unique_texts)


def get_full_prompt(
prompt: Optional[str] = None, system: Optional[str] = None, language: Optional[str] = None
):
def get_full_prompt(prompt: Optional[str] = None, system: Optional[str] = None, language: Optional[str] = None):
plist: list[dict[str, str]] = []
if system:
plist.append({"role": "system", "content": system})
Expand Down Expand Up @@ -136,9 +135,7 @@ async def openai_req(
if temperature:
data["temperature"] = temperature
try:
req: Response = await client.post(
"https://api.openai.com/v1/chat/completions", json=data
)
req: Response = await client.post("https://api.openai.com/v1/chat/completions", json=data)
except Exception as e:
logger.exception(e)
return OpenAI(error=True, message=f"OpenAI 请求失败 {type(e)} {e}")
Expand Down
Loading

0 comments on commit abfb942

Please sign in to comment.