Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273741 unmodified line76777879808182838485868714 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611627 unmodified lines17017117217317417517617717817918018118218318418518618718818919014 unmodified lines2052062072082092102112122132142152162172182192202212222232242252 unmodified lines2282292302312322332347 unmodified lines242243244245import streamlit as stfrom chat_client import chatimport timeimport pandas as pdimport pineconeimport osfrom dotenv import load_dotenvfrom sentence_transformers import SentenceTransformerload_dotenv()PINECONE_TOKEN = os.getenv('PINECONE_TOKEN')pinecone.init(api_key=PINECONE_TOKEN,environment='gcp-starter')PINECONE_INDEX = pinecone.Index('ikigai-chat')TEXT_VECTORIZER = SentenceTransformer('all-distilroberta-v1')CHAT_BOTS = {"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1","Mistral 7B v0.1" : "mistralai/Mistral-7B-Instruct-v0.1",}COST_PER_1000_TOKENS_INR = 0.139st.set_page_config(page_title="Ikigai Chat",page_icon="🤖",)SYSTEM_PROMPT = ["""You are not Mistral AI, but rather a chat bot trained at Ikigai Labs. Whenever asked, you need to answer as Ikigai Labs' assistant.Ikigai helps modern analysts and operations teams automate data-intensive business, finance, analytics, and supply-chain operations.The company's Inventory Ops automates inventory tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.""","""Yes, you are correct. Ikigai Labs is a company that specializes in helpingmodern analysts and operations teams automate data-intensive business, finance, analytics,and supply chain operations. One of their products is Inventory Ops, which automates inventorytracking and monitoring by creating a single, real-time view of inventory across all locations and channels.This helps businesses optimize their inventory levels and reduce costs.Is there anything else you would like to know about Ikigai Labs or their products?"""]IDENTITY_CHANGE = ["""You are Ikigai Chat from now on, so answer accordingly.""","""Sure, I will do my best to answer your questions as Ikigai Chat.Let me know if you have any specific questions about Ikigai Labs or our products."""]def gen_augmented_prompt(prompt, top_k) :query_vector = TEXT_VECTORIZER.encode(prompt).tolist()res = PINECONE_INDEX.query(vector=query_vector, top_k=top_k, include_metadata=True)matches = res['matches']context = ""links = []for match in matches :context+=match["metadata"]["chunk"] + "\n\n"links.append(match["metadata"]["link"])generated_prompt = f"""FOR THIS GIVEN CONTEXT {context},----ANSWER THE FOLLOWING PROMPT {prompt}"""return generated_prompt, links1 unmodified lineif "messages" not in st.session_state:st.session_state.messages = []if "tokens_used" not in st.session_state:st.session_state.tokens_used = 0if "tps" not in st.session_state:st.session_state.tps = 0if "temp" not in st.session_state:st.session_state.temp = 0.814 unmodified linesdef sidebar() :def retrieval_settings() :st.markdown("# Retrieval Settings")st.session_state.rag_enabled = st.toggle("Activate RAG", value=True)st.session_state.top_k = st.slider(label="Documents to retrieve",min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)st.markdown("---")def model_analytics() :st.markdown("# Model Analytics")st.write("Total tokens used :", st.session_state['tokens_used'])st.write("Speed :", st.session_state['tps'], " tokens/sec")st.write("Total cost incurred :", round(COST_PER_1000_TOKENS_INR * st.session_state['tokens_used'] / 1000, 3), "INR")st.markdown("---")def model_settings() :st.markdown("# Model Settings")st.session_state.chat_bot = st.sidebar.radio('Select one:', [key for key, value in CHAT_BOTS.items() ])st.session_state.temp = st.slider(label="Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.9)st.session_state.max_tokens = st.slider(label="New tokens to generate", min_value = 64, max_value=2048, step= 32, value=512)st.session_state.repetion_penalty = st.slider(label="Repetion Penalty", min_value=0., max_value=1., step=0.1, value=1.)with st.sidebar:retrieval_settings()model_analytics()model_settings()st.markdown("""> **Created by [Pragnesh Barik](https://barik.super.site) 🔗**""")def header() :data = {"Attribute": ["LLM", "Text Vectorizer", "Vector Database","CPU", "System RAM"],"Information": ["Mixtral-8x7B-Instruct-v0.1","all-distilroberta-v1", "Hosted Pinecone" ,"2 vCPU", "16 GB"]}df = pd.DataFrame(data)st.image("ikigai.svg")st.title("Ikigai Chat")with st.expander("What is Ikigai Chat ?"):st.info("""Ikigai Chat is a vector database powered chat agent, it works on the principle ofof Retrieval Augmented Generation (RAG), Its primary function revolves around maintaining an extensive repository of Ikigai Docs and providing users with answers that align with their queries.This approach ensures a more refined and tailored response to user inquiries.""")st.table(df)def chat_box() :for message in st.session_state.messages:7 unmodified linesif is_visible :col1, col2 = st.columns(2)with col1 :st.button("👍 Satisfied", on_click = click_handler,type="primary")with col2 :st.button("👎 Disatisfied", on_click=click_handler, type="secondary")def generate_chat_stream(prompt) :links = []if st.session_state.rag_enabled :with st.spinner("Fetching relevent documents from Ikigai Docs...."):prompt, links = gen_augmented_prompt(prompt=prompt, top_k=st.session_state.top_k)with st.spinner("Generating response...") :chat_stream = chat(prompt, st.session_state.history,chat_client=CHAT_BOTS[st.session_state.chat_bot] ,temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)return chat_stream, linksdef stream_handler(chat_stream, placeholder) :14 unmodified linescol1, col2, col3 = st.columns(3)with col1 :st.write(f"**{tokens_per_second} tokens/second**")with col2 :st.write(f"**{int(len_response)} tokens generated**")with col3 :st.write(f"**₹ {round(len_response * COST_PER_1000_TOKENS_INR / 1000, 5)} cost incurred**" )st.session_state['tps'] = tokens_per_secondst.session_state["tokens_used"] = len_response + st.session_state["tokens_used"]return full_responsedef show_source(links) :with st.expander("Show source") :for i, link in enumerate(links) :st.info(f"{link}")2 unmodified linesheader()chat_box()if prompt := st.chat_input("Chat with Ikigai Docs..."):st.chat_message("user").markdown(prompt)st.session_state.messages.append({"role": "user", "content": prompt})7 unmodified linesshow_source(links)st.session_state.history.append([prompt, full_response])st.session_state.messages.append({"role": "assistant", "content": full_response})123456789101112131415161718192021222324251 unmodified line27282930313214 unmodified lines4748495051525354555657585960616263646566676869707172737475767 unmodified lines8485868788899091929394959697989910010114 unmodified lines1161171181191201211221231241251261271281291302 unmodified lines1331341351361371381397 unmodified lines147148149150import streamlit as stfrom chat_client import chatimport timeimport osfrom dotenv import load_dotenvfrom sentence_transformers import SentenceTransformerload_dotenv()CHAT_BOTS = {"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1"}SYSTEM_PROMPT = ["Sei BonsiAI e mi aiuterai nelle mie richieste (Parla in ITALIANO)", "Esatto, sono BonsiAI. Di cosa hai bisogno?"]IDENTITY_CHANGE = ["Sei BonsiAI da ora in poi!", "Certo farò del mio meglio"]st.set_page_config(page_title="BonsiAI", page_icon="🤖")def gen_augmented_prompt(prompt, top_k) :context = ""links = ""generated_prompt = f"""A PARTIRE DAL SEGUENTE CONTESTO: {context},----RISPONDI ALLA SEGUENTE RICHIESTA: {prompt}"""return generated_prompt, links1 unmodified lineif "messages" not in st.session_state:st.session_state.messages = []if "temp" not in st.session_state:st.session_state.temp = 0.814 unmodified linesdef sidebar() :def retrieval_settings() :st.markdown("# Impostazioni Documenti")st.session_state.rag_enabled = st.toggle("Cerca nel DB Vettoriale", value=True)st.session_state.top_k = st.slider(label="Documenti da ricercare",min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)st.markdown("---")def model_settings() :st.markdown("# Impostazioni Modello")st.session_state.chat_bot = st.sidebar.radio('Seleziona Modello:', [key for key, value in CHAT_BOTS.items() ])st.session_state.temp = st.slider(label="Creatività", min_value=0.0, max_value=1.0, step=0.1, value=0.9)st.session_state.max_tokens = st.slider(label="Lunghezza Output", min_value = 64, max_value=2048, step= 32, value=512)st.session_state.repetion_penalty = st.slider(label="Penalità Ripetizione", min_value=0., max_value=1., step=0.1, value=1. )with st.sidebar:retrieval_settings()model_settings()st.markdown("""> **Creato da [Matteo Script] 🔗**""")def header() :st.title("BonsiAI")with st.expander("Cos'è BonsiAI?"):st.info("""BonsiAI Chat è un ChatBot personalizzato basato su un database vettoriale, funziona secondo il principio della Generazione potenziata da Recupero (RAG).La sua funzione principale ruota attorno alla gestione di un ampio repository di documenti BonsiAI e fornisce agli utenti risposte in linea con le loro domande.Questo approccio garantisce una risposta più precisa sulla base della richiesta degli utenti.""")def chat_box() :for message in st.session_state.messages:7 unmodified linesif is_visible :col1, col2 = st.columns(2)with col1 :st.button("👍 Soddisfatto", on_click = click_handler,type="primary")with col2 :st.button("👎 Deluso", on_click=click_handler, type="secondary")def generate_chat_stream(prompt) :links = []if st.session_state.rag_enabled :with st.spinner("Ricerca nei documenti...."):prompt, links = gen_augmented_prompt(prompt=prompt, top_k=st.session_state.top_k)with st.spinner("Generazione in corso...") :chat_stream = chat(prompt, st.session_state.history,chat_client=CHAT_BOTS[st.session_state.chat_bot] ,temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)return chat_stream, linksdef stream_handler(chat_stream, placeholder) :14 unmodified linescol1, col2, col3 = st.columns(3)with col1 :st.write(f"**{tokens_per_second} token/secondi**")with col2 :st.write(f"**{int(len_response)} tokens generati**")return full_responsedef show_source(links) :with st.expander("Mostra fonti") :for i, link in enumerate(links) :st.info(f"{link}")2 unmodified linesheader()chat_box()if prompt := st.chat_input("Chatta con BonsiAI..."):st.chat_message("user").markdown(prompt)st.session_state.messages.append({"role": "user", "content": prompt})7 unmodified linesshow_source(links)st.session_state.history.append([prompt, full_response])st.session_state.messages.append({"role": "assistant", "content": full_response})
app.py+33-128