File size: 933 Bytes
139fefe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from climateqa.engine.prompts import reformulation_prompt_template
response_schemas = [
ResponseSchema(name="language", description="The detected language of the input message"),
ResponseSchema(name="question", description="The reformulated question always in English")
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
def make_reformulation_chain(llm):
prompt = PromptTemplate(
template=reformulation_prompt_template,
input_variables=["query"],
partial_variables={"format_instructions": format_instructions}
)
chain = (prompt | llm.bind(stop=["```"]) | output_parser)
return chain
|