Spaces:
Running
Running
File size: 4,856 Bytes
db694c4 b2b3b83 db694c4 b2b3b83 db694c4 a561bc6 db694c4 b2b3b83 db694c4 d026604 a561bc6 d026604 db694c4 d026604 db694c4 d026604 a561bc6 d026604 db694c4 d026604 b2b3b83 d026604 db694c4 d026604 db694c4 d026604 db694c4 c6352d6 db694c4 5abcb47 db694c4 b6cdc6a db694c4 09f2e2a db694c4 c6352d6 db694c4 d026604 09f2e2a c3572db db694c4 |
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 |
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"
)
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) |