Update OpenAI API to support text in mixed-mode data

This commit is contained in:
varon 2023-11-17 02:09:31 +02:00
parent 8da46278e1
commit 167f9b20fc

View file

@ -32,6 +32,15 @@ def is_present(json, key):
return False return False
return True return True
def extract_content_text(content):
if isinstance(content, str):
return content
if isinstance(content, list):
return ''.join(extract_content_text(item) for item in content)
else:
return content['text']
#convert chat to prompt #convert chat to prompt
def convert_chat(messages): def convert_chat(messages):
prompt = "" + args.chat_prompt.replace("\\n", "\n") prompt = "" + args.chat_prompt.replace("\\n", "\n")
@ -44,11 +53,11 @@ def convert_chat(messages):
for line in messages: for line in messages:
if (line["role"] == "system"): if (line["role"] == "system"):
prompt += f"{system_n}{line['content']}" prompt += f"{system_n}{extract_content_text(line['content'])}"
if (line["role"] == "user"): if (line["role"] == "user"):
prompt += f"{user_n}{line['content']}" prompt += f"{user_n}{extract_content_text(line['content'])}"
if (line["role"] == "assistant"): if (line["role"] == "assistant"):
prompt += f"{ai_n}{line['content']}{stop}" prompt += f"{ai_n}{extract_content_text(line['content'])}{stop}"
prompt += ai_n.rstrip() prompt += ai_n.rstrip()
return prompt return prompt
@ -130,6 +139,7 @@ def make_resData_stream(data, chat=False, time_now = 0, start=False):
} }
] ]
} }
if "slot_id" in data:
slot_id = data["slot_id"] slot_id = data["slot_id"]
if (chat): if (chat):
if (start): if (start):