memoryscope.core.utils.prompt_handler
- class memoryscope.core.utils.prompt_handler.PromptHandler(class_path: str, language: LanguageEnum | str, prompt_file: str = '', prompt_dict: dict | None = None, **kwargs)[source]
Bases:
object
The PromptHandler class manages prompt messages by loading them from YAML or JSON files and dictionaries, supporting language selection based on a context, and providing dictionary-like access to the prompt messages.
- __init__(class_path: str, language: LanguageEnum | str, prompt_file: str = '', prompt_dict: dict | None = None, **kwargs)[source]
Initializes the PromptHandler with paths to prompt sources and additional keyword arguments.
- Parameters:
class_path (str) – The path to the class where prompts are utilized.
prompt_file (str, optional) – The path to an external file containing prompts. Defaults to “”.
prompt_dict (dict, optional) – A dictionary directly containing prompt definitions. Defaults to None.
language (LanguageEnum, str) – context language.
**kwargs – Additional keyword arguments that might be used in prompt handling.
- static file_path_completion(file_path: str, raise_exception: bool = True) str [source]
Attempts to complete the given file path by appending either a .yaml or .json extension based on the existence of the respective file. If neither exists, an exception is raised.
- Parameters:
file_path (str) – The base path of the file to be completed.
raise_exception (bool) – If the file cannot be found, report an error.
- Returns:
The completed file path with the appropriate extension.
- Return type:
str
- Raises:
RuntimeError – If neither the .yaml nor .json file exists at the given path.
- add_prompt_file(file_path: str, raise_exception: bool = True)[source]
Adds prompt messages from a YAML or JSON file to the internal dictionary.
This method supports loading prompts from files ending with ‘.yaml’ or ‘.json’. It uses the respective libraries to parse the content and merge it into the current prompt dictionary.
- Parameters:
file_path (str) – The path to the YAML or JSON file containing the prompts.
raise_exception (bool) – If the file cannot be found, report an error.
- add_prompt_dict(prompt_dict: dict)[source]
Adds prompt messages from a dictionary, ensuring each message has a valid entry for the current language.
- Parameters:
prompt_dict (dict) – A dictionary where keys represent prompt identifiers and values are nested dictionaries containing language-specific prompt messages.
- Raises:
RuntimeError – If a prompt message for the current language is not found.
- property prompt_dict: dict
Retrieves the internal dictionary containing all prompt messages.
- Returns:
The dictionary of prompt messages with keys as identifiers and values as prompt strings.
- Return type:
dict