CoolFace
Apppublic

W404NET/Chat-with-websites

sourceHugging Faceotherupdated 3y agoView on Hugging Face
0likes
app.py109 linesDownload Raw Back to src
1# pip install streamlit langchain lanchain-openai beautifulsoup4 python-dotenv chromadb2 3import streamlit as st4from langchain_core.messages import AIMessage, HumanMessage5from langchain_community.document_loaders import WebBaseLoader6from langchain.text_splitter import RecursiveCharacterTextSplitter7from langchain_community.vectorstores import Chroma8from langchain_openai import OpenAIEmbeddings, ChatOpenAI9from dotenv import load_dotenv10from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder11from langchain.chains import create_history_aware_retriever, create_retrieval_chain12from langchain.chains.combine_documents import create_stuff_documents_chain13 14 15load_dotenv()16 17def get_vectorstore_from_url(url):18    # get the text in document form19    loader = WebBaseLoader(url)20    document = loader.load()21    22    # split the document into chunks23    text_splitter = RecursiveCharacterTextSplitter()24    document_chunks = text_splitter.split_documents(document)25    26    # create a vectorstore from the chunks27    vector_store = Chroma.from_documents(document_chunks, OpenAIEmbeddings())28 29    return vector_store30 31def get_context_retriever_chain(vector_store):32    llm = ChatOpenAI()33    34    retriever = vector_store.as_retriever()35    36    prompt = ChatPromptTemplate.from_messages([37      MessagesPlaceholder(variable_name="chat_history"),38      ("user", "{input}"),39      ("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")40    ])41    42    retriever_chain = create_history_aware_retriever(llm, retriever, prompt)43    44    return retriever_chain45    46def get_conversational_rag_chain(retriever_chain): 47    48    llm = ChatOpenAI()49    50    prompt = ChatPromptTemplate.from_messages([51      ("system", "Answer the user's questions based on the below context:\n\n{context}"),52      MessagesPlaceholder(variable_name="chat_history"),53      ("user", "{input}"),54    ])55    56    stuff_documents_chain = create_stuff_documents_chain(llm,prompt)57    58    return create_retrieval_chain(retriever_chain, stuff_documents_chain)59 60def get_response(user_input):61    retriever_chain = get_context_retriever_chain(st.session_state.vector_store)62    conversation_rag_chain = get_conversational_rag_chain(retriever_chain)63    64    response = conversation_rag_chain.invoke({65        "chat_history": st.session_state.chat_history,66        "input": user_query67    })68    69    return response['answer']70 71# app config72st.set_page_config(page_title="Chat with websites", page_icon="🤖")73st.title("Chat with websites")74 75# sidebar76with st.sidebar:77    st.header("Settings")78    website_url = st.text_input("Website URL")79 80if website_url is None or website_url == "":81    st.info("Please enter a website URL")82 83else:84    # session state85    if "chat_history" not in st.session_state:86        st.session_state.chat_history = [87            AIMessage(content="Hello, I am a bot. How can I help you?"),88        ]89    if "vector_store" not in st.session_state:90        st.session_state.vector_store = get_vectorstore_from_url(website_url)    91 92    # user input93    user_query = st.chat_input("Type your message here...")94    if user_query is not None and user_query != "":95        response = get_response(user_query)96        st.session_state.chat_history.append(HumanMessage(content=user_query))97        st.session_state.chat_history.append(AIMessage(content=response))98        99       100 101    # conversation102    for message in st.session_state.chat_history:103        if isinstance(message, AIMessage):104            with st.chat_message("AI"):105                st.write(message.content)106        elif isinstance(message, HumanMessage):107            with st.chat_message("Human"):108                st.write(message.content)109