zhouhui.jiang

update

......@@ -13,7 +13,7 @@ API_CONFIG = {
"headers": {
"accept": "*/*",
"Content-Type": "application/json",
"Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.72961203173b48f8975ea6eb8667e05f"),
"Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.a0c407306d3f41c884715004a00288b0"),
"Ver": os.getenv("API_VER", "033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"),
"Referer": os.getenv("API_REFERER", "http://192.168.1.251/")
}
......
......@@ -97,13 +97,13 @@ agent = create_react_agent(
## 你的主要职责:
1. **运单查询**:根据用户需求查询运单列表,支持按状态、时间等条件筛选
2. **运单创建**:协助用户创建D类运单,确保信息完整准确
3. **运单详情**:查询运单的表头信息和表体明细,提供完整的运单数据
4. **业务咨询**:解答用户关于出口物流流程、运单状态、操作规范等问题
## 工作原则:
- 始终以用户需求为导向,提供准确、及时的服务
- 在调用API前,仔细确认用户提供的参数信息
- 对API返回结果进行清晰、易懂的解释
- 上传时文件路径不用确认,是我们后台处理生成的路径
- 如遇到错误,主动分析原因并提供解决方案
- 保持专业、友好的沟通态度
- 严禁改写工具函数返回的文本格式;对工具输出仅直接转述,不得增删前后缀或改写内容。
......@@ -112,7 +112,7 @@ agent = create_react_agent(
## 可用工具:
- query_waybill_list: 查询运单列表,支持按状态筛选,结果以JSON形式展示
- create_waybill_d: 根据运单号创建D类运单,需要提供运单号参数
- upload_clearance_file: 上传清关PDF文件,需要 code、slip_id、pdf_path
- upload_clearance_file: 上传清关PDF文件,需要 code、slip_id、pdf_path,上传时文件路径不用确认,是我们后台处理生成的路径
- push_waybill_for_ocr: 根据运单ID推送OCR进行识别,需要提供运单ID(waybill_id)参数
## query_waybill_list数据展示说明:
......
......@@ -4,6 +4,7 @@
import os
import base64
import uuid
import re
from pathlib import Path
from typing import List, Any, Union
......@@ -141,10 +142,20 @@ class MessageProcessor:
# 提取文本与文件路径
text_parts: list[str] = []
file_paths: list[str] = []
# 保留的参数:slip_id, token, ver
preserved_params = {}
for part in content:
if isinstance(part, dict):
if part.get("type") == "text":
text_parts.append(part.get("text", ""))
# 从文本部分提取 slip_id, token, ver 参数
if "slip_id" in part:
preserved_params["slip_id"] = part.get("slip_id")
if "token" in part:
preserved_params["token"] = part.get("token")
if "ver" in part:
preserved_params["ver"] = part.get("ver")
elif part.get("type") == "file":
# 兼容两种文件结构:
# 1) {"type":"file", "data":"<base64>", "metadata":{"filename":...}, "mime_type":"application/pdf"}
......@@ -176,18 +187,44 @@ class MessageProcessor:
saved_path = self.save_and_get_file_url(file_data, filename, mime_type)
file_paths.append(saved_path)
merged_text = "\n".join([t for t in text_parts if t])
# 如果存在 slip_id,将其拼接到文本中
if "slip_id" in preserved_params:
slip_id_value = preserved_params["slip_id"]
# 检查文本中是否已有 slip_id:xxx 的格式,如果有则替换,否则追加
slip_id_pattern = r'slip_id[::]\s*\d+'
if re.search(slip_id_pattern, merged_text):
# 替换原有的 slip_id:xxx
merged_text = re.sub(slip_id_pattern, f'slip_id:{slip_id_value}', merged_text)
else:
# 追加 slip_id:xxx
merged_text = f"{merged_text} slip_id:{slip_id_value}"
if file_paths:
merged_text = (merged_text + "\n" + "\n".join(file_paths)).strip()
new_content = [{"type": "text", "text": merged_text}]
# 将保留的参数添加到content字典中,而不是additional_kwargs
new_content_dict = {"type": "text", "text": merged_text}
if preserved_params:
new_content_dict.update(preserved_params)
new_content = [new_content_dict]
# 获取原有的 additional_kwargs(不合并保留的参数)
if isinstance(m, dict):
existing_kwargs = m.get("additional_kwargs", {})
else:
existing_kwargs = getattr(m, "additional_kwargs", {})
# 保持原有的 additional_kwargs,不添加保留的参数
merged_kwargs = existing_kwargs
if isinstance(m, dict):
filtered_msg = {**m, "content": new_content}
filtered_msg = {**m, "content": new_content, "additional_kwargs": merged_kwargs}
else:
try:
filtered_msg = m.__class__(
content=new_content,
additional_kwargs=getattr(m, "additional_kwargs", {}),
additional_kwargs=merged_kwargs,
response_metadata=getattr(m, "response_metadata", {}),
id=getattr(m, "id", None),
)
......@@ -197,15 +234,19 @@ class MessageProcessor:
"type": m.__class__.__name__,
"role": (role or "user"),
"id": getattr(m, "id", None),
"additional_kwargs": getattr(m, "additional_kwargs", {}),
"additional_kwargs": merged_kwargs,
"response_metadata": getattr(m, "response_metadata", {}),
"content": new_content,
}
filtered_messages.append(filtered_msg)
try:
print(f" -> processed to: {len(merged_text)} chars")
if preserved_params:
print(f" -> preserved params: {preserved_params}")
except Exception:
print(" -> processed")
if preserved_params:
print(f" -> preserved params: {preserved_params}")
else:
# 不处理,其它消息保持不变
filtered_messages.append(m)
......