|
| 1 | +import uuid |
| 2 | + |
| 3 | +import openai |
| 4 | +import streamlit as st |
| 5 | +from trubrics_utils import trubrics_config, trubrics_successful_feedback |
| 6 | + |
| 7 | +from trubrics.integrations.streamlit import FeedbackCollector |
| 8 | + |
| 9 | +st.title("💬 Trubrics - Streaming chat with user feedback") |
| 10 | + |
| 11 | +with st.sidebar: |
| 12 | + email, password = trubrics_config() |
| 13 | + |
| 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 | + |
| 21 | + |
| 22 | +@st.cache_data |
| 23 | +def init_trubrics(email, password): |
| 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() |
| 30 | + |
| 31 | + |
| 32 | +collector = init_trubrics(email, password) |
| 33 | + |
| 34 | +if "messages" not in st.session_state: |
| 35 | + st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] |
| 36 | +if "prompt_ids" not in st.session_state: |
| 37 | + st.session_state["prompt_ids"] = [] |
| 38 | +if "session_id" not in st.session_state: |
| 39 | + st.session_state["session_id"] = str(uuid.uuid4()) |
| 40 | + |
| 41 | +model = st.secrets.get("OPENAI_API_MODEL") or "gpt-3.5-turbo" |
| 42 | + |
| 43 | +openai_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(st.session_state.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 | +if prompt := st.chat_input("What is up?"): |
| 75 | + st.session_state.messages.append({"role": "user", "content": prompt}) |
| 76 | + with st.chat_message("user"): |
| 77 | + st.markdown(prompt) |
| 78 | + with st.chat_message("assistant"): |
| 79 | + message_placeholder = st.empty() |
| 80 | + full_response = "" |
| 81 | + if not openai_api_key: |
| 82 | + st.info("Please add your OpenAI API key to continue.") |
| 83 | + st.stop() |
| 84 | + else: |
| 85 | + openai.api_key = openai_api_key |
| 86 | + |
| 87 | + for response in openai.ChatCompletion.create( |
| 88 | + model=model, |
| 89 | + messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages], |
| 90 | + stream=True, |
| 91 | + ): |
| 92 | + full_response += response.choices[0].delta.get("content", "") |
| 93 | + message_placeholder.markdown(full_response + "▌") |
| 94 | + message_placeholder.markdown(full_response) |
| 95 | + st.session_state.messages.append({"role": "assistant", "content": full_response}) |
| 96 | + logged_prompt = collector.log_prompt( |
| 97 | + config_model={"model": model}, |
| 98 | + prompt=prompt, |
| 99 | + generation=full_response, |
| 100 | + session_id=st.session_state.session_id, |
| 101 | + tags=["llm_chatbot.py"], |
| 102 | + user_id=email, |
| 103 | + ) |
| 104 | + st.session_state.prompt_ids.append(logged_prompt.id) |
| 105 | + feedback = collector.st_feedback( |
| 106 | + **feedback_kwargs, |
| 107 | + key=f"feedback_{int(len(messages)/2)}", |
| 108 | + prompt_id=logged_prompt.id, |
| 109 | + user_id=email, |
| 110 | + ) |
| 111 | + if feedback: |
| 112 | + trubrics_successful_feedback(feedback) |
0 commit comments