colin4554 commited on
Commit
f86e7a9
1 Parent(s): 2a67e1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -1,11 +1,44 @@
 
1
  import streamlit as st
2
- import numpy as np
 
3
 
4
 
5
- with st.chat_message("user"):
6
- st.write("Hello 👋")
7
- st.line_chart(np.random.randn(30, 3))
8
 
9
- prompt = st.chat_input("Say something")
10
- if prompt:
11
- st.write(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
  import streamlit as st
3
+ import os
4
+ from openai import AzureOpenAI
5
 
6
 
7
+ st.title("Support Chat UI")
 
 
8
 
9
+ client = AzureOpenAI(
10
+ api_key = os.environ['OPENAI_API_KEY'],
11
+ api_version = "2023-07-01-preview",
12
+ azure_endpoint = os.environ['AZURE_ENDPOINT'],
13
+ )
14
+
15
+ if "openai_model" not in st.session_state:
16
+ st.session_state["openai_model"] = "gpt-35-turbo-0613"
17
+
18
+ if "messages" not in st.session_state:
19
+ st.session_state.messages = []
20
+
21
+ for message in st.session_state.messages:
22
+ with st.chat_message(message["role"]):
23
+ st.markdown(message["content"])
24
+
25
+ if prompt := st.chat_input("What is up?"):
26
+ st.session_state.messages.append({"role": "user", "content": prompt})
27
+ with st.chat_message("user"):
28
+ st.markdown(prompt)
29
+
30
+ with st.chat_message("assistant"):
31
+ message_placeholder = st.empty()
32
+ full_response = ""
33
+ for response in client.chat.completions.create(
34
+ model=st.session_state["openai_model"],
35
+ messages=[
36
+ {"role": m["role"], "content": m["content"]}
37
+ for m in st.session_state.messages
38
+ ],
39
+ stream=True,
40
+ ):
41
+ full_response += (response.choices[0].delta.content or "")
42
+ message_placeholder.markdown(full_response + "▌")
43
+ message_placeholder.markdown(full_response)
44
+ st.session_state.messages.append({"role": "assistant", "content": full_response})