asthaa30 commited on
Commit
294c6d6
·
verified ·
1 Parent(s): bbe4abb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -33,39 +33,41 @@ def respond(
33
  temperature=temperature,
34
  top_p=top_p,
35
  ):
36
- if isinstance(message, ChatCompletionStreamOutput):
37
- # Extract the content from the ChatCompletionStreamOutput object
38
- content = message.choices[0].delta.content
39
- if content is not None:
40
- response += content
41
- yield response
42
- # Check if this is the last message in the stream
43
- if message.choices[0].finish_reason == 'stop':
44
- break
45
- elif isinstance(message, dict):
46
- # Handle dictionary responses (possible API format change)
47
- content = message.get('choices', [{}])[0].get('delta', {}).get('content')
48
- if content:
49
- response += content
50
- yield response
51
- if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
52
- break
53
- elif isinstance(message, str):
54
- # Handle string responses (possible raw content)
55
- response += message
56
- yield response
57
- else:
58
- print(f"Unexpected message type: {type(message)}")
59
- print(f"Message content: {message}")
 
 
60
 
61
  # Final yield to ensure all content is returned
62
  if response:
63
  yield response
64
 
65
  except Exception as e:
66
- print(f"An error occurred: {e}")
67
  if response:
68
- yield response + f"\n\nAn error occurred: {e}"
69
  else:
70
  yield f"An error occurred: {e}"
71
 
 
33
  temperature=temperature,
34
  top_p=top_p,
35
  ):
36
+ try:
37
+ if isinstance(message, ChatCompletionStreamOutput):
38
+ content = message.choices[0].delta.content
39
+ if content is not None:
40
+ response += content
41
+ yield response
42
+ if message.choices[0].finish_reason == 'stop':
43
+ break
44
+ elif isinstance(message, dict):
45
+ content = message.get('choices', [{}])[0].get('delta', {}).get('content')
46
+ if content:
47
+ response += content
48
+ yield response
49
+ if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
50
+ break
51
+ elif isinstance(message, str):
52
+ if message.strip(): # Only process non-empty strings
53
+ response += message
54
+ yield response
55
+ else:
56
+ print(f"Unexpected message type: {type(message)}")
57
+ print(f"Message content: {message}")
58
+ except Exception as e:
59
+ print(f"Error processing message: {e}")
60
+ print(f"Problematic message: {message}")
61
+ continue # Continue to the next message even if there's an error
62
 
63
  # Final yield to ensure all content is returned
64
  if response:
65
  yield response
66
 
67
  except Exception as e:
68
+ print(f"An error occurred in the main loop: {e}")
69
  if response:
70
+ yield response
71
  else:
72
  yield f"An error occurred: {e}"
73