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)[源代码]
基类:
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)[源代码]
Initializes the PromptHandler with paths to prompt sources and additional keyword arguments.
- 参数:
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 [源代码]
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.
- 参数:
file_path (str) -- The base path of the file to be completed.
raise_exception (bool) -- If the file cannot be found, report an error.
- 返回:
The completed file path with the appropriate extension.
- 返回类型:
str
- 抛出:
RuntimeError -- If neither the .yaml nor .json file exists at the given path.
- add_prompt_file(file_path: str, raise_exception: bool = True)[源代码]
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.
- 参数:
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)[源代码]
Adds prompt messages from a dictionary, ensuring each message has a valid entry for the current language.
- 参数:
prompt_dict (dict) -- A dictionary where keys represent prompt identifiers and values are nested dictionaries containing language-specific prompt messages.
- 抛出:
RuntimeError -- If a prompt message for the current language is not found.
- property prompt_dict: dict
Retrieves the internal dictionary containing all prompt messages.
- 返回:
The dictionary of prompt messages with keys as identifiers and values as prompt strings.
- 返回类型:
dict