Kevin Hu commited on
Commit
684f1d7
·
1 Parent(s): 58ecd6d

refine reteival of multi-turn conversation (#2520)

Browse files

### What problem does this PR solve?

#2362 #2484

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
- [x] Performance Improvement

Files changed (1) hide show
  1. api/db/services/dialog_service.py +57 -0
api/db/services/dialog_service.py CHANGED
@@ -149,6 +149,11 @@ def chat(dialog, messages, stream=True, **kwargs):
149
  prompt_config["system"] = prompt_config["system"].replace(
150
  "{%s}" % p["key"], " ")
151
 
 
 
 
 
 
152
  rerank_mdl = None
153
  if dialog.rerank_id:
154
  rerank_mdl = LLMBundle(dialog.tenant_id, LLMType.RERANK, dialog.rerank_id)
@@ -410,6 +415,58 @@ def rewrite(tenant_id, llm_id, question):
410
  return ans
411
 
412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  def tts(tts_mdl, text):
414
  if not tts_mdl or not text: return
415
  bin = b""
 
149
  prompt_config["system"] = prompt_config["system"].replace(
150
  "{%s}" % p["key"], " ")
151
 
152
+ if len(questions) > 1 and prompt_config.get("refine_multiturn"):
153
+ questions = [full_question(dialog.tenant_id, dialog.llm_id, messages)]
154
+ else:
155
+ questions = questions[-1:]
156
+
157
  rerank_mdl = None
158
  if dialog.rerank_id:
159
  rerank_mdl = LLMBundle(dialog.tenant_id, LLMType.RERANK, dialog.rerank_id)
 
415
  return ans
416
 
417
 
418
+ def full_question(tenant_id, llm_id, messages):
419
+ if llm_id2llm_type(llm_id) == "image2text":
420
+ chat_mdl = LLMBundle(tenant_id, LLMType.IMAGE2TEXT, llm_id)
421
+ else:
422
+ chat_mdl = LLMBundle(tenant_id, LLMType.CHAT, llm_id)
423
+ conv = []
424
+ for m in messages:
425
+ if m["role"] not in ["user", "assistant"]: continue
426
+ conv.append("{}: {}".format(m["role"].upper(), m["content"]))
427
+ conv = "\n".join(conv)
428
+ prompt = f"""
429
+ Role: A helpful assistant
430
+ Task: Generate a full user question that would follow the conversation.
431
+ Requirements & Restrictions:
432
+ - Text generated MUST be in the same language of the original user's question.
433
+ - If the user's latest question is completely, don't do anything, just return the original question.
434
+ - DON'T generate anything except a refined question.
435
+
436
+ ######################
437
+ -Examples-
438
+ ######################
439
+
440
+ # Example 1
441
+ ## Conversation
442
+ USER: What is the name of Donald Trump's father?
443
+ ASSISTANT: Fred Trump.
444
+ USER: And his mother?
445
+ ###############
446
+ Output: What's the name of Donald Trump's mother?
447
+
448
+ ------------
449
+ # Example 2
450
+ ## Conversation
451
+ USER: What is the name of Donald Trump's father?
452
+ ASSISTANT: Fred Trump.
453
+ USER: And his mother?
454
+ ASSISTANT: Mary Trump.
455
+ User: What's her full name?
456
+ ###############
457
+ Output: What's the full name of Donald Trump's mother Mary Trump?
458
+
459
+ ######################
460
+
461
+ # Real Data
462
+ ## Conversation
463
+ {conv}
464
+ ###############
465
+ """
466
+ ans = chat_mdl.chat(prompt, [{"role": "user", "content": "Output: "}], {"temperature": 0.2})
467
+ return ans if ans.find("**ERROR**") < 0 else messages[-1]["content"]
468
+
469
+
470
  def tts(tts_mdl, text):
471
  if not tts_mdl or not text: return
472
  bin = b""