Spaces:
Build error
Build error
Download app.py from abdvl/datahub_qa_bot: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/abdvl/datahub_qa_bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/abdvl/datahub_qa_bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abdvl/datahub_qa_bot/resolve/main/app.py
1.89 kB
| import os | |
| import streamlit as st | |
| from langchain.chains import RetrievalQA | |
| from langchain.llms import OpenAI | |
| from langchain.document_loaders import DirectoryLoader | |
| from langchain.text_splitter import CharacterTextSplitter | |
| from langchain.indexes import VectorstoreIndexCreator | |
| from langchain.embeddings import OpenAIEmbeddings | |
| from langchain.vectorstores import Chroma | |
| from langchain.callbacks import get_openai_callback | |
| # variables | |
| db_folder = "db" | |
| qa = None | |
| db = None | |
| # init LLM and retriever | |
| def init(api_key): | |
| # API key | |
| os.environ["OPENAI_API_KEY"] = api_key | |
| # initialize the language model | |
| llm = OpenAI(model_name="gpt-3.5-turbo") | |
| with get_openai_callback() as cb: | |
| # create the embeddings and index | |
| embeddings = OpenAIEmbeddings() | |
| # Init vectorstore | |
| db = Chroma(persist_directory=db_folder, embedding_function=embeddings) | |
| # create retriever from the DB | |
| retriever = db.as_retriever(search_type="mmr") | |
| # initialize the chain | |
| qa = RetrievalQA.from_chain_type( | |
| llm=llm, chain_type="stuff", retriever=retriever) | |
| return qa, db | |
| # UI | |
| st.title('DataHub QA Chat Demo') | |
| # set your OpenAI API key | |
| api_key = st.text_input('OPENAI API KEY') | |
| # query input box | |
| question = st.text_input('Ask a question', 'What is DataHub') | |
| # query button | |
| if st.button('Query'): | |
| if api_key == "": | |
| st.write("Please provide your OpenAI API KEY, the query is cheap ($0.0001 per query))") | |
| else: | |
| qa, db = init(api_key) | |
| # | |
| st.subheader('Result from OpenAI after querying the vectorstore') | |
| st.write(qa.run(question)) | |
| # | |
| st.subheader('Raw result from vectorstore') | |
| docs = db.similarity_search(question) | |
| result = docs[0] | |
| st.write(result.page_content) | |
| st.subheader('Source') | |
| st.write(result.metadata) | |