Showing
1 changed file
with
33 additions
and
12 deletions
| ... | @@ -94,7 +94,8 @@ def pre_model_inspect_attachments(state, **kwargs): | ... | @@ -94,7 +94,8 @@ def pre_model_inspect_attachments(state, **kwargs): |
| 94 | def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: | 94 | def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: |
| 95 | """ | 95 | """ |
| 96 | 从 state 中提取参数 | 96 | 从 state 中提取参数 |
| 97 | - 获取最后一个类型为 HumanMessage 或 human 的消息中的参数(token、consignmentCode、consignmentId、loginName、userId) | 97 | + 优先从 additional_kwargs 中提取参数(token、consignmentCode、consignmentId、loginName、userId) |
| 98 | + 如果 additional_kwargs 中没有,再从 content 中提取(作为备选) | ||
| 98 | 99 | ||
| 99 | Args: | 100 | Args: |
| 100 | state: LangGraph 状态字典,包含 messages 数组 | 101 | state: LangGraph 状态字典,包含 messages 数组 |
| ... | @@ -135,37 +136,57 @@ def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: | ... | @@ -135,37 +136,57 @@ def extract_token(state: Dict[str, Any]) -> Dict[str, Any]: |
| 135 | # 直接取最后一个 human 消息(不需要循环判断) | 136 | # 直接取最后一个 human 消息(不需要循环判断) |
| 136 | last_human_msg = human_messages[-1] | 137 | last_human_msg = human_messages[-1] |
| 137 | 138 | ||
| 138 | - # 从 content 中提取参数 | 139 | + # 优先从 additional_kwargs 中提取参数 |
| 140 | + if isinstance(last_human_msg, dict): | ||
| 141 | + additional_kwargs = last_human_msg.get("additional_kwargs", {}) | ||
| 142 | + else: | ||
| 143 | + additional_kwargs = getattr(last_human_msg, "additional_kwargs", {}) | ||
| 144 | + | ||
| 145 | + if additional_kwargs: | ||
| 146 | + # 从 additional_kwargs 中提取参数 | ||
| 147 | + if "token" in additional_kwargs and additional_kwargs.get("token"): | ||
| 148 | + result["token"] = additional_kwargs.get("token") | ||
| 149 | + if "consignmentCode" in additional_kwargs: | ||
| 150 | + result["consignmentCode"] = additional_kwargs.get("consignmentCode") | ||
| 151 | + if "consignmentId" in additional_kwargs: | ||
| 152 | + result["consignmentId"] = additional_kwargs.get("consignmentId") | ||
| 153 | + if "loginName" in additional_kwargs: | ||
| 154 | + result["loginName"] = additional_kwargs.get("loginName") | ||
| 155 | + if "userId" in additional_kwargs: | ||
| 156 | + result["userId"] = additional_kwargs.get("userId") | ||
| 157 | + | ||
| 158 | + # 如果 additional_kwargs 中没有某些参数,再从 content 中提取(作为备选) | ||
| 139 | if isinstance(last_human_msg, dict): | 159 | if isinstance(last_human_msg, dict): |
| 140 | content = last_human_msg.get("content") | 160 | content = last_human_msg.get("content") |
| 141 | else: | 161 | else: |
| 142 | content = getattr(last_human_msg, "content", None) | 162 | content = getattr(last_human_msg, "content", None) |
| 143 | 163 | ||
| 164 | + # 只有在 additional_kwargs 中没有找到对应参数时,才从 content 中提取 | ||
| 144 | if isinstance(content, list): | 165 | if isinstance(content, list): |
| 145 | # content 是列表,遍历查找包含参数的 part | 166 | # content 是列表,遍历查找包含参数的 part |
| 146 | for part in content: | 167 | for part in content: |
| 147 | if isinstance(part, dict): | 168 | if isinstance(part, dict): |
| 148 | - if "token" in part and part.get("token"): | 169 | + if not result["token"] and "token" in part and part.get("token"): |
| 149 | result["token"] = part.get("token") | 170 | result["token"] = part.get("token") |
| 150 | - if "consignmentCode" in part: | 171 | + if result["consignmentCode"] is None and "consignmentCode" in part: |
| 151 | result["consignmentCode"] = part.get("consignmentCode") | 172 | result["consignmentCode"] = part.get("consignmentCode") |
| 152 | - if "consignmentId" in part: | 173 | + if result["consignmentId"] is None and "consignmentId" in part: |
| 153 | result["consignmentId"] = part.get("consignmentId") | 174 | result["consignmentId"] = part.get("consignmentId") |
| 154 | - if "loginName" in part: | 175 | + if result["loginName"] is None and "loginName" in part: |
| 155 | result["loginName"] = part.get("loginName") | 176 | result["loginName"] = part.get("loginName") |
| 156 | - if "userId" in part: | 177 | + if result["userId"] is None and "userId" in part: |
| 157 | result["userId"] = part.get("userId") | 178 | result["userId"] = part.get("userId") |
| 158 | elif isinstance(content, dict): | 179 | elif isinstance(content, dict): |
| 159 | # content 是字典,直接获取参数 | 180 | # content 是字典,直接获取参数 |
| 160 | - if "token" in content and content.get("token"): | 181 | + if not result["token"] and "token" in content and content.get("token"): |
| 161 | result["token"] = content.get("token") | 182 | result["token"] = content.get("token") |
| 162 | - if "consignmentCode" in content: | 183 | + if result["consignmentCode"] is None and "consignmentCode" in content: |
| 163 | result["consignmentCode"] = content.get("consignmentCode") | 184 | result["consignmentCode"] = content.get("consignmentCode") |
| 164 | - if "consignmentId" in content: | 185 | + if result["consignmentId"] is None and "consignmentId" in content: |
| 165 | result["consignmentId"] = content.get("consignmentId") | 186 | result["consignmentId"] = content.get("consignmentId") |
| 166 | - if "loginName" in content: | 187 | + if result["loginName"] is None and "loginName" in content: |
| 167 | result["loginName"] = content.get("loginName") | 188 | result["loginName"] = content.get("loginName") |
| 168 | - if "userId" in content: | 189 | + if result["userId"] is None and "userId" in content: |
| 169 | result["userId"] = content.get("userId") | 190 | result["userId"] = content.get("userId") |
| 170 | 191 | ||
| 171 | return result | 192 | return result | ... | ... |
-
Please register or login to post a comment