Skip to content

Commit

Permalink
fix bug of chat without stream (infiniflow#830)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
  • Loading branch information
KevinHuSh committed May 17, 2024
1 parent 2bfacd0 commit 673a28e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
11 changes: 7 additions & 4 deletions api/apps/api_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,13 @@ def stream():
resp.headers.add_header("Content-Type", "text/event-stream; charset=utf-8")
return resp
else:
ans = chat(dia, msg, **req)
fillin_conv(ans)
API4ConversationService.append_message(conv.id, conv.to_dict())
return get_json_result(data=ans)
answer = None
for ans in chat(dia, msg, **req):
answer = ans
fillin_conv(ans)
API4ConversationService.append_message(conv.id, conv.to_dict())
break
return get_json_result(data=answer)

except Exception as e:
return server_error_response(e)
Expand Down
11 changes: 7 additions & 4 deletions api/apps/conversation_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,13 @@ def stream():
return resp

else:
ans = chat(dia, msg, **req)
fillin_conv(ans)
ConversationService.update_by_id(conv.id, conv.to_dict())
return get_json_result(data=ans)
answer = None
for ans in chat(dia, msg, **req):
answer = ans
fillin_conv(ans)
ConversationService.update_by_id(conv.id, conv.to_dict())
break
return get_json_result(data=answer)
except Exception as e:
return server_error_response(e)

8 changes: 3 additions & 5 deletions api/db/services/dialog_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def chat(dialog, messages, stream=True, **kwargs):
kbs = KnowledgebaseService.get_by_ids(dialog.kb_ids)
embd_nms = list(set([kb.embd_id for kb in kbs]))
if len(embd_nms) != 1:
if stream:
yield {"answer": "**ERROR**: Knowledge bases use different embedding models.", "reference": []}
yield {"answer": "**ERROR**: Knowledge bases use different embedding models.", "reference": []}
return {"answer": "**ERROR**: Knowledge bases use different embedding models.", "reference": []}

questions = [m["content"] for m in messages if m["role"] == "user"]
Expand Down Expand Up @@ -126,8 +125,7 @@ def chat(dialog, messages, stream=True, **kwargs):
"{}->{}".format(" ".join(questions), "\n->".join(knowledges)))

if not knowledges and prompt_config.get("empty_response"):
if stream:
yield {"answer": prompt_config["empty_response"], "reference": kbinfos}
yield {"answer": prompt_config["empty_response"], "reference": kbinfos}
return {"answer": prompt_config["empty_response"], "reference": kbinfos}

kwargs["knowledge"] = "\n".join(knowledges)
Expand Down Expand Up @@ -177,7 +175,7 @@ def decorate_answer(answer):
**kwargs), msg, gen_conf)
chat_logger.info("User: {}|Assistant: {}".format(
msg[-1]["content"], answer))
return decorate_answer(answer)
yield decorate_answer(answer)


def use_sql(question, field_map, tenant_id, chat_mdl, quota=True):
Expand Down

0 comments on commit 673a28e

Please sign in to comment.