Spaces:
Running
Running
Commit ·
2006c2b
1
Parent(s): c5bc833
added mistralAI
Browse filesFiles changed (8)
- __pycache__/mistral7b.cpython-311.pycAdded
- app.py+62-13
- chat_log.txtAdded
- id_log.txtAdded
- ikigai.svgAdded
- mistral7b.pyAdded+47
- requirements.txt+2-1
- utils.py-2
1234567891011121314151617181920211 unmodified line232425262728293031323334import streamlit as stfrom utils import generate_text_embeddingsst.title("Echo Bot")if "messages" not in st.session_state:st.session_state.messages = []with st.sidebar :st.markdown("# Inference Analytics")st.markdown("---")st.markdown("Tokens used :")st.markdown("Average Querying Time :")st.markdown("Average Inference Time :")st.markdown("Cost Incurred :")for message in st.session_state.messages:1 unmodified linest.markdown(message["content"])if prompt := st.chat_input("What is up?"):query_embeddings = generate_text_embeddings(prompt)st.chat_message("user").markdown(prompt)st.session_state.messages.append({"role": "user", "content": prompt})response = f"Echo: {prompt}"with st.chat_message("assistant"):st.markdown(response)st.session_state.messages.append({"role": "assistant", "content": response})123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657581 unmodified line606162636465666768697071727374757677787980818283import streamlit as stfrom utils import generate_text_embeddingsfrom mistral7b import mistralimport timeif "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 "model_settings" not in st.session_state :st.session_state.model_settings = {"temp" : 0.9,"max_tokens" : 512,}if "history" not in st.session_state :st.session_state.history = []if "top_k" not in st.session_state :st.session_state.top_k = 5with st.sidebar: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))st.write("Cost Incured :",round( 0.033 * st.session_state['tokens_used']/ 1000, 3), "INR")st.markdown("---")st.markdown("# Retrieval Settings")st.slider(label="Documents to retrieve", min_value=1, max_value=10, value=3)st.markdown("---")st.markdown("# Model Settings")selected_model = st.sidebar.radio('Select one:', ["Mistral 7B", "GPT 3.5 Turbo", "GPT 4", "Llama 7B"])selected_temperature = st.slider(label="Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.5)st.write(" ")st.info("**2023 ©️ Pragnesh Barik**")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.""")for message in st.session_state.messages:1 unmodified linest.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()response = mistral(prompt, st.session_state.history, temperature=st.session_state.model_settings["temp"] , max_new_tokens=st.session_state.model_settings["max_tokens"])tock = time.time()st.session_state.inference_time.append(tock - tick)response = response.replace("</s>", "")len_response = len(response.split())st.session_state["tokens_used"] = len_response + st.session_state["tokens_used"]with st.chat_message("assistant"):st.markdown(response)st.session_state.history.append([prompt, response])st.session_state.messages.append({"role": "assistant", "content": response})
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647from huggingface_hub import InferenceClientimport osfrom dotenv import load_dotenvload_dotenv()
API_TOKEN = os.getenv('HF_TOKEN')client = InferenceClient( "mistralai/Mistral-7B-Instruct-v0.1", token=API_TOKEN)
def format_prompt(message, history): prompt = "<s>" for user_prompt, bot_response in history: prompt += f"[INST] {user_prompt} [/INST]" prompt += f" {bot_response}</s> " prompt += f"[INST] {message} [/INST]" return prompt
def mistral( prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,): temperature = float(temperature) if temperature < 1e-2: temperature = 1e-2 top_p = float(top_p)
generate_kwargs = dict( temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, )
formatted_prompt = format_prompt(prompt, history)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False) output = ""
for response in stream: # print(response) output += response.token["text"] # yield output return output3 unmodified lines45678943 unmodified lines535455565758593 unmodified linesbitarray==2.8.1blinker==1.6.3cachetools==5.3.1certifi==2023.7.22charset-normalizer==3.2.0click==8.1.743 unmodified linespyreadline3==3.4.1python-dotenv==1.0.0pytz==2023.3.post1PyYAML==6.0.1readme-renderer==42.0referencing==0.30.2regex==2023.8.83 unmodified lines4567891043 unmodified lines545556575859603 unmodified linesbitarray==2.8.1blinker==1.6.3cachetools==5.3.1huggingface-hub==0.16.4certifi==2023.7.22charset-normalizer==3.2.0click==8.1.743 unmodified linespyreadline3==3.4.1python-dotenv==1.0.0pytz==2023.3.post1PyYAML==6.0.1gitreadme-renderer==42.0referencing==0.30.2regex==2023.8.8
12345import jsonimport requestsimport osfrom dotenv import load_dotenvfrom sentence_transformers import SentenceTransformer123import osfrom dotenv import load_dotenvfrom sentence_transformers import SentenceTransformer
__pycache__/mistral7b.cpython-311.pycAdded
app.py+62-13
chat_log.txtAdded
id_log.txtAdded
ikigai.svgAdded
mistral7b.pyAdded+47
requirements.txt+2-1
utils.py-2