Exercise#4 ToT Prompt

NOTE Langchain

Use of Langchain will simplify the code below. Langchain has not been used deliberately to show you the concept of LLM chains :) in the next iteration you will use Langchain to rewrite this code. If you already know Langchain, then feel free to use it instead of the solution proposed in this notebook.

Objective

Learn to use Train of Thought approach. You will are responsible for putting together a creative writeup for a new product “The Javellin 20” shoe. The instructions for generating the writeup are already given you. Your task is to put together prompt for each of the 4 steps in create writeup LLM chain discussed in the lesson.

tot-creative-writing-llm-chain

Since the prompts will involve multiple-parts, it is suggested that you use Python formatted string and placeholders. Create a notebook to with code to generate the prompts using print statement.

# Example
prompt_step_1 = """
  This is an instruction.

  Query:
  {placeholder_for_query}
"""

print(prompt_step_1.format(placeholder_for_query=...))

Hints:

  • Use output indicators (e.g., Writeup: Plan: …)
  • Print the prompt to see if it looks good
  • You may even try it in ChatGPT or other public chatbot !!! by copying/pasting data OR you may use dummy data to see output

Same tasks may carried out with prompts written differently. Your solution may be different for my solution and its OK :) as long as we can get the desired results !!!

Prompt #1

You are already given the prompt with the instructions. Your task is to write a prompt that will instruct the LLM to create 3 plans for the writeup.

prompt_instructions = """Write a passage to describe our new product:
Javelin 20 High Performance Shoes.

The passage should be no more than 100 words.

The title must contain the sentence: 
A new you

The first sentence must contain a few of these words: 
motivation, workout, sports, attractive

The last few sentences must motivate the reader to visit out website http://acme.com/jav20

The passage must contain the phrase : 
trend setter
"""

Solution Prompt #1

ouput_passage_indicator = "Passage:"
ouput_plan_indicator = "Plan:"

prompt = prompt_instructions + """
Your output will be in following format.

{ouput_plan_indicator}
Your plan here.

{ouput_passage_indicator}
Your passage here.
""" 

Prompt #2

In this step you will instruct the LLM to go over the plans and pick the best one. You will need pass the plans in the prompt. Make an assumption that plans are in a variable plans as a string.

  • Use the variables declared earlier
  • Introduce new output indicators as needed

Solution Prompt #2

In this prompt we are asking to explicitly share a single number (index) to identify the best plan, that needed us to create an output indicator.

output_best_plan_indicator = "Best Plan:"

prompt_plan_vote = """
3 experts were given the following task.

Task:

{prompt_instructions}

you need to review the 3 plans and identify the best plan out of the 3. 

Output your response as follows, where N is the plan number. 
{output_best_plan_indicator} N

Give 3 reasons why you select the plan.

{plans}

"""

Prompt #3

Now you are ready to create the passage using the best plan. Write a prompt to instruct LLM to generate 3 passages using the best plan. Assume that there is a variable best_plan that holds the best plan as a string, use it as a placeholder in the prompt.

Solution #3


prompt_generate_3_passages = """
Create three passages using the following instructions and plan.

Instructions:
{prompt_instructions}

{best_plan}

Your output will be in following format.

{ouput_passage_indicator}
Your response here

"""

Prompt #4

Now instruct the LLM to vote on the 3 passages or writeups generated in the previous step. To identify the passage introduce a output indicator. Assume that all the passages generated in last step are available in a variable passages, use it as a placeholder in the prompt.

Solution #4


output_best_passage_indicator = "Best Passage:"

prompt_passage_vote = """
3 experts were given the following task.

Task:
{prompt_instructions}

you need to review the 3 passages and identify the best passage out of the 3. 

Output your response as follows, where N is the passage number. 
{output_best_passage_indicator} N

Give 3 reasons why you select the passage.

{passages}

"""