Spaces:
Sleeping
Sleeping
File size: 6,772 Bytes
5426d51 |
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 |
import streamlit as st
#Import library
import yaml
#load config.yml and parse into variables
with open("config.yml", "r") as ymlfile:
cfg = yaml.safe_load(ymlfile)
_BARD_API_KEY = cfg["API_KEY"]["Bard"]
main_path = cfg["LOCAL_PATH"]["main_path"]
chat_context_length = cfg["CHAT"]["chat_context_length"]
model_name = cfg["EMBEDDINGS"]["HuggingFaceEmbeddings"]["model_name"]
model_kwargs = cfg["EMBEDDINGS"]["HuggingFaceEmbeddings"]["model_kwargs"]
chunk_size = cfg["CHUNK"]["chunk_size"]
chunk_overlap = cfg["CHUNK"]["chunk_overlap"]
import os
from langchain.vectorstores import Chroma
import streamlit.components.v1 as components
import streamlit as st
import sys
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
# Bard
from bardapi import Bard
from typing import Any, List, Mapping, Optional
from getpass import getpass
import os
from langchain.llms.base import LLM
from langchain.callbacks.manager import CallbackManagerForLLMRun
from streamlit_feedback import streamlit_feedback
#define Bard
class BardLLM(LLM):
@property
def _llm_type(self) -> str:
return "custom"
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
) -> str:
response = Bard(token=_BARD_API_KEY).get_answer(prompt)['content']
return response
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {}
def load_embeddings():
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)
chroma_index = Chroma(persist_directory="./chroma_index_1", embedding_function=embeddings)
print("Successfully loading embeddings and indexing")
return chroma_index
def ask_with_memory(vector_store, question, chat_history=[], document_description=""):
llm=BardLLM()
retriever = vector_store.as_retriever( # now the vs can return documents
search_type='similarity', search_kwargs={'k': 3})
general_system_template = f"""
Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to
make up an answer.
Imagine you're talking to a friend and use natural language and phrasing.
You can only use Vietnamese do not use other languages.
Suggest using out searching function for more information.
----
CONTEXT: {{context}}
----
"""
general_user_template = """Here is the next question, remember to only answer if you can from the provided context.
If the question is not relevant to real estate , just answer that you do not know, do not create your own answer.
Do not recommend or propose any infomation of the properties.
Be sure to respond in a complete sentence, being comprehensive, including all information in the provided context.
Imagine you're talking to a friend and use natural language and phrasing.
Only respond in Vietnamese.
QUESTION:```{question}```"""
messages = [
SystemMessagePromptTemplate.from_template(general_system_template),
HumanMessagePromptTemplate.from_template(general_user_template)
]
qa_prompt = ChatPromptTemplate.from_messages( messages )
crc = ConversationalRetrievalChain.from_llm(llm, retriever, combine_docs_chain_kwargs={'prompt': qa_prompt})
result = crc({'question': question, 'chat_history': chat_history})
return result
def clear_history():
if "history" in st.session_state:
st.session_state.history = []
st.session_state.messages = []
# Define a function for submitting feedback
def _submit_feedback(user_response, emoji=None):
st.toast(f"Feedback submitted: {user_response}", icon=emoji)
return user_response.update({"some metadata": 123})
def format_chat_history(chat_history):
formatted_history = ""
for entry in chat_history:
question, answer = entry
# Added an extra '\n' for the blank line
formatted_history += f"Question: {question}\nAnswer: {answer}\n\n"
return formatted_history
def run_chatbot_2():
with st.sidebar.title("Sidebar"):
if st.button("Clear History"):
clear_history()
st.title("🤖 Real Estate chatbot")
# Initialize the chatbot and load embeddings
if "messages" not in st.session_state:
with st.spinner("Initializing, please wait a moment!!!"):
st.session_state.vector_store = load_embeddings()
st.success("Finish!!!")
st.session_state["messages"] = [{"role": "assistant", "content": "Tôi có thể giúp gì được cho bạn?"}]
messages = st.session_state.messages
feedback_kwargs = {
"feedback_type": "thumbs",
"optional_text_label": "Please provide extra information",
"on_submit": _submit_feedback,
}
for n, msg in enumerate(messages):
st.chat_message(msg["role"]).write(msg["content"])
if msg["role"] == "assistant" and n > 1:
feedback_key = f"feedback_{int(n/2)}"
if feedback_key not in st.session_state:
st.session_state[feedback_key] = None
streamlit_feedback(
**feedback_kwargs,
key=feedback_key,
)
chat_history_placeholder = st.empty()
if "history" not in st.session_state:
st.session_state.history = []
if prompt := st.chat_input():
if "vector_store" in st.session_state:
vector_store = st.session_state["vector_store"]
q = prompt
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = ask_with_memory(vector_store, q, st.session_state.history)
if len(st.session_state.history) >= chat_context_length:
st.session_state.history = st.session_state.history[1:]
st.session_state.history.append((q, response['answer']))
chat_history_str = format_chat_history(st.session_state.history)
msg = {"role": "assistant", "content": response['answer']}
st.session_state.messages.append(msg)
st.chat_message("assistant").write(msg["content"])
# Display the feedback component after the chatbot responds
feedback_key = f"feedback_{len(st.session_state.messages) - 1}"
streamlit_feedback(
**feedback_kwargs,
key=feedback_key,
) |