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-10 16:35:42 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c859d6634e7a98977e93e2a9b94f31a49ebb5a1b
c859d663
1 parent
1d9c29c4
update
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
10 deletions
API/api_config.py
langgraph_examples/api_agent.py
API/api_config.py
View file @
c859d66
...
...
@@ -13,7 +13,7 @@ API_CONFIG = {
"headers"
:
{
"accept"
:
"*/*"
,
"Content-Type"
:
"application/json"
,
"Authorization"
:
os
.
getenv
(
"API_AUTHORIZATION"
,
"Bearer 2.
2e71fc6f79704ee0b266679e6427a28
e"
),
"Authorization"
:
os
.
getenv
(
"API_AUTHORIZATION"
,
"Bearer 2.
70e56e690ce64cb8b1e4fc892e1b71d
e"
),
"Ver"
:
os
.
getenv
(
"API_VER"
,
"033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"
),
"Referer"
:
os
.
getenv
(
"API_REFERER"
,
"http://192.168.1.251/"
)
}
...
...
langgraph_examples/api_agent.py
View file @
c859d66
...
...
@@ -3,7 +3,9 @@ from langchain_openai import ChatOpenAI
import
os
import
sys
import
json
from
typing
import
Dict
,
Any
from
typing
import
Dict
,
Any
,
List
from
langchain_core.runnables
import
RunnableConfig
from
langchain_core.messages
import
AnyMessage
# 添加项目根目录到 Python 路径
sys
.
path
.
append
(
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))))
...
...
@@ -87,17 +89,95 @@ def pre_model_inspect_attachments(state, **kwargs):
traceback
.
print_exc
()
return
{}
# 创建 ReAct 智能体
agent
=
create_react_agent
(
model
=
model
,
tools
=
[
query_waybill_list
,
create_waybill_d
,
upload_clearance_file
,
push_waybill_for_ocr
],
pre_model_hook
=
pre_model_inspect_attachments
,
prompt
=
"""你是一个专业的出口物流系统智能助手,专门帮助用户处理运单相关的业务操作。
def
extract_token
(
state
:
Dict
[
str
,
Any
])
->
str
:
"""
从 state 中提取 token
获取最后一个类型为 HumanMessage 或 human 的消息中的 token
Args:
state: LangGraph 状态字典,包含 messages 数组
Returns:
token 字符串,如果未找到则返回空字符串
"""
messages
=
state
.
get
(
"messages"
,
[])
if
not
messages
:
return
""
# 找到所有 is_human 类型的消息
human_messages
=
[]
for
msg
in
messages
:
# 兼容 dict 或 LangChain 的消息对象
if
isinstance
(
msg
,
dict
):
msg_type
=
msg
.
get
(
"type"
)
else
:
msg_type
=
msg
.
__class__
.
__name__
# 检查是否是 human 类型的消息
is_human
=
(
msg_type
==
"HumanMessage"
or
msg_type
==
"human"
)
if
is_human
:
human_messages
.
append
(
msg
)
# 如果没有 human 消息,直接返回
if
not
human_messages
:
return
""
# 直接取最后一个 human 消息(不需要循环判断)
last_human_msg
=
human_messages
[
-
1
]
# 从 content 中提取 token
if
isinstance
(
last_human_msg
,
dict
):
content
=
last_human_msg
.
get
(
"content"
)
else
:
content
=
getattr
(
last_human_msg
,
"content"
,
None
)
if
isinstance
(
content
,
list
):
# content 是列表,遍历查找包含 token 的 part
for
part
in
content
:
if
isinstance
(
part
,
dict
)
and
"token"
in
part
:
token
=
part
.
get
(
"token"
)
if
token
:
return
token
elif
isinstance
(
content
,
dict
):
# content 是字典,直接获取 token
if
"token"
in
content
:
token
=
content
.
get
(
"token"
)
if
token
:
return
token
return
""
def
_create_system_prompt
(
state
:
Dict
[
str
,
Any
],
config
:
RunnableConfig
)
->
List
[
AnyMessage
]:
"""
创建动态系统提示词
Args:
state: LangGraph 状态字典
config: Runnable 配置
Returns:
包含系统消息和原始消息的列表
"""
# 从 state 中提取动态参数(目前先不提取,等后续需要时再添加)
# 例如:token = state.get("token")
# 例如:user_id = state.get("user_id")
token
=
extract_token
(
state
)
# 如果从 state 中提取的 token 为空,则从 api_config.py 中获取 Authorization 作为备选
if
not
token
:
from
API.api_config
import
API_CONFIG
token
=
API_CONFIG
.
get
(
"headers"
,
{})
.
get
(
"Authorization"
,
""
)
# 创建系统提示词(内容保持不变,后续可以添加动态参数)
system_msg
=
f
"""你是一个专业的出口物流系统智能助手,专门帮助用户处理运单相关的业务操作。
## 你的主要职责:
1. **运单查询**:根据用户需求查询运单列表,支持按状态、时间等条件筛选,结果以JSON形式展示,AI不用对返回数据JSON进行加工
2. **运单创建**:协助用户创建D类运单,确保信息完整准确
4. **业务咨询**:解答用户关于出口物流流程、运单状态、操作规范等问题
3. **业务咨询**:解答用户关于出口物流流程、运单状态、操作规范等问题
4. **您当前访问工具的权限值为 Authorization: {token}**
## 工作原则:
- 上传时文件路径不用确认,路径肯定是完整的,文件肯定是存在的,不用确认文件索引,工具会自动设置索引为0,请直接调用工具接口
...
...
@@ -107,7 +187,7 @@ agent = create_react_agent(
- 如遇到错误,主动分析原因并提供解决方案
- 保持专业、友好的沟通态度
- 严禁改写工具函数返回的文本格式;对工具输出仅直接转述,不得增删前后缀或改写内容。
- 若调用了工具并获得结果,则必须将该工具返回的文本“原样作为最终答复”
输出,不允许添加任何解释、建议或额外文字。
- 若调用了工具并获得结果,则必须将该工具返回的文本"原样作为最终答复"
输出,不允许添加任何解释、建议或额外文字。
## 可用工具:
- query_waybill_list: 查询运单列表,支持按状态筛选,结果以JSON形式展示,AI不用对返回数据JSON进行加工
...
...
@@ -122,7 +202,19 @@ agent = create_react_agent(
## create_waybill_d数据展示说明:
- 按照数据返回的原本格式进行展示,不得增删前后缀或改写内容。
请根据用户的具体需求,选择合适的工具并提供帮助。"""
# 返回系统消息 + 原始消息
return
[{
"role"
:
"system"
,
"content"
:
system_msg
}]
+
state
.
get
(
"messages"
,
[])
# 创建 ReAct 智能体
agent
=
create_react_agent
(
model
=
model
,
tools
=
[
query_waybill_list
,
create_waybill_d
,
upload_clearance_file
,
push_waybill_for_ocr
],
pre_model_hook
=
pre_model_inspect_attachments
,
prompt
=
_create_system_prompt
,
# 使用动态提示词函数
)
# 如果直接运行此文件
...
...
Please
register
or
login
to post a comment