CoolFace
Apppublic

dipeshtech/10k_llm_streamlit

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
app.py109 linesDownload Raw Back to root
1import streamlit as st2from PyPDF2 import PdfReader3from langchain.embeddings.openai import OpenAIEmbeddings4from langchain.text_splitter import CharacterTextSplitter5from langchain.vectorstores import FAISS6 7from langchain.agents import initialize_agent, AgentType8from langchain.callbacks import StreamlitCallbackHandler9from langchain.chat_models import ChatOpenAI10 11from langchain.chains.question_answering import load_qa_chain12from langchain.llms import OpenAI13 14from langchain_openai import ChatOpenAI15from langchain_core.output_parsers import StrOutputParser16from langchain_core.runnables import RunnablePassthrough17 18import os19from dotenv import load_dotenv20 21load_dotenv()22 23 24# provide the path of  pdf file/files.25pdfreader = PdfReader('input_data/nvidia_10k.pdf')26 27with st.sidebar:28    openai_api_key = st.text_input("OpenAI API Key", type="password")29    "[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"30 31@st.cache_data32def split_chunk_text(input_path="input_data/nvidia_10k.pdf"):33    from typing_extensions import Concatenate34    # read text from pdf35    36    pdfreader = PdfReader(input_path)37    raw_text = ''38    for i, page in enumerate(pdfreader.pages):39        content = page.extract_text()40        if content:41            raw_text += content42    text_splitter = CharacterTextSplitter(43    separator = "\n",44    chunk_size = 800,45    chunk_overlap  = 200,46    length_function = len,47    )48    texts = text_splitter.split_text(raw_text)49    50    return texts51    52 53 54 55 56 57with st.form("my_form"):58    texts = split_chunk_text()59    60    embeddings = OpenAIEmbeddings(61        model="text-embedding-3-small"62        )63    64    vector_store = FAISS.from_texts(texts, embeddings)65    retriever = vector_store.as_retriever()66    text = st.text_area("Enter question:", " ")67    68    submitted = st.form_submit_button("Submit")69    if not openai_api_key:70        st.info("Please add your OpenAI API key to continue.")71    elif submitted:72        #texts = split_chunk_text()73        74        from operator import itemgetter75 76        from langchain.prompts import ChatPromptTemplate77 78        template = """Answer the question based only on the following context. If you cannot answer the question with the context, please respond with 'I don't know':79 80        Context:81        {context}82 83        Question:84        {question}85        """86 87        prompt = ChatPromptTemplate.from_template(template)88 89        primary_qa_llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)90 91        retrieval_augmented_qa_chain = (92            # INVOKE CHAIN WITH: {"question" : "<<SOME USER QUESTION>>"}93            # "question" : populated by getting the value of the "question" key94            # "context"  : populated by getting the value of the "question" key and chaining it into the base_retriever95            {"context": itemgetter("question") | retriever, "question": itemgetter("question")}96            # "context"  : is assigned to a RunnablePassthrough object (will not be called or considered in the next step)97            #              by getting the value of the "context" key from the previous step98            | RunnablePassthrough.assign(context=itemgetter("context"))99            # "response" : the "context" and "question" values are used to format our prompt object and then piped100            #              into the LLM and stored in a key called "response"101            # "context"  : populated by getting the value of the "context" key from the previous step102            | {"response": prompt | primary_qa_llm, "context": itemgetter("context")}103        )104        #query = "Who is liable in case of an accident if a learner is driving with an instructor?"105        106        result = retrieval_augmented_qa_chain.invoke({"question" : text})107        108        109        st.info(result["response"].content)