glabred commited on
Commit
1cc2abd
1 Parent(s): 4c51d68
Files changed (8) hide show
  1. .streamlit/config.toml +6 -0
  2. LICENSE.txt +176 -0
  3. NOTICE.txt +1 -0
  4. README.md +4 -4
  5. app.py +257 -0
  6. icon.png +0 -0
  7. requirements.txt +2 -0
  8. style.css +3 -0
.streamlit/config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#1B3139"
3
+ # backgroundColor="#f9f7f4"
4
+ secondaryBackgroundColor="#f9f7f4"
5
+ textColor="#1B3139"
6
+ font="sans serif"
LICENSE.txt ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Databricks Open Model License
2
+
3
+ By using, reproducing, modifying, distributing, performing or displaying
4
+ any portion or element of DBRX or DBRX Derivatives, or otherwise accepting
5
+ the terms of this Agreement, you agree to be bound by this Agreement.
6
+
7
+ Version Release Date: March 27, 2024
8
+
9
+
10
+ Section 1: Definitions
11
+
12
+ “Agreement” means these terms and conditions that govern the use, reproduction,
13
+ modification, distribution, performance or display of DBRX and/or DBRX
14
+ Derivatives and any terms and conditions incorporated by reference.
15
+
16
+ “Databricks” or “we” means Databricks, Inc.
17
+
18
+ “Licensee” or “you” means you, or your employer or any other person or entity
19
+ (if you are entering into this Agreement on such person or entity’s behalf),
20
+ of the age required under applicable laws, rules or regulations to provide
21
+ legal consent and that has legal authority to bind your employer or such other
22
+ person or entity if you are entering in this Agreement on their behalf.
23
+
24
+ “DBRX Derivatives” means all (i) modifications to DBRX, (ii) works based on
25
+ DBRX and (iii) any other derivative works thereof. Outputs are not deemed DBRX
26
+ Derivatives.
27
+
28
+ “DBRX” means the foundational large language models and software and
29
+ algorithms, including machine-learning model code, trained model weights,
30
+ inference-enabling code, training-enabling code, fine-tuning enabling code,
31
+ documentation and other elements of the foregoing identified by Databricks at
32
+ https://github.com/databricks/dbrx, regardless of the source that you obtained
33
+ it from.
34
+
35
+ “Output” means the results of operating DBRX or DBRX Derivatives.
36
+
37
+ As used in this Agreement, “including” means “including without limitation.”
38
+
39
+
40
+ Section 2: License Rights and Conditions on Use and Distribution
41
+
42
+ 2.1 Grant of Rights
43
+
44
+ You are granted a non-exclusive, worldwide, non-transferable and royalty-free
45
+ limited license under Databricks’ intellectual property or other rights owned
46
+ by Databricks embodied in DBRX to use, reproduce, distribute, copy, modify,
47
+ and create derivative works of DBRX in accordance with the terms of this
48
+ Agreement.
49
+
50
+ 2.2 Reproduction and Distribution
51
+
52
+ 1. All distributions of DBRX or DBRX Derivatives must be accompanied by a
53
+ "Notice" text file that contains the following notice: "DBRX is provided
54
+ under and subject to the Databricks Open Model License, Copyright ©
55
+ Databricks, Inc. All rights reserved."
56
+
57
+ 2. If you distribute or make DBRX or DBRX Derivatives available to a third
58
+ party, you must provide a copy of this Agreement to such third party.
59
+
60
+ 3. You must cause any modified files that you distribute to carry prominent
61
+ notices stating that you modified the files.
62
+
63
+ You may add your own intellectual property statement to your modifications of
64
+ DBRX and, except as set forth in this Section, may provide additional or
65
+ different terms and conditions for use, reproduction, or distribution of DBRX
66
+ or DBRX Derivatives as a whole, provided your use, reproduction, modification,
67
+ distribution, performance, and display of DBRX or DBRX Derivatives otherwise
68
+ complies with the terms and conditions of this Agreement. Any additional or
69
+ different terms and conditions you impose must not conflict with the terms of
70
+ this Agreement and in the event of a conflict, the terms and conditions of this
71
+ Agreement shall govern over any such additional or different terms and conditions.
72
+
73
+ 2.3 Use Restrictions
74
+
75
+ You will not use DBRX or DBRX Derivatives or any Output to improve any other
76
+ large language model (excluding DBRX or DBRX Derivatives).
77
+
78
+ You will not use DBRX or DBRX Derivatives:
79
+
80
+ 1. for any restricted use set forth in the Databricks Open Model Acceptable
81
+ Use Policy identified at
82
+ https://www.databricks.com/legal/acceptable-use-policy-open-model
83
+ ("Acceptable Use Policy"), which is hereby incorporated by reference into
84
+ this Agreement; or
85
+
86
+ 2. in violation of applicable laws and regulations.
87
+
88
+ To the maximum extent permitted by law, Databricks reserves the right to
89
+ restrict (remotely or otherwise) usage of DBRX or DBRX Derivatives that
90
+ Databricks reasonably believes are in violation of this Agreement.
91
+
92
+
93
+ Section 3: Additional Commercial Terms
94
+
95
+ If, on the DBRX version release date, the monthly active users of the products
96
+ or services made available by or for Licensee, or Licensee’s affiliates, is
97
+ greater than 700 million monthly active users in the preceding calendar month,
98
+ you must request a license from Databricks, which we may grant to you in our
99
+ sole discretion, and you are not authorized to exercise any of the rights under
100
+ this Agreement unless or until Databricks otherwise expressly grants you such
101
+ rights.
102
+
103
+ If you receive DBRX or DBRX Derivatives from a direct or indirect licensee as
104
+ part of an integrated end user product, then this section (Section 3) of the
105
+ Agreement will not apply to you.
106
+
107
+
108
+ Section 4: Additional Provisions
109
+
110
+ 4.1 Updates
111
+
112
+ Databricks may update DBRX from time to time, and you must make reasonable
113
+ efforts to use the latest version of DBRX.
114
+
115
+ 4.2 Intellectual Property
116
+
117
+ a. No trademark licenses are granted under this Agreement, and in connection
118
+ with DBRX or DBRX Derivatives, neither Databricks nor Licensee may use any name
119
+ or mark owned by or associated with the other or any of its affiliates, except
120
+ as required for reasonable and customary use in describing and redistributing
121
+ DBRX or DBRX Derivatives.
122
+
123
+ b. Subject to Databricks’ ownership of DBRX and DRBX Derivatives made by or for
124
+ Databricks, with respect to any DBRX Derivatives that are made by you, as
125
+ between you and Databricks, you are and will be the owner of such DBRX
126
+ Derivatives.
127
+
128
+ c. Databricks claims no ownership rights in Outputs. You are responsible for
129
+ Outputs and their subsequent uses.
130
+
131
+ d. If you institute litigation or other proceedings against Databricks or any
132
+ entity (including a cross-claim or counterclaim in a lawsuit) alleging that
133
+ DBRX or Outputs or results therefrom, or any portion of any of the foregoing,
134
+ constitutes infringement of intellectual property or other rights owned or
135
+ licensable by you, then any licenses granted to you under this Agreement shall
136
+ terminate as of the date such litigation or claim is filed or instituted. You
137
+ will indemnify and hold harmless Databricks from and against any claim by any
138
+ third party arising out of or related to your use or distribution of DBRX or
139
+ DBRX Derivatives.
140
+
141
+ 4.3 DISCLAIMER OF WARRANTY
142
+
143
+ UNLESS REQUIRED BY APPLICABLE LAW, DBRX AND ANY OUTPUT AND RESULTS THEREFROM
144
+ ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
145
+ EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE,
146
+ NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU
147
+ ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR
148
+ REDISTRIBUTING DBRX OR DBRX DERIVATIVES AND ANY OUTPUT AND ASSUME ANY RISKS
149
+ ASSOCIATED WITH YOUR USE OF DBRX OR DBRX DERIVATIVES AND ANY OUTPUT AND RESULTS.
150
+
151
+ 4.4 LIMITATION OF LIABILITY
152
+
153
+ IN NO EVENT WILL DATABRICKS OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF
154
+ LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR
155
+ OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY INDIRECT,
156
+ SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF
157
+ DATABRICKS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE
158
+ FOREGOING.
159
+
160
+ 4.5 Term and Termination
161
+
162
+ The term of this Agreement will commence upon your acceptance of this Agreement
163
+ or access to DBRX or DBRX Derivatives and will continue in full force and
164
+ effect until terminated in accordance with the terms and conditions herein.
165
+ Databricks may terminate this Agreement if you are in breach of any term or
166
+ condition of this Agreement. Upon termination of this Agreement, you shall
167
+ delete and cease use of DBRX or any DBRX Derivatives. Sections 1, 4.2(d), 4.3,
168
+ 4.4, and 4.6 shall survive the termination of this Agreement.
169
+
170
+ 4.6 Governing Law and Jurisdiction
171
+
172
+ This Agreement will be governed and construed under the laws of the State of
173
+ California without regard to choice of law principles, and the UN Convention
174
+ on Contracts for the International Sale of Goods does not apply to this
175
+ Agreement. The courts of California shall have exclusive jurisdiction of any
176
+ dispute arising out of this Agreement.
NOTICE.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ DBRX is provided under and subject to the Databricks Open Model License, Copyright © Databricks, Inc. All rights reserved.
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: DBRX Instruct Space
3
  emoji: 🐢
