openfree commited on
Commit
566e2ba
1 Parent(s): 2b6f16a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -132,16 +132,21 @@ Please provide detailed analysis from these perspectives:
132
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
133
 
134
  # Convert history to message format
135
- for h in history:
136
- messages.append({"role": "user", "content": h[0]})
137
- if h[1]:
138
- messages.append({"role": "assistant", "content": h[1]})
139
-
 
 
 
 
140
  messages.append({"role": "user", "content": message})
141
 
142
  try:
143
  client = get_client(model_name)
144
  partial_message = ""
 
145
 
146
  for msg in client.chat_completion(
147
  messages,
@@ -153,18 +158,19 @@ Please provide detailed analysis from these perspectives:
153
  token = msg.choices[0].delta.get('content', None)
154
  if token:
155
  partial_message += token
156
- new_history = history + [[message, partial_message]]
157
- formatted_history = []
158
- for h in new_history:
159
- formatted_history.append({"role": "user", "content": h[0]})
160
- if h[1]:
161
- formatted_history.append({"role": "assistant", "content": h[1]})
162
- yield "", formatted_history
163
 
164
  except Exception as e:
165
  error_msg = f"❌ Inference error: {str(e)}"
166
- formatted_history = history + [[message, error_msg]]
167
- yield "", [{"role": "user", "content": h[0], "role": "assistant", "content": h[1]} for h in formatted_history]
 
 
 
168
 
169
  css = """
170
  footer {visibility: hidden}
 
132
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
133
 
134
  # Convert history to message format
135
+ if history is not None:
136
+ for item in history:
137
+ if isinstance(item, dict):
138
+ messages.append(item)
139
+ elif isinstance(item, (list, tuple)) and len(item) == 2:
140
+ messages.append({"role": "user", "content": item[0]})
141
+ if item[1]:
142
+ messages.append({"role": "assistant", "content": item[1]})
143
+
144
  messages.append({"role": "user", "content": message})
145
 
146
  try:
147
  client = get_client(model_name)
148
  partial_message = ""
149
+ current_history = []
150
 
151
  for msg in client.chat_completion(
152
  messages,
 
158
  token = msg.choices[0].delta.get('content', None)
159
  if token:
160
  partial_message += token
161
+ current_history = [
162
+ {"role": "user", "content": message},
163
+ {"role": "assistant", "content": partial_message}
164
+ ]
165
+ yield "", current_history
 
 
166
 
167
  except Exception as e:
168
  error_msg = f"❌ Inference error: {str(e)}"
169
+ error_history = [
170
+ {"role": "user", "content": message},
171
+ {"role": "assistant", "content": error_msg}
172
+ ]
173
+ yield "", error_history
174
 
175
  css = """
176
  footer {visibility: hidden}