Spaces:
Runtime error
Runtime error
# flake8: noqa | |
from langchain.chains.prompt_selector import ConditionalPromptSelector, is_chat_model | |
from langchain_core.prompts import PromptTemplate | |
from langchain_core.prompts.chat import ( | |
ChatPromptTemplate, | |
HumanMessagePromptTemplate, | |
SystemMessagePromptTemplate, | |
) | |
prompt_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. | |
{context} | |
Question: {question} | |
Helpful Answer:""" | |
PROMPT = PromptTemplate( | |
template=prompt_template, input_variables=["context", "question"] | |
) | |
system_template = """Use the following pieces of context to answer the user's question. | |
If you don't know the answer, just say that you don't know, don't try to make up an answer. | |
---------------- | |
{context}""" | |
messages = [ | |
SystemMessagePromptTemplate.from_template(system_template), | |
HumanMessagePromptTemplate.from_template("{question}"), | |
] | |
CHAT_PROMPT = ChatPromptTemplate.from_messages(messages) | |
PROMPT_SELECTOR = ConditionalPromptSelector( | |
default_prompt=PROMPT, conditionals=[(is_chat_model, CHAT_PROMPT)] | |
) | |