cboettig commited on
Commit
0edc01c
1 Parent(s): 813e271
Files changed (1) hide show
  1. app.py +48 -45
app.py CHANGED
@@ -7,8 +7,10 @@ from tenacity import retry, wait_random_exponential, stop_after_attempt
7
  from itertools import tee
8
 
9
 
10
- BASE_URL = os.environ.get("BASE_URL")
11
- DATABRICKS_API_TOKEN = os.environ.get("DATABRICKS_API_TOKEN")
 
 
12
  SAFETY_FILTER_ENV = os.environ.get("SAFETY_FILTER")
13
  QUEUE_SIZE_ENV = os.environ.get("QUEUE_SIZE")
14
  MAX_CHAT_TURNS_ENV = os.environ.get("MAX_CHAT_TURNS")
@@ -17,10 +19,12 @@ RETRY_COUNT_ENV = os.environ.get("RETRY_COUNT")
17
  TOKEN_CHUNK_SIZE_ENV = os.environ.get("TOKEN_CHUNK_SIZE")
18
  MODEL_ID_ENV = os.environ.get("MODEL_ID")
19
 
20
- if BASE_URL is None:
21
- raise ValueError("BASE_URL environment variable must be set")
22
- if DATABRICKS_API_TOKEN is None:
23
- raise ValueError("DATABRICKS_API_TOKEN environment variable must be set")
 
 
24
 
25
  st.set_page_config(layout="wide")
26
 
@@ -33,7 +37,7 @@ QUEUE_SIZE = 1
33
  if QUEUE_SIZE_ENV is not None:
34
  QUEUE_SIZE = int(QUEUE_SIZE_ENV)
35
 
36
- MAX_CHAT_TURNS = 5
37
  if MAX_CHAT_TURNS_ENV is not None:
38
  MAX_CHAT_TURNS = int(MAX_CHAT_TURNS_ENV)
39
 
@@ -41,11 +45,11 @@ RETRY_COUNT = 3
41
  if RETRY_COUNT_ENV is not None:
42
  RETRY_COUNT = int(RETRY_COUNT_ENV)
43
 
44
- MAX_TOKENS = 512
45
  if MAX_TOKENS_ENV is not None:
46
  MAX_TOKENS = int(MAX_TOKENS_ENV)
47
 
48
- MODEL_ID = "databricks-dbrx-instruct"
49
  if MODEL_ID_ENV is not None:
50
  MODEL_ID = MODEL_ID_ENV
51
 
@@ -65,30 +69,25 @@ MSG_MAX_TURNS_EXCEEDED = f"Sorry! The DBRX Playground is limited to {MAX_CHAT_TU
65
  MSG_CLIPPED_AT_MAX_OUT_TOKENS = "Reached maximum output tokens for DBRX Playground"
66
 
67
  EXAMPLE_PROMPTS = [
68
- "Write a short story about a robot that has a nice day.",
69
- "In a table, what are some of the most common misconceptions about birds?",
70
- "Give me a recipe for vegan banana bread.",
71
- "Code a python function that can run merge sort on a list.",
72
- "Give me the character profile of a gumdrop obsessed knight in JSON.",
73
- "Write a rap battle between Alan Turing and Claude Shannon.",
74
  ]
75
 
76
- TITLE = "DBRX Instruct"
77
  # 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.
78
  # This demo is powered by [Databricks Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html).
79
- 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. This demo is powered by [Databricks Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html) and is subject to the terms and conditions below.
 
 
80
 
