Spaces:
Build error
Build error
Commit ·
9d25320
1
Parent(s): a741634
refined UI
Browse filesFiles changed (4)
123459 unmodified lines151617181920212223242526278 unmodified lines36373839404142433 unmodified lines474849505152535455562 unmodified lines59606162636465662 unmodified lines69707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216import streamlit as stfrom mistral7b import chatimport timeimport pandas as pdimport pinecone9 unmodified linesenvironment='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",}prompt_injection = ["""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.8 unmodified linesIs 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.""",3 unmodified lines"""]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 = ""2 unmodified linescontext+=match["metadata"]["chunk"] + "\n\n"links.append(match["metadata"]["link"])generated_prompt = f"""FOR THIS GIVEN CONTEXT {context},2 unmodified lines"""return generated_prompt, linksdata = {"Attribute": ["LLM", "Text Vectorizer", "Vector Database","CPU", "System RAM"],"Information": ["Mistral-7B-Instruct-v0.2","all-distilroberta-v1", "Hosted Pinecone" ,"2 vCPU", "16 GB"]}df = pd.DataFrame(data)st.set_page_config(page_title="Ikigai Chat",page_icon="🤖",)if "messages" not in st.session_state:st.session_state.messages = []if "tokens_used" not in st.session_state:st.session_state.tokens_used = 0if "inference_time" not in st.session_state:st.session_state.inference_time = [0.00]if "temp" not in st.session_state:st.session_state.temp = 0.8if "history" not in st.session_state:st.session_state.history = [prompt_injection]if "top_k" not in st.session_state:st.session_state.top_k = 4if "repetion_penalty" not in st.session_state :st.session_state.repetion_penalty = 1if "rag_enabled" not in st.session_state :st.session_state.rag_enabled = Trueif "chat_bot" not in st.session_state :st.session_state.chat_bot = "Mixtral 8x7B v0.1"with st.sidebar: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=10, value=4, disabled=not st.session_state.rag_enabled)st.markdown("---")st.markdown("# Model Analytics")st.write("Tokens used :", st.session_state['tokens_used'])st.write("Average Inference Time: ", round(sum(st.session_state["inference_time"]) / len(st.session_state["inference_time"]), 3), "Secs")st.write("Cost Incured :", round(0.033 * st.session_state['tokens_used'] / 1000, 3), "INR")st.markdown("---")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.)st.markdown("""> **2023 ©️ Pragnesh Barik**""")st.image("ikigai.svg")st.title("Ikigai Chat")# st.caption("Maintained and developed by Pragnesh Barik.")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)for message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])if prompt := st.chat_input("Chat with Ikigai Docs..."):st.chat_message("user").markdown(prompt)st.session_state.messages.append({"role": "user", "content": prompt})tick = time.time()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)tock = time.time()st.session_state.inference_time.append(tock - tick)formatted_links = ", ".join(links)with st.chat_message("assistant"):full_response = ""placeholder = st.empty()if st.session_state.rag_enabled :for chunk in chat_stream :if chunk.token.text!='</s>' :full_response += chunk.token.textplaceholder.markdown(full_response + "▌")placeholder.markdown(full_response)st.info( f"""\n\nFetched from :\n {formatted_links}""")else :for chunk in chat_stream :if chunk.token.text!='</s>' :full_response += chunk.token.textplaceholder.markdown(full_response + "▌")placeholder.markdown(full_response)len_response = (len(prompt.split()) + len(full_response.split())) * 1.25st.session_state["tokens_used"] = len_response + st.session_state["tokens_used"]st.session_state.history.append([prompt, full_response])st.session_state.history.append(identity_change)if st.session_state.rag_enabled :st.session_state.messages.append({"role": "assistant", "content": full_response + f"""\n\nFetched from :\n {formatted_links}"""})else :st.session_state.messages.append({"role": "assistant", "content": full_response})123459 unmodified lines15161718192021222324252627282930313233348 unmodified lines434445464748493 unmodified lines5354555657585960612 unmodified lines6465666768692 unmodified lines72737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245import streamlit as stfrom chat_client import chatimport timeimport pandas as pdimport pinecone9 unmodified linesenvironment='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.8 unmodified linesIs 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.""",3 unmodified lines"""]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 = ""2 unmodified linescontext+=match["metadata"]["chunk"] + "\n\n"links.append(match["metadata"]["link"])generated_prompt = f"""FOR THIS GIVEN CONTEXT {context},2 unmodified lines"""return generated_prompt, linksdef init_state() :if "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.8if "history" not in st.session_state:st.session_state.history = [SYSTEM_PROMPT]if "top_k" not in st.session_state:st.session_state.top_k = 5if "repetion_penalty" not in st.session_state :st.session_state.repetion_penalty = 1if "rag_enabled" not in st.session_state :st.session_state.rag_enabled = Trueif "chat_bot" not in st.session_state :st.session_state.chat_bot = "Mixtral 8x7B v0.1"def 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=10, 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("""> **2023 ©️ [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:with st.chat_message(message["role"]):st.markdown(message["content"])def feedback_buttons() :is_visible = Truedef click_handler() :is_visible = Falseif 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) :start_time = time.time()full_response = ''for chunk in chat_stream :if chunk.token.text!='</s>' :full_response += chunk.token.textplaceholder.markdown(full_response + "▌")placeholder.markdown(full_response)end_time = time.time()elapsed_time = end_time - start_timetotal_tokens_processed = len(full_response.split())tokens_per_second = total_tokens_processed // elapsed_timelen_response = (len(prompt.split()) + len(full_response.split())) * 1.25col1, 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}")init_state()sidebar()header()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})chat_stream, links = generate_chat_stream(prompt)with st.chat_message("assistant"):placeholder = st.empty()full_response = stream_handler(chat_stream, placeholder)if st.session_state.rag_enabled :show_source(links)st.session_state.history.append([prompt, full_response])st.session_state.messages.append({"role": "assistant", "content": full_response})
__pycache__/chat_client.cpython-39.pycAdded
__pycache__/mistral7b.cpython-39.pycAdded
app.py+156-127
mistral7b.py → chat_client.pyRenamed