File size: 10,717 Bytes
12536c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586470a
12536c3
 
 
586470a
12536c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0ed9fe
12536c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
780ae98
 
12536c3
 
 
 
 
 
 
 
 
780ae98
b4cd312
12536c3
 
 
 
 
 
 
 
 
 
 
780ae98
12536c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# --- Importing Libraries ---
import streamlit as st
import config  # used only for default values
from openai import OpenAI
from typing import Dict, Any, List


def build_complete_system_prompt(selected_language: str, prompt_body: str) -> str:
    """
    Build the complete system prompt by combining the language instruction with the prompt body.

    Args:
        selected_language (str): The language chosen from the dropdown.
        prompt_body (str): The editable system prompt body.

    Returns:
        str: The complete system prompt with language instruction.
    """
    language_instruction = f"All your responses should be in {selected_language}.\n"
    return language_instruction + prompt_body


def reset_chat_history(initial_context: Dict[str, str]) -> None:
    """
    Reset the chat history to the initial context and rerun the app.

    Args:
        initial_context (Dict[str, str]): The initial chat context.
    """
    st.session_state["display_messages"] = [initial_context]
    st.rerun()


def stream_assistant_response(client: OpenAI, model: str, messages: List[Dict[str, str]],
                              temperature: float, max_tokens: int,
                              frequency_penalty: float, presence_penalty: float) -> str:
    """
    Stream the assistant's response from the OpenAI API and return the full response.

    Args:
        client (OpenAI): The initialized OpenAI client.
        model (str): The model to use.
        messages (List[Dict[str, str]]): The list of chat messages.
        temperature (float): The temperature setting.
        max_tokens (int): The maximum number of tokens.
        frequency_penalty (float): The frequency penalty.
        presence_penalty (float): The presence penalty.

    Returns:
        str: The complete assistant response.
    """
    full_response = ""
    message_placeholder = st.empty()
    try:
        stream = client.chat.completions.create(
            model=model,
            messages=messages,
            stream=True,
            temperature=temperature,
            max_tokens=max_tokens,
            frequency_penalty=frequency_penalty,
            presence_penalty=presence_penalty,
        )
        # Iterate through the stream to collect the response.
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                full_response += chunk.choices[0].delta.content
                message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)
    except Exception as e:
        st.exception(e)
        st.error(f"An error occurred: {str(e)}")
    return full_response


