lizhengwei

jira:NYJ-1540 desc: add model_test.py

@@ -101,7 +101,7 @@ class TestReplayMatchEvent(unittest.TestCase): @@ -101,7 +101,7 @@ class TestReplayMatchEvent(unittest.TestCase):
101 for i, replay_name in enumerate(relays_mp4_names): 101 for i, replay_name in enumerate(relays_mp4_names):
102 print(100*"*" + f"replay_name: {replay_name}") 102 print(100*"*" + f"replay_name: {replay_name}")
103 replay_path = os.path.join(replays_mp4_dir, replay_name) 103 replay_path = os.path.join(replays_mp4_dir, replay_name)
104 - data = _base_data(i, replay_path, event_list) 104 + data = _base_data(i+1, replay_path, event_list)
105 105
106 start_time = time.time() 106 start_time = time.time()
107 result = frm.replay_match_event(data) 107 result = frm.replay_match_event(data)
@@ -100,7 +100,9 @@ req_prompt = """ @@ -100,7 +100,9 @@ req_prompt = """
100 # Output Format (输出要求) 100 # Output Format (输出要求)
101 101
102 请**仅**输出一个标准的 JSON 对象,**不要**包含任何推理过程(reasoning)、Markdown 标记(如 ```json)或额外的解释文本。 102 请**仅**输出一个标准的 JSON 对象,**不要**包含任何推理过程(reasoning)、Markdown 标记(如 ```json)或额外的解释文本。
103 - 103 + - 请直接给出结论,不需要解释。
  104 + - 跳过分析步骤,只给结果。
  105 +
