Friday, April 26, 2024

Langchain How to create Custom PromptTemplate for RAG

Below is an example. 

from langchain_core.prompts import PromptTemplate


template = """Use the following pieces of context to answer the question at the end.

If you don't know the answer, just say that you don't know, don't try to make up an answer.

Use three sentences maximum and keep the answer as concise as possible.

Always say "thanks for asking!" at the end of the answer.


{context}


Question: {question}


Helpful Answer:"""

custom_rag_prompt = PromptTemplate.from_template(template)


rag_chain = (

    {"context": retriever | format_docs, "question": RunnablePassthrough()}

    | custom_rag_prompt

    | llm

    | StrOutputParser()

)


rag_chain.invoke("What is Task Decomposition?")


references:

https://python.langchain.com/docs/use_cases/question_answering/quickstart/


No comments:

Post a Comment