So far, you've learned how to ask questions over the content of custom
documents using Langchain.
This approach is called REG, Retrieval
Augmented Generation.
You've also learned how to add memory
using the conversational retrieval chain, enabling users to ask follow -up questions.
However, we've used the default prompt so far.
Let's explore how to change the system
prompt and use prompt engineering techniques to ask questions in specific ways.
Note that we discussed prompt templates in the first section of the course, so
you can take a look there if you feel the need.
In a new cell, I will copy and paste the
code from the previous video and change it to add the custom prompt.
From Langchain prompts, I will import three classes, ChatPromptTemplate,
SystemMessagePromptTemplate and HumanMessagePromptTemplate.
I am defining the template for the system
prompt, this sets the general behavior of the LLM.
I am using a multi -line raw string because the string may contain both
single and double quotes.
Use the following pieces of context to
answer the user's question.
I am adding some dashes and the context
colon and the context in curly braces.
This is the dynamic part of the prompt.
I will also enclose it in triple backticks.
Now the template for the user prompt.
UserTemplate equals and the multi -line string question colon and question in
curly braces and triple backticks and the chat history colon chat history in curly braces.
This is the key of the memory object.
I am defining a list called messages.
The first item in the list is an object of type SystemMessagePromptTemplate.
Element in the list is an object of HumanMessagePromptTemplate.
That from template of user template.
And I am creating the question and answer prompt from the message.
This is a chat prompt template to use the new prompt.
CombineDocsChainKeyworks.
It is a long name.
Equals a dictionary, the key is prompt and the value is QAPrompt, the object of
type ChatPromptTemplate.
And that is it.
I am running the code.
I am also displaying the QAPrompt.
Very well.
Let us ask questions.
I am loading the vector store from the directory on disk.
I am calling loadEmbeddingsChroma.
I am copy pasting the last question.
How many pairs of questions and answers had the StackOverflow dataset?
I am running it.
And you see the prompt.
You see the context.
Let us ask something else.
When was Elon Musk born?
And this is the answer.
Now, I am changing the system prompt so that it will respond in another language,
let us say Spanish.
Use the following pieces of context to
answer the user s question in Spanish.
I am running the code and I am sending
the question to the LLM.
Very well.
The response is in Spanish as I requested it.
In my experience, a common business
request is to answer only from the provided documents.
At this moment, the application tries to answer from the context it receives from
the retriever and if it does not find the answer within the provided context, it
will respond from its general knowledge base.
Of course, the date of birth of Elon Musk
is not specified in our PDF document.
Let us add to the system prompt.
If you do not find the answer in the provided context, just respond, I do not know.
I am running the code.
I am running the query again asking for
Bill Gates date of birth this time.
When was Bill Gates born?
And I am running it.
It responded, I do not know in Spanish,
exactly what we wanted.
We ve done it.
This video showed how to use a custom prompt to tailor the LLM s answer to your
specific needs.
We created a powerful question answering
application that can tackle any query about custom or private documents.
It leverages OpenAI, Pinecon or Chroma and Langchain to quickly and efficiently
process vast amounts of text data.
Soon, we ll likely add a slick web
interface, some other nice features and even deploy it in production.
We might even market it to other businesses.