File size: 4,086 Bytes
4c8c217 52b8278 74d32cc 0ff2378 1dd3a02 986e2b7 0ff2378 1dd3a02 b5fe45f 0ff2378 b5fe45f 0ff2378 b5fe45f 0ff2378 b5fe45f 0ff2378 885ec28 b1d9a7f 885ec28 0ff2378 4c8c217 1dd3a02 b0d0303 4c8c217 c46950c 6b80b65 52b8278 1572555 52b8278 0ff2378 c306f1c 986e2b7 52b8278 b5fe45f 8c67f60 52b8278 6ad6f7e 6b80b65 4c8c217 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import streamlit as st
from langchain_core.messages import AIMessage, HumanMessage
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
def get_response(user_input):
return "I dont know"
def get_vector_store_from_url(url):
loader = WebBaseLoader(url)
document = loader.load()
# split the document into chunks
text_splitter = RecursiveCharacterTextSplitter()
document_chunks = text_splitter.split_documents(document)
# create a vectorstore from the chunks
vector_store = Chroma.from_documents(document_chunks, OpenAIEmbeddings())
return vector_store
def get_context_retriever_chain(vector_store):
llm = ChatOpenAI()
retriever = vector_store.as_retriever()
prompt = ChatPromptTemplate.from_messages([
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")
])
retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
return retriever_chain
def get_conversational_rag_chain(retriever_chain,openai_apikey):
llm = ChatOpenAI(openai_api_key=openai_apikey)
prompt = ChatPromptTemplate.from_messages([
("system", "Answer the user's questions based on the below context:\n\n{context}"),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
])
stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
return create_retrieval_chain(retriever_chain, stuff_documents_chain)
def get_response(user_input,openai_apikey):
retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
conversation_rag_chain = get_conversational_rag_chain(retriever_chain,openai_apikey)
response = conversation_rag_chain.invoke({
"chat_history": st.session_state.chat_history,
"input": user_query
})
return response['answer']
# app config
st.set_page_config(page_title= "Chat with Websites", page_icon="🤖")
st.title("Chat with Websites")
#sidebar
with st.sidebar:
st.header("Settings")
website_url = st.text_input("Website URL")
openai_apikey = st.text_input("Enter your OpenAI API key")
if (website_url is None or website_url == "") or (openai_apikey is None or openai_apikey == ""):
st.info("Please ensure if website URL and Open AI api key are entered")
else:
if "chat_history" not in st.session_state:
st.session_state.chat_history = [
AIMessage(content = "Hello, I am a bot. How can I help you"),
]
if "vector_store" not in st.session_state:
st.session_state.vector_store = get_vector_store_from_url(website_url)
#user_input
user_query = st.chat_input("Type your message here...")
if user_query is not None and user_query !="":
response = get_response(user_query,openai_apikey)
st.session_state.chat_history.append(HumanMessage(content=user_query))
st.session_state.chat_history.append(AIMessage(content=response))
#conversation
for message in st.session_state.chat_history:
if isinstance(message, AIMessage): # checking if the messsage is the instance of an AI message
with st.chat_message("AI"):
st.write(message.content)
elif isinstance(message, HumanMessage): # checking if the messsage is the instance of a Human
with st.chat_message("Human"):
st.write(message.content)
|