File size: 7,762 Bytes
4c8c217 77432f1 52b8278 74d32cc 0ff2378 7d9fec0 77432f1 0ff2378 7d9fec0 98f8b95 49b8bd6 8651ca5 521f252 91f1b8a b594097 c3d6f40 9c60047 19b4752 48e3505 7d9fec0 1dd3a02 7d9fec0 1dd3a02 b594097 986e2b7 9d2bf07 7d9fec0 986e2b7 0ff2378 7d9fec0 0ff2378 c790556 7d9fec0 c790556 0ff2378 1dd3a02 e1b701a 0ff2378 e1b701a 0ff2378 e1b701a 48e3505 e1b701a ba7b4e3 e1b701a 0ff2378 ba7b4e3 0ff2378 ba7b4e3 0ff2378 e1b701a 19b4752 e1b701a 0ff2378 8a6e9d6 a9a1953 9d2bf07 49b8bd6 b5a3ee0 0abcd0d 19b4752 2da7358 ff23be6 c790556 0ff2378 885ec28 b1d9a7f 885ec28 0ff2378 4c8c217 1dd3a02 b0d0303 4c8c217 81a4020 6b80b65 81a4020 1572555 52b8278 0ff2378 c306f1c 986e2b7 52b8278 81a4020 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
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_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
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
# from langchain_retrieval import BaseRetrieverChain
# from dotenv import load_dotenv
# load_dotenv()
def get_vector_store_from_url(url):
# model_name = "BAAI/bge-large-en"
# model_kwargs = {'device': 'cpu'}
# encode_kwargs = {'normalize_embeddings': False}
# embeddings = HuggingFaceBgeEmbeddings(
# model_name=model_name,
# model_kwargs=model_kwargs,
# encode_kwargs=encode_kwargs
# )
embeddings = HuggingFaceEmbeddings(model_name='thenlper/gte-large',
model_kwargs={'device': 'cpu'})
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())
vector_store = Chroma.from_documents(document_chunks, embeddings)
return vector_store
def get_context_retriever_chain(vector_store,llm):
# llm = ChatOpenAI()
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):
# llm=llm
# 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),
# ])
# stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
# return create_retrieval_chain(retriever_chain, stuff_documents_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)
# input_str = input_str.to_messages()
# Call the original llm, which should now work correctly
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 = CTransformers(
# # model = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF",
# model= "TheBloke/Llama-2-7B-Chat-GGUF",
# model_file = "llama-2-7b-chat.Q3_K_S.gguf",
# model_type="llama",
# max_new_tokens = 300,
# temperature = 0.3,
# lib="avx2", # for CPU
# )
# model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
# # llm = HuggingFaceHub(
# # repo_id=llm_model,
# # model_kwargs={"temperature": 0.3, "max_new_tokens": 250, "top_k": 3}
# # )
# llm = transformers.AutoModelForCausalLM.from_pretrained(
# model_name,
# trust_remote_code=True,
# torch_dtype=torch.bfloat16,
# device_map='auto'
# )
# llm = HuggingFacePipeline.from_model_id(
# model_id="google/flan-t5-base",
# task="text2text-generation",
# # model_kwargs={"temperature": 0.2},
# )
# llm = HuggingFacePipeline.from_model_id(
# model_id="google-t5/t5-small",
# task="text2text-generation",
# # model_kwargs={"temperature": 0.2},
# )
# llm = pipeline(task="conversational", model="facebook/blenderbot-400M-distill")
llm = LlamaCpp(
model_path="TheBloke/OpenOrca-Platypus2-13B-GGUF",
temperature=0.75,
max_tokens=2000,
top_p=1,
# callback_manager=callback_manager,
verbose=True, # Verbose is required to pass to the callback manager
)
# llm = HuggingFacePipeline.from_model_id(
# model_id="lmsys/fastchat-t5-3b-v1.0",
# task="text2text-generation",
# # model_kwargs={"temperature": 0.2},
# )
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']
# 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 == ""):
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_input
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))
#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)
|