Download climateqa/engine/chains/translation.py from Ekimetrics/climate-question-answering: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/translation.py
- Command line
-
hf download hf://spaces/Ekimetrics/climate-question-answering@5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/translation.py
-
curl -L -o translation.py https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/5bb55d9e89f932b6b2b42879aa18aeff00e051b9/climateqa/engine/chains/translation.py
1.36 kB
| from langchain_core.pydantic_v1 import BaseModel, Field | |
| from typing import List | |
| from typing import Literal | |
| from langchain.prompts import ChatPromptTemplate | |
| from langchain_core.utils.function_calling import convert_to_openai_function | |
| from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser | |
| class Translation(BaseModel): | |
| """Analyzing the user message input""" | |
| translation: str = Field( | |
| description="Translate the message input to English", | |
| ) | |
| def make_translation_chain(llm): | |
| openai_functions = [convert_to_openai_function(Translation)] | |
| llm_with_functions = llm.bind(functions = openai_functions,function_call={"name":"Translation"}) | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", "You are a helpful assistant, you will translate the user input message to English using the function provided"), | |
| ("user", "input: {input}") | |
| ]) | |
| chain = prompt | llm_with_functions | JsonOutputFunctionsParser() | |
| return chain | |
| def make_translation_node(llm): | |
| translation_chain = make_translation_chain(llm) | |
| def translate_query(state): | |
| print("---- Translate query ----") | |
| user_input = state["user_input"] | |
| translation = translation_chain.invoke({"input":user_input}) | |
| return {"query":translation["translation"]} | |
| return translate_query | |