Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 17c6f14d
authored
Jun 13, 2026
by
sunqi02
Committed by
胡悦
Jul 06, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 更新 OpenAIChatFormatter,静默丢弃 thinking block,避免 LLM 误解上下文
1 parent
84fe91e3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
11 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
17c6f14
...
@@ -14,11 +14,15 @@ from agentscope.formatter import OpenAIChatFormatter as _OpenAIChatFormatter
...
@@ -14,11 +14,15 @@ from agentscope.formatter import OpenAIChatFormatter as _OpenAIChatFormatter
class
OpenAIChatFormatter
(
_OpenAIChatFormatter
):
class
OpenAIChatFormatter
(
_OpenAIChatFormatter
):
"""扩展 AgentScope 的 OpenAI formatter,
支持 thinking block → text 转换
。
"""扩展 AgentScope 的 OpenAI formatter,
静默丢弃 thinking block
。
ReActAgent 在 reasoning 阶段产出的 thinking block 会存入 InMemoryMemory,
ReActAgent 在 reasoning 阶段产出的 thinking block 会存入 InMemoryMemory,
后续 API 调用格式化历史消息时会遇到该类型。原生 formatter 不认识 thinking
后续 API 调用格式化历史消息时会遇到该类型。原生 formatter 不认识 thinking
类型会打 warning,这里将其转为 text block 保留推理上下文。
类型会打 warning。
thinking 文本是 LLM 的内部推理(如"我需要调用XX Agent"),只能通过
post_reasoning hook 摘要后发 IM 给用户看,绝不能回灌给 LLM 作为上下文,
否则 LLM 会误以为是未完成的任务指令而重复执行。
"""
"""
async
def
_format
(
self
,
msgs
:
list
)
->
list
:
async
def
_format
(
self
,
msgs
:
list
)
->
list
:
...
@@ -26,15 +30,10 @@ class OpenAIChatFormatter(_OpenAIChatFormatter):
...
@@ -26,15 +30,10 @@ class OpenAIChatFormatter(_OpenAIChatFormatter):
blocks
=
msg
.
content
if
isinstance
(
msg
.
content
,
list
)
else
[]
blocks
=
msg
.
content
if
isinstance
(
msg
.
content
,
list
)
else
[]
if
not
blocks
:
if
not
blocks
:
continue
continue
new_blocks
=
[]
msg
.
content
=
[
for
block
in
blocks
:
b
for
b
in
blocks
if
isinstance
(
block
,
dict
)
and
block
.
get
(
"type"
)
==
"thinking"
:
if
not
(
isinstance
(
b
,
dict
)
and
b
.
get
(
"type"
)
==
"thinking"
)
text
=
block
.
get
(
"thinking"
,
""
)
]
if
text
.
strip
():
new_blocks
.
append
({
"type"
:
"text"
,
"text"
:
text
})
else
:
new_blocks
.
append
(
block
)
msg
.
content
=
new_blocks
return
await
super
()
.
_format
(
msgs
)
return
await
super
()
.
_format
(
msgs
)
from
agentscope.message
import
Msg
from
agentscope.message
import
Msg
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment