Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
3 |
+
from langchain_community.document_loaders import WebBaseLoader
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
+
from langchain_chroma import Chroma
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
|
8 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
+
from langchain.chains.history_aware_retriever import create_history_aware_retriever
|
10 |
+
from langchain.chains.retrieval import create_retrieval_chain
|
11 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
import os
|
14 |
+
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
llm = ChatOpenAI(temperature=0.5, model="mistralai/mistral-7b-instruct:free",base_url="https://openrouter.ai/api/v1",api_key=os.getenv("OPENAI_API_KEY"))
|
18 |
+
|
19 |
+
def get_vector_store(url):
|
20 |
+
loader = WebBaseLoader(url)
|
21 |
+
documents = loader.load()
|
22 |
+
|
23 |
+
# splitting into chunks
|
24 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
25 |
+
documents_chunks = text_splitter.split_documents(documents=documents)
|
26 |
+
|
27 |
+
# converting the chunks into embeddings
|
28 |
+
embeddings = HuggingFaceEndpointEmbeddings(huggingfacehub_api_token=os.getenv("HF_TOKEN"))
|
29 |
+
|
30 |
+
# store the embeddings in a database
|
31 |
+
vector_store = Chroma.from_documents(documents_chunks, embeddings)
|
32 |
+
return vector_store
|
33 |
+
|
34 |
+
def get_context_retiriever(vector_store):
|
35 |
+
retriever = vector_store.as_retriever()
|
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 |
+
retriever_chain = create_history_aware_retriever(llm=llm, retriever=retriever, prompt=prompt)
|
42 |
+
return retriever_chain
|
43 |
+
|
44 |
+
def get_conversational_chain(retriever_chain):
|
45 |
+
prompt = ChatPromptTemplate.from_messages([
|
46 |
+
("system", "Answer the user's questions based on the below context:\n\n{context}"),
|
47 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
48 |
+
("user", "{input}"),
|
49 |
+
])
|
50 |
+
stuff_documnets_chain = create_stuff_documents_chain(llm, prompt)
|
51 |
+
return create_retrieval_chain(retriever_chain, stuff_documnets_chain)
|
52 |
+
|
53 |
+
def get_response(user_input):
|
54 |
+
retriever_chain = get_context_retiriever(st.session_state.vector_store)
|
55 |
+
conversational_chain = get_conversational_chain(retriever_chain)
|
56 |
+
response = conversational_chain.invoke({
|
57 |
+
"chat_history": st.session_state.chat_history,
|
58 |
+
"input": user_input
|
59 |
+
})
|
60 |
+
return response['answer']
|
61 |
+
|
62 |
+
st.set_page_config(page_title="Chat with websites", page_icon="🤖")
|
63 |
+
st.title("Chat with websites")
|
64 |
+
st.caption("Follow me on Github: [samagra44](https://github.com/samagra44)")
|
65 |
+
with st.sidebar:
|
66 |
+
st.header("Settings")
|
67 |
+
website_url = st.text_input("Website URL")
|
68 |
+
|
69 |
+
if website_url is None or website_url == "":
|
70 |
+
st.info("Please enter a website URL")
|
71 |
+
|
72 |
+
else:
|
73 |
+
if "chat_history" not in st.session_state:
|
74 |
+
st.session_state.chat_history = [
|
75 |
+
AIMessage(content="Hello, I am a bot. How can I help you?"),
|
76 |
+
]
|
77 |
+
if "vector_store" not in st.session_state:
|
78 |
+
st.session_state.vector_store = get_vector_store(website_url)
|
79 |
+
|
80 |
+
user_query = st.chat_input("Type your message here...")
|
81 |
+
if user_query is not None and user_query != "":
|
82 |
+
response = get_response(user_query)
|
83 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
84 |
+
st.session_state.chat_history.append(AIMessage(content=response))
|
85 |
+
|
86 |
+
for message in st.session_state.chat_history:
|
87 |
+
if isinstance(message, AIMessage):
|
88 |
+
with st.chat_message("AI"):
|
89 |
+
st.write(message.content)
|
90 |
+
elif isinstance(message, HumanMessage):
|
91 |
+
with st.chat_message("Human"):
|
92 |
+
st.write(message.content)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
langchain-core
|
3 |
+
langchain-community
|
4 |
+
langchain-openai
|
5 |
+
langchain-chroma
|
6 |
+
langchain-huggingface
|
7 |
+
streamlit
|
8 |
+
python-dotenv
|