Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcarroll committed Aug 15, 2023
1 parent 1bf4ce5 commit de8d39d
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions pages/5_Chat_with_user_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,49 @@
import trubrics

with st.sidebar:
openai_api_key = st.text_input(
"OpenAI API Key", key="langchain_search_api_key_openai", type="password"
)
openai_api_key = st.text_input("OpenAI API Key", key="feedback_api_key", type="password")
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/5_Chat_with_user_feedback.py)"
"[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)"

st.title("πŸ“ Chat with user feedback (Trubrics)")
st.title("πŸ“ Chat with feedback (Trubrics)")

"""
In this example, we're using [streamlit-feedback](https://github.com/trubrics/streamlit-feedback) from Trubrics to collect and store feedback
In this example, we're using [streamlit-feedback](https://github.com/trubrics/streamlit-feedback) and Trubrics to collect and store feedback
from the user about the LLM responses.
"""

if "messages" not in st.session_state:
st.session_state["messages"] = [
{
"role": "assistant",
"content": "Hi, how can I help you? Leave feedback to help my team understand my weaknesses!",
}
st.session_state.messages = [
{"role": "assistant", "content": "How can I help you? Leave feedback to help me improve!"}
]
if "response" not in st.session_state:
st.session_state["response"] = None

for msg in st.session_state.messages:
messages = st.session_state.messages
for msg in messages:
st.chat_message(msg["role"]).write(msg["content"])

if prompt := st.chat_input(placeholder="Tell me a joke about sharks"):
st.session_state.messages.append({"role": "user", "content": prompt})
messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)

if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
else:
openai.api_key = openai_api_key
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=st.session_state.messages
)
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
st.session_state["response"] = response.choices[0].message.content
with st.chat_message("assistant"):
st.session_state.messages.append(
{"role": "assistant", "content": st.session_state["response"]}
)
messages.append({"role": "assistant", "content": st.session_state["response"]})
st.write(st.session_state["response"])

if st.session_state["response"]:
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
key=f"feedback_{len(st.session_state.messages)}",
key=f"feedback_{len(messages)}",
)
# This app is logging feedback to Trubrics backend, but you can send it anywhere.
# The return value of streamlit_feedback() is just a dict.
Expand All @@ -68,7 +60,7 @@
component_name="default",
model="gpt",
response=feedback,
metadata={"chat": st.session_state.messages},
metadata={"chat": messages},
)
trubrics.save(config, collection)
# st.toast("Feedback recorded!", icon="πŸ“")
st.toast("Feedback recorded!", icon="πŸ“")

0 comments on commit de8d39d

Please sign in to comment.