Spaces:
Runtime error
Runtime error
File size: 3,561 Bytes
4ecdaad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
from abc import abstractmethod
class PromptComponent:
def __init__(self):
pass
@abstractmethod
def get_prompt(self, agent):
pass
class TaskComponent(PromptComponent):
def __init__(self, task):
super().__init__()
self.task = task
def get_prompt(self, agent):
return f"""The task you need to execute is: {self.task}.\n"""
class OutputComponent(PromptComponent):
def __init__(self, output):
super().__init__()
self.output = output
def get_prompt(self, agent):
return f"""Please contact the above to extract <{self.output}> and </{self.output}>, \
do not perform additional output, please output in strict accordance with the above format!\n"""
class SystemComponent(PromptComponent):
def __init__(self,system_prompt):
super().__init__()
self.system_prompt = system_prompt
def get_prompt(self, agent):
return self.system_prompt
class LastComponent(PromptComponent):
def __init__(self, last_prompt):
super().__init__()
self.last_prompt = last_prompt
def get_prompt(self, agent):
return self.last_prompt
class StyleComponent(PromptComponent):
"""
角色、风格组件
"""
def __init__(self, role):
super().__init__()
self.role = role
def get_prompt(self, agent):
name = agent.name
style = agent.style
return f"""Now your role is:\n{self.role}, your name is:\n{name}. \
You need to follow the output style:\n{style}.\n"""
class RuleComponent(PromptComponent):
def __init__(self, rule):
super().__init__()
self.rule = rule
def get_prompt(self, agent):
return f"""The rule you need to follow is:\n{self.rule}.\n"""
class DemonstrationComponent(PromptComponent):
"""
input a list,the example of answer.
"""
def __init__(self, demonstrations):
super().__init__()
self.demonstrations = demonstrations
def get_prompt(self, agent):
prompt = f"Here are demonstrations you can refer to:\n{self.demonstrations}"
return prompt
class CoTComponent(PromptComponent):
"""
input a list,the example of answer.
"""
def __init__(self, demonstrations):
super().__init__()
self.demonstrations = demonstrations
def add_demonstration(self, demonstration):
self.demonstrations.append(demonstration)
def get_prompt(self, agent):
prompt = "You need to think in detail before outputting, the thinking case is as follows:\n"
for demonstration in self.demonstrations:
prompt += "\n" + demonstration
return prompt
class CustomizeComponent(PromptComponent):
"""
Custom template
template(str) : example: "i am {}"
keywords(list) : example : ["name"]
example : agent.environment.shared_memory["name"] = "Lilong"
the component will get the keyword attribute from the environment, and then add it to the template.
Return : "i am Lilong"
"""
def __init__(self, template, keywords) -> None:
super().__init__()
self.template = template
self.keywords = keywords
def get_prompt(self, agent):
template_keyword = {}
for keyword in self.keywords:
current_keyword = agent.environment.shared_memory[keyword] if keyword in agent.environment.shared_memory else ""
template_keyword[keyword] = current_keyword
return self.template.format(**template_keyword) |