完善双AI对话
This commit is contained in:
parent
c5b81be7fd
commit
7c8210039a
@ -593,24 +593,30 @@ class DualAIDialogueEngine:
|
||||
print(f"上下文设置: 历史{history_context_count}轮, 信息{context_info_count}个")
|
||||
|
||||
# 使用双模型系统生成对话
|
||||
conversation_results = self.llm_generator.run_dual_character_conversation(
|
||||
topic=topic,
|
||||
turns=turns,
|
||||
context=context_str,
|
||||
temperature=0.8,
|
||||
max_new_tokens=150
|
||||
)
|
||||
|
||||
# 保存对话到数据库
|
||||
for result in conversation_results:
|
||||
self.conv_mgr.add_dialogue_turn(
|
||||
session_id,
|
||||
result['speaker'],
|
||||
result['dialogue'],
|
||||
[result.get('context_used', '')],
|
||||
0.8 # 默认相关性分数
|
||||
for turn in range(turns):
|
||||
# 获取对话历史
|
||||
dialogue_history = self.conv_mgr.get_conversation_history(session_id)
|
||||
conversation_results = self.llm_generator.run_dual_character_conversation(
|
||||
topic=topic,
|
||||
turn_index = turn,
|
||||
context=context_str,
|
||||
dialogue_history = dialogue_history,
|
||||
history_context_count = history_context_count,
|
||||
temperature=0.8,
|
||||
max_new_tokens=150
|
||||
)
|
||||
|
||||
# 保存对话到数据库
|
||||
for result in conversation_results:
|
||||
self.conv_mgr.add_dialogue_turn(
|
||||
session_id,
|
||||
result['speaker'],
|
||||
result['dialogue'],
|
||||
[result.get('context_used', '')],
|
||||
0.8 # 默认相关性分数
|
||||
)
|
||||
|
||||
|
||||
return conversation_results
|
||||
|
||||
# def main():
|
||||
|
||||
@ -141,10 +141,11 @@ class NPCDialogueGenerator:
|
||||
self,
|
||||
character_name: str,
|
||||
context: str = "",
|
||||
user_input: str = "",
|
||||
temperature: float = 0.8,
|
||||
max_new_tokens: int = 150,
|
||||
top_p: float = 0.9
|
||||
top_p: float = 0.9,
|
||||
dialogue_history: List[Dict] = None,
|
||||
history_context_count: int = 3
|
||||
) -> str:
|
||||
"""
|
||||
生成指定角色的对话
|
||||
@ -156,6 +157,8 @@ class NPCDialogueGenerator:
|
||||
temperature: 采样温度
|
||||
max_new_tokens: 最大生成token数
|
||||
top_p: 核采样参数
|
||||
dialogue_history: 对话历史记录列表,每个元素包含speaker和content
|
||||
history_context_count: 使用的历史对话轮数(默认3轮)
|
||||
|
||||
Returns:
|
||||
生成的对话内容
|
||||
@ -165,12 +168,12 @@ class NPCDialogueGenerator:
|
||||
|
||||
profile = self.character_profiles[character_name]
|
||||
|
||||
# 构建系统提示
|
||||
system_prompt = self._build_system_prompt(profile, context)
|
||||
# 构建系统提示,包含历史对话数据
|
||||
system_prompt = self._build_system_prompt(profile, context, dialogue_history, history_context_count)
|
||||
|
||||
# 构建用户输入
|
||||
if not user_input:
|
||||
user_input = "请说一段符合你角色设定的话。"
|
||||
user_input = "请说一段符合你角色设定的话。"
|
||||
|
||||
|
||||
# 准备消息
|
||||
messages = [
|
||||
@ -209,8 +212,15 @@ class NPCDialogueGenerator:
|
||||
|
||||
return dialogue
|
||||
|
||||
def _build_system_prompt(self, profile: Dict, context: str = "") -> str:
|
||||
"""构建系统提示"""
|
||||
def _build_system_prompt(self, profile: Dict, context: str = "", dialogue_history: List[Dict] = None, history_context_count: int = 3) -> str:
|
||||
"""构建系统提示
|
||||
|
||||
Args:
|
||||
profile: 角色配置信息
|
||||
context: 当前情境
|
||||
dialogue_history: 对话历史记录列表,每个元素包含speaker和content
|
||||
history_context_count: 使用的历史对话轮数(默认3轮)
|
||||
"""
|
||||
personality_str = "、".join(profile["personality"])
|
||||
speech_pattern_str = ";".join(profile["speech_patterns"])
|
||||
|
||||
@ -219,9 +229,24 @@ class NPCDialogueGenerator:
|
||||
性格特点:{personality_str}
|
||||
说话风格:{speech_pattern_str}
|
||||
请严格按照这个角色的设定来回应,保持角色的一致性和独特性。"""
|
||||
|
||||
# 添加当前情境
|
||||
if context:
|
||||
system_prompt += f"\n\n当前情境:{context}"
|
||||
return system_prompt
|
||||
|
||||
# 添加历史对话数据(参考generate_character_prompt的实现)
|
||||
if dialogue_history:
|
||||
system_prompt += "\n\n最近的对话:"
|
||||
# 使用参数控制历史对话轮数
|
||||
history_to_use = dialogue_history[-history_context_count:] if history_context_count > 0 else []
|
||||
for turn in history_to_use:
|
||||
system_prompt += f"{turn.speaker}: {turn.content}"
|
||||
# speaker = turn.get('speaker', '未知')
|
||||
# content = turn.get('content', '')
|
||||
# if content:
|
||||
# system_prompt += f"\n{speaker}: {content}"
|
||||
|
||||
return system_prompt
|
||||
|
||||
def generate_dialogue_conversation(self, character1: str, character2: str, topic: str, turns: int = 4) -> List[Dict]:
|
||||
"""生成两个角色之间的对话
|
||||
@ -327,9 +352,10 @@ class DualModelDialogueGenerator:
|
||||
def generate_dual_character_dialogue(self,
|
||||
character_name: str,
|
||||
context: str = "",
|
||||
user_input: str = "",
|
||||
temperature: float = 0.8,
|
||||
max_new_tokens: int = 150) -> str:
|
||||
max_new_tokens: int = 150,
|
||||
dialogue_history: str = "",
|
||||
history_context_count: int = 3) -> str:
|
||||
"""
|
||||
生成指定角色的对话(使用对应的模型)
|
||||
|
||||
@ -345,19 +371,21 @@ class DualModelDialogueGenerator:
|
||||
"""
|
||||
if character_name == self.character1_config['name']:
|
||||
return self.character1_generator.generate_character_dialogue(
|
||||
character_name, context, user_input, temperature, max_new_tokens
|
||||
character_name, context, temperature, max_new_tokens, dialogue_history = dialogue_history, history_context_count=history_context_count,
|
||||
)
|
||||
elif character_name == self.character2_config['name']:
|
||||
return self.character2_generator.generate_character_dialogue(
|
||||
character_name, context, user_input, temperature, max_new_tokens
|
||||
character_name, context, temperature, max_new_tokens, dialogue_history = dialogue_history, history_context_count=history_context_count,
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Unknown character: {character_name}")
|
||||
|
||||
def run_dual_character_conversation(self,
|
||||
topic: str = "",
|
||||
turns: int = 4,
|
||||
turn_index: int = 4,
|
||||
context: str = "",
|
||||
dialogue_history: str = "",
|
||||
history_context_count: int = 3,
|
||||
temperature: float = 0.8,
|
||||
max_new_tokens: int = 150) -> List[Dict]:
|
||||
"""
|
||||
@ -380,45 +408,43 @@ class DualModelDialogueGenerator:
|
||||
# 构建完整上下文
|
||||
full_context = f"现在{char1_name}和{char2_name}在讨论关于{topic}的话题。{context}"
|
||||
|
||||
print(f"\n=== 开始双角色对话 ===")
|
||||
print(f"主题: {topic}")
|
||||
print(f"角色: {char1_name} vs {char2_name}")
|
||||
print(f"轮数: {turns}")
|
||||
print("-" * 50)
|
||||
# print(f"\n=== 开始双角色对话 ===")
|
||||
# print(f"主题: {topic}")
|
||||
# print(f"角色: {char1_name} vs {char2_name}")
|
||||
# print("-" * 50)
|
||||
|
||||
for turn in range(turns):
|
||||
if turn % 2 == 0:
|
||||
# 角色1说话
|
||||
speaker = char1_name
|
||||
if turn == 0:
|
||||
user_input = f"开始和{char2_name}讨论{topic}这个话题。"
|
||||
else:
|
||||
last_dialogue = conversation[-1]["dialogue"]
|
||||
user_input = f"{char2_name}刚才说:\"{last_dialogue}\"。请回应。"
|
||||
else:
|
||||
# 角色2说话
|
||||
speaker = char2_name
|
||||
last_dialogue = conversation[-1]["dialogue"]
|
||||
user_input = f"{char1_name}刚才说:\"{last_dialogue}\"。请回应。"
|
||||
|
||||
print(f"\n[第{turn+1}轮] {speaker}正在思考...")
|
||||
|
||||
# 使用对应角色的模型生成对话
|
||||
dialogue = self.generate_dual_character_dialogue(
|
||||
speaker, full_context, user_input, temperature, max_new_tokens
|
||||
)
|
||||
|
||||
conversation.append({
|
||||
"turn": turn + 1,
|
||||
"speaker": speaker,
|
||||
"dialogue": dialogue,
|
||||
"context_used": full_context[:100] + "..." if len(full_context) > 100 else full_context
|
||||
})
|
||||
|
||||
print(f"{speaker}: {dialogue}")
|
||||
|
||||
if turn_index % 2 == 0:
|
||||
# 角色1说话
|
||||
speaker = char1_name
|
||||
# if turn_index == 0:
|
||||
# user_input = f"开始和{char2_name}讨论{topic}这个话题。"
|
||||
# else:
|
||||
# last_dialogue = conversation[-1]["dialogue"]
|
||||
# user_input = f"{char2_name}刚才说:\"{last_dialogue}\"。请回应。"
|
||||
else:
|
||||
# 角色2说话
|
||||
speaker = char2_name
|
||||
# last_dialogue = conversation[-1]["dialogue"]
|
||||
# user_input = f"{char1_name}刚才说:\"{last_dialogue}\"。请回应。"
|
||||
|
||||
print("-" * 50)
|
||||
print("✓ 双角色对话完成")
|
||||
print(f"\n[第{turn_index+1}轮] {speaker}正在思考...")
|
||||
|
||||
# 使用对应角色的模型生成对话
|
||||
dialogue = self.generate_dual_character_dialogue(
|
||||
speaker, full_context, temperature, max_new_tokens, dialogue_history, history_context_count
|
||||
)
|
||||
|
||||
conversation.append({
|
||||
"turn": turn_index + 1,
|
||||
"speaker": speaker,
|
||||
"dialogue": dialogue,
|
||||
"context_used": full_context[:100] + "..." if len(full_context) > 100 else full_context
|
||||
})
|
||||
|
||||
print(f"{speaker}: {dialogue}")
|
||||
|
||||
|
||||
|
||||
return conversation
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user