CassiopeiaCode commited on
Commit
f05f74b
·
1 Parent(s): 8260bce

优化流式响应:等待首个真实内容后再返回200

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -368,19 +368,22 @@ def chat_completions(req: ChatCompletionRequest, account: Dict[str, Any] = Depen
368
 
369
  def event_gen() -> Generator[str, None, None]:
370
  tracker = None
 
371
  try:
372
- yield _sse_format({
373
- "id": stream_id,
374
- "object": "chat.completion.chunk",
375
- "created": created,
376
- "model": model_used,
377
- "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
378
- })
379
  _, it, tracker = _send_upstream(stream=True)
380
  assert it is not None
381
  for piece in it:
382
  if not piece:
383
  continue
 
 
 
 
 
 
 
 
 
384
  yield _sse_format({
385
  "id": stream_id,
386
  "object": "chat.completion.chunk",
 
368
 
369
  def event_gen() -> Generator[str, None, None]:
370
  tracker = None
371
+ first_chunk = True
372
  try:
 
 
 
 
 
 
 
373
  _, it, tracker = _send_upstream(stream=True)
374
  assert it is not None
375
  for piece in it:
376
  if not piece:
377
  continue
378
+ if first_chunk:
379
+ yield _sse_format({
380
+ "id": stream_id,
381
+ "object": "chat.completion.chunk",
382
+ "created": created,
383
+ "model": model_used,
384
+ "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
385
+ })
386
+ first_chunk = False
387
  yield _sse_format({
388
  "id": stream_id,
389
  "object": "chat.completion.chunk",