Skip to content

Commit fd44da8

Browse files
committed
sample chatbot
1 parent b3ed815 commit fd44da8

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[theme]
2+
base="dark"
3+
primaryColor="#7855FA"
4+
font="sans serif"
5+
6+
[ui]
7+
hideTopBar = true

python/sample-chatbot/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Chatbot demo
2+
3+
This is a real time chatbot demo that works with Nutanix Enterprise AI.
4+
5+
## Usage
6+
7+
### Create Virtual Environment
8+
9+
```
10+
python -m venv venv
11+
. venv/bin/activate
12+
```
13+
14+
### Install Python requirements
15+
16+
```
17+
pip install -r requirements.txt
18+
```
19+
20+
### Run app
21+
22+
```
23+
streamlit run chat.py
24+
```

python/sample-chatbot/chat.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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?")

python/sample-chatbot/nutanix.svg

Lines changed: 32 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
openai==1.20.0
2+
streamlit==1.31
3+
streamlit-extras==0.3.5

0 commit comments

Comments
 (0)