Spaces:
Sleeping
Sleeping
commit
Browse files- .gitignore +1 -0
- app.py +45 -0
- asset/alien_008.jpg +0 -0
- asset/human.jpg +0 -0
- requirements.txt +8 -0
- src/__init__.py +1 -0
- src/__pycache__/__init__.cpython-38.pyc +0 -0
- src/__pycache__/get_vector.cpython-38.pyc +0 -0
- src/get_vector.py +74 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 3 |
+
from src import get_vectorstore_from_url,get_response
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# app config
|
| 7 |
+
st.set_page_config(page_title="Chat with websites", page_icon="asset/alien_008.jpg")
|
| 8 |
+
st.title("Chat with websites")
|
| 9 |
+
|
| 10 |
+
# sidebar
|
| 11 |
+
|
| 12 |
+
with st.sidebar:
|
| 13 |
+
st.header("Settings")
|
| 14 |
+
website_url = st.text_input("Website URL",
|
| 15 |
+
placeholder='https://en.wikipedia.org/wiki/Artificial_intelligence')
|
| 16 |
+
|
| 17 |
+
if website_url is None or website_url == "":
|
| 18 |
+
st.info("Please enter a website URL in the sidebar")
|
| 19 |
+
|
| 20 |
+
else:
|
| 21 |
+
# session state
|
| 22 |
+
if "chat_history" not in st.session_state:
|
| 23 |
+
st.session_state.chat_history = [
|
| 24 |
+
AIMessage(content="Hello. How can I help you?"),
|
| 25 |
+
]
|
| 26 |
+
if "vector_store" not in st.session_state:
|
| 27 |
+
st.session_state.vector_store = get_vectorstore_from_url(website_url)
|
| 28 |
+
|
| 29 |
+
user_query = st.chat_input("Type your message here...")
|
| 30 |
+
|
| 31 |
+
if user_query is not None and user_query != "":
|
| 32 |
+
response = get_response(user_query)
|
| 33 |
+
# input chat
|
| 34 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
| 35 |
+
# output chat
|
| 36 |
+
st.session_state.chat_history.append(AIMessage(content=response))
|
| 37 |
+
|
| 38 |
+
# conversation
|
| 39 |
+
for message in st.session_state.chat_history:
|
| 40 |
+
if isinstance(message, AIMessage):
|
| 41 |
+
with st.chat_message("AI",avatar='asset/alien_008.jpg'):
|
| 42 |
+
st.write(message.content)
|
| 43 |
+
elif isinstance(message, HumanMessage):
|
| 44 |
+
with st.chat_message("Human",avatar='asset/human.jpg'):
|
| 45 |
+
st.write(message.content)
|
asset/alien_008.jpg
ADDED
|
asset/human.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==1.12.0
|
| 2 |
+
langchain==0.1.9
|
| 3 |
+
langchain_openai
|
| 4 |
+
streamlit
|
| 5 |
+
python-dotenv
|
| 6 |
+
openpyxl==3.1.2
|
| 7 |
+
chromadb==0.4.24
|
| 8 |
+
bs4==4.12.3
|
src/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .get_vector import *
|
src/__pycache__/__init__.cpython-38.pyc
ADDED
|
Binary file (198 Bytes). View file
|
|
|
src/__pycache__/get_vector.cpython-38.pyc
ADDED
|
Binary file (2.37 kB). View file
|
|
|
src/get_vector.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_community.vectorstores import Chroma
|
| 5 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
+
from langchain.chains import create_history_aware_retriever,create_retrieval_chain
|
| 8 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
KEY=os.getenv("MY_KEY")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_vectorstore_from_url(url):
|
| 17 |
+
# get the text in document form
|
| 18 |
+
loader = WebBaseLoader(url)
|
| 19 |
+
document = loader.load()
|
| 20 |
+
|
| 21 |
+
# split the document into chunks
|
| 22 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=200,
|
| 23 |
+
chunk_overlap=10,
|
| 24 |
+
length_function=len)
|
| 25 |
+
document_chunks = text_splitter.split_documents(document)
|
| 26 |
+
|
| 27 |
+
# create a vectorstore from the chunks
|
| 28 |
+
vector_store = Chroma.from_documents(document_chunks,
|
| 29 |
+
OpenAIEmbeddings(openai_api_key=KEY))
|
| 30 |
+
|
| 31 |
+
return vector_store
|
| 32 |
+
|
| 33 |
+
def get_response(user_input):
|
| 34 |
+
retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
|
| 35 |
+
conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
|
| 36 |
+
|
| 37 |
+
response = conversation_rag_chain.invoke({
|
| 38 |
+
"chat_history": st.session_state.chat_history,
|
| 39 |
+
"input": user_input
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
return response['answer']
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_context_retriever_chain(vector_store):
|
| 46 |
+
llm = ChatOpenAI(openai_api_key=KEY)
|
| 47 |
+
|
| 48 |
+
retriever = vector_store.as_retriever()
|
| 49 |
+
|
| 50 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 51 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 52 |
+
("user", "{input}"),
|
| 53 |
+
("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")
|
| 54 |
+
])
|
| 55 |
+
|
| 56 |
+
retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
|
| 57 |
+
|
| 58 |
+
return retriever_chain
|
| 59 |
+
|
| 60 |
+
def get_conversational_rag_chain(retriever_chain):
|
| 61 |
+
|
| 62 |
+
llm = ChatOpenAI(openai_api_key=KEY)
|
| 63 |
+
|
| 64 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 65 |
+
("system", "Answer the user's questions based on the below context:\n\n{context}"),
|
| 66 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 67 |
+
("user", "{input}"),
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
|
| 71 |
+
|
| 72 |
+
return create_retrieval_chain(retriever_chain, stuff_documents_chain)
|
| 73 |
+
|
| 74 |
+
|