Toggle navigation
Toggle navigation
This project
Loading...
Sign in
zhouhui.jiang
/
Test_LangGraph
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
zhouhui.jiang
2025-11-15 18:41:13 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
aa360d2f103cc7e5f413e2ffe878760b9eae180b
aa360d2f
1 parent
893d2120
update
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
12 deletions
langgraph_examples/api_agent.py
langgraph_examples/api_agent.py
View file @
aa360d2
...
...
@@ -94,7 +94,8 @@ def pre_model_inspect_attachments(state, **kwargs):
def
extract_token
(
state
:
Dict
[
str
,
Any
])
->
Dict
[
str
,
Any
]:
"""
从 state 中提取参数
获取最后一个类型为 HumanMessage 或 human 的消息中的参数(token、consignmentCode、consignmentId、loginName、userId)
优先从 additional_kwargs 中提取参数(token、consignmentCode、consignmentId、loginName、userId)
如果 additional_kwargs 中没有,再从 content 中提取(作为备选)
Args:
state: LangGraph 状态字典,包含 messages 数组
...
...
@@ -135,37 +136,57 @@ def extract_token(state: Dict[str, Any]) -> Dict[str, Any]:
# 直接取最后一个 human 消息(不需要循环判断)
last_human_msg
=
human_messages
[
-
1
]
# 从 content 中提取参数
# 优先从 additional_kwargs 中提取参数
if
isinstance
(
last_human_msg
,
dict
):
additional_kwargs
=
last_human_msg
.
get
(
"additional_kwargs"
,
{})
else
:
additional_kwargs
=
getattr
(
last_human_msg
,
"additional_kwargs"
,
{})
if
additional_kwargs
:
# 从 additional_kwargs 中提取参数
if
"token"
in
additional_kwargs
and
additional_kwargs
.
get
(
"token"
):
result
[
"token"
]
=
additional_kwargs
.
get
(
"token"
)
if
"consignmentCode"
in
additional_kwargs
:
result
[
"consignmentCode"
]
=
additional_kwargs
.
get
(
"consignmentCode"
)
if
"consignmentId"
in
additional_kwargs
:
result
[
"consignmentId"
]
=
additional_kwargs
.
get
(
"consignmentId"
)
if
"loginName"
in
additional_kwargs
:
result
[
"loginName"
]
=
additional_kwargs
.
get
(
"loginName"
)
if
"userId"
in
additional_kwargs
:
result
[
"userId"
]
=
additional_kwargs
.
get
(
"userId"
)
# 如果 additional_kwargs 中没有某些参数,再从 content 中提取(作为备选)
if
isinstance
(
last_human_msg
,
dict
):
content
=
last_human_msg
.
get
(
"content"
)
else
:
content
=
getattr
(
last_human_msg
,
"content"
,
None
)
# 只有在 additional_kwargs 中没有找到对应参数时,才从 content 中提取
if
isinstance
(
content
,
list
):
# content 是列表,遍历查找包含参数的 part
for
part
in
content
:
if
isinstance
(
part
,
dict
):
if
"token"
in
part
and
part
.
get
(
"token"
):
if
not
result
[
"token"
]
and
"token"
in
part
and
part
.
get
(
"token"
):
result
[
"token"
]
=
part
.
get
(
"token"
)
if
"consignmentCode"
in
part
:
if
result
[
"consignmentCode"
]
is
None
and
"consignmentCode"
in
part
:
result
[
"consignmentCode"
]
=
part
.
get
(
"consignmentCode"
)
if
"consignmentId"
in
part
:
if
result
[
"consignmentId"
]
is
None
and
"consignmentId"
in
part
:
result
[
"consignmentId"
]
=
part
.
get
(
"consignmentId"
)
if
"loginName"
in
part
:
if
result
[
"loginName"
]
is
None
and
"loginName"
in
part
:
result
[
"loginName"
]
=
part
.
get
(
"loginName"
)
if
"userId"
in
part
:
if
result
[
"userId"
]
is
None
and
"userId"
in
part
:
result
[
"userId"
]
=
part
.
get
(
"userId"
)
elif
isinstance
(
content
,
dict
):
# content 是字典,直接获取参数
if
"token"
in
content
and
content
.
get
(
"token"
):
if
not
result
[
"token"
]
and
"token"
in
content
and
content
.
get
(
"token"
):
result
[
"token"
]
=
content
.
get
(
"token"
)
if
"consignmentCode"
in
content
:
if
result
[
"consignmentCode"
]
is
None
and
"consignmentCode"
in
content
:
result
[
"consignmentCode"
]
=
content
.
get
(
"consignmentCode"
)
if
"consignmentId"
in
content
:
if
result
[
"consignmentId"
]
is
None
and
"consignmentId"
in
content
:
result
[
"consignmentId"
]
=
content
.
get
(
"consignmentId"
)
if
"loginName"
in
content
:
if
result
[
"loginName"
]
is
None
and
"loginName"
in
content
:
result
[
"loginName"
]
=
content
.
get
(
"loginName"
)
if
"userId"
in
content
:
if
result
[
"userId"
]
is
None
and
"userId"
in
content
:
result
[
"userId"
]
=
content
.
get
(
"userId"
)
return
result
...
...
Please
register
or
login
to post a comment