ndurner commited on
Commit
44f8085
·
1 Parent(s): dabcf29

streaming support

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -195,11 +195,11 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
195
 
196
  history_openai_format.append({"role": "assistant", "content": assi})
197
 
198
- if message['text']:
199
- user_msg_parts.append({"type": "text", "text": message['text']})
200
- if message['files']:
201
- for file in message['files']:
202
- user_msg_parts.extend(encode_file(file['path']))
203
  history_openai_format.append({"role": "user", "content": user_msg_parts})
204
  user_msg_parts = []
205
 
@@ -211,13 +211,28 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
211
  messages= history_openai_format,
212
  temperature=temperature,
213
  seed=seed_i,
214
- max_tokens=max_tokens
 
 
215
  )
216
 
217
- if log_to_console:
218
- print(f"br_response: {str(response)}")
 
 
 
 
 
 
 
 
 
219
 
220
- result = response.choices[0].message.content
 
 
 
 
221
 
222
  if log_to_console:
223
  print(f"br_result: {str(history)}")
@@ -225,8 +240,6 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
225
  except Exception as e:
226
  raise gr.Error(f"Error: {str(e)}")
227
 
228
- return result
229
-
230
  def import_history(history, file):
231
  with open(file.name, mode="rb") as f:
232
  content = f.read()
 
195
 
196
  history_openai_format.append({"role": "assistant", "content": assi})
197
 
198
+ if message.text:
199
+ user_msg_parts.append({"type": "text", "text": message.text})
200
+ if message.files:
201
+ for file in message.files:
202
+ user_msg_parts.extend(encode_file(file.path))
203
  history_openai_format.append({"role": "user", "content": user_msg_parts})
204
  user_msg_parts = []
205
 
 
211
  messages= history_openai_format,
212
  temperature=temperature,
213
  seed=seed_i,
214
+ max_tokens=max_tokens,
215
+ stream=True,
216
+ stream_options={"include_usage": True}
217
  )
218
 
219
+ partial_response=""
220
+ for chunk in response:
221
+ if chunk.choices:
222
+ txt = ""
223
+ for choice in chunk.choices:
224
+ cont = choice.delta.content
225
+ if cont:
226
+ txt += cont
227
+
228
+ if log_to_console:
229
+ print(f"br_response: {txt}")
230
 
231
+ partial_response += txt
232
+ yield partial_response
233
+
234
+ if chunk.usage and log_to_console:
235
+ print(f"usage: {chunk.usage}")
236
 
237
  if log_to_console:
238
  print(f"br_result: {str(history)}")
 
240
  except Exception as e:
241
  raise gr.Error(f"Error: {str(e)}")
242
 
 
 
243
  def import_history(history, file):
244
  with open(file.name, mode="rb") as f:
245
  content = f.read()