tree of thoughts prompting for worker

main
Kye 2 years ago
parent f10b2f5906
commit 84aa448ac8

@ -96,6 +96,7 @@ class WorkerNode:
self.tools = tools
self.vectorstore = vectorstore
def create_agent(self, ai_name, ai_role, human_in_the_loop, search_kwargs):
@ -145,12 +146,107 @@ class WorkerNode:
def run_agent(self, prompt):
# Run the agent with the given prompt
self.agent.run([prompt])
tree_of_thoughts_prompt = """
Imagine three different experts are answering this question. All experts will write down each chain of thought of each step of their thinking, then share it with the group. Then all experts will go on to the next step, etc. If any expert realises they're wrong at any point then they leave. The question is...
"""
self.agent.run([f"{tree_of_thoughts_prompt} {prompt}"])
class MetaWorkerNode:
def __init__(self, llm, tools, vectorstore):
self.llm = llm
self.tools = tools
self.vectorstore = vectorstore
self.agent = None
self.meta_chain = None
def init_chain(self, instructions):
self.agent = WorkerNode(self.llm, self.tools, self.vectorstore)
self.agent.create_agent("Assistant", "Assistant Role", False, {})
def meta_chain(self):
#define meta template and meta prompting as per your needs
self.meta_chain = init_meta_chain()
def initialize_meta_chain():
meta_template = """
Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future.
####
{chat_history}
####
Please reflect on these interactions.
You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...".
You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...".
"""
meta_prompt = PromptTemplate(
input_variables=["chat_history"], template=meta_template
)
meta_chain = LLMChain(
llm=OpenAI(temperature=0),
prompt=meta_prompt,
verbose=True,
)
return meta_chain
def get_chat_history(chain_memory):
memory_key = chain_memory.memory_key
chat_history = chain_memory.load_memory_variables(memory_key)[memory_key]
return chat_history
def get_new_instructions(meta_output):
delimiter = "Instructions: "
new_instructions = meta_output[meta_output.find(delimiter) + len(delimiter) :]
return new_instructions
def main(self, task, max_iters=3, max_meta_iters=5):
failed_phrase = "task failed"
success_phrase = "task succeeded"
key_phrases = [success_phrase, failed_phrase]
instructions = "None"
for i in range(max_meta_iters):
print(f"[Episode {i+1}/{max_meta_iters}]")
self.initialize_chain(instructions)
output = self.agent.perform('Assistant', {'request': task})
for j in range(max_iters):
print(f"(Step {j+1}/{max_iters})")
print(f"Assistant: {output}")
print(f"Human: ")
human_input = input()
if any(phrase in human_input.lower() for phrase in key_phrases):
break
output = self.agent.perform('Assistant', {'request': human_input})
if success_phrase in human_input.lower():
print(f"You succeeded! Thanks for playing!")
return
self.initialize_meta_chain()
meta_output = self.meta_chain.predict(chat_history=self.get_chat_history())
print(f"Feedback: {meta_output}")
instructions = self.get_new_instructions(meta_output)
print(f"New Instructions: {instructions}")
print("\n" + "#" * 80 + "\n")
print(f"You failed! Thanks for playing!")
#init instance of MetaWorkerNode
meta_worker_node = MetaWorkerNode(llm=OpenAI, tools=tools, vectorstore=vectorstore)
#specify a task and interact with the agent
task = "Provide a sysmatic argument for why we should always eat past with olives"
meta_worker_node.main(task)
####################################################################### => Boss Node

Loading…
Cancel
Save