Commit 7f1d6fb
Changed files (3)
src
src/llm/summary.py
@@ -14,7 +14,7 @@ from messages.chat_history import get_parsed_chat_history
from messages.parser import parse_msg
from messages.progress import modify_progress
from messages.sender import send2tg
-from messages.utils import equal_prefix
+from messages.utils import equal_prefix, to_int
HELP = f"""🤖**GPT总结历史消息** (最多{COMBINATION_MAX_HISTORY}条)
当前模型:
@@ -67,15 +67,21 @@ async def ai_summary(client: Client, message: Message, **kwargs):
return
# reply a message with /summary
- offset_id = message.id
+ offset_id = info["mid"]
if message.reply_to_message:
- message = message.reply_to_message
- offset_id = message.id + 1 # include the reply message
+ offset_id = message.reply_to_message.id + 1 # include the reply message
- history = await get_parsed_chat_history(client, message, offset_id, num_history)
+ # set custom chat_id and message_id (useful for debug)
+ if matched := re.search(r"cid=(-?\w+)", info["text"], re.IGNORECASE):
+ info["cid"] = to_int(matched.group(1))
+ if matched := re.search(r"mid=(\d+)", info["text"], re.IGNORECASE):
+ info["mid"] = int(matched.group(1))
+ offset_id = info["mid"] + 1 # include this message
+
+ history = await get_parsed_chat_history(client, info["cid"], offset_id, num_history)
# filter by user
if filter_user:
- history = [info for info in history if info["full_name"].replace(" ", "").lower() == filter_user.lower() or str(info["uid"]) == filter_user]
+ history = [x for x in history if x["full_name"].replace(" ", "").lower() == filter_user.lower() or str(x["uid"]) == filter_user]
if not history:
await send2tg(client, message, texts=f"最近{num_history}条消息中未找到符合条件的消息", **kwargs)
@@ -91,7 +97,7 @@ async def ai_summary(client: Client, message: Message, **kwargs):
kwargs["progress"] = res[0]
response = await get_gpt_response(model_conf, contexts, **kwargs)
logger.debug(response)
- await send2tg(client, message, texts=response, **kwargs)
+ await send2tg(client, message, texts=response.strip("`"), **kwargs)
await modify_progress(del_status=True, **kwargs)
@@ -131,11 +137,12 @@ async def get_contexts(client: Client, history: list[dict]) -> list[dict]: # no
# 步骤
1. 阅读聊天记录: 仔细查看对话内容, 了解讨论的流程和上下文。
2. 识别关键主题: 提取整个聊天中讨论的主要话题。
-3. 突出争议话题: 记录任何分歧或意见不同的地方。
-4. 识别重要观点: 捕捉参与者提出的重要观点或论点。
-5. 保留意图和上下文: 确保总结反映对话的原始意义和上下文。
-6. 引用用户名和时间戳: 在适当情况下, 引用用户名和时间戳以为某些陈述提供上下文。
-7. 撰写总结: 以简洁的语言编写总结, 同时包含必要的引用。
+3. 忽略废话及无关内容, 专注于关键信息。
+4. 突出争议话题: 记录任何分歧或意见不同的地方。
+5. 识别重要观点: 捕捉参与者提出的重要观点或论点。
+6. 保留意图和上下文: 确保总结反映对话的原始意义和上下文。
+7. 引用用户名和时间戳: 在适当情况下, 引用用户名和时间戳以为某些陈述提供上下文。
+8. 撰写总结: 以简洁的语言编写总结, 同时包含必要的引用。
# 输出格式
- 使用中文撰写总结。
src/messages/chat_history.py
@@ -9,12 +9,12 @@ from messages.parser import parse_msg
@cache.memoize(ttl=10)
-async def get_history_messages(client: Client, message: Message, offset_id: int, num: int = 0) -> list[Message]:
+async def get_history_messages(client: Client, chat_id: int | str, offset_id: int, num: int = 0) -> list[Message]:
"""Get given number of chat history from old to new."""
if num <= 0:
return []
history = []
- async for msg in client.get_chat_history(chat_id=message.chat.id, offset_id=offset_id, limit=num): # type: ignore
+ async for msg in client.get_chat_history(chat_id=chat_id, offset_id=offset_id, limit=num): # type: ignore
if msg.empty:
continue
history.append(msg)
@@ -22,9 +22,9 @@ async def get_history_messages(client: Client, message: Message, offset_id: int,
@cache.memoize(ttl=10)
-async def get_parsed_chat_history(client: Client, message: Message, offset_id: int, num: int = 0) -> list[dict]:
+async def get_parsed_chat_history(client: Client, chat_id: int | str, offset_id: int, num: int = 0) -> list[dict]:
"""Get given number of chat history in parserd json format."""
if num <= 0:
return []
- history = await get_history_messages(client, message, offset_id, num)
+ history = await get_history_messages(client, chat_id, offset_id, num)
return [parse_msg(msg, silent=True) for msg in history]
src/others/combine_history.py
@@ -59,12 +59,21 @@ async def combine_history(client: Client, message: Message, **kwargs):
await send2tg(client, message, texts=HELP, **kwargs)
return
- offset_id = message.id
+ offset_id = info["mid"]
# reply a message with /combine
if message.reply_to_message:
message = message.reply_to_message
- offset_id = message.id + 1 # include the reply message
- history = await get_parsed_chat_history(client, message, offset_id, num_history)
+ info = parse_msg(message, silent=True)
+ offset_id = info["mid"] + 1 # include the reply message
+
+ # set custom chat_id and message_id (useful for debug)
+ if matched := re.search(r"cid=(-?\w+)", info["text"], re.IGNORECASE):
+ info["cid"] = to_int(matched.group(1))
+ if matched := re.search(r"mid=(\d+)", info["text"], re.IGNORECASE):
+ info["mid"] = int(matched.group(1))
+ offset_id = info["mid"] + 1 # include this message
+
+ history = await get_parsed_chat_history(client, info["cid"], offset_id, num_history)
if filter_user:
history = [x for x in history if x["full_name"].replace(" ", "").lower() == filter_user.lower() or str(x["uid"]) == filter_user]
if not history:
@@ -80,7 +89,6 @@ async def combine_history(client: Client, message: Message, **kwargs):
combination += "\n\n"
length = len(combination)
reading_minutes = length / READING_SPEED # minutes
-
target_chat = kwargs["target_chat"] if kwargs.get("target_chat") else message.chat.id
reply_msg_id = kwargs.get("reply_msg_id", 0)
reply_parameters = get_reply_to(message.id, reply_msg_id)