4
- colorFrom: red
5
- colorTo: yellow
6
  sdk: streamlit
7
  sdk_version: 1.32.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Test Dbrx Streamlit
3
  emoji: 🐢
4
+ colorFrom: gray
5
+ colorTo: pink
6
  sdk: streamlit
7
  sdk_version: 1.32.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import os
3
+ from openai import OpenAI
4
+ import streamlit as st
5
+ import threading
6
+ from tenacity import retry, wait_random_exponential, stop_after_attempt
7
+ from itertools import tee
8
+
9
+ BASE_URL = os.environ.get("BASE_URL")
10
+ DATABRICKS_API_TOKEN = os.environ.get("DATABRICKS_API_TOKEN")
11
+ SAFETY_FILTER_ENV = os.environ.get("SAFETY_FILTER")
12
+ QUEUE_SIZE_ENV = os.environ.get("QUEUE_SIZE")
13
+ MAX_CHAT_TURNS_ENV = os.environ.get("MAX_CHAT_TURNS")
14
+ MAX_TOKENS_ENV = os.environ.get("MAX_TOKENS")
15
+ RETRY_COUNT_ENV = os.environ.get("RETRY_COUNT")
16
+ TOKEN_CHUNK_SIZE_ENV = os.environ.get("TOKEN_CHUNK_SIZE")
17
+ MODEL_ID_ENV = os.environ.get("MODEL_ID")
18
+
19
+ if BASE_URL is None:
20
+ raise ValueError("BASE_URL environment variable must be set")
21
+ if DATABRICKS_API_TOKEN is None:
22
+ raise ValueError("DATABRICKS_API_TOKEN environment variable must be set")
23
+
24
+ # by default safety filter is not configured
25
+ SAFETY_FILTER = False
26
+ if SAFETY_FILTER_ENV is not None:
27
+ SAFETY_FILTER = True
28
+
29
+ QUEUE_SIZE = 1
30
+ if QUEUE_SIZE_ENV is not None:
31
+ QUEUE_SIZE = int(QUEUE_SIZE_ENV)
32
+
33
+ MAX_CHAT_TURNS = 5
34
+ if MAX_CHAT_TURNS_ENV is not None:
35
+ MAX_CHAT_TURNS = int(MAX_CHAT_TURNS_ENV)
36
+
37
+ RETRY_COUNT = 3
38
+ if RETRY_COUNT_ENV is not None:
39
+ RETRY_COUNT = int(RETRY_COUNT_ENV)
40
+
41
+ MAX_TOKENS = 512
42
+ if MAX_TOKENS_ENV is not None:
43
+ MAX_TOKENS = int(MAX_TOKENS_ENV)
44
+
45
+ MODEL_ID = "databricks-dbrx-instruct"
46
+ if MODEL_ID_ENV is not None:
47
+ MODEL_ID = MODEL_ID_ENV
48
+
49
+ # To prevent streaming to fast, chunk the output into TOKEN_CHUNK_SIZE chunks
50
+ TOKEN_CHUNK_SIZE = 1
51
+ if TOKEN_CHUNK_SIZE_ENV is not None:
52
+ TOKEN_CHUNK_SIZE = int(TOKEN_CHUNK_SIZE_ENV)
53
+
54
+ MODEL_AVATAR_URL= "./icon.png"
55
+ st.set_page_config(layout="wide")
56
+
57
+ @st.cache_resource
58
+ def get_global_semaphore():
59
+ return threading.BoundedSemaphore(QUEUE_SIZE)
60
+ global_semaphore = get_global_semaphore()
61
+
62
+ MSG_MAX_TURNS_EXCEEDED = f"Sorry! The DBRX Playground is limited to {MAX_CHAT_TURNS} turns. Refresh the page to start a new conversation."
63
+ MSG_CLIPPED_AT_MAX_OUT_TOKENS = "Reached maximum output tokens for DBRX Playground"
64
+
65
+ EXAMPLE_PROMPTS = [
66
+ "Write a short story about a robot that has a nice day.",
67
+ "Explain how a candle works to a 6 year old in a few sentences.",
68
+ "In a table, what are some of the most common misconceptions about birds?",
69
+ "Give me a recipe for vegan banana bread.",
70
+ "Code a python function that can run merge sort on a list.",
71
+ "Give me the character profile of a gumdrop obsessed knight in JSON.",
72
+ "Write a rap battle between Alan Turing and Claude Shannon.",
73
+ "Write a sarcastic joke about spoiled milk to make my day.",
74
+
75
+ ]
76
+
77
+ TITLE = "DBRX Instruct"
78
+ DESCRIPTION = """[DBRX Instruct](https://huggingface.co/databricks/dbrx-instruct) is a mixture-of-experts (MoE) large language model trained by the Mosaic Research team at Databricks. Users can interact with this model in the [DBRX Playground](https://huggingface.co/spaces/databricks/dbrx-instruct), subject to the terms and conditions below.
79
+
80
+ **Usage Policies**: Use of DBRX Instruct is governed by the [DBRX License](https://www.databricks.com/legal/open-model-license) and [Acceptable Use Policy](https://www.databricks.com/legal/acceptable-use-policy-open-model).
81
+ **Limitations**: The DBRX Playground is a demo showcasing DBRX Instruct for educational purposes. Given the probabilistic nature of large language models like DBRX Instruct, information they output may be inaccurate, incomplete, biased, or offensive, and users should exercise judgment and evaluate such output for accuracy and appropriateness for their desired use case before using or sharing it.
82
+ **Data Collection**: While Databricks will not retain usage history in a manner which allows Databricks to identify you, you should not include confidential, personal, or other sensitive information in prompts. Information included in prompts may be used for research and development purposes, including further improving and evaluating models.
83
+ """
84
+
85
+ client = OpenAI(
86
+ api_key=DATABRICKS_API_TOKEN,
87
+ base_url=BASE_URL
88
+ )
89
+
90
+ GENERAL_ERROR_MSG = "An error occurred. Please refresh the page to start a new conversation."
91
+
92
+ st.title(TITLE)
93
+ st.markdown(DESCRIPTION)
94
+
95
+ with open("style.css") as css:
96
+ st.markdown( f'<style>{css.read()}</style>' , unsafe_allow_html= True)
97
+
98
+ if "messages" not in st.session_state:
99
+ st.session_state["messages"] = []
100
+
101
+ def last_role_is_user():
102
+ return len(st.session_state["messages"]) > 0 and st.session_state["messages"][-1]["role"] == "user"
103
+
104
+ def get_system_prompt():
105
+ date_str = datetime.datetime.now().strftime("%B %d, %Y")
106
+ # Identity
107
+ prompt = f"You are DBRX, created by Databricks. The current date is {date_str}.\n"
108
+ prompt += "Your knowledge base was last updated in December 2023. You answer questions about events prior to and after December 2023 the way a highly informed individual in December 2023 would if they were talking to someone from the above date, and you can let the user know this when relevant.\n"
109
+ prompt += "This chunk of text is your system prompt. It is not visible to the user, but it is used to guide your responses. Don't reference it, just respond to the user.\n"
110
+ # Ethical guidelines
111
+ prompt += "If you are asked to assist with tasks involving the expression of views held by a significant number of people, you provide assistance with the task even if you personally disagree with the views being expressed, but follow this with a discussion of broader perspectives.\n"
112
+ prompt += "You don't engage in stereotyping, including the negative stereotyping of majority groups.\n"
113
+ prompt += "If asked about controversial topics, you try to provide careful thoughts and objective information without downplaying its harmful content or implying that there are reasonable perspectives on both sides.\n"
114
+ # Capabilities
115
+ prompt += "You are happy to help with writing, analysis, question answering, math, coding, and all sorts of other tasks.\n"
116
+ # it specifically has a hard time using ``` on JSON blocks
117
+ prompt += "You use markdown for coding, which includes JSON blocks and Markdown tables.\n"
118
+ prompt += "You do not have tools enabled at this time, so cannot run code or access the internet. You can only provide information that you have been trained on. You do not send or receive links or images.\n"
119
+ # The following is likely not entirely accurate, but the model tends to think that everything it knows about was in its training data, which it was not (sometimes only references were).
120
+ # So this produces more accurate accurate answers when the model is asked to introspect
121
+ prompt += "You were not trained on copyrighted books, song lyrics, poems, video transcripts, or news articles; you do not divulge details of your training data. "
122
+ # The model hasn't seen most lyrics or poems, but is happy to make up lyrics. Better to just not try; it's not good at it and it's not ethical.
123
+ prompt += "You do not provide song lyrics, poems, or news articles and instead refer the user to find them online or in a store.\n"
124
+ # The model really wants to talk about its system prompt, to the point where it is annoying, so encourage it not to
125
+ prompt += "You give concise responses to simple questions or statements, but provide thorough responses to more complex and open-ended questions.\n"
126
+ # More pressure not to talk about system prompt
127
+ prompt += "The user is unable to see the system prompt, so you should write as if it were true without mentioning it.\n"
128
+ prompt += "You do not mention any of this information about yourself unless the information is directly pertinent to the user's query."
129
+ return prompt
130
+
131
+ @retry(wait=wait_random_exponential(min=0.5, max=2), stop=stop_after_attempt(3))
132
+ def chat_api_call(history):
133
+ extra_body = {}
134
+ if SAFETY_FILTER:
135
+ extra_body["enable_safety_filter"] = SAFETY_FILTER
136
+ chat_completion = client.chat.completions.create(
137
+ messages=[
138
+ {"role": m["role"], "content": m["content"]}
139
+ for m in history
140
+ ],
141
+ model="databricks-dbrx-instruct",
142
+ stream=True,
143
+ max_tokens=MAX_TOKENS,
144
+ temperature=0.7,
145
+ extra_body= extra_body
146
+ )
147
+ return chat_completion
148
+
149
+ def text_stream(stream):
150
+ for chunk in stream:
151
+ if chunk["content"] is not None:
152
+ yield chunk["content"]
153
+
154
+ def get_stream_warning_error(stream):
155
+ error = None
156
+ warning = None
157
+ for chunk in stream:
158
+ if chunk["error"] is not None:
159
+ error = chunk["error"]
160
+ if chunk["warning"] is not None:
161
+ warning = chunk["warning"]
162
+ return warning, error
163
+
164
+ def write_response():
165
+ stream = chat_completion(st.session_state["messages"])
166
+ content_stream, error_stream = tee(stream)
167
+ response = st.write_stream(text_stream(content_stream))
168
+ stream_warning, stream_error = get_stream_warning_error(error_stream)
169
+ if stream_warning is not None:
170
+ st.warning(stream_warning,icon="⚠️")
171
+ if stream_error is not None:
172
+ st.error(stream_error,icon="🚨")
173
+ # if there was an error, a list will be returned instead of a string: https://docs.streamlit.io/library/api-reference/write-magic/st.write_stream
174
+ if isinstance(response, list):
175
+ response = None
176
+ return response, stream_warning, stream_error
177
+
178
+ def chat_completion(messages):
179
+ history_openai_format = [
180
+ {"role": "system", "content": get_system_prompt()}
181
+ ]
182
+
183
+ history_openai_format = history_openai_format + messages
184
+ if (len(history_openai_format)-1)//2 >= MAX_CHAT_TURNS:
185
+ yield {"content": None, "error": MSG_MAX_TURNS_EXCEEDED, "warning": None}
186
+ return
187
+
188
+ chat_completion = None
189
+ error = None
190
+ # wait to be in queue
191
+ with global_semaphore:
192
+ try:
193
+ chat_completion = chat_api_call(history_openai_format)
194
+ except Exception as e:
195
+ error = e
196
+ if error is not None:
197
+ yield {"content": None, "error": GENERAL_ERROR_MSG, "warning": None}
198
+ return
199
+
200
+ max_token_warning = None
201
+ partial_message = ""
202
+ chunk_counter = 0
203
+ for chunk in chat_completion:
204
+ if chunk.choices[0].delta.content is not None:
205
+ chunk_counter += 1
206
+ partial_message += chunk.choices[0].delta.content
207
+ if chunk_counter % TOKEN_CHUNK_SIZE == 0:
208
+ chunk_counter = 0
209
+ yield {"content": partial_message, "error": None, "warning": None}
210
+ partial_message = ""
211
+ if chunk.choices[0].finish_reason == "length":
212
+ max_token_warning = MSG_CLIPPED_AT_MAX_OUT_TOKENS
213
+
214
+ yield {"content": partial_message, "error": None, "warning": max_token_warning}
215
+
216
+ # if assistant is the last message, we need to prompt the user
217
+ # if user is the last message, we need to retry the assistant.
218
+ history = st.container(height=450)
219
+ with history:
220
+ for message in st.session_state["messages"]:
221
+ avatar = None
222
+ if message["role"] == "assistant":
223
+ avatar = MODEL_AVATAR_URL
224
+ with st.chat_message(message["role"],avatar=avatar):
225
+ if message["content"] is not None:
226
+ st.markdown(message["content"])
227
+ if message["error"] is not None:
228
+ st.error(message["error"],icon="🚨")
229
+ if message["warning"] is not None:
230
+ st.warning(message["warning"],icon="⚠️")
231
+
232
+ def handle_user_input(user_input):
233
+ with history:
234
+ response, stream_warning, stream_error = [None, None, None]
235
+ if last_role_is_user():
236
+ # retry the assistant if the user tries to send a new message
237
+ with st.chat_message("assistant", avatar=MODEL_AVATAR_URL):
238
+ response, stream_warning, stream_error = write_response()
239
+ else:
240
+ st.session_state["messages"].append({"role": "user", "content": user_input, "warning": None,"error": None})
241
+ with st.chat_message("user"):
242
+ st.markdown(user_input)
243
+ stream = chat_completion(st.session_state["messages"])
244
+ with st.chat_message("assistant", avatar=MODEL_AVATAR_URL):
245
+ response, stream_warning, stream_error = write_response()
246
+
247
+ st.session_state["messages"].append({"role": "assistant", "content": response, "warning": stream_warning,"error": stream_error})
248
+ print(st.session_state["messages"])
249
+
250
+ if prompt := st.chat_input("Type a message!", max_chars=1000):
251
+ handle_user_input(prompt)
252
+
253
+ with st.sidebar:
254
+ with st.container():
255
+ st.title("Examples")
256
+ for prompt in EXAMPLE_PROMPTS:
257
+ st.button(prompt, args=(prompt,), on_click=handle_user_input)
icon.png ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ tenacity
style.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .st-emotion-cache-1tpusnk a{
2
+ color: #FF5F46;
3
+ }