Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from chat_client import chat
|
| 3 |
import time
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import pinecone
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
api_key=PINECONE_TOKEN,
|
| 15 |
-
environment='gcp-starter'
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
PINECONE_INDEX = pinecone.Index('ikigai-chat')
|
| 19 |
-
TEXT_VECTORIZER = SentenceTransformer('all-distilroberta-v1')
|
| 20 |
-
CHAT_BOTS = {
|
| 21 |
-
"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 22 |
-
"Mistral 7B v0.1" : "mistralai/Mistral-7B-Instruct-v0.1",
|
| 23 |
-
}
|
| 24 |
-
COST_PER_1000_TOKENS_INR = 0.139
|
| 25 |
-
|
| 26 |
-
st.set_page_config(
|
| 27 |
-
page_title="Ikigai Chat",
|
| 28 |
-
page_icon="🤖",
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
SYSTEM_PROMPT = [
|
| 32 |
-
"""
|
| 33 |
-
You are not Mistral AI, but rather a chat bot trained at Ikigai Labs. Whenever asked, you need to answer as Ikigai Labs' assistant.
|
| 34 |
-
Ikigai helps modern analysts and operations teams automate data-intensive business, finance, analytics, and supply-chain operations.
|
| 35 |
-
The company's Inventory Ops automates inventory tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.
|
| 36 |
-
""",
|
| 37 |
-
"""
|
| 38 |
-
Yes, you are correct. Ikigai Labs is a company that specializes in helping
|
| 39 |
-
modern analysts and operations teams automate data-intensive business, finance, analytics,
|
| 40 |
-
and supply chain operations. One of their products is Inventory Ops, which automates inventory
|
| 41 |
-
tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.
|
| 42 |
-
This helps businesses optimize their inventory levels and reduce costs.
|
| 43 |
-
Is there anything else you would like to know about Ikigai Labs or their products?
|
| 44 |
-
"""
|
| 45 |
-
]
|
| 46 |
-
IDENTITY_CHANGE = [
|
| 47 |
-
"""
|
| 48 |
-
You are Ikigai Chat from now on, so answer accordingly.
|
| 49 |
-
""",
|
| 50 |
-
"""
|
| 51 |
-
Sure, I will do my best to answer your questions as Ikigai Chat.
|
| 52 |
-
Let me know if you have any specific questions about Ikigai Labs or our products.
|
| 53 |
-
"""
|
| 54 |
-
]
|
| 55 |
|
| 56 |
def gen_augmented_prompt(prompt, top_k) :
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
matches = res['matches']
|
| 60 |
-
|
| 61 |
-
context = ""
|
| 62 |
-
links = []
|
| 63 |
-
for match in matches :
|
| 64 |
-
context+=match["metadata"]["chunk"] + "\n\n"
|
| 65 |
-
links.append(match["metadata"]["link"])
|
| 66 |
-
|
| 67 |
generated_prompt = f"""
|
| 68 |
-
|
| 69 |
|
| 70 |
----
|
| 71 |
-
|
| 72 |
"""
|
| 73 |
return generated_prompt, links
|
| 74 |
|
|
@@ -76,12 +27,6 @@ def init_state() :
|
|
| 76 |
if "messages" not in st.session_state:
|
| 77 |
st.session_state.messages = []
|
| 78 |
|
| 79 |
-
if "tokens_used" not in st.session_state:
|
| 80 |
-
st.session_state.tokens_used = 0
|
| 81 |
-
|
| 82 |
-
if "tps" not in st.session_state:
|
| 83 |
-
st.session_state.tps = 0
|
| 84 |
-
|
| 85 |
if "temp" not in st.session_state:
|
| 86 |
st.session_state.temp = 0.8
|
| 87 |
|
|
@@ -102,61 +47,30 @@ def init_state() :
|
|
| 102 |
|
| 103 |
def sidebar() :
|
| 104 |
def retrieval_settings() :
|
| 105 |
-
st.markdown("#
|
| 106 |
-
st.session_state.rag_enabled = st.toggle("
|
| 107 |
-
st.session_state.top_k = st.slider(label="
|
| 108 |
min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)
|
| 109 |
st.markdown("---")
|
| 110 |
|
| 111 |
-
def model_analytics() :
|
| 112 |
-
st.markdown("# Model Analytics")
|
| 113 |
-
|
| 114 |
-
st.write("Total tokens used :", st.session_state['tokens_used'])
|
| 115 |
-
st.write("Speed :", st.session_state['tps'], " tokens/sec")
|
| 116 |
-
st.write("Total cost incurred :", round(
|
| 117 |
-
COST_PER_1000_TOKENS_INR * st.session_state['tokens_used'] / 1000, 3), "INR")
|
| 118 |
-
|
| 119 |
-
st.markdown("---")
|
| 120 |
-
|
| 121 |
def model_settings() :
|
| 122 |
-
st.markdown("#
|
| 123 |
-
|
| 124 |
-
st.session_state.
|
| 125 |
-
|
| 126 |
-
st.session_state.
|
| 127 |
-
label="Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.9)
|
| 128 |
-
|
| 129 |
-
st.session_state.max_tokens = st.slider(
|
| 130 |
-
label="New tokens to generate", min_value = 64, max_value=2048, step= 32, value=512
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
st.session_state.repetion_penalty = st.slider(
|
| 134 |
-
label="Repetion Penalty", min_value=0., max_value=1., step=0.1, value=1.
|
| 135 |
-
)
|
| 136 |
|
| 137 |
with st.sidebar:
|
| 138 |
retrieval_settings()
|
| 139 |
-
model_analytics()
|
| 140 |
model_settings()
|
| 141 |
-
|
| 142 |
-
st.markdown("""
|
| 143 |
-
> **Created by [Pragnesh Barik](https://barik.super.site) 🔗**
|
| 144 |
-
""")
|
| 145 |
|
| 146 |
def header() :
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
st.image("ikigai.svg")
|
| 153 |
-
st.title("Ikigai Chat")
|
| 154 |
-
with st.expander("What is Ikigai Chat ?"):
|
| 155 |
-
st.info("""Ikigai Chat is a vector database powered chat agent, it works on the principle of
|
| 156 |
-
of 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.
|
| 157 |
-
This approach ensures a more refined and tailored response to user inquiries.""")
|
| 158 |
-
|
| 159 |
-
st.table(df)
|
| 160 |
|
| 161 |
def chat_box() :
|
| 162 |
for message in st.session_state.messages:
|
|
@@ -170,21 +84,18 @@ def feedback_buttons() :
|
|
| 170 |
if is_visible :
|
| 171 |
col1, col2 = st.columns(2)
|
| 172 |
with col1 :
|
| 173 |
-
st.button("👍
|
| 174 |
-
|
| 175 |
with col2 :
|
| 176 |
-
st.button("👎
|
| 177 |
|
| 178 |
def generate_chat_stream(prompt) :
|
| 179 |
links = []
|
| 180 |
if st.session_state.rag_enabled :
|
| 181 |
-
with st.spinner("
|
| 182 |
-
prompt, links = gen_augmented_prompt(prompt=prompt, top_k=st.session_state.top_k)
|
| 183 |
-
|
| 184 |
-
with st.spinner("Generating response...") :
|
| 185 |
chat_stream = chat(prompt, st.session_state.history,chat_client=CHAT_BOTS[st.session_state.chat_bot] ,
|
| 186 |
-
temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)
|
| 187 |
-
|
| 188 |
return chat_stream, links
|
| 189 |
|
| 190 |
def stream_handler(chat_stream, placeholder) :
|
|
@@ -205,21 +116,15 @@ def stream_handler(chat_stream, placeholder) :
|
|
| 205 |
col1, col2, col3 = st.columns(3)
|
| 206 |
|
| 207 |
with col1 :
|
| 208 |
-
st.write(f"**{tokens_per_second}
|
| 209 |
|
| 210 |
with col2 :
|
| 211 |
-
st.write(f"**{int(len_response)} tokens
|
| 212 |
-
|
| 213 |
-
with col3 :
|
| 214 |
-
st.write(f"**₹ {round(len_response * COST_PER_1000_TOKENS_INR / 1000, 5)} cost incurred**" )
|
| 215 |
|
| 216 |
-
st.session_state['tps'] = tokens_per_second
|
| 217 |
-
st.session_state["tokens_used"] = len_response + st.session_state["tokens_used"]
|
| 218 |
-
|
| 219 |
return full_response
|
| 220 |
|
| 221 |
def show_source(links) :
|
| 222 |
-
with st.expander("
|
| 223 |
for i, link in enumerate(links) :
|
| 224 |
st.info(f"{link}")
|
| 225 |
|
|
@@ -228,7 +133,7 @@ sidebar()
|
|
| 228 |
header()
|
| 229 |
chat_box()
|
| 230 |
|
| 231 |
-
if prompt := st.chat_input("
|
| 232 |
st.chat_message("user").markdown(prompt)
|
| 233 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 234 |
|
|
@@ -242,4 +147,4 @@ if prompt := st.chat_input("Chat with Ikigai Docs..."):
|
|
| 242 |
show_source(links)
|
| 243 |
|
| 244 |
st.session_state.history.append([prompt, full_response])
|
| 245 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from chat_client import chat
|
| 3 |
import time
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
CHAT_BOTS = {"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1"}
|
| 10 |
+
SYSTEM_PROMPT = ["Sei BonsiAI e mi aiuterai nelle mie richieste (Parla in ITALIANO)", "Esatto, sono BonsiAI. Di cosa hai bisogno?"]
|
| 11 |
+
IDENTITY_CHANGE = ["Sei BonsiAI da ora in poi!", "Certo farò del mio meglio"]
|
| 12 |
|
| 13 |
+
st.set_page_config(page_title="BonsiAI", page_icon="🤖")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def gen_augmented_prompt(prompt, top_k) :
|
| 16 |
+
context = ""
|
| 17 |
+
links = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
generated_prompt = f"""
|
| 19 |
+
A PARTIRE DAL SEGUENTE CONTESTO: {context},
|
| 20 |
|
| 21 |
----
|
| 22 |
+
RISPONDI ALLA SEGUENTE RICHIESTA: {prompt}
|
| 23 |
"""
|
| 24 |
return generated_prompt, links
|
| 25 |
|
|
|
|
| 27 |
if "messages" not in st.session_state:
|
| 28 |
st.session_state.messages = []
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if "temp" not in st.session_state:
|
| 31 |
st.session_state.temp = 0.8
|
| 32 |
|
|
|
|
| 47 |
|
| 48 |
def sidebar() :
|
| 49 |
def retrieval_settings() :
|
| 50 |
+
st.markdown("# Impostazioni Documenti")
|
| 51 |
+
st.session_state.rag_enabled = st.toggle("Cerca nel DB Vettoriale", value=True)
|
| 52 |
+
st.session_state.top_k = st.slider(label="Documenti da ricercare",
|
| 53 |
min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)
|
| 54 |
st.markdown("---")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def model_settings() :
|
| 57 |
+
st.markdown("# Impostazioni Modello")
|
| 58 |
+
st.session_state.chat_bot = st.sidebar.radio('Seleziona Modello:', [key for key, value in CHAT_BOTS.items() ])
|
| 59 |
+
st.session_state.temp = st.slider(label="Creatività", min_value=0.0, max_value=1.0, step=0.1, value=0.9)
|
| 60 |
+
st.session_state.max_tokens = st.slider(label="Lunghezza Output", min_value = 64, max_value=2048, step= 32, value=512)
|
| 61 |
+
st.session_state.repetion_penalty = st.slider(label="Penalità Ripetizione", min_value=0., max_value=1., step=0.1, value=1. )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with st.sidebar:
|
| 64 |
retrieval_settings()
|
|
|
|
| 65 |
model_settings()
|
| 66 |
+
st.markdown("""> **Creato da [Matteo Script] 🔗**""")
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def header() :
|
| 69 |
+
st.title("BonsiAI")
|
| 70 |
+
with st.expander("Cos'è BonsiAI?"):
|
| 71 |
+
st.info("""BonsiAI Chat è un ChatBot personalizzato basato su un database vettoriale, funziona secondo il principio della Generazione potenziata da Recupero (RAG).
|
| 72 |
+
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.
|
| 73 |
+
Questo approccio garantisce una risposta più precisa sulla base della richiesta degli utenti.""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def chat_box() :
|
| 76 |
for message in st.session_state.messages:
|
|
|
|
| 84 |
if is_visible :
|
| 85 |
col1, col2 = st.columns(2)
|
| 86 |
with col1 :
|
| 87 |
+
st.button("👍 Soddisfatto", on_click = click_handler,type="primary")
|
|
|
|
| 88 |
with col2 :
|
| 89 |
+
st.button("👎 Deluso", on_click=click_handler, type="secondary")
|
| 90 |
|
| 91 |
def generate_chat_stream(prompt) :
|
| 92 |
links = []
|
| 93 |
if st.session_state.rag_enabled :
|
| 94 |
+
with st.spinner("Ricerca nei documenti...."):
|
| 95 |
+
prompt, links = gen_augmented_prompt(prompt=prompt, top_k=st.session_state.top_k)
|
| 96 |
+
with st.spinner("Generazione in corso...") :
|
|
|
|
| 97 |
chat_stream = chat(prompt, st.session_state.history,chat_client=CHAT_BOTS[st.session_state.chat_bot] ,
|
| 98 |
+
temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)
|
|
|
|
| 99 |
return chat_stream, links
|
| 100 |
|
| 101 |
def stream_handler(chat_stream, placeholder) :
|
|
|
|
| 116 |
col1, col2, col3 = st.columns(3)
|
| 117 |
|
| 118 |
with col1 :
|
| 119 |
+
st.write(f"**{tokens_per_second} token/secondi**")
|
| 120 |
|
| 121 |
with col2 :
|
| 122 |
+
st.write(f"**{int(len_response)} tokens generati**")
|
|
|
|
|
|
|
|
|
|
| 123 |
|
|
|
|
|
|
|
|
|
|
| 124 |
return full_response
|
| 125 |
|
| 126 |
def show_source(links) :
|
| 127 |
+
with st.expander("Mostra fonti") :
|
| 128 |
for i, link in enumerate(links) :
|
| 129 |
st.info(f"{link}")
|
| 130 |
|
|
|
|
| 133 |
header()
|
| 134 |
chat_box()
|
| 135 |
|
| 136 |
+
if prompt := st.chat_input("Chatta con BonsiAI..."):
|
| 137 |
st.chat_message("user").markdown(prompt)
|
| 138 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 139 |
|
|
|
|
| 147 |
show_source(links)
|
| 148 |
|
| 149 |
st.session_state.history.append([prompt, full_response])
|
| 150 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|