Spaces:
Running
Running
File size: 7,704 Bytes
d60dac5 e506679 cebfd3c e506679 5fd5c1f 6ec5095 e506679 2e2d510 e506679 cebfd3c 2e2d510 cebfd3c 6ec5095 cebfd3c 2006c2b 8b18fd0 cebfd3c 8b18fd0 d60dac5 6c6516f 2006c2b 8b18fd0 2006c2b 6c6516f 99ff44d 6c6516f 2e2d510 2006c2b 6c6516f e506679 2006c2b 8b18fd0 cebfd3c e506679 5fd5c1f e506679 2006c2b cebfd3c e506679 cebfd3c e506679 cebfd3c 2006c2b 8b18fd0 2006c2b 6c6516f 8b18fd0 6c6516f 8b18fd0 2006c2b 8b18fd0 e506679 2e2d510 8b18fd0 a741634 8b18fd0 cebfd3c 8b18fd0 2006c2b e506679 d60dac5 2006c2b cebfd3c d60dac5 8b18fd0 d60dac5 6c6516f cebfd3c 2006c2b cebfd3c 8b18fd0 2e2d510 8b18fd0 2006c2b d60dac5 2006c2b 8b18fd0 2006c2b 6c6516f cebfd3c 6c6516f 2e2d510 cebfd3c 2e2d510 bac1424 2e2d510 bb3e3ae 2e2d510 cebfd3c 2e2d510 bb3e3ae bac1424 bb3e3ae 2e2d510 cebfd3c 6e67a0c 2e2d510 a741634 2dee873 2e2d510 cebfd3c 2e2d510 cebfd3c 2e2d510 |
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import streamlit as st
from mistral7b import chat
import time
import pandas as pd
import pinecone
import os
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer
load_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",
}
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.
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 helping
modern analysts and operations teams automate data-intensive business, finance, analytics,
and supply chain operations. One of their products is Inventory Ops, which automates inventory
tracking 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, links
data = {
"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 = 0
if "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.8
if "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 = 4
if "repetion_penalty" not in st.session_state :
st.session_state.repetion_penalty = 1
if "rag_enabled" not in st.session_state :
st.session_state.rag_enabled = True
if "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 of
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.
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.text
placeholder.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.text
placeholder.markdown(full_response + "▌")
placeholder.markdown(full_response)
len_response = (len(prompt.split()) + len(full_response.split())) * 1.25
st.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})
|