|
| 1 | +from openai import OpenAI, DefaultHttpxClient |
| 2 | +import streamlit as st |
| 3 | +import os |
| 4 | +import time |
| 5 | + |
| 6 | +LOGO_SVG = "nutanix.svg" |
| 7 | + |
| 8 | +if "iep_host_disabled" not in st.session_state: |
| 9 | + st.session_state["iep_host_disabled"] = False |
| 10 | + |
| 11 | +def iep_host_disable(): |
| 12 | + st.session_state["iep_host_disabled"] = True |
| 13 | + |
| 14 | +if "endpoint_disabled" not in st.session_state: |
| 15 | + st.session_state["endpoint_disabled"] = False |
| 16 | + |
| 17 | +def endpoint_disable(): |
| 18 | + st.session_state["endpoint_disabled"] = True |
| 19 | + |
| 20 | +if "apikey_disabled" not in st.session_state: |
| 21 | + st.session_state["apikey_disabled"] = False |
| 22 | + |
| 23 | +def apikey_disable(): |
| 24 | + st.session_state["apikey_disabled"] = True |
| 25 | + |
| 26 | +if 'chat_disabled' not in st.session_state: |
| 27 | + st.session_state.chat_disabled = False |
| 28 | + |
| 29 | +if 'set_values' not in st.session_state: |
| 30 | + st.session_state.set_values = False |
| 31 | + |
| 32 | +if 'reset_values' not in st.session_state: |
| 33 | + st.session_state.reset_values = False |
| 34 | + |
| 35 | +if 'chat_default' not in st.session_state: |
| 36 | + st.session_state.chat_default = "Ask me" |
| 37 | + |
| 38 | +# App title |
| 39 | +st.title("Demo Chatbot") |
| 40 | + |
| 41 | +def clear_chat_history(): |
| 42 | + """ |
| 43 | + Clears the chat history by resetting the session state messages. |
| 44 | + """ |
| 45 | + st.session_state.messages = [] |
| 46 | + |
| 47 | + |
| 48 | +with st.sidebar: |
| 49 | + if os.path.exists(LOGO_SVG): |
| 50 | + _, col2, _, _ = st.columns(4) |
| 51 | + with col2: |
| 52 | + st.image(LOGO_SVG, width=150) |
| 53 | + |
| 54 | + st.title("Nutanix Enterprise AI") |
| 55 | + st.markdown( |
| 56 | + "Nutanix Enterprise AI a simple way to securely deploy, scale, and run LLMs " |
| 57 | + " with NVIDIA NIM optimized inference microservices as well as open foundation " |
| 58 | + "models from Hugging Face. Read the [announcement]" |
| 59 | + "(https://www.nutanix.com/press-releases/2024/nutanix-extends-ai-platform-to-public-cloud)" |
| 60 | + ) |
| 61 | + iep_host_name = st.sidebar.text_input( |
| 62 | + "Enter the Inference Endpoint URL", disabled=st.session_state.iep_host_disabled, on_change=iep_host_disable |
| 63 | + ) |
| 64 | + |
| 65 | + st.subheader("Endpoint Configuration") |
| 66 | + endpoint_name = st.sidebar.text_input( |
| 67 | + "Enter the Endpoint name", disabled=st.session_state.endpoint_disabled, on_change=endpoint_disable |
| 68 | + ) |
| 69 | + endpoint_api_key = st.sidebar.text_input( |
| 70 | + "Enter the Endpoint API key", disabled=st.session_state.apikey_disabled, on_change=apikey_disable, type="password" |
| 71 | + ) |
| 72 | + |
| 73 | + if "iep_host_name" in st.session_state and st.session_state["iep_host_name"] != iep_host_name: |
| 74 | + clear_chat_history() |
| 75 | + |
| 76 | + if "endpoint_name" in st.session_state and st.session_state["endpoint_name"] != endpoint_name: |
| 77 | + clear_chat_history() |
| 78 | + |
| 79 | + if "endpoint_api_key" in st.session_state and st.session_state["endpoint_api_key"] != endpoint_api_key: |
| 80 | + clear_chat_history() |
| 81 | + |
| 82 | + st.session_state["iep_host_name"] = iep_host_name.strip() |
| 83 | + st.session_state["endpoint_name"] = endpoint_name.strip() |
| 84 | + st.session_state["endpoint_api_key"] = endpoint_api_key.strip() |
| 85 | + |
| 86 | + def set_values(): |
| 87 | + st.session_state.set_values = True |
| 88 | + st.session_state.iep_host_disabled = True |
| 89 | + st.session_state.endpoint_disabled = True |
| 90 | + st.session_state.apikey_disabled = True |
| 91 | + |
| 92 | + def reset_values(): |
| 93 | + st.session_state.reset_values = True |
| 94 | + st.session_state.iep_host_disabled = False |
| 95 | + st.session_state.endpoint_disabled = False |
| 96 | + st.session_state.apikey_disabled = False |
| 97 | + clear_chat_history() |
| 98 | + |
| 99 | + col1, _ ,col3 = st.columns(3) |
| 100 | + with col1: |
| 101 | + st.button('Save', on_click=set_values) |
| 102 | + with col3: |
| 103 | + st.button('Reset', on_click=reset_values, use_container_width=True) |
| 104 | + |
| 105 | +#if st.session_state.set_values: |
| 106 | +if not endpoint_name or not endpoint_api_key or not iep_host_name: |
| 107 | + st.session_state.chat_default="Endpoint URL, Name, and API key must be set" |
| 108 | + st.session_state.chat_disabled=True |
| 109 | +else: |
| 110 | + st.session_state.chat_default="Ask me" |
| 111 | + st.session_state.chat_disabled=False |
| 112 | + |
| 113 | +client = OpenAI(base_url=iep_host_name.removesuffix("/chat/completions"), api_key=endpoint_api_key, http_client=DefaultHttpxClient(verify=False)) |
| 114 | + |
| 115 | +if "messages" not in st.session_state: |
| 116 | + st.session_state.messages = [] |
| 117 | + |
| 118 | +for message in st.session_state.messages: |
| 119 | + with st.chat_message(message["role"]): |
| 120 | + st.markdown(message["content"]) |
| 121 | + |
| 122 | +if prompt := st.chat_input(st.session_state.chat_default, disabled=st.session_state.chat_disabled): |
| 123 | + st.session_state.messages.append({"role": "user", "content": prompt}) |
| 124 | + with st.chat_message("user"): |
| 125 | + st.markdown(prompt) |
| 126 | + |
| 127 | + try: |
| 128 | + with st.chat_message("assistant"): |
| 129 | + start = time.perf_counter() |
| 130 | + stream = client.chat.completions.create( |
| 131 | + model=st.session_state["endpoint_name"], |
| 132 | + messages=[ |
| 133 | + {"role": m["role"], "content": m["content"]} |
| 134 | + for m in st.session_state.messages |
| 135 | + ], |
| 136 | + max_tokens=1024, |
| 137 | + stream=True, |
| 138 | + ) |
| 139 | + response = st.write_stream(stream) |
| 140 | + request_time = "{:.2f}".format(time.perf_counter() - start) |
| 141 | + st.session_state.messages.append({"role": "assistant", "content": response}) |
| 142 | + st.markdown(f"Latency: {request_time} seconds") |
| 143 | + print(request_time) |
| 144 | + |
| 145 | + except Exception as e: |
| 146 | + print(e) |
| 147 | + st.error("Error. Did you set Inference Endpoint host name, Endpoint name and API key correctly?") |
0 commit comments