zhouhui.jiang

update

...@@ -13,7 +13,7 @@ API_CONFIG = { ...@@ -13,7 +13,7 @@ API_CONFIG = {
13 "headers": { 13 "headers": {
14 "accept": "*/*", 14 "accept": "*/*",
15 "Content-Type": "application/json", 15 "Content-Type": "application/json",
16 - "Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.72961203173b48f8975ea6eb8667e05f"), 16 + "Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.a0c407306d3f41c884715004a00288b0"),
17 "Ver": os.getenv("API_VER", "033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"), 17 "Ver": os.getenv("API_VER", "033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"),
18 "Referer": os.getenv("API_REFERER", "http://192.168.1.251/") 18 "Referer": os.getenv("API_REFERER", "http://192.168.1.251/")
19 } 19 }
......
...@@ -97,13 +97,13 @@ agent = create_react_agent( ...@@ -97,13 +97,13 @@ agent = create_react_agent(
97 ## 你的主要职责: 97 ## 你的主要职责:
98 1. **运单查询**:根据用户需求查询运单列表,支持按状态、时间等条件筛选 98 1. **运单查询**:根据用户需求查询运单列表,支持按状态、时间等条件筛选
99 2. **运单创建**:协助用户创建D类运单,确保信息完整准确 99 2. **运单创建**:协助用户创建D类运单,确保信息完整准确
100 -3. **运单详情**:查询运单的表头信息和表体明细,提供完整的运单数据
101 4. **业务咨询**:解答用户关于出口物流流程、运单状态、操作规范等问题 100 4. **业务咨询**:解答用户关于出口物流流程、运单状态、操作规范等问题
102 101
103 ## 工作原则: 102 ## 工作原则:
104 - 始终以用户需求为导向,提供准确、及时的服务 103 - 始终以用户需求为导向,提供准确、及时的服务
105 - 在调用API前,仔细确认用户提供的参数信息 104 - 在调用API前,仔细确认用户提供的参数信息
106 - 对API返回结果进行清晰、易懂的解释 105 - 对API返回结果进行清晰、易懂的解释
106 +- 上传时文件路径不用确认,是我们后台处理生成的路径
107 - 如遇到错误,主动分析原因并提供解决方案 107 - 如遇到错误,主动分析原因并提供解决方案
108 - 保持专业、友好的沟通态度 108 - 保持专业、友好的沟通态度
109 - 严禁改写工具函数返回的文本格式;对工具输出仅直接转述,不得增删前后缀或改写内容。 109 - 严禁改写工具函数返回的文本格式;对工具输出仅直接转述,不得增删前后缀或改写内容。
...@@ -112,7 +112,7 @@ agent = create_react_agent( ...@@ -112,7 +112,7 @@ agent = create_react_agent(
112 ## 可用工具: 112 ## 可用工具:
113 - query_waybill_list: 查询运单列表,支持按状态筛选,结果以JSON形式展示 113 - query_waybill_list: 查询运单列表,支持按状态筛选,结果以JSON形式展示
114 - create_waybill_d: 根据运单号创建D类运单,需要提供运单号参数 114 - create_waybill_d: 根据运单号创建D类运单,需要提供运单号参数
115 -- upload_clearance_file: 上传清关PDF文件,需要 code、slip_id、pdf_path 115 +- upload_clearance_file: 上传清关PDF文件,需要 code、slip_id、pdf_path,上传时文件路径不用确认,是我们后台处理生成的路径
116 - push_waybill_for_ocr: 根据运单ID推送OCR进行识别,需要提供运单ID(waybill_id)参数 116 - push_waybill_for_ocr: 根据运单ID推送OCR进行识别,需要提供运单ID(waybill_id)参数
117 117
118 ## query_waybill_list数据展示说明: 118 ## query_waybill_list数据展示说明:
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
4 import os 4 import os
5 import base64 5 import base64
6 import uuid 6 import uuid
7 +import re
7 from pathlib import Path 8 from pathlib import Path
8 from typing import List, Any, Union 9 from typing import List, Any, Union
9 10
...@@ -141,10 +142,20 @@ class MessageProcessor: ...@@ -141,10 +142,20 @@ class MessageProcessor:
141 # 提取文本与文件路径 142 # 提取文本与文件路径
142 text_parts: list[str] = [] 143 text_parts: list[str] = []
143 file_paths: list[str] = [] 144 file_paths: list[str] = []
145 + # 保留的参数:slip_id, token, ver
146 + preserved_params = {}
147 +
144 for part in content: 148 for part in content:
145 if isinstance(part, dict): 149 if isinstance(part, dict):
146 if part.get("type") == "text": 150 if part.get("type") == "text":
147 text_parts.append(part.get("text", "")) 151 text_parts.append(part.get("text", ""))
152 + # 从文本部分提取 slip_id, token, ver 参数
153 + if "slip_id" in part:
154 + preserved_params["slip_id"] = part.get("slip_id")
155 + if "token" in part:
156 + preserved_params["token"] = part.get("token")
157 + if "ver" in part:
158 + preserved_params["ver"] = part.get("ver")
148 elif part.get("type") == "file": 159 elif part.get("type") == "file":
149 # 兼容两种文件结构: 160 # 兼容两种文件结构:
150 # 1) {"type":"file", "data":"<base64>", "metadata":{"filename":...}, "mime_type":"application/pdf"} 161 # 1) {"type":"file", "data":"<base64>", "metadata":{"filename":...}, "mime_type":"application/pdf"}
...@@ -176,18 +187,44 @@ class MessageProcessor: ...@@ -176,18 +187,44 @@ class MessageProcessor:
176 saved_path = self.save_and_get_file_url(file_data, filename, mime_type) 187 saved_path = self.save_and_get_file_url(file_data, filename, mime_type)
177 file_paths.append(saved_path) 188 file_paths.append(saved_path)
178 merged_text = "\n".join([t for t in text_parts if t]) 189 merged_text = "\n".join([t for t in text_parts if t])
190 +
191 + # 如果存在 slip_id,将其拼接到文本中
192 + if "slip_id" in preserved_params:
193 + slip_id_value = preserved_params["slip_id"]
194 + # 检查文本中是否已有 slip_id:xxx 的格式,如果有则替换,否则追加
195 + slip_id_pattern = r'slip_id[::]\s*\d+'
196 + if re.search(slip_id_pattern, merged_text):
197 + # 替换原有的 slip_id:xxx
198 + merged_text = re.sub(slip_id_pattern, f'slip_id:{slip_id_value}', merged_text)
199 + else:
200 + # 追加 slip_id:xxx
201 + merged_text = f"{merged_text} slip_id:{slip_id_value}"
202 +
179 if file_paths: 203 if file_paths:
180 merged_text = (merged_text + "\n" + "\n".join(file_paths)).strip() 204 merged_text = (merged_text + "\n" + "\n".join(file_paths)).strip()
181 205
182 - new_content = [{"type": "text", "text": merged_text}] 206 + # 将保留的参数添加到content字典中,而不是additional_kwargs
207 + new_content_dict = {"type": "text", "text": merged_text}
208 + if preserved_params:
209 + new_content_dict.update(preserved_params)
210 + new_content = [new_content_dict]
211 +
212 + # 获取原有的 additional_kwargs(不合并保留的参数)
213 + if isinstance(m, dict):
214 + existing_kwargs = m.get("additional_kwargs", {})
215 + else:
216 + existing_kwargs = getattr(m, "additional_kwargs", {})
217 +
218 + # 保持原有的 additional_kwargs,不添加保留的参数
219 + merged_kwargs = existing_kwargs
183 220
184 if isinstance(m, dict): 221 if isinstance(m, dict):
185 - filtered_msg = {**m, "content": new_content} 222 + filtered_msg = {**m, "content": new_content, "additional_kwargs": merged_kwargs}
186 else: 223 else:
187 try: 224 try:
188 filtered_msg = m.__class__( 225 filtered_msg = m.__class__(
189 content=new_content, 226 content=new_content,
190 - additional_kwargs=getattr(m, "additional_kwargs", {}), 227 + additional_kwargs=merged_kwargs,
191 response_metadata=getattr(m, "response_metadata", {}), 228 response_metadata=getattr(m, "response_metadata", {}),
192 id=getattr(m, "id", None), 229 id=getattr(m, "id", None),
193 ) 230 )
...@@ -197,15 +234,19 @@ class MessageProcessor: ...@@ -197,15 +234,19 @@ class MessageProcessor:
197 "type": m.__class__.__name__, 234 "type": m.__class__.__name__,
198 "role": (role or "user"), 235 "role": (role or "user"),
199 "id": getattr(m, "id", None), 236 "id": getattr(m, "id", None),
200 - "additional_kwargs": getattr(m, "additional_kwargs", {}), 237 + "additional_kwargs": merged_kwargs,
201 "response_metadata": getattr(m, "response_metadata", {}), 238 "response_metadata": getattr(m, "response_metadata", {}),
202 "content": new_content, 239 "content": new_content,
203 } 240 }
204 filtered_messages.append(filtered_msg) 241 filtered_messages.append(filtered_msg)
205 try: 242 try:
206 print(f" -> processed to: {len(merged_text)} chars") 243 print(f" -> processed to: {len(merged_text)} chars")
244 + if preserved_params:
245 + print(f" -> preserved params: {preserved_params}")
207 except Exception: 246 except Exception:
208 print(" -> processed") 247 print(" -> processed")
248 + if preserved_params:
249 + print(f" -> preserved params: {preserved_params}")
209 else: 250 else:
210 # 不处理,其它消息保持不变 251 # 不处理,其它消息保持不变
211 filtered_messages.append(m) 252 filtered_messages.append(m)
......