KevinHuSh commited on
Commit
3110924
·
1 Parent(s): e3322d7

limit the system context length of conversation messages. (#962)

Browse files

### What problem does this PR solve?

#951

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Files changed (1) hide show
  1. api/db/services/dialog_service.py +8 -5
api/db/services/dialog_service.py CHANGED
@@ -130,9 +130,13 @@ def chat(dialog, messages, stream=True, **kwargs):
130
 
131
  kwargs["knowledge"] = "\n".join(knowledges)
132
  gen_conf = dialog.llm_setting
133
- msg = [{"role": m["role"], "content": m["content"]}
134
- for m in messages if m["role"] != "system"]
 
 
135
  used_token_count, msg = message_fit_in(msg, int(max_tokens * 0.97))
 
 
136
  if "max_tokens" in gen_conf:
137
  gen_conf["max_tokens"] = min(
138
  gen_conf["max_tokens"],
@@ -165,14 +169,13 @@ def chat(dialog, messages, stream=True, **kwargs):
165
 
166
  if stream:
167
  answer = ""
168
- for ans in chat_mdl.chat_streamly(prompt_config["system"].format(**kwargs), msg, gen_conf):
169
  answer = ans
170
  yield {"answer": answer, "reference": {}}
171
  yield decorate_answer(answer)
172
  else:
173
  answer = chat_mdl.chat(
174
- prompt_config["system"].format(
175
- **kwargs), msg, gen_conf)
176
  chat_logger.info("User: {}|Assistant: {}".format(
177
  msg[-1]["content"], answer))
178
  yield decorate_answer(answer)
 
130
 
131
  kwargs["knowledge"] = "\n".join(knowledges)
132
  gen_conf = dialog.llm_setting
133
+
134
+ msg = [{"role": "system", "content": prompt_config["system"].format(**kwargs)}]
135
+ msg.extend([{"role": m["role"], "content": m["content"]}
136
+ for m in messages if m["role"] != "system"])
137
  used_token_count, msg = message_fit_in(msg, int(max_tokens * 0.97))
138
+ assert len(msg) >= 2, f"message_fit_in has bug: {msg}"
139
+
140
  if "max_tokens" in gen_conf:
141
  gen_conf["max_tokens"] = min(
142
  gen_conf["max_tokens"],
 
169
 
170
  if stream:
171
  answer = ""
172
+ for ans in chat_mdl.chat_streamly(msg[0]["content"], msg[1:], gen_conf):
173
  answer = ans
174
  yield {"answer": answer, "reference": {}}
175
  yield decorate_answer(answer)
176
  else:
177
  answer = chat_mdl.chat(
178
+ msg[0]["content"], msg[1:], gen_conf)
 
179
  chat_logger.info("User: {}|Assistant: {}".format(
180
  msg[-1]["content"], answer))
181
  yield decorate_answer(answer)