Spaces:
Runtime error
Runtime error
Download chain.py from Aryan619348/rag: direct link, hf CLI and curl.
- Browser
- Download file 882 Bytes
-
https://huggingface.co/spaces/Aryan619348/rag/resolve/main/chain.py
- Command line
-
hf download hf://spaces/Aryan619348/rag/chain.py
-
curl -L -o chain.py https://huggingface.co/spaces/Aryan619348/rag/resolve/main/chain.py
882 Bytes
| import os | |
| # from dotenv import load_dotenv | |
| # load_dotenv() | |
| #os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY") | |
| OPENAI_API_KEY = os.environ['OPENAI_API_KEY'] | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.prompts import ChatPromptTemplate | |
| from langchain_core.output_parsers import StrOutputParser | |
| from langchain_core.runnables import RunnablePassthrough | |
| def my_chain(retriever,question): | |
| template = """Answer the question based only on the following context: | |
| {context} | |
| Question: {question} | |
| """ | |
| prompt = ChatPromptTemplate.from_template(template) | |
| llm = ChatOpenAI( | |
| model_name="gpt-3.5-turbo", temperature=0, streaming=True | |
| ) | |
| chain = ( | |
| {"context": retriever, "question": RunnablePassthrough()} | |
| | prompt | |
| | llm | |
| | StrOutputParser() | |
| ) | |
| answer=chain.invoke(question) | |
| return answer | |