66
77from trubrics .integrations .streamlit import FeedbackCollector
88
9+ st .title ("💬 Trubrics - Chat with user feedback" )
10+
911with st .sidebar :
1012 email , password = trubrics_config ()
1113
14+ if not email or not password :
15+ st .info (
16+ "To chat with an LLM and save your feedback to Trubrics, add your email and password in the sidebar."
17+ " Don't have an account yet? Create one for free [here](https://trubrics.streamlit.app/)!"
18+ )
19+ st .stop ()
20+
1221
1322@st .cache_data
1423def init_trubrics (email , password ):
15- collector = FeedbackCollector (email = email , password = password , project = "default" )
16- return collector
24+ try :
25+ collector = FeedbackCollector (email = email , password = password , project = "default" )
26+ return collector
27+ except Exception :
28+ st .error (f"Error authenticating '{ email } ' with [Trubrics](https://trubrics.streamlit.app/). Please try again." )
29+ st .stop ()
1730
1831
1932collector = init_trubrics (email , password )
2033
21-
22- st .title ("💬 Trubrics - Chat with user feedback" )
2334if "messages" not in st .session_state :
2435 st .session_state ["messages" ] = [{"role" : "assistant" , "content" : "How can I help you?" }]
2536if "prompt_ids" not in st .session_state :
@@ -30,46 +41,66 @@ def init_trubrics(email, password):
3041model = st .secrets .get ("OPENAI_API_MODEL" ) or "gpt-3.5-turbo"
3142
3243openai_api_key = st .secrets .get ("OPENAI_API_KEY" )
44+
45+ messages = st .session_state .messages
46+ feedback_kwargs = {
47+ "component" : "default" ,
48+ "feedback_type" : "thumbs" ,
49+ "open_feedback_label" : "[Optional] Provide additional feedback" ,
50+ "model" : model ,
51+ "tags" : ["llm_chatbot.py" ],
52+ }
53+
54+ for n , msg in enumerate (messages ):
55+ st .chat_message (msg ["role" ]).write (msg ["content" ])
56+
57+ if msg ["role" ] == "assistant" and n > 1 :
58+ feedback_key = f"feedback_{ int (n / 2 )} "
59+
60+ if feedback_key not in st .session_state :
61+ st .session_state [feedback_key ] = None
62+
63+ disable_with_score = st .session_state [feedback_key ].get ("score" ) if st .session_state [feedback_key ] else None
64+ feedback = collector .st_feedback (
65+ ** feedback_kwargs ,
66+ key = feedback_key ,
67+ disable_with_score = disable_with_score ,
68+ prompt_id = st .session_state .prompt_ids [int (n / 2 ) - 1 ],
69+ user_id = email ,
70+ )
71+ if feedback :
72+ trubrics_successful_feedback (feedback )
73+
74+
3375if prompt := st .chat_input ():
76+ messages .append ({"role" : "user" , "content" : prompt })
77+ st .chat_message ("user" ).write (prompt )
3478 if not openai_api_key :
3579 st .info ("Please add your OpenAI API key to continue." )
3680 st .stop ()
3781 else :
3882 openai .api_key = openai_api_key
39-
40- st .session_state .messages .append ({"role" : "user" , "content" : prompt })
41- response = openai .ChatCompletion .create (model = model , messages = st .session_state .messages )
42- msg = response .choices [0 ].message
43- logged_prompt = collector .log_prompt (
44- config_model = {"model" : model },
45- prompt = prompt ,
46- generation = msg ["content" ],
47- session_id = st .session_state .session_id ,
48- tags = ["llm_chatbot.py" ],
83+ response = openai .ChatCompletion .create (model = model , messages = messages )
84+ generation = response .choices [0 ].message .content
85+
86+ with st .chat_message ("assistant" ):
87+ logged_prompt = collector .log_prompt (
88+ config_model = {"model" : model },
89+ prompt = prompt ,
90+ generation = generation ,
91+ session_id = st .session_state .session_id ,
92+ tags = ["llm_chatbot.py" ],
93+ user_id = email ,
94+ )
95+ st .session_state .prompt_ids .append (logged_prompt .id )
96+ messages .append ({"role" : "assistant" , "content" : generation })
97+ st .write (generation )
98+
99+ feedback = collector .st_feedback (
100+ ** feedback_kwargs ,
101+ key = f"feedback_{ int (len (messages )/ 2 )} " ,
102+ prompt_id = st .session_state .prompt_ids [int (len (messages ) / 2 ) - 1 ],
103+ user_id = email ,
49104 )
50- st .session_state .prompt_ids .append (logged_prompt .id )
51- st .session_state .messages .append (msg )
52-
53- feedback = None
54- for n , msg in enumerate (st .session_state .messages ):
55- st .chat_message (msg ["role" ]).write (msg ["content" ])
56-
57- if msg ["role" ] == "assistant" and msg ["content" ] != "How can I help you?" :
58- if email and password :
59- feedback = collector .st_feedback (
60- component = "default" ,
61- feedback_type = "thumbs" ,
62- model = model ,
63- prompt_id = st .session_state .prompt_ids [int (n / 2 ) - 1 ],
64- open_feedback_label = "[Optional] Provide additional feedback" ,
65- align = "flex-end" ,
66- single_submit = True ,
67- key = f"feedback_{ int (n / 2 )} " ,
68- tags = ["llm_chatbot.py" ],
69- )
70-
71- else :
72- st .warning ("To save some feedback to Trubrics, add your account details in the sidebar." )
73-
74- if feedback :
75- trubrics_successful_feedback (feedback )
105+ if feedback :
106+ trubrics_successful_feedback (feedback )
0 commit comments