Thoughts on Prompt Engineering

The process of creating meaningful prompts or instructions for an AI system or language model is known as “prompt engineering”. The model’s responses and behaviors are greatly influenced by these cues. Prompts with a good design facilitate researchers and developers to modify the output of AI systems to accomplish desired results and enhance the model’s functionality.

    
folder INSTRUCTION {
    artifact """"Answer the question based on the context below.\nIf the question cannot be answer using the \ninformation provided answer with "I don't know"."
}

folder CONTEXT {
    artifact "Italian cuisine has a rich and diverse history that spans centuries \nand has been influenced by numerous cultures. In the Roman Empire, food \nwas a significant part of social life. Romans loved feasting and trying \nnew dishes, and their banquets often featured complex, intricate \nflavors that required sophisticated preparation techniques. They \nembraced the flavors and ingredients of many of the lands they had \nconquered, such as spices from the Middle East, fish from the \nMediterranean, and cereals from North Africa. This fusion of diverse \ningredients made the Empire a hot spot for culinary innovation"
}

folder QUESTIONS {
    artifact "What is the history of Italian cuisine?"
}

folder ANSWER {
    artifact """""""
}

INSTRUCTION -[hidden]- CONTEXT
CONTEXT -[hidden]- QUESTIONS
QUESTIONS -[hidden] ANSWER

    

Figure 2.3: Prompt Template

In the context of LLMs, it consists of several key components:

  • Instruction: An instruction is a specific directive given to the model, guiding its responses. It can be a statement, command, or question that dictates how the AI should process input data. For instance, instructions like “Translate this English text to French” or “Summarize the article” shape the model’s output. Crafting precise prompts significantly impacts the quality of an LLM’s results. My personal experience in LLM development reveals that output quality varies based on prompt quality, as each LLM has its preferred prompt format and template. Hence, the potential for standardizing prompts and prompt templates in metadata before adapting them to an LLM presents an innovative opportunity, such as Lepton and AutoPrompt open-source projects that I mentioned earlier. I aspire to delve deeper into this area through further research.

  • Context: Context offers relevant background details to steer the model’s response, aiding in specific situational, topical, or domain-related prompts. For example, in chatbot interactions, context encompasses prior messages. In machine translation, it includes surrounding text. In practice, maintaining a completely private context ensures that similarity search outputs from a local LLM remain private and secure.

  • Question: The question is the specific query or input data that we want the model to respond to. It’s the focus of the prompt and is typically what you want the model to process and generate a response for. The question should be clear and concise to ensure the model can understand and accurately respond to it.

  • Answer: The answer (or output, or result) is the response generated by the model based on the instruction, context, and question. The format and type of answer can be guided by an output indicator, which specifies the kind of response expected from the model. For instance, an output indicator might specify that the answer should be a list of items, a single word, or a full sentence.

Each of these components plays a vital role in shaping the model’s response, and their effective use can greatly enhance the model’s performance on a wide range of tasks. However, it is important to note that not all components are required for every prompt, and the format of the prompt can vary depending on the task at hand.

Here’s an example of defining prompt in ChatPromptTemplate class in LangChain. To augment the prompt by incorporating extra context, it is essential to create a prompt template. This template allows for easy customization of the prompt, as illustrated below.

from langchain.prompts import ChatPromptTemplate

template = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
Question: {question}
Context: {context}
Answer:
"""
prompt = ChatPromptTemplate.from_template(template)
print(prompt)

The output will display input_variables containing context and question in PromptTemplate.

input_variables=['context', 'question'] input_types={} partial_variables={} messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], input_types={}, partial_variables={}, template="You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\nQuestion: {question}\nContext: {context}\nAnswer:\n"), additional_kwargs={})]

By understanding the significance of prompts and prompt templates in LangChain, we lay the foundation for exploring more advanced concepts like VectorStore and embeddings.