[docs]defvalidate_file_path(file_path):""" Validate that a file exists and is readable. Args: file_path (str): Path to the file to validate Returns: bool: True if file exists and is readable Raises: FileNotFoundError: If file doesn't exist PermissionError: If file isn't readable """importosifnotos.path.exists(file_path):raiseFileNotFoundError(f"File not found: {file_path}")ifnotos.access(file_path,os.R_OK):raisePermissionError(f"File is not readable: {file_path}")returnTrue
classLLMResponseCache:""" A response cacher for the LLM prompts. This is for not calling the API too many times. """def__init__(self):self.cache={}defget(self,prompt):""" Getter :param prompt: :return: """returnself.cache.get(prompt)defset(self,prompt,response):""" Setter :param prompt: :param response: :return: """self.cache[prompt]=responsedefget_or_set(self,prompt,llm_function):""" Getter or Setter :param prompt: :param llm_function: :return: """ifpromptinself.cache:returnself.cache[prompt]response=llm_function(prompt)self.cache[prompt]=responsereturnresponsedefsave_to_file(self,filepath):""" Save the response to a JSON file. :param filepath: :return: """importjsonwithopen(filepath,'w')asf:json.dump(self.cache,f)defload_from_file(self,filepath):""" Load the response from a JSON file. :param filepath: :return: """importjsonimportosifos.path.exists(filepath):withopen(filepath,'r')asf:self.cache=json.load(f)