DEV Community

Liam Tobei
Liam Tobei

Posted on

First Prompt Template

Prompts being input to LLMs are often structured in different ways so that we can get different results. For Q&A, we could take a user’s question and reformat it for different Q&A styles, like conventional Q&A, a bullet list of answers, or even a summary of problems relevant to the given question.

Creating Prompts in LangChain
Let’s put together a simple question-answering prompt template. We first need to install the langchain library.

!pip install langchain
Enter fullscreen mode Exit fullscreen mode

From here, we import the PromptTemplate class and initialize a template like so:

from langchain import PromptTemplate

template = """Question: {question}

Answer: """
prompt = PromptTemplate(
        template=template,
    input_variables=['question']
)

# user question
question = "Which NFL team won the Super Bowl in the 2010 season?"
Enter fullscreen mode Exit fullscreen mode

When using these prompt template with the given question we will get:

Question: Which NFL team won the Super Bowl in the 2010 season? Answer:
Enter fullscreen mode Exit fullscreen mode

For now, that’s all we need. We’ll use the same prompt template across both Hugging Face Hub and OpenAI LLM generations.

Hugging Face Hub LLM

The Hugging Face Hub endpoint in LangChain connects to the Hugging Face Hub and runs the models via their free inference endpoints. We need a Hugging Face account and API key to use these endpoints.

Once you have an API key, we add it to the HUGGINGFACEHUB_API_TOKEN environment variable. We can do this with Python like so:

import os

os.environ['HUGGINGFACEHUB_API_TOKEN'] = 'HF_API_KEY'
Enter fullscreen mode Exit fullscreen mode

Next, we must install the huggingface_hub library via Pip.

!pip install huggingface_hub
Enter fullscreen mode Exit fullscreen mode

Now we can generate text using a Hub model. We’ll use google/flan-t5-x1.

from langchain import HuggingFaceHub, LLMChain

# initialize Hub LLM
hub_llm = HuggingFaceHub(
        repo_id='google/flan-t5-xl',
    model_kwargs={'temperature':1e-10}
)

# create prompt template > LLM chain
llm_chain = LLMChain(
    prompt=prompt,
    llm=hub_llm
)

# ask the user question about NFL 2010
print(llm_chain.run(question))
Enter fullscreen mode Exit fullscreen mode

For this question, we get the correct answer of "green bay packers".

Asking Multiple Questions

If we’d like to ask multiple questions, we can try two approaches:

Iterate through all questions using the generate method, answering them one at a time.
Place all questions into a single prompt for the LLM; this will only work for more advanced LLMs.

Starting with option (1), let’s see how to use the generate method:

qs = [
    {'question': "Which NFL team won the Super Bowl in the 2010 season?"},
    {'question': "If I am 6 ft 4 inches, how tall am I in centimeters?"},
    {'question': "Who was the 12th person on the moon?"},
    {'question': "How many eyes does a blade of grass have?"}
]
res = llm_chain.generate(qs)
res
Enter fullscreen mode Exit fullscreen mode

Here we get bad results except for the first question. This is simply a limitation of the LLM being used.

If the model cannot answer individual questions accurately, grouping all queries into a single prompt is unlikely to work. However, for the sake of experimentation, let’s try it.

multi_template = """Answer the following questions one at a time.

Questions:
{questions}

Answers:
"""
long_prompt = PromptTemplate(template=multi_template, input_variables=["questions"])

llm_chain = LLMChain(
    prompt=long_prompt,
    llm=flan_t5
)

qs_str = (
    "Which NFL team won the Super Bowl in the 2010 season?\n" +
    "If I am 6 ft 4 inches, how tall am I in centimeters?\n" +
    "Who was the 12th person on the moon?" +
    "How many eyes does a blade of grass have?"
)

print(llm_chain.run(qs_str))
Enter fullscreen mode Exit fullscreen mode

As expected, the results are not helpful. We’ll see later that more powerful LLMs can do this.

Top comments (0)