-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBASIC_MISTRAL_8X7B_PAGE.py
95 lines (82 loc) · 3.77 KB
/
BASIC_MISTRAL_8X7B_PAGE.py
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
import streamlit as st
import replicate
import os
# App title
st.set_page_config(page_title="🦙💬 Llama 2 Chatbot")
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
# Verify API key
api_key = "r8_6C54yX21fa7UOSVTn0k7k1WOtsDGMEB1Hf3NJ" # Replace with your actual API key
print(f"API key being used: {api_key}")
# Set the API key for the entire session
os.environ["REPLICATE_API_TOKEN"] = api_key
# Initialize Replicate client with the API key
client = replicate.Client(api_token=api_key)
# Function to clear chat history
def clear_chat_history():
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
# Sidebar for model selection, parameters, and clear chat button
with st.sidebar:
st.title('🦙💬 Llama 2 Chatbot')
st.success('API key set!', icon='✅')
st.subheader('Models and parameters')
selected_model = st.selectbox('Choose a Llama2 model', ['Llama2-7B', 'Llama2-13B'], key='selected_model')
if selected_model == 'Llama2-7B':
llm = 'a16z-infra/llama7b-v2-chat:4f0a4744c7295c024a1de15e1a63c880d3da035fa1f49bfd344fe076074c8eea'
elif selected_model == 'Llama2-13B':
llm = 'a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5'
temperature = st.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01)
top_p = st.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
max_length = st.slider('max_length', min_value=32, max_value=128, value=120, step=8)
# Clear chat button
if st.button('Clear Chat History'):
clear_chat_history()
# Function to generate Llama2 response
def generate_llama2_response(prompt_input):
string_dialogue = "You are a helpful assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
for dict_message in st.session_state.messages:
if dict_message["role"] == "user":
string_dialogue += "User: " + dict_message["content"] + "\n\n"
else:
string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
try:
output = client.run(
llm, # Use the selected model
input={
"prompt": f"{string_dialogue} {prompt_input} Assistant: ",
"temperature": temperature,
"top_p": top_p,
"max_length": max_length,
"repetition_penalty": 1
}
)
return output
except Exception as e:
st.error(f"An error occurred: {str(e)}")
return None
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# User-provided prompt
if prompt := st.chat_input(disabled=False):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.write(prompt)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = generate_llama2_response(prompt)
if response:
placeholder = st.empty()
full_response = ''
for item in response:
full_response += item
placeholder.markdown(full_response)
placeholder.markdown(full_response)
message = {"role": "assistant", "content": full_response}
st.session_state.messages.append(message)
else:
st.error("Failed to generate response. Please check your API key and try again.")