forked from autoGLM/funAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdi.py
129 lines (106 loc) · 4.06 KB
/
bdi.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
import logging
from logic import LogicTables
from SocraticReasoning import SocraticReasoning
from memory.memory import store_in_stm, DialogEntry
from chatter import GPT4o
from api import APIManager
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('BDI')
# BDI Classes Start
# Belief Class
class Belief:
def __init__(self, belief):
self.belief = belief
self.logic = LogicTables() # Initialize LogicTables class for logical operations
self.api_manager = APIManager() # Initialize APIManager for managing API keys
api_key = self.api_manager.get_api_key('gpt4o') # Retrieve the API key for the chatter service
if not api_key:
raise ValueError("API key for GPT4o is missing. Please add it using APIManager.")
self.socratic = SocraticReasoning(GPT4o(api_key)) # Initialize SocraticReasoning class with a chatter instance
def __str__(self):
return self.belief
def evaluate_belief(self):
try:
# Use logic to evaluate the belief
valid = self.logic.validate_truth(self.belief)
if valid:
return f"Belief '{self.belief}' is valid."
else:
return f"Belief '{self.belief}' is invalid."
except Exception as e:
logger.error(f"Error evaluating belief '{self.belief}': {e}")
return f"Error evaluating belief '{self.belief}': {e}"
def reason_belief(self):
try:
# Use SocraticReasoning to reason about the belief
self.socratic.add_premise(self.belief)
self.socratic.draw_conclusion()
return self.socratic.logical_conclusion
except Exception as e:
logger.error(f"Error reasoning belief '{self.belief}': {e}")
return f"Error reasoning belief '{self.belief}': {e}"
# Desire Class
class Desire:
def __init__(self, goal):
self.goal = goal
def __str__(self):
return f"Goal: {self.goal}"
# Intention Class
class Intention:
def __init__(self, plan):
self.plan = plan
def execute(self):
try:
logger.info(f"Executing plan: {self.plan}")
print(f"Executing plan: {self.plan}")
except Exception as e:
logger.error(f"Error executing plan '{self.plan}': {e}")
# Goal Class
class Goal:
def __init__(self, name, conditions, priority=0):
self.name = name
self.conditions = conditions
self.priority = priority
def is_fulfilled(self, belief_system, desire_system, intentions_system):
try:
# Evaluate conditions based on beliefs, desires, and intentions
# Return True if the goal is fulfilled, otherwise False
return all(belief_system.evaluate_belief(cond) == f"Belief '{cond}' is valid." for cond in self.conditions)
except Exception as e:
logger.error(f"Error evaluating if goal '{self.name}' is fulfilled: {e}")
return False
def __str__(self):
return f"Goal: {self.name}, Priority: {self.priority}"
# Reward Class
class Reward:
def __init__(self):
self.total_reward = 0
def update_reward(self, goal):
try:
if goal.is_fulfilled():
# Update the total reward based on the priority or other criteria
self.total_reward += goal.priority
except Exception as e:
logger.error(f"Error updating reward for goal '{goal.name}': {e}")
def get_reward(self):
return self.total_reward
# BDI Classes End
# Example usage
if __name__ == "__main__":
belief = Belief("A and B")
print(belief.evaluate_belief())
print(belief.reason_belief())
desire = Desire("Achieve C")
print(desire)
intention = Intention("Plan to achieve C")
intention.execute()
goal = Goal("Goal1", ["A and B"], priority=5)
beliefs = [belief]
desires = [desire]
intentions = [intention]
print(goal.is_fulfilled(beliefs, desires, intentions))
print(goal)
reward = Reward()
reward.update_reward(goal)
print(reward.get_reward())