Coverage for tinytroupe / agent / mental_faculty.py: 0%
153 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-28 17:48 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-28 17:48 +0000
1from tinytroupe.agent import logger
2from tinytroupe.agent.grounding import LocalFilesGroundingConnector, WebPagesGroundingConnector
3from tinytroupe.utils import JsonSerializableRegistry
4import tinytroupe.utils as utils
6import tinytroupe.agent as agent
8from typing import Callable
9import textwrap # to dedent strings
11#######################################################################################################################
12# Mental faculties
13#######################################################################################################################
15class TinyMentalFaculty(JsonSerializableRegistry):
16 """
17 Represents a mental faculty of an agent. Mental faculties are the cognitive abilities that an agent has.
18 """
20 def __init__(self, name: str, requires_faculties: list=None) -> None:
21 """
22 Initializes the mental faculty.
24 Args:
25 name (str): The name of the mental faculty.
26 requires_faculties (list): A list of mental faculties that this faculty requires to function properly.
27 """
28 self.name = name
30 if requires_faculties is None:
31 self.requires_faculties = []
32 else:
33 self.requires_faculties = requires_faculties
35 def __str__(self) -> str:
36 return f"Mental Faculty: {self.name}"
38 def __eq__(self, other):
39 if isinstance(other, TinyMentalFaculty):
40 return self.name == other.name
41 return False
43 def process_action(self, agent, action: dict) -> bool:
44 """
45 Processes an action related to this faculty.
47 Args:
48 action (dict): The action to process.
50 Returns:
51 bool: True if the action was successfully processed, False otherwise.
52 """
53 raise NotImplementedError("Subclasses must implement this method.")
55 def actions_definitions_prompt(self) -> str:
56 """
57 Returns the prompt for defining a actions related to this faculty.
58 """
59 raise NotImplementedError("Subclasses must implement this method.")
61 def actions_constraints_prompt(self) -> str:
62 """
63 Returns the prompt for defining constraints on actions related to this faculty.
64 """
65 raise NotImplementedError("Subclasses must implement this method.")
68class CustomMentalFaculty(TinyMentalFaculty):
69 """
70 Represents a custom mental faculty of an agent. Custom mental faculties are the cognitive abilities that an agent has
71 and that are defined by the user just by specifying the actions that the faculty can perform or the constraints that
72 the faculty introduces. Constraints might be related to the actions that the faculty can perform or be independent,
73 more general constraints that the agent must follow.
74 """
76 def __init__(self, name: str, requires_faculties: list = None,
77 actions_configs: dict = None, constraints: dict = None):
78 """
79 Initializes the custom mental faculty.
81 Args:
82 name (str): The name of the mental faculty.
83 requires_faculties (list): A list of mental faculties that this faculty requires to function properly.
84 Format is ["faculty1", "faculty2", ...]
85 actions_configs (dict): A dictionary with the configuration of actions that this faculty can perform.
86 Format is {<action_name>: {"description": <description>, "function": <function>}}
87 constraints (dict): A list with the constraints introduced by this faculty.
88 Format is [<constraint1>, <constraint2>, ...]
89 """
91 super().__init__(name, requires_faculties)
93 # {<action_name>: {"description": <description>, "function": <function>}}
94 if actions_configs is None:
95 self.actions_configs = {}
96 else:
97 self.actions_configs = actions_configs
99 # [<constraint1>, <constraint2>, ...]
100 if constraints is None:
101 self.constraints = {}
102 else:
103 self.constraints = constraints
105 def add_action(self, action_name: str, description: str, function: Callable=None):
106 self.actions_configs[action_name] = {"description": description, "function": function}
108 def add_actions(self, actions: dict):
109 for action_name, action_config in actions.items():
110 self.add_action(action_name, action_config['description'], action_config['function'])
112 def add_action_constraint(self, constraint: str):
113 self.constraints.append(constraint)
115 def add_actions_constraints(self, constraints: list):
116 for constraint in constraints:
117 self.add_action_constraint(constraint)
119 def process_action(self, agent, action: dict) -> bool:
120 logger.debug(f"Processing action: {action}")
122 action_type = action['type']
123 if action_type in self.actions_configs:
124 action_config = self.actions_configs[action_type]
125 action_function = action_config.get("function", None)
127 if action_function is not None:
128 action_function(agent, action)
130 # one way or another, the action was processed
131 return True
133 else:
134 return False
136 def actions_definitions_prompt(self) -> str:
137 prompt = ""
138 for action_name, action_config in self.actions_configs.items():
139 prompt += f" - {action_name.upper()}: {action_config['description']}\n"
141 return prompt
143 def actions_constraints_prompt(self) -> str:
144 prompt = ""
145 for constraint in self.constraints:
146 prompt += f" - {constraint}\n"
148 return prompt
151class RecallFaculty(TinyMentalFaculty):
153 def __init__(self):
154 super().__init__("Memory Recall")
157 def process_action(self, agent, action: dict) -> bool:
158 logger.debug(f"Processing action: {action}")
160 if action['type'] == "RECALL" and action['content'] is not None:
161 content = action['content']
163 semantic_memories = agent.retrieve_relevant_memories(relevance_target=content)
165 logger.info(f"Recalling information related to '{content}'. Found {len(semantic_memories)} relevant memories.")
167 if len(semantic_memories) > 0:
168 # a string with each element in the list in a new line starting with a bullet point
169 agent.think("I have remembered the following information from my semantic memory and will use it to guide me in my subsequent actions: \n" + \
170 "\n".join([f" - {item}" for item in semantic_memories]))
171 else:
172 agent.think(f"I can't remember anything additional about '{content}'. I'll just use what I already currently have in mind to proceed as well as I can.")
174 return True
176 elif action['type'] == "RECALL_WITH_FULL_SCAN" and action['content'] is not None:
177 logger.debug(f"Processing RECALL_WITH_FULL_SCAN action. Recalling and summarizing information related to '{action['content']}' with full scan.")
179 content = action['content']
180 memories_summary = agent.summarize_relevant_memories_via_full_scan(relevance_target=content)
182 logger.debug(f"Summary produced via full scan: {memories_summary}")
184 if len(memories_summary) > 0:
185 # the summary is presented as a block of text
186 agent.think(f"I have remembered the following information from my semantic memory and will use it to guide me in my subsequent actions: \n \"{memories_summary}\"")
187 else:
188 agent.think(f"I can't remember anything additional about '{content}'. I'll just use what I already currently have in mind to proceed as well as I can.")
190 return True
191 else:
192 return False
194 def actions_definitions_prompt(self) -> str:
195 prompt = \
196 """
197 - RECALL: you can recall information that relates to specific topics from your memory. To do, you must specify a "mental query" to locate the desired memory. If the memory is found, it is brought to your conscience.
198 - RECALL_WITH_FULL_SCAN: you can recall information from your memory in an exhaustive way, scanning all your memories. To do, you must specify a "mental query" that will be used to extract the relevant information from each memory.
199 All the information found will be brought to your conscience. This action is more expensive than RECALL, and is meant to be used when you want to ensure that you are not missing any relevant information.
200 """
202 return textwrap.dedent(prompt)
204 def actions_constraints_prompt(self) -> str:
205 prompt = \
206 """
207 - Before concluding you don't know something or don't have access to some information, you **must** try to RECALL or RECALL_WITH_FULL_SCAN it from your memory.
208 - If you you know precisely what you are looking for, you can use RECALL to retrieve it. If you are not sure, or if you want to ensure that you are not missing any relevant information, you should use RECALL_WITH_FULL_SCAN instead.
209 * RECALL example: if you want to remember "what are the expected inflation rates in Brazil", you will likely use RECALL with the "Brazil inflation 2024" mental query, as it is likely that the appropriate memory easily matches this query.
210 * RECALL_WITH_FULL_SCAN example: if you want to remember "what are the pros and cons of the product", you will likely use RECALL_WITH_FULL_SCAN with a more complex mental query like "Looking for: product pros and cons. Reason: the agent is performing a product evaluation",
211 as there is probably no clear memory that matches the related keywords, and you want to ensure that you are not missing any relevant information, so you scan all your memories for this information and explain why.
212 - You try to RECALL information from your memory, so that you can have more relevant elements to think and talk about, whenever such an action would be likely
213 to enrich the current interaction. To do so, you must specify able "mental query" that is related to the things you've been thinking, listening and talking about.
214 Example:
215 ```
216 <THINK A>
217 <RECALL / RECALL_WITH_FULL_SCAN B, which is something related to A>
218 <THINK about A and B>
219 <TALK about A and B>
220 DONE
221 ```
222 - You can try to RECALL_WITH_FULL_SCAN information from your memory when you want or are tasked with finding all relevant information about a topic, and you want to ensure that you are not missing any relevant information.
223 In other words, you "try hard" to remember.
224 Example:
225 ```
226 <LISTEN what are the main pros and cons of the product>
227 <RECALL_WITH_FULL_SCAN Looking for: product pros and cons. Reason: the agent is performing a product evaluation.>
228 <THINK about all the pros and cons found>
229 <TALK about the pros and cons recalled>
230 DONE
231 ```
232 - If you RECALL:
233 * you use a "mental query" that describe the elements you are looking for, you do not use a question. It is like a keyword-based search query.
234 For example, instead of "What are the symptoms of COVID-19?", you would use "COVID-19 symptoms".
235 * you use keywords likely to be found in the text you are looking for. For example, instead of "Brazil economic outlook", you would use "Brazil economy", "Brazil GPD", "Brazil inflation", etc.
236 - If you RECALL_WITH_FULL_SCAN:
237 * you use can use many types of "mental queries": describe the elements you are looking for; a specific question; or any other specification that can extract the relevant information from any given memory. It is NOT like a keyword-based search query,
238 but instead a specification of what is important to the agent at the moment.
239 * regardless of the type of "mental query" you use, you **also** add information about the agent's context, mainly regarding the current tasks, so that the recall mechanism can understand **why** the information is needed and can therefore
240 retrieve the most relevant information.
241 * in particular, you don't need to use keywords likely to be found in the text you are looking for, but instead focus on the precise information need that you have at the moment plus the agent's context. For example,
242 if the agent has been evaluating a product and now wants to summarize the pros and cons of the product, you can use a more complex "mental query" like
243 "Looking for: product pros and cons. Reason: the agent was asked to perform a product evaluation and has examined many of the product features already.".
244 - It may take several tries of RECALL to get the relevant information you need. If you don't find what you are looking for, you can try again with a **very** different "mental query".
245 Be creative: you can use synonyms, related concepts, or any other strategy you think might help you to find the information you need. Avoid using the same terms in different queries, as it is likely to return the same results. Whenever necessary, you should retry RECALL a couple of times before giving up the location of more information.
246 Example:
247 ```
248 <THINK something>
249 <RECALL "cat products">
250 <THINK something>
251 <RECALL "feline artifacts">
252 <THINK something>
253 <RECALL "pet store">
254 <THINK something>
255 <TALK something>
256 DONE
257 ```
258 - If you did not find what you needed using RECALL after a few attempts, you can try RECALL_WITH_FULL_SCAN instead.
259 - You **may** interleave THINK and RECALL / RECALL_WITH_FULL_SCAN so that you can better reflect on the information you are trying to recall.
260 - If you need information about a specific document, you **must** use CONSULT instead of RECALL / RECALL_WITH_FULL_SCAN. This is because RECALL / RECALL_WITH_FULL_SCAN **does not** allow you to select the specific document, and only brings small
261 relevant parts of variious documents - while CONSULT brings the precise document requested for your inspection, with its full content.
262 Example:
263 ```
264 LIST_DOCUMENTS
265 <CONSULT some document name>
266 <THINK something about the retrieved document>
267 <TALK something>
268 DONE
269 ```
270 """
272 return textwrap.dedent(prompt)
275class FilesAndWebGroundingFaculty(TinyMentalFaculty):
276 """
277 Allows the agent to access local files and web pages to ground its knowledge.
278 """
281 def __init__(self, folders_paths: list=None, web_urls: list=None):
282 super().__init__("Local Files and Web Grounding")
284 self.local_files_grounding_connector = LocalFilesGroundingConnector(folders_paths=folders_paths)
285 self.web_grounding_connector = WebPagesGroundingConnector(web_urls=web_urls)
287 def process_action(self, agent, action: dict) -> bool:
288 if action['type'] == "CONSULT" and action['content'] is not None:
289 target_name = action['content']
291 results = []
292 results.append(self.local_files_grounding_connector.retrieve_by_name(target_name))
293 results.append(self.web_grounding_connector.retrieve_by_name(target_name))
295 if len(results) > 0:
296 agent.think(f"I have read the following document: \n{results}")
297 else:
298 agent.think(f"I can't find any document with the name '{target_name}'.")
300 return True
302 elif action['type'] == "LIST_DOCUMENTS" and action['content'] is not None:
303 available_names = []
304 available_names += self.local_files_grounding_connector.list_sources()
305 available_names += self.web_grounding_connector.list_sources()
307 if len(available_names) > 0:
308 agent.think(f"I have the following documents available to me: {available_names}")
309 else:
310 agent.think(f"I don't have any documents available for inspection.")
312 return True
314 else:
315 return False
318 def actions_definitions_prompt(self) -> str:
319 prompt = \
320 """
321 - LIST_DOCUMENTS: you can list the names of the documents you have access to, so that you can decide which to access, if any, to accomplish your goals. Documents is a generic term and includes any
322 kind of "packaged" information you can access, such as emails, files, chat messages, calendar events, etc. It also includes, in particular, web pages.
323 The order of in which the documents are listed is not relevant.
324 - CONSULT: you can retrieve and consult a specific document, so that you can access its content and accomplish your goals. To do so, you specify the name of the document you want to consult.
325 """
327 return textwrap.dedent(prompt)
329 def actions_constraints_prompt(self) -> str:
330 prompt = \
331 """
332 - You are aware that you have documents available to you to help in your tasks. Even if you already have knowledge about a topic, you
333 should believe that the documents can provide you with additional information that can be useful to you.
334 - If you want information that might be in documents, you first LIST_DOCUMENTS to see what is available and decide if you want to access any of them.
335 - You LIST_DOCUMENTS when you suspect that relevant information might be in some document, but you are not sure which one.
336 - You only CONSULT the relevant documents for your present goals and context. You should **not** CONSULT documents that are not relevant to the current situation.
337 You use the name of the document to determine its relevance before accessing it.
338 - If you need information about a specific document, you **must** use CONSULT instead of RECALL. This is because RECALL **does not** allow you to select the specific document, and only brings small
339 relevant parts of variious documents - while CONSULT brings the precise document requested for your inspection, with its full content.
340 Example:
341 ```
342 LIST_DOCUMENTS
343 <CONSULT some document name>
344 <THINK something about the retrieved document>
345 <TALK something>
346 DONE
347 ```
348 - If you need information from specific documents, you **always** CONSULT it, **never** RECALL it.
349 - You can only CONSULT few documents before issuing DONE.
350 Example:
351 ```
352 <CONSULT some document name>
353 <THINK something about the retrieved document>
354 <TALK something>
355 <CONSULT some document name>
356 <THINK something about the retrieved document>
357 <TALK something>
358 DONE
359 ```
360 - When deciding whether to use RECALL or CONSULT, you should consider whether you are looking for any information about some topic (use RECALL) or if you are looking for information from
361 specific documents (use CONSULT). To know if you have potentially relevant documents available, use LIST_DOCUMENTS first.
362 """
364 return textwrap.dedent(prompt)
367class TinyToolUse(TinyMentalFaculty):
368 """
369 Allows the agent to use tools to accomplish tasks. Tool usage is one of the most important cognitive skills
370 humans and primates have as we know.
371 """
373 def __init__(self, tools:list) -> None:
374 super().__init__("Tool Use")
376 self.tools = tools
378 def process_action(self, agent, action: dict) -> bool:
379 for tool in self.tools:
380 if tool.process_action(agent, action):
381 return True
383 return False
385 def actions_definitions_prompt(self) -> str:
386 # each tool should provide its own actions definitions prompt
387 prompt = ""
388 for tool in self.tools:
389 prompt += tool.actions_definitions_prompt()
391 return prompt
393 def actions_constraints_prompt(self) -> str:
394 # each tool should provide its own actions constraints prompt
395 prompt = ""
396 for tool in self.tools:
397 prompt += tool.actions_constraints_prompt()
399 return prompt
402class SequentialThinkingFaculty(TinyMentalFaculty):
403 def __init__(self):
404 super().__init__("Sequential Thinking")
405 from tinytroupe.tools.sequential_thinking import SequentialThinkingTool
406 self.sequential_thinking_tool = SequentialThinkingTool()
408 def process_action(self, agent, action: dict) -> bool:
409 return self.sequential_thinking_tool.process_action(agent, action)
411 def actions_definitions_prompt(self) -> str:
412 return """
413 - SEQUENTIAL_THINKING: Engage in a dynamic and reflective problem-solving process by breaking down complex problems into a sequence of thoughts. The content of this action should be a JSON string with the following schema:
414 {
415 "type": "object",
416 "properties": {
417 "thought": {
418 "type": "string",
419 "description": "Your current thinking step"
420 },
421 "nextThoughtNeeded": {
422 "type": "boolean",
423 "description": "Whether another thought step is needed"
424 },
425 "thoughtNumber": {
426 "type": "integer",
427 "description": "Current thought number (numeric value, e.g., 1, 2, 3)",
428 "minimum": 1
429 },
430 "totalThoughts": {
431 "type": "integer",
432 "description": "Estimated total thoughts needed (numeric value, e.g., 5, 10)",
433 "minimum": 1
434 },
435 "isRevision": {
436 "type": "boolean",
437 "description": "Whether this revises previous thinking"
438 },
439 "revisesThought": {
440 "type": "integer",
441 "description": "Which thought is being reconsidered",
442 "minimum": 1
443 },
444 "branchFromThought": {
445 "type": "integer",
446 "description": "Branching point thought number",
447 "minimum": 1
448 },
449 "branchId": {
450 "type": "string",
451 "description": "Branch identifier"
452 },
453 "needsMoreThoughts": {
454 "type": "boolean",
455 "description": "If more thoughts are needed"
456 }
457 },
458 "required": ["thought", "nextThoughtNeeded", "thoughtNumber", "totalThoughts"]
459 }
460 """
462 def actions_constraints_prompt(self) -> str:
463 return """
464 - When you need to solve a complex problem, use the SEQUENTIAL_THINKING action to break it down into smaller, manageable thoughts.
465 - Each thought should build upon, question, or revise previous insights.
466 """