ragtest-sakimilo / archive /streamlit_app /streamlit_app_15Jan2024.py
lingyit1108's picture
finishing QnA and functions calling plus pydantic
ac8a60b
raw
history blame
No virus
4.9 kB
import streamlit as st
import os
import pandas as pd
import openai
from openai import OpenAI
import pkg_resources
import shutil
import main
### To trigger trulens evaluation
main.main()
### Finally, start streamlit app
leaderboard_path = pkg_resources.resource_filename(
"trulens_eval", "Leaderboard.py"
)
evaluation_path = pkg_resources.resource_filename(
"trulens_eval", "pages/Evaluations.py"
)
ux_path = pkg_resources.resource_filename(
"trulens_eval", "ux"
)
os.makedirs("./pages", exist_ok=True)
shutil.copyfile(leaderboard_path, os.path.join("./pages", "1_Leaderboard.py"))
shutil.copyfile(evaluation_path, os.path.join("./pages", "2_Evaluations.py"))
if os.path.exists("./ux"):
shutil.rmtree("./ux")
shutil.copytree(ux_path, "./ux")
# App title
st.set_page_config(page_title="πŸ’¬ Open AI Chatbot")
openai_api = os.getenv("OPENAI_API_KEY")
data_df = pd.DataFrame(
{
"Completion": [30, 40, 100, 10],
}
)
data_df.index = ["Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4"]
# Replicate Credentials
with st.sidebar:
st.title("πŸ’¬ Open AI Chatbot")
st.write("This chatbot is created using the GPT model from Open AI.")
if openai_api:
pass
elif "OPENAI_API_KEY" in st.secrets:
st.success("API key already provided!", icon="βœ…")
openai_api = st.secrets["OPENAI_API_KEY"]
else:
openai_api = st.text_input("Enter OpenAI API token:", type="password")
if not (openai_api.startswith("sk-") and len(openai_api)==51):
st.warning("Please enter your credentials!", icon="⚠️")
else:
st.success("Proceed to entering your prompt message!", icon="πŸ‘‰")
### for streamlit purpose
os.environ["OPENAI_API_KEY"] = openai_api
st.subheader("Models and parameters")
selected_model = st.sidebar.selectbox("Choose an OpenAI model",
["gpt-3.5-turbo-1106", "gpt-4-1106-preview"],
key="selected_model")
temperature = st.sidebar.slider("temperature", min_value=0.01, max_value=2.0,
value=0.1, step=0.01)
st.data_editor(
data_df,
column_config={
"Completion": st.column_config.ProgressColumn(
"Completion %",
help="Percentage of content covered",
format="%.1f%%",
min_value=0,
max_value=100,
),
},
hide_index=False,
)
st.markdown("πŸ“– Reach out to SakiMilo to learn how to create this app!")
# Store LLM generated responses
if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant",
"content": "How may I assist you today?"}]
# Display or clear chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
def clear_chat_history():
st.session_state.messages = [{"role": "assistant",
"content": "How may I assist you today?"}]
st.sidebar.button("Clear Chat History", on_click=clear_chat_history)
def generate_llm_response(client, prompt_input):
system_content = ("You are a helpful assistant. "
"You do not respond as 'User' or pretend to be 'User'. "
"You only respond once as 'Assistant'."
)
completion = client.chat.completions.create(
model=selected_model,
messages=[
{"role": "system", "content": system_content},
] + st.session_state.messages,
temperature=temperature,
stream=True
)
return completion
# User-provided prompt
if prompt := st.chat_input(disabled=not openai_api):
client = OpenAI()
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_llm_response(client, prompt)
placeholder = st.empty()
full_response = ""
for chunk in response:
if chunk.choices[0].delta.content is not None:
full_response += chunk.choices[0].delta.content
placeholder.markdown(full_response)
placeholder.markdown(full_response)
message = {"role": "assistant", "content": full_response}
st.session_state.messages.append(message)