[BUFG][Conversation]

pull/352/head
Kye 1 year ago
parent b65cd5b1fb
commit 0e94f9d12a

@ -18,19 +18,11 @@ class Conversation(BaseStructure):
Args: Args:
time_enabled (bool, optional): Whether to enable time. Defaults to False. time_enabled (bool): Whether to enable timestamps for the conversation history. Default is False.
database (AbstractDatabase, optional): The database to use. Defaults to None. database (AbstractDatabase): The database to use for storing the conversation history. Default is None.
autosave (bool, optional): Whether to autosave. Defaults to True. autosave (bool): Whether to autosave the conversation history to a file. Default is None.
save_filepath (str, optional): The filepath to save to. Defaults to "runs/conversation.json". save_filepath (str): The filepath to save the conversation history to. Default is None.
*args: Additional arguments.
**kwargs: Additional keyword arguments.
Attributes:
time_enabled (bool): Whether to enable time.
database (AbstractDatabase): The database to use.
autosave (bool): Whether to autosave.
save_filepath (str): The filepath to save to.
conversation_history (list): The conversation history.
Methods: Methods:
add(role: str, content: str): Add a message to the conversation history. add(role: str, content: str): Add a message to the conversation history.
@ -71,7 +63,7 @@ class Conversation(BaseStructure):
self, self,
time_enabled: bool = False, time_enabled: bool = False,
database: AbstractDatabase = None, database: AbstractDatabase = None,
autosave: bool = None, autosave: bool = False,
save_filepath: str = None, save_filepath: str = None,
*args, *args,
**kwargs, **kwargs,
@ -223,18 +215,17 @@ class Conversation(BaseStructure):
] ]
) )
def save_as_json(self, filename: str): def save_as_json(self, filename: str = None):
"""Save the conversation history as a JSON file """Save the conversation history as a JSON file
Args: Args:
filename (str): Save the conversation history as a JSON file filename (str): Save the conversation history as a JSON file
""" """
# Create the directory if it does not exist # Create the directory if it does not exist
os.makedirs(os.path.dirname(filename), exist_ok=True) # os.makedirs(os.path.dirname(filename), exist_ok=True)
if filename is not None:
# Save the conversation history as a JSON file with open(filename, "w") as f:
with open(filename, "w") as f: json.dump(self.conversation_history, f)
json.dump(self.conversation_history, f)
def load_from_json(self, filename: str): def load_from_json(self, filename: str):
"""Load the conversation history from a JSON file """Load the conversation history from a JSON file
@ -243,8 +234,9 @@ class Conversation(BaseStructure):
filename (str): filename to load from filename (str): filename to load from
""" """
# Load the conversation history from a JSON file # Load the conversation history from a JSON file
with open(filename, "r") as f: if filename is not None:
self.conversation_history = json.load(f) with open(filename, "r") as f:
self.conversation_history = json.load(f)
def search_keyword_in_conversation(self, keyword: str): def search_keyword_in_conversation(self, keyword: str):
"""Search for a keyword in the conversation history """Search for a keyword in the conversation history

Loading…
Cancel
Save