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 } '. 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,64 @@ 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" )
33- if prompt := st .chat_input ():
34- if not openai_api_key :
35- st .info ("Please add your OpenAI API key to continue." )
36- st .stop ()
37- else :
38- openai .api_key = openai_api_key
3944
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" ],
49- )
50- st .session_state .prompt_ids .append (logged_prompt .id )
51- st .session_state .messages .append (msg )
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+ }
5253
53- feedback = None
54- for n , msg in enumerate (st .session_state .messages ):
54+ for n , msg in enumerate (messages ):
5555 st .chat_message (msg ["role" ]).write (msg ["content" ])
5656
57- if msg ["role" ] == "assistant" and msg [ "content" ] != "How can I help you?" :
57+ if msg ["role" ] == "assistant" and n > 1 :
5858 if email and password :
59+ feedback_key = f"feedback_{ int (n / 2 )} "
60+
61+ if feedback_key not in st .session_state :
62+ st .session_state [feedback_key ] = None
63+
64+ disable_with_score = st .session_state [feedback_key ].get ("score" ) if st .session_state [feedback_key ] else None
5965 feedback = collector .st_feedback (
60- component = "default" ,
61- feedback_type = "thumbs" ,
62- model = model ,
66+ ** feedback_kwargs ,
67+ key = feedback_key ,
68+ disable_with_score = disable_with_score ,
6369 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" ],
6970 )
71+ if feedback :
72+ trubrics_successful_feedback (feedback )
7073
71- else :
72- st .warning ("To save some feedback to Trubrics, add your account details in the sidebar." )
7374
74- if feedback :
75- trubrics_successful_feedback (feedback )
75+ if prompt := st .chat_input ():
76+ messages .append ({"role" : "user" , "content" : prompt })
77+ st .chat_message ("user" ).write (prompt )
78+ if not openai_api_key :
79+ st .info ("Please add your OpenAI API key to continue." )
80+ st .stop ()
81+ else :
82+ openai .api_key = openai_api_key
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+ )
94+ st .session_state .prompt_ids .append (logged_prompt .id )
95+ messages .append ({"role" : "assistant" , "content" : generation })
96+ st .write (generation )
97+
98+ feedback = collector .st_feedback (
99+ ** feedback_kwargs ,
100+ key = f"feedback_{ int (len (messages )/ 2 )} " ,
101+ prompt_id = st .session_state .prompt_ids [int (len (messages ) / 2 ) - 1 ],
102+ )
103+ if feedback :
104+ trubrics_successful_feedback (feedback )
0 commit comments