leonsimon23 commited on
Commit
7a58836
·
verified ·
1 Parent(s): fe3c930

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -10
app.py CHANGED
@@ -26,15 +26,6 @@ class ChatBot:
26
  "Authorization": f"Bearer {API_KEY}",
27
  "Content-Type": "application/json"
28
  }
29
-
30
- def verify_api_config(self):
31
- try:
32
- response = requests.options(f"{BASE_URL}/v1/chat/completions", headers=self.headers, timeout=5)
33
- if response.status_code >= 400:
34
- logger.error(f"API configuration may be incorrect: {response.status_code}")
35
- logger.error(f"API response: {response.text[:200]}")
36
- except Exception as e:
37
- logger.error(f"Failed to connect to API: {str(e)}")
38
 
39
  def format_message(role, content):
40
  return {"role": role, "content": content}
@@ -85,7 +76,7 @@ def chat_stream(message, history):
85
  break
86
 
87
  chunk = json.loads(line)
88
- if chunk and "choices" in chunk:
89
  delta = chunk["choices"][0]["delta"]
90
  if "content" in delta:
91
  content = delta["content"]
@@ -93,6 +84,8 @@ def chat_stream(message, history):
93
  history.append((message, partial_message))
94
  yield history
95
  history.pop()
 
 
96
  except json.JSONDecodeError as e:
97
  logger.error(f"JSON parsing error: {e}")
98
  continue
 
26
  "Authorization": f"Bearer {API_KEY}",
27
  "Content-Type": "application/json"
28
  }
 
 
 
 
 
 
 
 
 
29
 
30
  def format_message(role, content):
31
  return {"role": role, "content": content}
 
76
  break
77
 
78
  chunk = json.loads(line)
79
+ if chunk and "choices" in chunk and len(chunk["choices"]) > 0:
80
  delta = chunk["choices"][0]["delta"]
81
  if "content" in delta:
82
  content = delta["content"]
 
84
  history.append((message, partial_message))
85
  yield history
86
  history.pop()
87
+ else:
88
+ logger.warning("Received empty or malformed choice from API.")
89
  except json.JSONDecodeError as e:
90
  logger.error(f"JSON parsing error: {e}")
91
  continue