Download climateqa/engine/chains/standalone_question.py from Ekimetrics/climate-question-answering: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/standalone_question.py
- Command line
-
hf download hf://spaces/Ekimetrics/climate-question-answering@5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/standalone_question.py
-
curl -L -o standalone_question.py https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/standalone_question.py
1.62 kB
| from langchain.prompts import ChatPromptTemplate | |
| def make_standalone_question_chain(llm): | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", """You are a helpful assistant that transforms user questions into standalone questions | |
| by incorporating context from the chat history if needed. The output should be a self-contained | |
| question that can be understood without any additional context. | |
| Examples: | |
| Chat History: "Let's talk about renewable energy" | |
| User Input: "What about solar?" | |
| Output: "What are the key aspects of solar energy as a renewable energy source?" | |
| Chat History: "What causes global warming?" | |
| User Input: "And what are its effects?" | |
| Output: "What are the effects of global warming on the environment and society?" | |
| """), | |
| ("user", """Chat History: {chat_history} | |
| User Question: {question} | |
| Transform this into a standalone question: | |
| Make sure to keep the original language of the question.""") | |
| ]) | |
| chain = prompt | llm | |
| return chain | |
| def make_standalone_question_node(llm): | |
| standalone_chain = make_standalone_question_chain(llm) | |
| def transform_to_standalone(state): | |
| chat_history = state.get("chat_history", "") | |
| if chat_history == "": | |
| return {} | |
| output = standalone_chain.invoke({ | |
| "chat_history": chat_history, | |
| "question": state["user_input"] | |
| }) | |
| state["user_input"] = output.content | |
| return state | |
| return transform_to_standalone | |