pablocst commited on
Commit
3e6c9c3
·
1 Parent(s): 2a020c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -45,14 +45,20 @@ def process_response(response, history):
45
  partial_words = ""
46
  for chunk in response.iter_lines():
47
  if chunk:
48
- chunk_data = json.loads(chunk[6:])['choices'][0]['delta']
49
- if 'content' in chunk_data:
50
- partial_words += chunk_data['content']
51
- if token_counter == 0:
52
- history.append(" " + partial_words)
53
- else:
54
- history[-1] = partial_words
55
- token_counter += 1
 
 
 
 
 
 
56
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
57
  return chat, history, token_counter
58
 
 
45
  partial_words = ""
46
  for chunk in response.iter_lines():
47
  if chunk:
48
+ try:
49
+ chunk_json = json.loads(chunk.decode('utf-8'))
50
+ if 'choices' in chunk_json and len(chunk_json['choices']) > 0:
51
+ chunk_data = chunk_json['choices'][0].get('delta', {})
52
+ if 'content' in chunk_data:
53
+ partial_words += chunk_data['content']
54
+ if token_counter == 0:
55
+ history.append(" " + partial_words)
56
+ else:
57
+ history[-1] = partial_words
58
+ token_counter += 1
59
+ except json.JSONDecodeError:
60
+ print("Error decoding JSON response")
61
+ break # or handle the error as you see fit
62
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
63
  return chat, history, token_counter
64