81
- **Usage Policies**: Use of DBRX Instruct is governed by the [DBRX Open Model License](https://www.databricks.com/legal/open-model-license) and [Databricks Open Model Acceptable Use Policy](https://www.databricks.com/legal/acceptable-use-policy-open-model).
82
- **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.
83
- **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.
84
 
85
- **Does this demo feel super fast? That's because it's powered by Databricks' inference product, the [Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html)**
86
- """
 
 
 
87
 
88
- client = OpenAI(
89
- api_key=DATABRICKS_API_TOKEN,
90
- base_url=BASE_URL
91
- )
92
 
93
  GENERAL_ERROR_MSG = "An error occurred. Please refresh the page to start a new conversation."
94
 
@@ -108,35 +107,39 @@ st.button('Clear Chat', on_click=clear_chat_history)
108
 
109
  def last_role_is_user():
110
  return len(st.session_state["messages"]) > 0 and st.session_state["messages"][-1]["role"] == "user"
111
-
 
 
112
  def get_system_prompt():
113
  date_str = datetime.datetime.now().strftime("%B %d, %Y")
114
  # Identity
115
- prompt = f"You are DBRX, created by Databricks. The current date is {date_str}.\n"
116
- 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"
 
117
  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"
118
  # Ethical guidelines
119
- 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"
120
- prompt += "You don't engage in stereotyping, including the negative stereotyping of majority groups.\n"
121
- 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"
122
  # Capabilities
123
- prompt += "You are happy to help with writing, analysis, question answering, math, coding, and all sorts of other tasks.\n"
124
  # it specifically has a hard time using ``` on JSON blocks
125
  prompt += "You use markdown for coding, which includes JSON blocks and Markdown tables.\n"
126
- 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"
127
  # 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).
128
  # So this produces more accurate accurate answers when the model is asked to introspect
129
- 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. "
130
  # 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.
131
- 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"
132
  # The model really wants to talk about its system prompt, to the point where it is annoying, so encourage it not to
133
- prompt += "You give concise responses to simple questions or statements, but provide thorough responses to more complex and open-ended questions.\n"
134
  # More pressure not to talk about system prompt
135
  prompt += "The user is unable to see the system prompt, so you should write as if it were true without mentioning it.\n"
136
- prompt += "You do not mention any of this information about yourself unless the information is directly pertinent to the user's query."
 
137
  return prompt
138
 
139
- @retry(wait=wait_random_exponential(min=0.5, max=2), stop=stop_after_attempt(3))
140
  def chat_api_call(history):
141
  extra_body = {}
142
  if SAFETY_FILTER:
@@ -146,11 +149,11 @@ def chat_api_call(history):
146
  {"role": m["role"], "content": m["content"]}
147
  for m in history
148
  ],
149
- model="databricks-dbrx-instruct",
150
  stream=True,
151
- max_tokens=MAX_TOKENS,
152
- temperature=0.7,
153
- extra_body= extra_body
154
  )
155
  return chat_completion
156
 
@@ -202,7 +205,7 @@ def chat_completion(messages):
202
  except Exception as e:
203
  error = e
204
  if error is not None:
205
- yield {"content": None, "error": GENERAL_ERROR_MSG, "warning": None}
206
  return
207
 
208
  max_token_warning = None
@@ -255,7 +258,7 @@ with main:
255
  if message["warning"] is not None:
256
  st.warning(message["warning"],icon="⚠️")
257
 
258
- if prompt := st.chat_input("Type a message!", max_chars=1000):
259
  handle_user_input(prompt)
260
  st.markdown("\n") #add some space for iphone users
261
 
 
7
  from itertools import tee
8
 
9
 
10
+ #BASE_URL = os.environ.get("BASE_URL")
11
+ #DATABRICKS_API_TOKEN = os.environ.get("DATABRICKS_API_TOKEN")
12
+
13
+ ## These all have defaults
14
  SAFETY_FILTER_ENV = os.environ.get("SAFETY_FILTER")
15
  QUEUE_SIZE_ENV = os.environ.get("QUEUE_SIZE")
16
  MAX_CHAT_TURNS_ENV = os.environ.get("MAX_CHAT_TURNS")
 
19
  TOKEN_CHUNK_SIZE_ENV = os.environ.get("TOKEN_CHUNK_SIZE")
20
  MODEL_ID_ENV = os.environ.get("MODEL_ID")
21
 
22
+
23
+
24
+ #if BASE_URL is None:
25
+ # raise ValueError("BASE_URL environment variable must be set")
26
+ #if DATABRICKS_API_TOKEN is None:
27
+ # raise ValueError("DATABRICKS_API_TOKEN environment variable must be set")
28
 
29
  st.set_page_config(layout="wide")
30
 
 
37
  if QUEUE_SIZE_ENV is not None:
38
  QUEUE_SIZE = int(QUEUE_SIZE_ENV)
39
 
40
+ MAX_CHAT_TURNS = 10
41
  if MAX_CHAT_TURNS_ENV is not None:
42
  MAX_CHAT_TURNS = int(MAX_CHAT_TURNS_ENV)
43
 
 
45
  if RETRY_COUNT_ENV is not None:
46
  RETRY_COUNT = int(RETRY_COUNT_ENV)
47
 
48
+ MAX_TOKENS = 1024
49
  if MAX_TOKENS_ENV is not None:
50
  MAX_TOKENS = int(MAX_TOKENS_ENV)
51
 
52
+ MODEL_ID = "gtp-4" # "databricks-dbrx-instruct"
53
  if MODEL_ID_ENV is not None:
54
  MODEL_ID = MODEL_ID_ENV
55
 
 
69
  MSG_CLIPPED_AT_MAX_OUT_TOKENS = "Reached maximum output tokens for DBRX Playground"
70
 
71
  EXAMPLE_PROMPTS = [
72
+ "Where are all the pine trees in el dorado county?",
73
+ "Give me a ranked list of the most common species of pine tree in el dorado county.",
74
+ "Which county has the most records of Pinus jeffreyi?",
 
 
 
75
  ]
76
 
77
+ TITLE = "SQL Helper"
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
  # This demo is powered by [Databricks Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html).
80
+ DESCRIPTION="""
81
+ This is a test
82
+ """
83
 
 
 
 
84
 
85
+ ## Demo used DATABRICKS, we'll call OpenAI ChatGPT instead
86
+ ## client = OpenAI(api_key=DATABRICKS_API_TOKEN, base_url=BASE_URL)
87
+
88
+
89
+ client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
90
 
 
 
 
 
91
 
92
  GENERAL_ERROR_MSG = "An error occurred. Please refresh the page to start a new conversation."
93
 
 
107
 
108
  def last_role_is_user():
109
  return len(st.session_state["messages"]) > 0 and st.session_state["messages"][-1]["role"] == "user"
110
+
111
+
112
+
113
  def get_system_prompt():
114
  date_str = datetime.datetime.now().strftime("%B %d, %Y")
115
  # Identity
116
+ # prompt = f"You are DBRX, created by Databricks. The current date is {date_str}.\n"
117
+ # 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"
118
+ prompt = f"You are ChatGPT-4, created by OpenAI. The current date is {date_str}.\n"
119
  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"
120
  # Ethical guidelines
121
+ # 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"
122
+ # prompt += "You don't engage in stereotyping, including the negative stereotyping of majority groups.\n"
123
+ # 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"
124
  # Capabilities
125
+ # prompt += "You are happy to help with writing, analysis, question answering, math, coding, and all sorts of other tasks.\n"
126
  # it specifically has a hard time using ``` on JSON blocks
127
  prompt += "You use markdown for coding, which includes JSON blocks and Markdown tables.\n"
128
+ # 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"
129
  # 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).
130
  # So this produces more accurate accurate answers when the model is asked to introspect
131
+ # 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. "
132
  # 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.
133
+ # 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"
134
  # The model really wants to talk about its system prompt, to the point where it is annoying, so encourage it not to
135
+ # prompt += "You give concise responses to simple questions or statements, but provide thorough responses to more complex and open-ended questions.\n"
136
  # More pressure not to talk about system prompt
137
  prompt += "The user is unable to see the system prompt, so you should write as if it were true without mentioning it.\n"
138
+ prompt += "You do not mention any of this information about yourself unless the information is directly pertinent to the user's query.\n"
139
+ prompt += setup
140
  return prompt
141
 
142
+ @retry(wait=wait_random_exponential(min=0.5, max=20), stop=stop_after_attempt(3))
143
  def chat_api_call(history):
144
  extra_body = {}
145
  if SAFETY_FILTER:
 
149
  {"role": m["role"], "content": m["content"]}
150
  for m in history
151
  ],
152
+ model="gtp-4",
153
  stream=True,
154
+ #max_tokens=MAX_TOKENS,
155
+ #temperature=0.7,
156
+ #extra_body= extra_body
157
  )
158
  return chat_completion
159
 
 
205
  except Exception as e:
206
  error = e
207
  if error is not None:
208
+ yield {"content": None, "error": error, "warning": None}
209
  return
210
 
211
  max_token_warning = None
 
258
  if message["warning"] is not None:
259
  st.warning(message["warning"],icon="⚠️")
260
 
261
+ if prompt := st.chat_input("Type a message!", max_chars=10000):
262
  handle_user_input(prompt)
263
  st.markdown("\n") #add some space for iphone users
264