增加PREAMBLE标签
This commit is contained in:
parent
b000fe6b6d
commit
8be1acb9f2
@ -297,7 +297,7 @@ async def enhanced_generate_stream_response(
|
||||
preamble_text = await preamble_task
|
||||
# 只有当preamble_text不为空且不为"<empty>"时才输出
|
||||
if preamble_text and preamble_text.strip() and preamble_text != "<empty>":
|
||||
preamble_content = get_content_from_messages([{"role": "assistant","content": preamble_text + "\n"}], tool_response=tool_response)
|
||||
preamble_content = get_content_from_messages([{"role": "preamble","content": preamble_text + "\n"}], tool_response=tool_response)
|
||||
chunk_data = create_stream_chunk(f"chatcmpl-preamble", model_name, preamble_content)
|
||||
yield f"data: {json.dumps(chunk_data, ensure_ascii=False)}\n\n"
|
||||
logger.info(f"Stream mode: Generated preamble text ({len(preamble_text)} chars)")
|
||||
|
||||
@ -101,9 +101,9 @@ def get_content_from_messages(messages: List[dict], tool_response: bool = True)
|
||||
TOOL_RESULT_S = '[TOOL_RESPONSE]'
|
||||
THOUGHT_S = '[THINK]'
|
||||
ANSWER_S = '[ANSWER]'
|
||||
PREAMBLE_S = '[PREAMBLE]'
|
||||
|
||||
for msg in messages:
|
||||
|
||||
if msg['role'] == ASSISTANT:
|
||||
if msg.get('reasoning_content'):
|
||||
assert isinstance(msg['reasoning_content'], str), 'Now only supports text messages'
|
||||
@ -128,6 +128,8 @@ def get_content_from_messages(messages: List[dict], tool_response: bool = True)
|
||||
elif msg['role'] == FUNCTION:
|
||||
if tool_response:
|
||||
content.append(f'{TOOL_RESULT_S} {msg["name"]}\n{msg["content"]}')
|
||||
elif msg['role'] == "preamble":
|
||||
content.append(f'{PREAMBLE_S}\n{msg["content"]}')
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
@ -156,8 +158,8 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
# 确定当前ASSISTANT消息在所有ASSISTANT消息中的位置(从0开始)
|
||||
assistant_position = assistant_indices.index(i)
|
||||
|
||||
# 使用正则表达式按照 [TOOL_CALL]|[TOOL_RESPONSE]|[ANSWER] 进行切割
|
||||
parts = re.split(r'\[(TOOL_CALL|TOOL_RESPONSE|ANSWER)\]', msg.content)
|
||||
# 使用正则表达式按照 [THINK|TOOL_CALL]|[TOOL_RESPONSE]|[ANSWER] 进行切割
|
||||
parts = re.split(r'\[(THINK|PREAMBLE|TOOL_CALL|TOOL_RESPONSE|ANSWER)\]', msg.content)
|
||||
|
||||
# 重新组装内容,根据消息位置决定处理方式
|
||||
filtered_content = ""
|
||||
@ -198,7 +200,7 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
elif current_tag == "ANSWER":
|
||||
# 所有ASSISTANT消息都保留ANSWER数据
|
||||
filtered_content += f"[ANSWER]\n{text}\n"
|
||||
elif current_tag != "THINK":
|
||||
elif current_tag != "THINK" and current_tag != "PREAMBLE":
|
||||
filtered_content += text + "\n"
|
||||
else: # 标签
|
||||
current_tag = parts[i]
|
||||
@ -213,13 +215,13 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
else:
|
||||
processed_messages.append({"role": msg.role, "content": msg.content})
|
||||
|
||||
# 逆运算:将包含 [TOOL_RESPONSE] 的消息重新组装回 msg['role'] == 'function' 和 msg.get('function_call')
|
||||
# 逆运算:将包含 [THINK|TOOL_RESPONSE] 的消息重新组装回 msg['role'] == 'function' 和 msg.get('function_call')
|
||||
# 这是 get_content_from_messages 的逆运算
|
||||
final_messages = []
|
||||
for msg in processed_messages:
|
||||
if msg["role"] == ASSISTANT and "[TOOL_RESPONSE]" in msg["content"]:
|
||||
if msg["role"] == ASSISTANT:
|
||||
# 分割消息内容
|
||||
parts = re.split(r'\[(TOOL_CALL|TOOL_RESPONSE|ANSWER)\]', msg["content"])
|
||||
parts = re.split(r'\[(THINK|PREAMBLE|TOOL_CALL|TOOL_RESPONSE|ANSWER)\]', msg["content"])
|
||||
|
||||
current_tag = None
|
||||
assistant_content = ""
|
||||
@ -257,7 +259,7 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
"arguments": arguments
|
||||
}
|
||||
})
|
||||
elif current_tag != "THINK":
|
||||
elif current_tag != "THINK" and current_tag != "PREAMBLE":
|
||||
final_messages.append({
|
||||
"role": ASSISTANT,
|
||||
"content": text
|
||||
|
||||
Loading…
Reference in New Issue
Block a user