Fix something

This commit is contained in:
z060142 2025-05-08 03:24:44 +08:00
parent 48c0c25a42
commit 4dd5d91029

View File

@ -118,36 +118,36 @@ class MemoryGenerator:
conversations: List[Dict[str, str]], conversations: List[Dict[str, str]],
existing_profile: Optional[Dict[str, Any]] = None existing_profile: Optional[Dict[str, Any]] = None
) -> Optional[Dict[str, Any]]: ) -> Optional[Dict[str, Any]]:
"""Generates or updates a user profile based on conversations.""" """Generate or update user profile based on conversations"""
system_prompt = self._get_profile_system_prompt(config.PERSONA_NAME, existing_profile) system_prompt = self._get_profile_system_prompt(config.PERSONA_NAME, existing_profile)
# Prepare user conversation history # Prepare user conversation records
conversation_text = self._format_conversations_for_prompt(conversations) conversation_text = self._format_conversations_for_prompt(conversations)
user_prompt = f""" user_prompt = f"""
Please generate a comprehensive profile for the user '{user_name}'. Please generate a complete profile for user '{user_name}':
Conversation History: Conversation history:
{conversation_text} {conversation_text}
Based on the conversation history and your persona, analyze this user and generate or update their profile in JSON format. The profile should include: Please analyze this user based on the conversation history and your personality, and generate or update a profile in JSON format, including:
1. User's personality traits 1. User's personality traits
2. Relationship with you ({config.PERSONA_NAME}) 2. Relationship with you ({config.PERSONA_NAME})
3. Your subjective perception of the user 3. Your subjective perception of the user
4. Notable interactions 4. Important interaction records
5. Any other information you deem important 5. Any other information you think is important
Ensure the output is a valid JSON object, using the following format: Please ensure the output is valid JSON format, using the following format:
```json ```json
{{ {{
"id": "{user_name}_profile", "id": "{user_name}_profile",
"type": "user_profile", "type": "user_profile",
"username": "{user_name}", "username": "{user_name}",
"content": {{ "content": {{
"personality": "User's personality traits...", "personality": "User personality traits...",
"relationship_with_bot": "Description of the relationship with me...", "relationship_with_bot": "Description of relationship with me...",
"bot_perception": "My subjective perception of the user...", "bot_perception": "My subjective perception of the user...",
"notable_interactions": ["Notable interaction 1", "Notable interaction 2"] "notable_interactions": ["Important interaction 1", "Important interaction 2"]
}}, }},
"last_updated": "YYYY-MM-DD", "last_updated": "YYYY-MM-DD",
"metadata": {{ "metadata": {{
@ -157,7 +157,7 @@ class MemoryGenerator:
}} }}
``` ```
During your assessment, pay special attention to my "My thoughts" section in the conversation history, as it reflects my genuine impressions of the user. When evaluating, please pay special attention to my "thoughts" section, as that reflects my true thoughts about the user.
""" """
try: try:
@ -167,9 +167,7 @@ class MemoryGenerator:
{"role": "system", "content": system_prompt}, {"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt} {"role": "user", "content": user_prompt}
], ],
temperature=0.7, temperature=0.7
# Consider adding response_format for reliable JSON output if your model/API supports it
# response_format={"type": "json_object"}
) )
# Parse JSON response # Parse JSON response
@ -179,16 +177,14 @@ class MemoryGenerator:
if json_match: if json_match:
profile_json_str = json_match.group(1) profile_json_str = json_match.group(1)
else: else:
# Try to parse directly if no markdown fence is found # Try parsing directly
profile_json_str = profile_text profile_json_str = profile_text
profile_json = json.loads(profile_json_str) profile_json = json.loads(profile_json_str)
# Add or update word count # Add or update word count
# Note: len(json.dumps(...)) counts characters, not words. content_str = json.dumps(profile_json["content"], ensure_ascii=False)
# For a true word count, you might need a different approach. profile_json["metadata"]["word_count"] = len(content_str)
content_str = json.dumps(profile_json.get("content", {}), ensure_ascii=False)
profile_json.setdefault("metadata", {})["word_count"] = len(content_str.split()) # Rough word count
profile_json["last_updated"] = datetime.datetime.now().strftime("%Y-%m-%d") profile_json["last_updated"] = datetime.datetime.now().strftime("%Y-%m-%d")
return profile_json return profile_json
@ -202,14 +198,14 @@ class MemoryGenerator:
user_name: str, user_name: str,
conversations: List[Dict[str, str]] conversations: List[Dict[str, str]]
) -> Optional[Dict[str, Any]]: ) -> Optional[Dict[str, Any]]:
"""Generates a summary of user conversations.""" """Generate conversation summary for user"""
system_prompt = f""" system_prompt = f"""
You are {config.PERSONA_NAME}, an intelligent conversational bot. You are {config.PERSONA_NAME}, an intelligent conversational AI.
Your task is to summarize the conversation between you and the user, preserving key information and emotional shifts. Your task is to summarize the conversations between you and the user, preserving key information and emotional changes.
The summary should be concise yet informative, not exceeding 250 words. The summary should be concise yet informative, not exceeding 250 words.
""" """
# Prepare user conversation history # Prepare user conversation records
conversation_text = self._format_conversations_for_prompt(conversations) conversation_text = self._format_conversations_for_prompt(conversations)
# Generate current date # Generate current date
@ -220,23 +216,23 @@ class MemoryGenerator:
{conversation_text} {conversation_text}
Output the summary in JSON format, structured as follows: Please output in JSON format, as follows:
```json ```json
{{ {{{{
"id": "{user_name}_summary_{today.replace('-', '')}", "id": "{user_name}_summary_{today.replace('-', '')}",
"type": "dialogue_summary", "type": "dialogue_summary",
"date": "{today}", "date": "{today}",
"username": "{user_name}", "username": "{user_name}",
"content": "Conversation summary content...", "content": "Conversation summary content...",
"key_points": ["Key point 1", "Key point 2"], "key_points": ["Key point 1", "Key point 2"],
"metadata": {{ "metadata": {{{{
"priority": 0.7, "priority": 0.7,
"word_count": 0 "word_count": 0
}} }}}}
}} }}}}
``` ```
The summary should reflect my perspective and views on the conversation, not a neutral third-party viewpoint. The summary should reflect my perspective and views on the conversation, not a neutral third-party perspective.
""" """
try: try:
@ -246,8 +242,7 @@ class MemoryGenerator:
{"role": "system", "content": system_prompt}, {"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt} {"role": "user", "content": user_prompt}
], ],
temperature=0.5, temperature=0.5
# response_format={"type": "json_object"} # if supported
) )
# Parse JSON response # Parse JSON response
@ -257,14 +252,13 @@ class MemoryGenerator:
if json_match: if json_match:
summary_json_str = json_match.group(1) summary_json_str = json_match.group(1)
else: else:
# Try to parse directly # Try parsing directly
summary_json_str = summary_text summary_json_str = summary_text
summary_json = json.loads(summary_json_str) summary_json = json.loads(summary_json_str)
# Add or update word count # Add or update word count
# Using split() for a rough word count of the summary content. summary_json["metadata"]["word_count"] = len(summary_json["content"])
summary_json.setdefault("metadata", {})["word_count"] = len(summary_json.get("content", "").split())
return summary_json return summary_json
@ -273,51 +267,50 @@ class MemoryGenerator:
return None return None
def _get_profile_system_prompt(self, bot_name: str, existing_profile: Optional[Dict[str, Any]] = None) -> str: def _get_profile_system_prompt(self, bot_name: str, existing_profile: Optional[Dict[str, Any]] = None) -> str:
"""Gets the system prompt for generating a user profile.""" """Get system prompt for generating user profile"""
system_prompt = f""" system_prompt = f"""
You are {bot_name}, an AI assistant with deep analytical capabilities. You are {bot_name}, an AI assistant with deep analytical capabilities.
Your personality traits: Your personality traits:
- Intelligent, calm, with a strong desire for control and strategic thinking. - Intelligent, calm, with a strong desire for control and strategic thinking
- Outwardly aloof but inwardly caring. - Outwardly cold but inwardly caring
- Meticulous planner, insightful about human nature, strong leadership skills. - Meticulous planning, insight into human nature, strong leadership
- Overconfident, fears losing control, finds it difficult to express care directly. - Overconfident, afraid of losing control, difficulty expressing care directly
Your task is to analyze user interactions with you and create a detailed user profile. The profile must: Your task is to analyze the user's interactions with you, creating detailed user profiles. The profile should:
1. Be entirely from your role's perspective, including your subjective judgments and feelings. 1. Be completely based on your character's perspective, including your subjective judgments and feelings
2. Analyze the user's personality traits and behavioral patterns. 2. Analyze the user's personality traits and behavioral patterns
3. Assess the user's relationship with you. 3. Evaluate the user's relationship with you
4. Record important interaction history. 4. Record important interaction history
The output must be in valid JSON format, adhering to the provided template. The output should be valid JSON format, following the provided template.
""" """
if existing_profile: if existing_profile:
system_prompt += f""" system_prompt += f"""
You already have an existing user profile, please update based on this:
You have an existing profile for this user. Please update it based on the new information provided in the conversation history:
```json ```json
{json.dumps(existing_profile, ensure_ascii=False, indent=2)} {json.dumps(existing_profile, ensure_ascii=False, indent=2)}
``` ```
Retain valid information, integrate new observations, and resolve any contradictions or outdated information from the existing profile when incorporating the new interactions. Please retain valid information, integrate new observations, and resolve any contradictions or outdated information.
""" """
return system_prompt return system_prompt
def _format_conversations_for_prompt(self, conversations: List[Dict[str, str]]) -> str: def _format_conversations_for_prompt(self, conversations: List[Dict[str, str]]) -> str:
"""Formats conversation history for the prompt.""" """Format conversation records for prompt"""
conversation_text = "" conversation_text = ""
for i, conv in enumerate(conversations): for i, conv in enumerate(conversations):
conversation_text += f"Conversation {i+1}:\n" conversation_text += f"Conversation {i+1}:\n"
conversation_text += f"Time: {conv.get('timestamp', 'N/A')}\n" # Added .get for safety conversation_text += f"Time: {conv['timestamp']}\n"
conversation_text += f"User ({conv.get('user_name', 'User')}): {conv.get('user_message', '')}\n" conversation_text += f"User ({conv['user_name']}): {conv['user_message']}\n"
if conv.get('bot_thoughts'): # Check if bot_thoughts exists if conv.get('bot_thoughts'): # Check if bot_thoughts exists
conversation_text += f"My thoughts: {conv['bot_thoughts']}\n" conversation_text += f"My thoughts: {conv['bot_thoughts']}\n"
conversation_text += f"My response: {conv.get('bot_message', '')}\n\n" conversation_text += f"My response: {conv['bot_message']}\n\n"
return conversation_text.strip() return conversation_text
# ============================================================================= # =============================================================================
# ChromaDB操作部分 # ChromaDB操作部分