inteligenciamilgrau commited on
Commit
9e113d4
·
verified ·
1 Parent(s): e62890c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -20
app.py CHANGED
@@ -28,27 +28,47 @@ def respond(
28
  response = ""
29
 
30
 
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- print("MESSAGE", message)
39
- # Check if the message is in the expected format
40
- if not message or not isinstance(message, dict):
41
- continue
42
-
43
- # Extract token and handle potential errors in parsing
44
- try:
45
- token = message.choices[0].delta.content
46
 
47
- response += token
48
- yield response
49
- except (AttributeError, IndexError, KeyError) as e:
50
- print(f"Error processing message: {e}")
51
- continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
 
54
  """
 
28
  response = ""
29
 
30
 
31
+ try:
32
+ for message in client.chat_completion(
33
+ messages,
34
+ max_tokens=max_tokens,
35
+ stream=True,
36
+ temperature=temperature,
37
+ top_p=top_p,
38
+ ):
39
+ # Ensure the message has a valid structure
40
+ if not message or not isinstance(message, dict):
41
+ continue
 
 
 
 
42
 
43
+ try:
44
+ # Extract content and finish reason
45
+ content = message.choices[0].delta.content
46
+ finish_reason = message.choices[0].finish_reason
47
+
48
+ # Check if the content is empty
49
+ if content.strip() == "":
50
+ # If the finish reason is 'stop', it's expected and we can break the loop
51
+ if finish_reason == "stop":
52
+ print("Stream ended normally.")
53
+ break
54
+ else:
55
+ print("Received unexpected empty content, skipping...")
56
+ continue
57
+
58
+ response += content
59
+ yield response
60
+
61
+ except (AttributeError, IndexError, KeyError) as e:
62
+ print(f"Error processing message: {e}")
63
+ continue
64
+
65
+ except Exception as e:
66
+ print(f"Unexpected error: {e}")
67
+ yield "An error occurred while generating the response."
68
+
69
+ # Final check if the response is empty
70
+ if response.strip() == "":
71
+ yield "No response generated. Please try again or adjust the settings."
72
 
73
 
74
  """