Download climateqa/engine/chains/reformulation.py from Ekimetrics/climate-question-answering: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/reformulation.py
- Command line
-
hf download hf://spaces/Ekimetrics/climate-question-answering@5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/reformulation.py
-
curl -L -o reformulation.py https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/reformulation.py
1.37 kB
| from langchain.output_parsers.structured import StructuredOutputParser, ResponseSchema | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain_core.runnables import RunnablePassthrough, RunnableLambda, RunnableBranch | |
| from climateqa.engine.chains.prompts import reformulation_prompt_template | |
| from climateqa.engine.utils import pass_values, flatten_dict | |
| 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 fallback_default_values(x): | |
| if x["question"] is None: | |
| x["question"] = x["query"] | |
| x["language"] = "english" | |
| return x | |
| 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) | |
| reformulation_chain = ( | |
| {"reformulation":chain,**pass_values(["query"])} | |
| | RunnablePassthrough() | |
| | flatten_dict | |
| | fallback_default_values | |
| ) | |
| return reformulation_chain | |