"""This file uses inputted NL descriptions to generate prompts for LLM. The user does nothave to use this class, but it is generally advisable for ease of use. - format (str) should be fixed prompt from: /templates"""
[docs]classPromptBuilder:def__init__(self,role:str=None,format:str=None,examples:list=None,task:str=None,):self.role=role# role for LLM to follow (i.e. PDDL predicate constructor)self.format=format# prompting formatself.examples=(examplesifexamplesisnotNoneelse[])# n-shot examples for LLM to followself.task=task# dynamic placeholder given information to LLM
[docs]defset_role(self,role):"""Sets the role for the LLM to perform task"""self.role=role
[docs]defset_format(self,format):"""Sets the prompting format for LLM to perform task"""self.format=format
[docs]defset_examples(self,example):"""Appends a shot examples for LLM to follow"""self.examples.append(example)
[docs]defset_task(self,task):""" Sets a task for the LLM by providing dynamic placeholders to generate and describe domain components. The `task` parameter is a structured input that includes various elements to guide the LLM in understanding and executing the task. The task may include descriptions, types, actions, and predicates that the LLM will process to generate appropriate outputs. Here is an example of a dynamic placeholder: ''' ## Domain {domain_desc} - A placeholder for the description of the domain, explaining the context and purpose. ''' Args: task (str): A structured string or template containing dynamic placeholders to specify the task. """self.task=task
[docs]defget_role(self):"""Returns role of the prompt given"""returnself.role
[docs]defget_format(self):"""Returns prompting format of the prompt given"""returnself.format
[docs]defget_examples(self):"""Returns list of n-examples of the prompt given"""returnself.examples
[docs]defgenerate_prompt(self):"""Generates the whole prompt in proper format"""prompt=""ifself.role:prompt+=f"[ROLE]: {self.role}\n\n"prompt+="------------------------------------------------\n"ifself.format:prompt+=f"[FORMAT]: {self.format}\n\n"prompt+="------------------------------------------------\n"iflen(self.examples)>0:prompt+=f"[EXAMPLE(S)]:\n"fori,exampleinenumerate(self.examples,1):prompt+=f"Example {i}:\n{example}\n\n"prompt+="------------------------------------------------\n"ifself.task:prompt+=f"[TASK]:\nHere is the task to solve:\n{self.task}\n\n"returnprompt.strip()