class RecallFaculty(Faculty):
+class RecallFaculty(TinyMentalFaculty):
def __init__(self):
super().__init__("Memory Recall")
@@ -2620,18 +2271,18 @@ Args
Ancestors
Inherited members
@@ -2656,7 +2307,7 @@ Args
Expand source code
-class SemanticMemory(Memory):
+class SemanticMemory(TinyMemory):
"""
Semantic memory is the memory of meanings, understandings, and other concept-based knowledge unrelated to specific experiences.
It is not ordered temporally, and it is not about remembering specific events or episodes. This class provides a simple implementation
@@ -2787,168 +2438,517 @@ Args
- ###########################################################
- # IO
- ###########################################################
+ ###########################################################
+ # IO
+ ###########################################################
+
+ def _post_deserialization_init(self):
+ super()._post_deserialization_init()
+
+ self.add_documents_paths(self.documents_paths)
+ self.add_web_urls(self.documents_web_urls)
+
+Ancestors
+
+Class variables
+
+var suppress_attributes_from_serialization
+-
+
+
+
+Methods
+
+
+def add_documents_path(self, documents_path: str) ‑> None
+
+-
+
Adds a path to a folder with documents used for semantic memory.
+
+
+Expand source code
+
+def add_documents_path(self, documents_path:str) -> None:
+ """
+ Adds a path to a folder with documents used for semantic memory.
+ """
+
+ if documents_path not in self.documents_paths:
+ self.documents_paths.append(documents_path)
+ new_documents = SimpleDirectoryReader(documents_path).load_data()
+ self._add_documents(new_documents, lambda doc: doc.metadata["file_name"])
+
+
+
+def add_documents_paths(self, documents_paths: list) ‑> None
+
+-
+
Adds a path to a folder with documents used for semantic memory.
+
+
+Expand source code
+
+def add_documents_paths(self, documents_paths:list) -> None:
+ """
+ Adds a path to a folder with documents used for semantic memory.
+ """
+
+ if documents_paths is not None:
+ for documents_path in documents_paths:
+ self.add_documents_path(documents_path)
+
+
+
+def add_web_url(self, web_url: str) ‑> None
+
+-
+
Adds the data retrieved from the specified URL to documents used for semantic memory.
+
+
+Expand source code
+
+def add_web_url(self, web_url:str) -> None:
+ """
+ Adds the data retrieved from the specified URL to documents used for semantic memory.
+ """
+ # we do it like this because the add_web_urls could run scrapes in parallel, so it is better
+ # to implement this one in terms of the other
+ self.add_web_urls([web_url])
+
+
+
+def add_web_urls(self, web_urls: list) ‑> None
+
+-
+
Adds the data retrieved from the specified URLs to documents used for semantic memory.
+
+
+Expand source code
+
+def add_web_urls(self, web_urls:list) -> None:
+ """
+ Adds the data retrieved from the specified URLs to documents used for semantic memory.
+ """
+ filtered_web_urls = [url for url in web_urls if url not in self.documents_web_urls]
+ self.documents_web_urls += filtered_web_urls
+
+ if len(filtered_web_urls) > 0:
+ new_documents = SimpleWebPageReader(html_to_text=True).load_data(filtered_web_urls)
+ self._add_documents(new_documents, lambda doc: doc.id_)
+
+
+
+def list_documents_names(self) ‑> list
+
+-
+
Lists the names of the documents in memory.
+
+
+Expand source code
+
+def list_documents_names(self) -> list:
+ """
+ Lists the names of the documents in memory.
+ """
+ if self.filename_to_document is not None:
+ return list(self.filename_to_document.keys())
+ else:
+ return []
+
+
+
+def retrieve_document_content_by_name(self, document_name: str) ‑> str
+
+-
+
Retrieves a document by its name.
+
+
+Expand source code
+
+def retrieve_document_content_by_name(self, document_name:str) -> str:
+ """
+ Retrieves a document by its name.
+ """
+ if self.filename_to_document is not None:
+ doc = self.filename_to_document[document_name]
+ if doc is not None:
+ content = "SOURCE: " + document_name
+ content += "\n" + "CONTENT: " + doc.text[:10000] # TODO a more intelligent way to limit the content
+ return content
+ else:
+ return None
+ else:
+ return None
+
+
+
+Inherited members
+
+
+
+class TinyMemory
+(name: str, requires_faculties: list = None)
+
+
+Base class for different types of memory.
+
Initializes the mental faculty.
+
Args
+
+name
: str
+- The name of the mental faculty.
+requires_faculties
: list
+- A list of mental faculties that this faculty requires to function properly.
+
+
+
+Expand source code
+
+class TinyMemory(TinyMentalFaculty):
+ """
+ Base class for different types of memory.
+ """
+
+ def store(self, value: Any) -> None:
+ """
+ Stores a value in memory.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def retrieve(self, first_n: int, last_n: int, include_omission_info:bool=True) -> list:
+ """
+ Retrieves the first n and/or last n values from memory. If n is None, all values are retrieved.
+
+ Args:
+ first_n (int): The number of first values to retrieve.
+ last_n (int): The number of last values to retrieve.
+ include_omission_info (bool): Whether to include an information message when some values are omitted.
+
+ Returns:
+ list: The retrieved values.
+
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def retrieve_recent(self) -> list:
+ """
+ Retrieves the n most recent values from memory.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def retrieve_all(self) -> list:
+ """
+ Retrieves all values from memory.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def retrieve_relevant(self, relevance_target:str, top_k=5) -> list:
+ """
+ Retrieves all values from memory that are relevant to a given target.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+Ancestors
+
+Subclasses
+
+Methods
+
+
+def retrieve(self, first_n: int, last_n: int, include_omission_info: bool = True) ‑> list
+
+-
+
Retrieves the first n and/or last n values from memory. If n is None, all values are retrieved.
+
Args
+
+first_n
: int
+- The number of first values to retrieve.
+last_n
: int
+- The number of last values to retrieve.
+include_omission_info
: bool
+- Whether to include an information message when some values are omitted.
+
+
Returns
+
+list
+- The retrieved values.
+
+
+
+Expand source code
+
+def retrieve(self, first_n: int, last_n: int, include_omission_info:bool=True) -> list:
+ """
+ Retrieves the first n and/or last n values from memory. If n is None, all values are retrieved.
+
+ Args:
+ first_n (int): The number of first values to retrieve.
+ last_n (int): The number of last values to retrieve.
+ include_omission_info (bool): Whether to include an information message when some values are omitted.
- def _post_deserialization_init(self):
- super()._post_deserialization_init()
+ Returns:
+ list: The retrieved values.
- self.add_documents_paths(self.documents_paths)
- self.add_web_urls(self.documents_web_urls)
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
-Ancestors
-
-Class variables
-
-var suppress_attributes_from_serialization
+
+
+def retrieve_all(self) ‑> list
+
-
-
+
Retrieves all values from memory.
+
+
+Expand source code
+
+def retrieve_all(self) -> list:
+ """
+ Retrieves all values from memory.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
-
-Methods
-
-
-def add_documents_path(self, documents_path: str) ‑> None
+
+def retrieve_recent(self) ‑> list
-
-
Adds a path to a folder with documents used for semantic memory.
+Retrieves the n most recent values from memory.
Expand source code
-def add_documents_path(self, documents_path:str) -> None:
+def retrieve_recent(self) -> list:
"""
- Adds a path to a folder with documents used for semantic memory.
+ Retrieves the n most recent values from memory.
"""
-
- if documents_path not in self.documents_paths:
- self.documents_paths.append(documents_path)
- new_documents = SimpleDirectoryReader(documents_path).load_data()
- self._add_documents(new_documents, lambda doc: doc.metadata["file_name"])
+ raise NotImplementedError("Subclasses must implement this method.")
-
-def add_documents_paths(self, documents_paths: list) ‑> None
+
+def retrieve_relevant(self, relevance_target: str, top_k=5) ‑> list
-
-
Adds a path to a folder with documents used for semantic memory.
+Retrieves all values from memory that are relevant to a given target.
Expand source code
-def add_documents_paths(self, documents_paths:list) -> None:
+def retrieve_relevant(self, relevance_target:str, top_k=5) -> list:
"""
- Adds a path to a folder with documents used for semantic memory.
+ Retrieves all values from memory that are relevant to a given target.
"""
-
- if documents_paths is not None:
- for documents_path in documents_paths:
- self.add_documents_path(documents_path)
+ raise NotImplementedError("Subclasses must implement this method.")
-
-def add_web_url(self, web_url: str) ‑> None
+
+def store(self, value: Any) ‑> None
-
-
Adds the data retrieved from the specified URL to documents used for semantic memory.
+Stores a value in memory.
Expand source code
-def add_web_url(self, web_url:str) -> None:
+def store(self, value: Any) -> None:
"""
- Adds the data retrieved from the specified URL to documents used for semantic memory.
+ Stores a value in memory.
"""
- # we do it like this because the add_web_urls could run scrapes in parallel, so it is better
- # to implement this one in terms of the other
- self.add_web_urls([web_url])
+ raise NotImplementedError("Subclasses must implement this method.")
-
-def add_web_urls(self, web_urls: list) ‑> None
+
+Inherited members
+
+
+
+class TinyMentalFaculty
+(name: str, requires_faculties: list = None)
-Adds the data retrieved from the specified URLs to documents used for semantic memory.
+Represents a mental faculty of an agent. Mental faculties are the cognitive abilities that an agent has.
+
Initializes the mental faculty.
+
Args
+
+name
: str
+- The name of the mental faculty.
+requires_faculties
: list
+- A list of mental faculties that this faculty requires to function properly.
+
Expand source code
-def add_web_urls(self, web_urls:list) -> None:
- """
- Adds the data retrieved from the specified URLs to documents used for semantic memory.
+class TinyMentalFaculty(JsonSerializableRegistry):
+ """
+ Represents a mental faculty of an agent. Mental faculties are the cognitive abilities that an agent has.
"""
- filtered_web_urls = [url for url in web_urls if url not in self.documents_web_urls]
- self.documents_web_urls += filtered_web_urls
- if len(filtered_web_urls) > 0:
- new_documents = SimpleWebPageReader(html_to_text=True).load_data(filtered_web_urls)
- self._add_documents(new_documents, lambda doc: doc.id_)
+ def __init__(self, name: str, requires_faculties: list=None) -> None:
+ """
+ Initializes the mental faculty.
+
+ Args:
+ name (str): The name of the mental faculty.
+ requires_faculties (list): A list of mental faculties that this faculty requires to function properly.
+ """
+ self.name = name
+
+ if requires_faculties is None:
+ self.requires_faculties = []
+ else:
+ self.requires_faculties = requires_faculties
+
+ def __str__(self) -> str:
+ return f"Mental Faculty: {self.name}"
+
+ def __eq__(self, other):
+ if isinstance(other, TinyMentalFaculty):
+ return self.name == other.name
+ return False
+
+ def process_action(self, agent, action: dict) -> bool:
+ """
+ Processes an action related to this faculty.
+
+ Args:
+ action (dict): The action to process.
+
+ Returns:
+ bool: True if the action was successfully processed, False otherwise.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def actions_definitions_prompt(self) -> str:
+ """
+ Returns the prompt for defining a actions related to this faculty.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+ def actions_constraints_prompt(self) -> str:
+ """
+ Returns the prompt for defining constraints on actions related to this faculty.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
+
+Ancestors
+
+Subclasses
+
+Methods
+
+
+def actions_constraints_prompt(self) ‑> str
+
+-
+
Returns the prompt for defining constraints on actions related to this faculty.
+
+
+Expand source code
+
+def actions_constraints_prompt(self) -> str:
+ """
+ Returns the prompt for defining constraints on actions related to this faculty.
+ """
+ raise NotImplementedError("Subclasses must implement this method.")
-
-def list_documents_names(self) ‑> list
+
+def actions_definitions_prompt(self) ‑> str
-
-
Lists the names of the documents in memory.
+Returns the prompt for defining a actions related to this faculty.
Expand source code
-def list_documents_names(self) -> list:
+def actions_definitions_prompt(self) -> str:
"""
- Lists the names of the documents in memory.
+ Returns the prompt for defining a actions related to this faculty.
"""
- if self.filename_to_document is not None:
- return list(self.filename_to_document.keys())
- else:
- return []
+ raise NotImplementedError("Subclasses must implement this method.")
-
-def retrieve_document_content_by_name(self, document_name: str) ‑> str
+
+def process_action(self, agent, action: dict) ‑> bool
-
-
Retrieves a document by its name.
+Processes an action related to this faculty.
+
Args
+
+action
: dict
+- The action to process.
+
+
Returns
+
+bool
+- True if the action was successfully processed, False otherwise.
+
Expand source code
-def retrieve_document_content_by_name(self, document_name:str) -> str:
+def process_action(self, agent, action: dict) -> bool:
"""
- Retrieves a document by its name.
+ Processes an action related to this faculty.
+
+ Args:
+ action (dict): The action to process.
+
+ Returns:
+ bool: True if the action was successfully processed, False otherwise.
"""
- if self.filename_to_document is not None:
- doc = self.filename_to_document[document_name]
- if doc is not None:
- content = "SOURCE: " + document_name
- content += "\n" + "CONTENT: " + doc.text[:10000] # TODO a more intelligent way to limit the content
- return content
- else:
- return None
- else:
- return None
+ raise NotImplementedError("Subclasses must implement this method.")
Inherited members
@@ -5245,8 +5245,8 @@ Inherited members
-
-class ToolUse
+
+class TinyToolUse
(tools: list)
@@ -5264,7 +5264,7 @@ Args
Expand source code
-class ToolUse(Faculty):
+class TinyToolUse(TinyMentalFaculty):
"""
Allows the agent to use tools to accomplish tasks. Tool usage is one of the most important cognitive skills
humans and primates have as we know.
@@ -5300,18 +5300,18 @@ Args
Ancestors
Inherited members
@@ -5342,27 +5342,9 @@
-
-
-
-
-
-
-
@@ -5378,6 +5360,24 @@
+
+
+
+
+
+
+
diff --git a/docs/api/tinytroupe/control.html b/docs/api/tinytroupe/control.html
index dae2587..c00b2d5 100644
--- a/docs/api/tinytroupe/control.html
+++ b/docs/api/tinytroupe/control.html
@@ -101,7 +101,7 @@ Module tinytroupe.control
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
if self.status == Simulation.STATUS_STOPPED:
self.status = Simulation.STATUS_STARTED
@@ -408,7 +408,7 @@ Module tinytroupe.control
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
self.obj_under_transaction = obj_under_transaction
self.simulation = simulation
@@ -513,7 +513,7 @@ Module tinytroupe.control
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
# if the output is a TinyPerson, encode it
@@ -541,7 +541,7 @@ Module tinytroupe.control
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
if encoded_output is None:
return None
@@ -901,7 +901,7 @@ Ancestors
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
if self.status == Simulation.STATUS_STOPPED:
self.status = Simulation.STATUS_STARTED
@@ -1303,7 +1303,7 @@ Args
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
if self.status == Simulation.STATUS_STOPPED:
self.status = Simulation.STATUS_STARTED
@@ -1455,7 +1455,7 @@ Ancestors
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
self.obj_under_transaction = obj_under_transaction
self.simulation = simulation
@@ -1560,7 +1560,7 @@ Ancestors
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
# if the output is a TinyPerson, encode it
@@ -1588,7 +1588,7 @@ Ancestors
# local import to avoid circular dependencies
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
- from tinytroupe.personfactory import TinyFactory
+ from tinytroupe.factory import TinyFactory
if encoded_output is None:
return None
diff --git a/docs/api/tinytroupe/enrichment.html b/docs/api/tinytroupe/enrichment.html
index 8bd87bb..c9256a7 100644
--- a/docs/api/tinytroupe/enrichment.html
+++ b/docs/api/tinytroupe/enrichment.html
@@ -35,14 +35,14 @@ Module tinytroupe.enrichment
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
-from tinytroupe.personfactory import TinyPersonFactory
+from tinytroupe.factory import TinyPersonFactory
from tinytroupe.utils import JsonSerializableRegistry
from tinytroupe import openai_utils
import tinytroupe.utils as utils
-class Enricher(JsonSerializableRegistry):
+class TinyEnricher(JsonSerializableRegistry):
def __init__(self, use_past_results_in_context=False) -> None:
self.use_past_results_in_context = use_past_results_in_context
@@ -83,8 +83,8 @@ Module tinytroupe.enrichment
-
-class Enricher
+
+class TinyEnricher
(use_past_results_in_context=False)
-
@@ -93,7 +93,7 @@
Expand source code
-
class Enricher(JsonSerializableRegistry):
+class TinyEnricher(JsonSerializableRegistry):
def __init__(self, use_past_results_in_context=False) -> None:
self.use_past_results_in_context = use_past_results_in_context
@@ -129,7 +129,7 @@ Ancestors
Methods
-
+
def enrich_content(self, requirements: str, content: str, content_type: str = None, context_info: str = '', context_cache: list = None, verbose: bool = False)
-
@@ -190,9 +190,9 @@
Index
-
diff --git a/docs/api/tinytroupe/environment.html b/docs/api/tinytroupe/environment.html
index cfabf87..3bc6c80 100644
--- a/docs/api/tinytroupe/environment.html
+++ b/docs/api/tinytroupe/environment.html
@@ -1086,7 +1086,7 @@
Inherited members
class TinyWorld
-(name: str = 'A TinyWorld', agents=[], initial_datetime=datetime.datetime(2024, 11, 6, 23, 14, 30, 997928), broadcast_if_no_target=True)
+(name: str = 'A TinyWorld', agents=[], initial_datetime=datetime.datetime(2024, 11, 11, 0, 50, 46, 904535), broadcast_if_no_target=True)
-
Base class for environments.
diff --git a/docs/api/tinytroupe/extraction.html b/docs/api/tinytroupe/extraction.html
index 3648888..07b645f 100644
--- a/docs/api/tinytroupe/extraction.html
+++ b/docs/api/tinytroupe/extraction.html
@@ -58,14 +58,14 @@
Module tinytroupe.extraction
from tinytroupe.agent import TinyPerson
from tinytroupe.environment import TinyWorld
-from tinytroupe.personfactory import TinyPersonFactory
+from tinytroupe.factory import TinyPersonFactory
from tinytroupe.utils import JsonSerializableRegistry
from tinytroupe import openai_utils
import tinytroupe.utils as utils
-class InteractionResultsExtractor:
+class ResultsExtractor:
def __init__(self):
self._extraction_prompt_template_path = os.path.join(os.path.dirname(__file__), 'prompts/interaction_results_extractor.mustache')
@@ -80,6 +80,7 @@
Module tinytroupe.extraction
extraction_objective:str="The main points present in the agent's interactions history.",
situation:str = "",
fields:list=None,
+ fields_hints:dict=None,
verbose:bool=False):
"""
Extracts results from a TinyPerson instance.
@@ -99,6 +100,9 @@
Module tinytroupe.extraction
if fields is not None:
rendering_configs["fields"] = ", ".join(fields)
+ if fields_hints is not None:
+ rendering_configs["fields_hints"] = list(fields_hints.items())
+
messages.append({"role": "system",
"content": chevron.render(
open(self._extraction_prompt_template_path).read(),
@@ -149,6 +153,7 @@
Module tinytroupe.extraction
extraction_objective:str="The main points that can be derived from the agents conversations and actions.",
situation:str="",
fields:list=None,
+ fields_hints:dict=None,
verbose:bool=False):
"""
Extracts results from a TinyWorld instance.
@@ -168,6 +173,9 @@
Module tinytroupe.extraction
if fields is not None:
rendering_configs["fields"] = ", ".join(fields)
+ if fields_hints is not None:
+ rendering_configs["fields_hints"] = list(fields_hints.items())
+
messages.append({"role": "system",
"content": chevron.render(
open(self._extraction_prompt_template_path).read(),
@@ -229,7 +237,7 @@
Module tinytroupe.extraction
-class InteractionResultsReducer:
+class ResultsReducer:
def __init__(self):
self.results = {}
@@ -538,7 +546,7 @@
Module tinytroupe.extraction
################################################################################
# default extractor
-default_extractor = InteractionResultsExtractor()
+default_extractor = ResultsExtractor()
@@ -796,8 +804,221 @@ Inherited members
-