Search code examples
promptlangchain

How to write custom prompt-template in llama2 model using langchain?


I am using Llama2[7b model]-hugging face and lang-chain to do a simple address segregation/classification task. I want the model to find the city, state and country from the input string.I want my answer/query formatted in a particular way for a question-answering/ text-generation task.I understand that i can use FewShotPromptTemplate, where in i can show some examples to the LLM and get the output in the format i want.

I generated a few examples to feed in as samples :

examples = [
    {"input": "Plot No. 7, Sector 22, Noida, Uttar Pradesh, 201301, India",
     "Address": "Plot No. 7, Sector 22, Noida",
     "City" : "Noida",
     "State" : "Uttar Pradesh",
     "Country" : "India"},


    {"input": "Banjara Hills, Telangana, 500034, India",
     "Address": "Banjara Hills",
     "City" : "Not present",
     "State" : "Telangana",
     "Country" : "India"},

]

I set the template

example_formatter_template = """
input: {input},
Address : {Address},
City : {Address},
State : {State},
Country : {Country},
         \n
"""
# prompt
example_prompt = PromptTemplate(
    input_variables=["input", "Address", "City", "State", "Country"],
    template=example_formatter_template)

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="What is the address, city, state, country in the string : ",
    suffix="input: {input}\n ",
    input_variables=["input"],
    example_separator="\n")


chain = LLMChain(llm=llm, prompt=few_shot_prompt, verbose = True)

# Run the chain only specifying the input variable.
print(chain.run("B-12, Gandhi Colony, Bhopal, Madhya Pradesh, 462016, India"))

Here is an example of what i want :

    {"input": "B-12, Gandhi Colony, Bhopal, Madhya Pradesh, 462016, India",

     "Address": "B-12, Gandhi Colony",
     "City" : "Bhopal",
     "State" : "Madhya Pradesh",
     "Country" : "India"},


I keep getting : format the expected output correctly from the model. And nothing is hence returned.

Additionally, I want to prevent the model from adding any extra information which is not present in the context/string otherwise the queries take very long to respond. i.e return '' or not found if city or state or country is not present in sting.

can someone help ?


Solution

  • Try after changing the examples and the format to this:

    {"input": "Banjara Hills, Telangana, 500034, India",
     "output":
     """
     Address: Banjara Hills
     City: Not present
     State: Telangana
     Country: India
     """
     },
    

    Refer to this as well https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/few_shot_examples