Skip to content

Commit

Permalink
limit the system context length of conversation messages. (#962)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

#951 

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
  • Loading branch information
KevinHuSh authored May 29, 2024
1 parent 495a643 commit daa4799
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions api/db/services/dialog_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ def chat(dialog, messages, stream=True, **kwargs):

kwargs["knowledge"] = "\n".join(knowledges)
gen_conf = dialog.llm_setting
msg = [{"role": m["role"], "content": m["content"]}
for m in messages if m["role"] != "system"]

msg = [{"role": "system", "content": prompt_config["system"].format(**kwargs)}]
msg.extend([{"role": m["role"], "content": m["content"]}
for m in messages if m["role"] != "system"])
used_token_count, msg = message_fit_in(msg, int(max_tokens * 0.97))
assert len(msg) >= 2, f"message_fit_in has bug: {msg}"

if "max_tokens" in gen_conf:
gen_conf["max_tokens"] = min(
gen_conf["max_tokens"],
Expand Down Expand Up @@ -165,14 +169,13 @@ def decorate_answer(answer):

if stream:
answer = ""
for ans in chat_mdl.chat_streamly(prompt_config["system"].format(**kwargs), msg, gen_conf):
for ans in chat_mdl.chat_streamly(msg[0]["content"], msg[1:], gen_conf):
answer = ans
yield {"answer": answer, "reference": {}}
yield decorate_answer(answer)
else:
answer = chat_mdl.chat(
prompt_config["system"].format(
**kwargs), msg, gen_conf)
msg[0]["content"], msg[1:], gen_conf)
chat_logger.info("User: {}|Assistant: {}".format(
msg[-1]["content"], answer))
yield decorate_answer(answer)
Expand Down

0 comments on commit daa4799

Please sign in to comment.