|
import streamlit as st |
|
import langchain_core |
|
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_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 |
|
from langchain_community.embeddings import HuggingFaceBgeEmbeddings |
|
from langchain_community.llms import CTransformers |
|
from ctransformers import AutoModelForCausalLM |
|
from langchain.llms import HuggingFaceHub |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.llms import HuggingFacePipeline |
|
from transformers import pipeline |
|
import os |
|
import transformers |
|
import torch |
|
from langchain_community.llms import LlamaCpp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_vector_store_from_url(url): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name='thenlper/gte-large', |
|
model_kwargs={'device': 'cpu'}) |
|
|
|
loader = WebBaseLoader(url) |
|
document = loader.load() |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter() |
|
document_chunks = text_splitter.split_documents(document) |
|
|
|
|
|
|
|
vector_store = Chroma.from_documents(document_chunks, embeddings) |
|
|
|
return vector_store |
|
|
|
|
|
def get_context_retriever_chain(vector_store,llm): |
|
|
|
llm = llm |
|
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,llm): |
|
|
|
if not retriever_chain: |
|
raise ValueError("`retriever_chain` cannot be None or an empty object.") |
|
|
|
template = "Answer the user's questions based on the below context:\n\n{context}" |
|
human_template = "{input}" |
|
|
|
prompt = ChatPromptTemplate.from_messages([ |
|
("system", template), |
|
MessagesPlaceholder(variable_name="chat_history"), |
|
("user", human_template), |
|
]) |
|
|
|
def safe_llm(input_str: str) -> str: |
|
if isinstance(input_str, langchain_core.prompts.chat.ChatPromptValue): |
|
input_str = str(input_str) |
|
|
|
|
|
|
|
return llm(input_str) |
|
|
|
stuff_documents_chain = create_stuff_documents_chain(safe_llm, prompt) |
|
|
|
return create_retrieval_chain(retriever_chain, stuff_documents_chain) |
|
|
|
def get_response(user_input): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm = LlamaCpp( |
|
model_path="tinyllama-1.1b-chat-v1.0.Q4_0.gguf", |
|
temperature=0.75, |
|
max_tokens=500, |
|
top_p=1, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
retriever_chain = get_context_retriever_chain(st.session_state.vector_store,llm) |
|
conversation_rag_chain = get_conversational_rag_chain(retriever_chain,llm) |
|
|
|
response = conversation_rag_chain.invoke({ |
|
"chat_history": st.session_state.chat_history, |
|
"input": user_query |
|
}) |
|
|
|
return response['answer'] |
|
|
|
|
|
|
|
st.set_page_config(page_title= "Chat with Websites", page_icon="🤖") |
|
st.title("Chat with Websites") |
|
|
|
|
|
|
|
|
|
|
|
|
|
with st.sidebar: |
|
st.header("Settings") |
|
website_url = st.text_input("Website URL") |
|
|
|
|
|
if (website_url is None or website_url == ""): |
|
st.info("Please ensure if website URL is 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_query = st.chat_input("Type your message here...") |
|
if user_query is not None and user_query !="": |
|
response = get_response(user_query) |
|
st.session_state.chat_history.append(HumanMessage(content=user_query)) |
|
st.session_state.chat_history.append(AIMessage(content=response)) |
|
|
|
|
|
|
|
for message in st.session_state.chat_history: |
|
if isinstance(message, AIMessage): |
|
with st.chat_message("AI"): |
|
st.write(message.content) |
|
elif isinstance(message, HumanMessage): |
|
with st.chat_message("Human"): |
|
st.write(message.content) |
|
|
|
|
|
|
|
|
|
|
|
|