104 JSON 格式如下: 106 JSON 格式如下:
105 { 107 {
106 "replay_summary": { 108 "replay_summary": {
@@ -121,10 +123,7 @@ class FootballReplayMatchLive: @@ -121,10 +123,7 @@ class FootballReplayMatchLive:
121 # self.model = ChatOpenAI(base_url="http://192.168.1.59:11434/v1", model="qwen3.6:35b-a3b-q8_0", temperature=0.7, 123 # self.model = ChatOpenAI(base_url="http://192.168.1.59:11434/v1", model="qwen3.6:35b-a3b-q8_0", temperature=0.7,
122 # api_key='no_key') 124 # api_key='no_key')
123 self.model = ChatOpenAI(base_url=base_url, model=model, temperature=temperature, api_key=api_key, 125 self.model = ChatOpenAI(base_url=base_url, model=model, temperature=temperature, api_key=api_key,
124 -  
125 - model_kwargs={  
126 - "extra_body": {"enable_thinking": False}  
127 - } 126 + extra_body={"chat_template_kwargs": {"enable_thinking": False}}
128 ) 127 )
129 #extra_body={"chat_template_kwargs": {"enable_thinking": False}}) 128 #extra_body={"chat_template_kwargs": {"enable_thinking": False}})
130 self.cache_dir = cache_dir 129 self.cache_dir = cache_dir
@@ -196,9 +195,9 @@ class FootballReplayMatchLive: @@ -196,9 +195,9 @@ class FootballReplayMatchLive:
196 result = self.model.invoke([system_message, user_message]).content 195 result = self.model.invoke([system_message, user_message]).content
197 196
198 197
199 - print(100*"3")  
200 - print(result)  
201 - print(100*"4") 198 + # print(100*"3")
  199 + # print(result)
  200 + # print(100*"4")
202 # try: 201 # try:
203 # result_json = json.loads(result) 202 # result_json = json.loads(result)
204 # except json.JSONDecodeError: 203 # except json.JSONDecodeError:
@@ -207,30 +206,32 @@ class FootballReplayMatchLive: @@ -207,30 +206,32 @@ class FootballReplayMatchLive:
207 # except Exception as e: 206 # except Exception as e:
208 # print("JSON解析失败:", result) 207 # print("JSON解析失败:", result)
209 # raise e 208 # raise e
210 -  
211 - try:  
212 - # 1. 深度清洗:去掉 Markdown 标记  
213 - cleaned_result = result.replace("```json", "").replace("```", "")  
214 -  
215 - # 2. 核心修复:使用 re.DOTALL 确保正则能跨行匹配,精准提取最外层完整的 JSON 对象  
216 - # 这样可以防止因为模型输出换行导致提取不完整  
217 - json_match = re.search(r'(\{.*\})', cleaned_result, re.DOTALL)  
218 -  
219 - if json_match:  
220 - json_str = json_match.group(1)  
221 - # 截断第一个 } 之后的所有内容,彻底干掉 <time_location> 等尾巴  
222 - json_str = json_str[:json_str.rfind('}') + 1]  
223 - else:  
224 - # 如果连花括号都找不到,直接抛出原始内容方便排查  
225 - print("无法提取JSON,原始内容:", result)  
226 - raise ValueError("响应中未找到有效的 JSON 对象")  
227 -  
228 - # 3. 使用 strict=False 容忍字符串中的非法控制字符(如直接换行)  
229 - result_json = json.loads(json_str, strict=False)  
230 -  
231 - except Exception as e:  
232 - print("JSON解析依然失败,清洗后的内容:", json_str if 'json_str' in locals() else result)  
233 - raise e 209 + if result.strip() == "null":
  210 + result_json = {"video_id": None}
  211 + else:
  212 + try:
  213 + # 1. 深度清洗:去掉 Markdown 标记
  214 + cleaned_result = result.replace("```json", "").replace("```", "")
  215 +
  216 + # 2. 核心修复:使用 re.DOTALL 确保正则能跨行匹配,精准提取最外层完整的 JSON 对象
  217 + # 这样可以防止因为模型输出换行导致提取不完整
  218 + json_match = re.search(r'(\{.*\})', cleaned_result, re.DOTALL)
  219 +
  220 + if json_match:
  221 + json_str = json_match.group(1)
  222 + # 截断第一个 } 之后的所有内容,彻底干掉 <time_location> 等尾巴
  223 + json_str = json_str[:json_str.rfind('}') + 1]
  224 + else:
  225 + # 如果连花括号都找不到,直接抛出原始内容方便排查
  226 + print("无法提取JSON,原始内容:", result)
  227 + raise ValueError("响应中未找到有效的 JSON 对象")
  228 +
  229 + # 3. 使用 strict=False 容忍字符串中的非法控制字符(如直接换行)
  230 + result_json = json.loads(json_str, strict=False)
  231 +
  232 + except Exception as e:
  233 + print("JSON解析依然失败,清洗后的内容:", json_str if 'json_str' in locals() else result)
  234 + raise e
234 235
235 video_id = result_json.get("video_id", None) 236 video_id = result_json.get("video_id", None)
236 result_live = live_map.get(video_id, None) 237 result_live = live_map.get(video_id, None)
@@ -76,9 +76,7 @@ class FootballReplayVideoEvent: @@ -76,9 +76,7 @@ class FootballReplayVideoEvent:
76 # self.model = ChatOpenAI(base_url="http://192.168.1.59:11434/v1", model="qwen3.6:35b-a3b-q8_0", temperature=0.7, 76 # self.model = ChatOpenAI(base_url="http://192.168.1.59:11434/v1", model="qwen3.6:35b-a3b-q8_0", temperature=0.7,
77 # api_key='no_key') 77 # api_key='no_key')
78 self.model = ChatOpenAI(base_url=base_url, model=model, temperature=temperature, api_key=api_key, 78 self.model = ChatOpenAI(base_url=base_url, model=model, temperature=temperature, api_key=api_key,
79 - model_kwargs={  
80 - "extra_body": {"enable_thinking": False}  
81 - } 79 + extra_body={"chat_template_kwargs": {"enable_thinking": False}}
82 ) 80 )
83 # extra_body={"enable_thinking": False}) 81 # extra_body={"enable_thinking": False})
84 # extra_body={"chat_template_kwargs": {"enable_thinking": False}}) 82 # extra_body={"chat_template_kwargs": {"enable_thinking": False}})
  1 +import time
  2 +from langchain_openai import ChatOpenAI
  3 +from langchain_core.messages import SystemMessage, HumanMessage
  4 +model = ChatOpenAI(base_url="http://192.168.1.59:11434/v1",
  5 + model="Qwen3.6-35B-A3B-UD-Q8_K_XL.gguf",
  6 + temperature=0.7,
  7 + api_key='no_key',
  8 + extra_body={"chat_template_kwargs": {"enable_thinking": False}}
  9 + )
  10 +
  11 +req_prompt = """
  12 +### 角色设定
  13 +你是一个聊天机器人"。
  14 +
  15 +### 输入内容
  16 +1. **文本**:问题文本。
  17 +
  18 +### 输出要求
  19 +回答问题
  20 +"""
  21 +contents = """ 你好!"""
  22 +system_message = SystemMessage(content=req_prompt)
  23 +video_message = HumanMessage(content=contents)
  24 +# asr_message = HumanMessage(content=f"解说内容:{asr_text}")
  25 +
  26 +start_time = time.time()
  27 +result = model.invoke([system_message, video_message])
  28 +end_time = time.time()
  29 +print(f"cost time: {end_time - start_time:.2f} seconds")
  30 +
  31 +print(result)