Skip to content

Commit

Permalink
feat(AIChat): refactor conversation management to streamline conversa…
Browse files Browse the repository at this point in the history
…tion creation with preset support
  • Loading branch information
g1331 committed Feb 11, 2025
1 parent 3fd9904 commit d189939
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions modules/self_contained/ai_chat/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,22 @@ def _get_conversation_key(self, group_id: str, member_id: str) -> ConversationKe
"""获取会话密钥对象"""
return self.ConversationKey(self, group_id, member_id)

def _create_conversation(self, conv_key: ConversationKey, preset: str = "") -> Conversation:
"""创建新的对话实例"""
provider = self.provider_factory(conv_key.key)
plugins = self.plugins_factory(conv_key.key)
conversation = Conversation(provider, plugins)
if preset:
preset_content = preset_dict[preset]["content"] if preset in preset_dict \
else (preset or preset_dict["umaru"]["content"])
conversation.set_preset(preset_content)
return conversation

def get_conversation(self, group_id: str, member_id: str) -> Conversation:
"""获取会话实例"""
conv_key = self._get_conversation_key(group_id, member_id)
if conv_key.key not in self.conversations:
provider = self.provider_factory(conv_key.key)
plugins = self.plugins_factory(conv_key.key)
self.conversations[conv_key.key] = Conversation(provider, plugins)
self.conversations[conv_key.key] = self._create_conversation(conv_key)
return self.conversations[conv_key.key]

def remove_conversation(self, group_id: str, member_id: str):
Expand All @@ -306,12 +315,7 @@ def new(self, group_id: str, member_id: str, preset: str = "") -> Conversation:
conv_key = self._get_conversation_key(group_id, member_id)
# 若对话不存在,则直接创建新对话
if conv_key.key not in self.conversations:
provider = self.provider_factory(conv_key.key)
plugins = self.plugins_factory(conv_key.key)
conversation = Conversation(provider, plugins)
preset = preset_dict[preset]["content"] if preset in preset_dict \
else (preset or preset_dict["umaru"]["content"])
conversation.set_preset(preset)
conversation = self._create_conversation(conv_key, preset)
self.conversations[conv_key.key] = conversation
self.clear_memory(group_id, member_id)
return conversation
Expand All @@ -338,12 +342,7 @@ def new(self, group_id: str, member_id: str, preset: str = "") -> Conversation:
if conv_key.is_shared and f"group:{group_id}" in self.locks:
del self.locks[f"group:{group_id}"]

provider = self.provider_factory(conv_key.key)
plugins = self.plugins_factory(conv_key.key)
conversation = Conversation(provider, plugins)
preset = preset_dict[preset]["content"] if preset in preset_dict \
else (preset or preset_dict["umaru"]["content"])
conversation.set_preset(preset)
conversation = self._create_conversation(conv_key, preset)
self.conversations[conv_key.key] = conversation
self.clear_memory(group_id, member_id)
return conversation
Expand Down
2 changes: 1 addition & 1 deletion tests/md2img/md2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def _custom_styles(theme: Theme) -> str:
table_header_bg = "#f6f8fa"
table_even_bg = "#f9f9f9"

blur_style = textwrap.dedent(f"""
blur_style = textwrap.dedent("""
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
background-color: rgba(255, 255, 255, 0.08);
Expand Down
2 changes: 1 addition & 1 deletion utils/text2img/md2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def _custom_styles(theme: Theme) -> str:
table_header_bg = "#f6f8fa"
table_even_bg = "#f9f9f9"

blur_style = textwrap.dedent(f"""
blur_style = textwrap.dedent("""
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
background-color: rgba(255, 255, 255, 0.08);
Expand Down

0 comments on commit d189939

Please sign in to comment.