|
import os |
|
import string |
|
from typing import Any, Dict, List, Tuple, Union |
|
|
|
import chromadb |
|
import numpy as np |
|
import openai |
|
import pandas as pd |
|
import requests |
|
import streamlit as st |
|
from datasets import load_dataset |
|
from langchain.document_loaders import TextLoader |
|
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.vectorstores import Chroma |
|
from scipy.spatial.distance import cosine |
|
|
|
from utils.helper_functions import * |
|
|
|
openai.api_key = os.environ["OPENAI_API_KEY"] |
|
|
|
|
|
|
|
st.title("YSA|Larkin Chatbot") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
st.sidebar.markdown( |
|
""" |
|
|
|
### Instructions: |
|
|
|
This app guides you through YSA's website, utilizing a RAG-ready Q&A dataset [here](https://huggingface.co/datasets/eagle0504/youthless-homeless-shelter-web-scrape-dataset-qa-formatted) for chatbot assistance. 🤖 Enter a question, and it finds similar ones in the database, offering answers with a distance score to gauge relevance—the lower the score, the closer the match. 🎯 For better accuracy and to reduce errors, user feedback helps refine the database. ✨ |
|
|
|
""" |
|
) |
|
st.sidebar.success( |
|
"Please enter a distance threshold (we advise to set it around 0.2)." |
|
) |
|
st.sidebar.warning("Select a website first!") |
|
option = st.sidebar.selectbox("Which website do you want to ask?", ("YSA", "Larkin")) |
|
special_threshold = st.sidebar.number_input( |
|
"Insert a number", value=0.2, placeholder="Type a number..." |
|
) |
|
clear_button = st.sidebar.button("Clear Conversation", key="clear") |
|
st.sidebar.warning( |
|
"The 'distances' measures how close your question is to the questions in our database (lower the score the better). The 'ai_judge' measures independent similarity ranking of database answers and user's question (the higher the better)." |
|
) |
|
|
|
if clear_button: |
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
if option == "YSA": |
|
dataset = load_dataset( |
|
"eagle0504/youthless-homeless-shelter-web-scrape-dataset-qa-formatted" |
|
) |
|
initial_input = "Tell me about YSA" |
|
else: |
|
dataset = load_dataset("eagle0504/larkin-web-scrape-dataset-qa-formatted") |
|
initial_input = "Tell me about Larkin" |
|
|
|
|
|
|
|
client = chromadb.Client() |
|
|
|
|
|
random_number: int = np.random.randint(low=1e9, high=1e10) |
|
|
|
|
|
random_string: str = "".join( |
|
np.random.choice(list(string.ascii_uppercase + string.digits), size=10) |
|
) |
|
|
|
|
|
combined_string: str = f"{random_number}{random_string}" |
|
|
|
|
|
collection = client.create_collection(combined_string) |
|
|
|
|
|
|
|
with st.spinner("Loading, please be patient with us ... 🙏"): |
|
L = len(dataset["train"]["questions"]) |
|
collection.add( |
|
ids=[str(i) for i in range(0, L)], |
|
documents=dataset["train"]["questions"], |
|
metadatas=[{"type": "support"} for _ in range(0, L)], |
|
) |
|
|
|
|
|
|
|
if prompt := st.chat_input(initial_input): |
|
with st.spinner("Loading, please be patient with us ... 🙏"): |
|
|
|
st.chat_message("user").markdown(prompt) |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
question = prompt |
|
results = collection.query(query_texts=question, n_results=5) |
|
idx = results["ids"][0] |
|
idx = [int(i) for i in idx] |
|
ref = pd.DataFrame( |
|
{ |
|
"idx": idx, |
|
"questions": [dataset["train"]["questions"][i] for i in idx], |
|
"answers": [dataset["train"]["answers"][i] for i in idx], |
|
"distances": results["distances"][0], |
|
} |
|
) |
|
|
|
filtered_ref = ref[ref["distances"] < special_threshold] |
|
if filtered_ref.shape[0] > 0: |
|
st.success("There are highly relevant information in our database.") |
|
ref_from_db_search = filtered_ref["answers"].str.cat(sep=" ") |
|
final_ref = filtered_ref |
|
else: |
|
st.warning( |
|
"The database may not have relevant information to help your question so please be aware of hallucinations." |
|
) |
|
ref_from_db_search = ref["answers"].str.cat(sep=" ") |
|
final_ref = ref |
|
|
|
if option == "YSA": |
|
try: |
|
llm_response = llama2_7b_ysa(question) |
|
except: |
|
st.warning("Sorry, the inference endpoint is temporarily down. 😔") |
|
llm_response = "NA." |
|
else: |
|
st.warning( |
|
"Apologies! We are in the progress of fine-tune the model, so it's currently unavailable. ⚙️" |
|
) |
|
llm_response = "NA" |
|
|
|
finetuned_llm_guess = ["from_llm", question, llm_response, 0] |
|
final_ref.loc[-1] = finetuned_llm_guess |
|
final_ref.index = np.arange(len(final_ref)) |
|
|
|
|
|
independent_ai_judge_score = [] |
|
for i in range(final_ref.shape[0]): |
|
this_content = final_ref["answers"][i] |
|
st.warning(this_content) |
|
if len(this_content) > 3: |
|
this_score = calculate_sts_openai_score(question, this_content) |
|
else: |
|
this_score = 0 |
|
independent_ai_judge_score.append(this_score) |
|
|
|
final_ref["ai_judge"] = independent_ai_judge_score |
|
|
|
engineered_prompt = f""" |
|
Based on the context: {ref_from_db_search} |
|
|
|
answer the user question: {question} |
|
|
|
Answer the question directly (don't say "based on the context, ...") |
|
""" |
|
|
|
answer = call_chatgpt(engineered_prompt) |
|
response = answer |
|
|
|
|
|
with st.chat_message("assistant"): |
|
with st.spinner("Wait for it..."): |
|
st.markdown(response) |
|
with st.expander("See reference:"): |
|
st.table(final_ref) |
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|