def main() -> None:
    """Main function to run the Streamlit Chatbot Prompt and Parameter Tester application."""
    # --- App Layout and Configuration ---
    st.set_page_config(
        layout="wide",
        page_title="OpenAI Chatbot Tester",
        page_icon=":lightbulb:",
        initial_sidebar_state="expanded"
    )
    st.title("OpenAI Chatbot Tester")

    # --- Sidebar: Configuration Inputs ---
    with st.sidebar:
        st.header("Configuration")

        # API Key Input Field
        api_key = st.text_input(
            "OpenAI API Key",
            type="password",
            key="api_key",
            help="This is the API key for your OpenAI account. You can find it [here](https://platform.openai.com/api-keys)."
        )
        if api_key:
            st.session_state["OPENAI_API_KEY"] = api_key
        else:
            st.warning("Please provide your OpenAI API key to enable chat functionality.")

        # Dropdown for model selection.
        model_options = ["gpt-4.1", "gpt-4o", "gpt-4o-mini"]
        default_model = config.ai_model if config.ai_model in model_options else model_options[0]
        selected_model = st.selectbox(
            "Model",
            options=model_options,
            index=model_options.index(default_model),
            help="This controls the model used for the chatbot. You can choose from the following models: gpt-4o, gpt-4o-mini"
        )
        st.session_state["openai_model"] = selected_model

        # Slider for temperature.
        temperature = st.slider(
            "Temperature",
            0.0, 1.0,
            value=config.temperature,
            step=0.05,
            help="This controls the randomness/creativity of the responses. A higher temperature results in more creative responses."
        )
        st.session_state["temperature"] = temperature

        # Number input for maximum tokens.
        max_tokens = st.number_input(
            "Max Tokens",
            min_value=1,
            max_value=2048,
            value=config.max_tokens,
            step=1,
            help="This controls the maximum number of tokens the AI can generate. 1000 tokens equals about 750 words."
        )
        st.session_state["max_tokens"] = max_tokens

        # Slider for frequency penalty.
        frequency_penalty = st.slider(
            "Frequency Penalty",
            0.0, 1.0,
            value=config.frequency_penalty,
            step=0.05,
            help="This controls the frequency penalty for the responses. Higher values produce more diverse responses."
        )
        st.session_state["frequency_penalty"] = frequency_penalty

        # Slider for presence penalty.
        presence_penalty = st.slider(
            "Presence Penalty",
            0.0, 1.0,
            value=config.presence_penalty,
            step=0.05,
            help="This controls the presence penalty for the responses. Higher values reduce repetition."
        )
        st.session_state["presence_penalty"] = presence_penalty

        # Language selection dropdown.
        language_options = [
            "English", "Albanian", "Amharic", "Arabic", "Armenian", "Bengali", "Bosnian", "Bulgarian", "Burmese",
            "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "Estonian", "Finnish", "French",
            "Georgian", "German", "Greek", "Gujarati", "Hindi", "Hungarian", "Icelandic", "Indonesian",
            "Italian", "Japanese", "Kannada", "Kazakh", "Korean", "Latvian", "Lithuanian", "Macedonian",
            "Malay", "Malayalam", "Marathi", "Mongolian", "Norwegian", "Persian", "Polish", "Portuguese",
            "Punjabi", "Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Somali", "Spanish",
            "Swahili", "Swedish", "Tagalog", "Tamil", "Telugu", "Thai", "Turkish", "Ukrainian", "Urdu",
            "Vietnamese"
        ]
        default_language = "English"
        if default_language not in language_options:
            language_options.insert(0, default_language)
        selected_language = st.selectbox(
            "Preferred Language",
            options=language_options,
            index=language_options.index(default_language),
            help="Select your preferred language for the chatbot's responses."
        )

        st.markdown("Click the 'Update Configuration' button each time you change an option.")

        st.markdown("---")

    # --- System Instructions ---
    # Retrieve the editable prompt body from session state or default config.
    default_body = st.session_state.get("system_prompt_body", config.prompt)
    system_prompt_body = st.text_area(
        "System Instructions",
        value=default_body,
        key="system_prompt_body",
        help="These instructions determine the chatbot's behavior. Click 'Update Configuration' to apply changes.",
        height=400
    )
    complete_system_prompt = build_complete_system_prompt(selected_language, system_prompt_body)
    st.session_state["system_prompt"] = complete_system_prompt

    # --- Chat State Initialization ---
    initial_context: Dict[str, str] = {
        "role": "system",
        "content": complete_system_prompt
    }

    # --- Update System Instructions Button ---
    if st.button("Update Configuration"):
        reset_chat_history(initial_context)

    # Ensure the API key is provided.
    if not st.session_state.get("OPENAI_API_KEY"):
        st.error("No API key provided. Please enter your OpenAI API key above.")
        st.stop()

    # Initialize the OpenAI client.
    client = OpenAI(api_key=st.session_state["OPENAI_API_KEY"])

    # Initialize session state variables if not already set.
    if "openai_model" not in st.session_state:
        st.session_state["openai_model"] = config.ai_model

    if "display_messages" not in st.session_state:
        st.session_state["display_messages"] = [initial_context]

    # --- Chat Input ---
    prompt = st.chat_input("Type your message here...")
    if prompt:
        st.session_state["display_messages"].append({"role": "user", "content": prompt})

    # --- Chat Display and Response Generation ---
    with st.container():
        # Display chat history.
        for message in st.session_state["display_messages"][1:]:
            if message["role"] == "user":
                with st.chat_message("user"):
                    st.markdown(message["content"])
            else:
                with st.chat_message("assistant"):
                    st.markdown(message["content"])

        # Generate the assistant's response if there is new input.
        if prompt:
            with st.chat_message("assistant"):
                full_response = stream_assistant_response(
                    client=client,
                    model=selected_model,
                    messages=[{"role": m["role"], "content": m["content"]}
                              for m in st.session_state["display_messages"]],
                    temperature=temperature,
                    max_tokens=max_tokens,
                    frequency_penalty=frequency_penalty,
                    presence_penalty=presence_penalty
                )
                st.session_state["display_messages"].append(
                    {"role": "assistant", "content": full_response}
                )

    # --- Footer ---
    with st.sidebar:
        st.markdown("Application created by [Keefe Reuther](https://reutherlab.netlify.app/), Assistant Teaching Professor in the UC San Diego School of Biological Sciences. "
                    "Code for this app is available [here](https://huggingface.co/spaces/keefereuther/ST_basebot) and is distributed under the [GNU GPL-3 License](https://www.gnu.org/licenses/gpl-3.0.en.html).")

    # --- Debug Block: Display Session State ---
    with st.sidebar.expander("Debug: Session State"):
        session_state_copy = {k: v for k, v in st.session_state.items() if k not in ["display_messages", "api_key", "OPENAI_API_KEY"]}
        st.json(session_state_copy)


if __name__ == "__main__":
    main()