-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththought_system.py
146 lines (125 loc) · 3.83 KB
/
thought_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from datetime import datetime
from llm import MistralLLM
from emotion_system import Emotion
from const import *
import json
class ThoughtSystem:
def __init__(
self,
config,
emotion_system,
memory_system,
relation_system,
personality_system
):
self.model = MistralLLM("mistral-large-latest")
self.config = config
self.emotion_system = emotion_system
self.memory_system = memory_system
self.relation_system = relation_system
self.personality_system = personality_system
self.show_thoughts = True
def _check_and_fix_thought_output(self, data):
data = data.copy()
data.setdefault("emotion_intensity", 5)
data["emotion_intensity"] = int(data["emotion_intensity"])
data.setdefault("emotion", "Neutral")
data.setdefault("high_level_insights", [])
data.setdefault("emotion_reason", "I feel this way based on how the conversation has been going.")
if data["emotion"] not in EMOTION_MAP:
for em in EMOTION_MAP:
if em.lower() == data["emotion"].lower():
data["emotion"] = em
break
else:
data["emotion"] = "Neutral"
data.setdefault("next_action", "final_answer")
return data
def think(self, messages, memories):
role_map = {
"user": "User",
"assistant": self.config.name
}
history_str = "\n\n".join(
f"{role_map[msg['role']]}: {msg['content']}"
for msg in messages[:-1]
)
mood_prompt = self.emotion_system.get_mood_prompt()
mood = self.emotion_system.mood
memories_str = "\n".join(mem.format_memory() for mem in memories)
prompt = THOUGHT_PROMPT.format(
history_str=history_str,
name=self.config.name,
user_input=messages[-1]["content"],
personality_summary=self.personality_system.get_summary(),
mood_long_desc=self.emotion_system.get_mood_long_description(),
curr_date=datetime.now().strftime("%a, %-m/%-d/%Y"),
curr_time=datetime.now().strftime("%-I:%M %p"),
mood_prompt=mood_prompt,
memories=memories_str,
relationship_str = self.relation_system.get_string()
)
thought_history = [
{"role":"system", "content":self.config.system_prompt},
{"role":"user", "content":prompt}
]
data = self.model.generate(
thought_history,
temperature=0.8,
return_json=True
)
data = self._check_and_fix_thought_output(data)
#print(data["possible_user_emotions"])
thought_history.append({
"role": "assistant",
"content": json.dumps(data, indent=4)
})
if self.show_thoughts:
print(f"{self.config.name}'s thoughts:")
for thought in data["thoughts"]:
print(f"- {thought}")
print()
continue_thinking = data["next_action"].lower() == "continue"
max_steps = 5
num_steps = 0
while continue_thinking:
num_steps += 1
thought_history.append({
"role": "user",
"content": HIGHER_ORDER_THOUGHTS
})
new_data = self.model.generate(
thought_history,
temperature=0.7,
presence_penalty=0.5,
return_json=True
)
new_data = self._check_and_fix_thought_output(new_data)
thought_history.append({
"role": "assistant",
"content": json.dumps(new_data, indent=4)
})
if self.show_thoughts:
print("Higher-order thoughts:")
for thought in new_data["thoughts"]:
print(f"- {thought}")
print()
all_thoughts = data["thoughts"] + new_data["thoughts"]
data = new_data.copy()
data["thoughts"] = all_thoughts
continue_thinking = data["next_action"].lower() == "continue" and num_steps < max_steps
intensity = data["emotion_intensity"]
emotion = data["emotion"]
insights = data["high_level_insights"]
self.emotion_system.experience_emotion(
data["emotion"],
intensity/10
)
if insights:
# Add new insights gained into memory
print("Insights gained:")
for insight in insights:
print(f"- {insight}")
self.memory_system.remember(f"I gained an insight while chatting with the user: {insight}")
print()
return data