From 352867dc718649d8a3b94bb77f588b5e16ff08c5 Mon Sep 17 00:00:00 2001 From: Alpha Hinex Date: Mon, 6 Nov 2023 15:13:55 +0800 Subject: [PATCH] Fix 'InvalidParameter:User and assistant need to appear alternately in the message' issue --- pilot/model/proxy/llms/tongyi.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pilot/model/proxy/llms/tongyi.py b/pilot/model/proxy/llms/tongyi.py index fb826e49c..13031e96f 100644 --- a/pilot/model/proxy/llms/tongyi.py +++ b/pilot/model/proxy/llms/tongyi.py @@ -36,7 +36,7 @@ def tongyi_generate_stream( if message.role == ModelMessageRoleType.HUMAN: history.append({"role": "user", "content": message.content}) for message in messages: - if message.role == ModelMessageRoleType.SYSTEM: + if message.role == ModelMessageRoleType.SYSTEM or message.role == ModelMessageRoleType.HUMAN: history.append({"role": "user", "content": message.content}) # elif message.role == ModelMessageRoleType.HUMAN: # history.append({"role": "user", "content": message.content}) @@ -45,17 +45,24 @@ def tongyi_generate_stream( else: pass - # temp_his = history[::-1] - temp_his = history + temp_his = history[::-1] last_user_input = None for m in temp_his: if m["role"] == "user": last_user_input = m break - if last_user_input: + temp_his = history + prompt_input = None + for m in temp_his: + if m["role"] == "user": + prompt_input = m + break + + if last_user_input and prompt_input and last_user_input != prompt_input: history.remove(last_user_input) - history.append(last_user_input) + history.remove(prompt_input) + history.append(prompt_input) gen = Generation() res = gen.call(