Showing
2 changed files
with
35 additions
and
32 deletions
| ... | @@ -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.37cfc9a928d14d0186e6896fa080bc99"), | 16 | + "Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.6adbae8492564f68b213f8d1e785b3d9"), |
| 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 | } | ... | ... |
| ... | @@ -4,16 +4,18 @@ import os | ... | @@ -4,16 +4,18 @@ import os |
| 4 | import sys | 4 | import sys |
| 5 | import json | 5 | import json |
| 6 | from typing import Dict, Any, List, Optional | 6 | from typing import Dict, Any, List, Optional |
| 7 | -from contextvars import ContextVar | ||
| 8 | from langchain_core.runnables import RunnableConfig | 7 | from langchain_core.runnables import RunnableConfig |
| 9 | from langchain_core.messages import AnyMessage | 8 | from langchain_core.messages import AnyMessage |
| 10 | 9 | ||
| 11 | -# 添加项目根目录到 Python 路径 | 10 | +# 添加项目根目录到 Python 路径(避免重复添加) |
| 12 | -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 11 | +_project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 12 | +if _project_root not in sys.path: | ||
| 13 | + sys.path.insert(0, _project_root) | ||
| 13 | 14 | ||
| 14 | # 导入 API 模块 | 15 | # 导入 API 模块 |
| 15 | from API.waybill_api import query_waybill_list, create_waybill_d, create_waybill_c, push_waybill_for_ocr, query_waybill_info, create_waybill_d_with_id | 16 | from API.waybill_api import query_waybill_list, create_waybill_d, create_waybill_c, push_waybill_for_ocr, query_waybill_info, create_waybill_d_with_id |
| 16 | from API.paperless_api import upload_clearance_file, upload_file_for_ocr | 17 | from API.paperless_api import upload_clearance_file, upload_file_for_ocr |
| 18 | +from API.api_config import API_CONFIG | ||
| 17 | 19 | ||
| 18 | # 导入工具类 | 20 | # 导入工具类 |
| 19 | from langgraph_examples.utils.message_processor import MessageProcessor | 21 | from langgraph_examples.utils.message_processor import MessageProcessor |
| ... | @@ -163,34 +165,8 @@ def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: | ... | @@ -163,34 +165,8 @@ def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: |
| 163 | 165 | ||
| 164 | return result | 166 | return result |
| 165 | 167 | ||
| 166 | -def _create_system_prompt(state: Dict[str, Any], config: RunnableConfig) -> List[AnyMessage]: | 168 | +# 系统提示词模板(提取为模块级常量,避免每次调用都创建大字符串) |
| 167 | - """ | 169 | +_SYSTEM_PROMPT_TEMPLATE = """你是一个专业的出口物流系统智能助手,专门帮助用户处理运单相关的业务操作。 |
| 168 | - 创建动态系统提示词 | ||
| 169 | - | ||
| 170 | - Args: | ||
| 171 | - state: LangGraph 状态字典 | ||
| 172 | - config: Runnable 配置 | ||
| 173 | - | ||
| 174 | - Returns: | ||
| 175 | - 包含系统消息和原始消息的列表 | ||
| 176 | - """ | ||
| 177 | - # 添加调试信息,确认函数被调用 | ||
| 178 | - # print("\n=== _create_system_prompt 被调用 ===") | ||
| 179 | - # print(f"state type: {type(state)}") | ||
| 180 | - # print(f"state keys: {list(state.keys()) if isinstance(state, dict) else 'not a dict'}") | ||
| 181 | - | ||
| 182 | - # 从 state 中提取动态参数 | ||
| 183 | - params = extract_token(state) | ||
| 184 | - token = params.get("token", "") | ||
| 185 | - | ||
| 186 | - # 如果从 state 中提取的 token 为空,则从 api_config.py 中获取 Authorization 作为备选 | ||
| 187 | - if not token: | ||
| 188 | - from API.api_config import API_CONFIG | ||
| 189 | - token = API_CONFIG.get("headers", {}).get("Authorization", "") | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - # 创建系统提示词(使用 f-string 以便插入 token) | ||
| 193 | - system_msg = f"""你是一个专业的出口物流系统智能助手,专门帮助用户处理运单相关的业务操作。 | ||
| 194 | 170 | ||
| 195 | ## 你的主要职责: | 171 | ## 你的主要职责: |
| 196 | 1. **运单查询**:根据用户需求查询运单信息 | 172 | 1. **运单查询**:根据用户需求查询运单信息 |
| ... | @@ -313,6 +289,33 @@ def _create_system_prompt(state: Dict[str, Any], config: RunnableConfig) -> List | ... | @@ -313,6 +289,33 @@ def _create_system_prompt(state: Dict[str, Any], config: RunnableConfig) -> List |
| 313 | 289 | ||
| 314 | 请根据用户的具体需求,选择合适的工具并提供帮助。""" | 290 | 请根据用户的具体需求,选择合适的工具并提供帮助。""" |
| 315 | 291 | ||
| 292 | +def _create_system_prompt(state: Dict[str, Any], config: RunnableConfig) -> List[AnyMessage]: | ||
| 293 | + """ | ||
| 294 | + 创建动态系统提示词 | ||
| 295 | + | ||
| 296 | + Args: | ||
| 297 | + state: LangGraph 状态字典 | ||
| 298 | + config: Runnable 配置 | ||
| 299 | + | ||
| 300 | + Returns: | ||
| 301 | + 包含系统消息和原始消息的列表 | ||
| 302 | + """ | ||
| 303 | + # 添加调试信息,确认函数被调用 | ||
| 304 | + # print("\n=== _create_system_prompt 被调用 ===") | ||
| 305 | + # print(f"state type: {type(state)}") | ||
| 306 | + # print(f"state keys: {list(state.keys()) if isinstance(state, dict) else 'not a dict'}") | ||
| 307 | + | ||
| 308 | + # 从 state 中提取动态参数 | ||
| 309 | + params = extract_token(state) | ||
| 310 | + token = params.get("token", "") | ||
| 311 | + | ||
| 312 | + # 如果从 state 中提取的 token 为空,则从 api_config.py 中获取 Authorization 作为备选 | ||
| 313 | + if not token: | ||
| 314 | + token = API_CONFIG.get("headers", {}).get("Authorization", "") | ||
| 315 | + | ||
| 316 | + # 使用模板创建系统提示词(只动态替换 token,避免每次创建大字符串) | ||
| 317 | + system_msg = _SYSTEM_PROMPT_TEMPLATE.format(token=token) | ||
| 318 | + | ||
| 316 | # 返回系统消息 + 原始消息 | 319 | # 返回系统消息 + 原始消息 |
| 317 | result = [{"role": "system", "content": system_msg}] + state.get("messages", []) | 320 | result = [{"role": "system", "content": system_msg}] + state.get("messages", []) |
| 318 | print(f"返回消息数量: {len(result)}") | 321 | print(f"返回消息数量: {len(result)}") | ... | ... |
-
Please register or login to post a comment