Qwen3-32B JSON格式化输出不稳定,微调输出格式
This commit is contained in:
@@ -188,17 +188,19 @@ class AsyncIntentRecognizer:
|
||||
classification_parser = PydanticOutputParser(pydantic_object=Classification)
|
||||
formatted_prompt = classification_prompt.format(user_input=query,
|
||||
classification_info=classification_info,
|
||||
output_format=classification_parser.get_format_instructions(),
|
||||
output_format=Classification.get_format_instructions(),
|
||||
# conversation_context=conversation_context,
|
||||
chat_history=json.dumps(chat_history, ensure_ascii=False))
|
||||
# 解析输出
|
||||
try:
|
||||
# 异步调用LLM
|
||||
response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response = await self._llm.ainvoke(formatted_prompt, response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
|
||||
# 尝试直接解析JSON响应
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
parsed_output = classification_parser.parse(clean_output)
|
||||
|
||||
# 计算并打印耗时
|
||||
@@ -262,11 +264,13 @@ class AsyncIntentRecognizer:
|
||||
formatted_prompt = formatted_prompt.replace("{output_format}", terms_list_parser.get_format_instructions())
|
||||
|
||||
# 异步调用LLM
|
||||
response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response = await self._llm.ainvoke(formatted_prompt, response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
|
||||
# 尝试使用Pydantic解析器解析TermList
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
parsed_output = terms_list_parser.parse(clean_output)
|
||||
return parsed_output.terms
|
||||
|
||||
@@ -335,18 +339,22 @@ class AsyncIntentRecognizer:
|
||||
</chat_history>
|
||||
|
||||
1、请从当前提问内容中提取电力造价行中定额编码、定额名称、清单编码、清单名称
|
||||
2、请勿随机编造,如果没有提取到,返回空内容
|
||||
3、返回结果为json格式
|
||||
2、请勿随机编造,如果没有提取到内容返回空的JSON
|
||||
3、返回结果为json格式,必须严格以纯JSON格式输出
|
||||
```json
|
||||
{{
|
||||
"dinge_info_list":{{"dinge_code_list":["xxxx","xxxx"], "dinge_name_list":["xxxx","xxxx"]}},
|
||||
"qingdan_info":{{"qingdan_code_list":["xxxx","xxxx"], "qingdan_name_list":["xxxx","xxxx"]}}
|
||||
}}
|
||||
```json
|
||||
"""
|
||||
|
||||
try:
|
||||
response = await self._llm.ainvoke(prompt, response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(prompt, extra_body={"enable_thinking": False})
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
parsed_output = JsonOutputParser().parse(clean_output)
|
||||
|
||||
# 计算并打印耗时
|
||||
@@ -378,16 +386,18 @@ class AsyncIntentRecognizer:
|
||||
keywords_str = json.dumps(terms_dict, ensure_ascii=False)
|
||||
query_rewrite_parser = PydanticOutputParser(pydantic_object=QueryRewrite)
|
||||
formatted_prompt = query_rewrite_prompt_pro.format(query=query,
|
||||
output_format=query_rewrite_parser.get_format_instructions(),
|
||||
output_format=QueryRewrite.get_format_instructions(),
|
||||
keywords=keywords_str,
|
||||
chat_history=chat_history,
|
||||
context=context)
|
||||
# 解析输出
|
||||
try:
|
||||
# 异步调用LLM
|
||||
response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response = await self._llm.ainvoke(formatted_prompt,response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
parsed_output = query_rewrite_parser.parse(clean_output)
|
||||
end_time = time.time()
|
||||
process_time=end_time-start_time
|
||||
@@ -630,9 +640,11 @@ class AsyncIntentRecognizer:
|
||||
)
|
||||
try:
|
||||
# 异步调用LLM
|
||||
response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response = await self._llm.ainvoke(formatted_prompt,response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
# 尝试解析LLM响应
|
||||
parsed_output = slot_parser.parse(clean_output)
|
||||
return parsed_output
|
||||
@@ -660,16 +672,18 @@ class AsyncIntentRecognizer:
|
||||
query=query,
|
||||
chat_history=json.dumps(chat_history, ensure_ascii=False) if chat_history else "[]",
|
||||
# conversation_context=conversation_context,
|
||||
output_format=step_back_parser.get_format_instructions()
|
||||
output_format=StepBackPrompt.get_format_instructions()
|
||||
)
|
||||
|
||||
try:
|
||||
# 异步调用LLM
|
||||
response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
response = await self._llm.ainvoke(formatted_prompt, response_format={"type": "json_object"}, extra_body={"enable_thinking": False})
|
||||
# response = await self._llm.ainvoke(formatted_prompt, extra_body={"enable_thinking": False})
|
||||
|
||||
# 解析输出
|
||||
response.content = response.content.strip()
|
||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||
clean_output = re.sub(r'\s+', '', clean_output)
|
||||
parsed_output = step_back_parser.parse(clean_output)
|
||||
step_back_end_time = time.time()
|
||||
step_back_time = step_back_end_time - step_back_start_time
|
||||
|
||||
Reference in New Issue
Block a user