Skip to content

Commit 86ecef6

Browse files
committed
fix llm_app and add user_id
1 parent fc0178f commit 86ecef6

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

examples/streamlit/llm_app.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
if "response" not in st.session_state:
88
st.session_state.response = ""
9+
if "feedback_key" not in st.session_state:
10+
st.session_state.feedback_key = 0
911
if "logged_prompt" not in st.session_state:
1012
st.session_state.logged_prompt = ""
1113

@@ -15,13 +17,17 @@
1517
email, password = trubrics_config()
1618

1719
if email and password:
18-
collector = FeedbackCollector(
19-
project="default",
20-
email=email,
21-
password=password,
22-
)
20+
try:
21+
collector = FeedbackCollector(email=email, password=password, project="default")
22+
except Exception:
23+
st.error(f"Error authenticating '{email}' with [Trubrics](https://trubrics.streamlit.app/). Please try again.")
24+
st.stop()
2325
else:
24-
st.warning("To save some feedback to Trubrics, add your account details in the sidebar.")
26+
st.info(
27+
"To ask a question to an LLM and save your feedback to Trubrics, add your email and password in the sidebar."
28+
" Don't have an account yet? Create one for free [here](https://trubrics.streamlit.app/)!"
29+
)
30+
st.stop()
2531

2632
models = ("gpt-3.5-turbo",)
2733
model = st.selectbox(
@@ -32,7 +38,8 @@
3238

3339
openai.api_key = st.secrets.get("OPENAI_API_KEY")
3440
if openai.api_key is None:
35-
raise ValueError("OpenAI key is missing. Set OPENAI_API_KEY in st.secrets")
41+
st.info("Please add your OpenAI API key to continue.")
42+
st.stop()
3643

3744
prompt = st.text_area(label="Prompt", label_visibility="collapsed", placeholder="What would you like to know?")
3845
button = st.button(f"Ask {model}")
@@ -41,9 +48,10 @@
4148
response = openai.ChatCompletion.create(model=model, messages=[{"role": "user", "content": prompt}])
4249
response_text = response.choices[0].message["content"]
4350
st.session_state.logged_prompt = collector.log_prompt(
44-
config_model={"model": model}, prompt=prompt, generation=response_text, tags=["llm_app.py"]
51+
config_model={"model": model}, prompt=prompt, generation=response_text, tags=["llm_app.py"], user_id=email
4552
)
4653
st.session_state.response = response_text
54+
st.session_state.feedback_key += 1
4755

4856
if st.session_state.response:
4957
st.markdown(f"#### :violet[{st.session_state.response}]")
@@ -55,8 +63,9 @@
5563
prompt_id=st.session_state.logged_prompt.id,
5664
model=model,
5765
align="flex-start",
58-
single_submit=False,
5966
tags=["llm_app.py"],
67+
key=f"feedback_{st.session_state.feedback_key}", # overwrite with new key
68+
user_id=email,
6069
)
6170

6271
if feedback:

examples/streamlit/llm_chatbot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def init_trubrics(email, password):
2525
collector = FeedbackCollector(email=email, password=password, project="default")
2626
return collector
2727
except Exception:
28-
st.error(f"Error authenticating '{email}'. Please try again.")
28+
st.error(f"Error authenticating '{email}' with [Trubrics](https://trubrics.streamlit.app/). Please try again.")
2929
st.stop()
3030

3131

@@ -67,6 +67,7 @@ def init_trubrics(email, password):
6767
key=feedback_key,
6868
disable_with_score=disable_with_score,
6969
prompt_id=st.session_state.prompt_ids[int(n / 2) - 1],
70+
user_id=email,
7071
)
7172
if feedback:
7273
trubrics_successful_feedback(feedback)
@@ -90,6 +91,7 @@ def init_trubrics(email, password):
9091
generation=generation,
9192
session_id=st.session_state.session_id,
9293
tags=["llm_chatbot.py"],
94+
user_id=email,
9395
)
9496
st.session_state.prompt_ids.append(logged_prompt.id)
9597
messages.append({"role": "assistant", "content": generation})
@@ -99,6 +101,7 @@ def init_trubrics(email, password):
99101
**feedback_kwargs,
100102
key=f"feedback_{int(len(messages)/2)}",
101103
prompt_id=st.session_state.prompt_ids[int(len(messages) / 2) - 1],
104+
user_id=email,
102105
)
103106
if feedback:
104107
trubrics_successful_feedback(feedback)

0 commit comments

Comments
 (0)