mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-31 18:38:48 +00:00
Format Templates (#12396)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
from stepback_qa_prompting.chain import chain
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
chain.invoke({"question": "was chatgpt around while trump was president?"})
|
||||
|
@@ -4,9 +4,9 @@ from langchain.schema.output_parser import StrOutputParser
|
||||
from langchain.schema.runnable import RunnableLambda
|
||||
from langchain.utilities import DuckDuckGoSearchAPIWrapper
|
||||
|
||||
|
||||
search = DuckDuckGoSearchAPIWrapper(max_results=4)
|
||||
|
||||
|
||||
def retriever(query):
|
||||
return search.run(query)
|
||||
|
||||
@@ -15,11 +15,11 @@ def retriever(query):
|
||||
examples = [
|
||||
{
|
||||
"input": "Could the members of The Police perform lawful arrests?",
|
||||
"output": "what can the members of The Police do?"
|
||||
"output": "what can the members of The Police do?",
|
||||
},
|
||||
{
|
||||
"input": "Jan Sindel’s was born in what country?",
|
||||
"output": "what is Jan Sindel’s personal history?"
|
||||
"input": "Jan Sindel’s was born in what country?",
|
||||
"output": "what is Jan Sindel’s personal history?",
|
||||
},
|
||||
]
|
||||
# We now transform these to example messages
|
||||
@@ -34,13 +34,20 @@ few_shot_prompt = FewShotChatMessagePromptTemplate(
|
||||
examples=examples,
|
||||
)
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages([
|
||||
("system", """You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is easier to answer. Here are a few examples:"""),
|
||||
# Few shot examples
|
||||
few_shot_prompt,
|
||||
# New question
|
||||
("user", "{question}"),
|
||||
])
|
||||
prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"You are an expert at world knowledge. Your task is to step back "
|
||||
"and paraphrase a question to a more generic step-back question, which "
|
||||
"is easier to answer. Here are a few examples:",
|
||||
),
|
||||
# Few shot examples
|
||||
few_shot_prompt,
|
||||
# New question
|
||||
("user", "{question}"),
|
||||
]
|
||||
)
|
||||
|
||||
question_gen = prompt | ChatOpenAI(temperature=0) | StrOutputParser()
|
||||
|
||||
@@ -50,16 +57,19 @@ response_prompt_template = """You are an expert of world knowledge. I am going t
|
||||
{step_back_context}
|
||||
|
||||
Original Question: {question}
|
||||
Answer:"""
|
||||
Answer:""" # noqa: E501
|
||||
response_prompt = ChatPromptTemplate.from_template(response_prompt_template)
|
||||
|
||||
chain = {
|
||||
# Retrieve context using the normal question
|
||||
"normal_context": RunnableLambda(lambda x: x['question']) | retriever,
|
||||
# Retrieve context using the step-back question
|
||||
"step_back_context": question_gen | retriever,
|
||||
# Pass on the question
|
||||
"question": lambda x: x["question"]
|
||||
} | response_prompt | ChatOpenAI(temperature=0) | StrOutputParser()
|
||||
|
||||
|
||||
chain = (
|
||||
{
|
||||
# Retrieve context using the normal question
|
||||
"normal_context": RunnableLambda(lambda x: x["question"]) | retriever,
|
||||
# Retrieve context using the step-back question
|
||||
"step_back_context": question_gen | retriever,
|
||||
# Pass on the question
|
||||
"question": lambda x: x["question"],
|
||||
}
|
||||
| response_prompt
|
||||
| ChatOpenAI(temperature=0)
|
||||
| StrOutputParser()
|
||||
)
|
||||
|
Reference in New Issue
Block a user