Skip to content

Build and run agents you can see, understand and trust.

fix(deepresearch): fix the response error in deep research agent (#1000)

Haohao Luo1c9d88b9ca1a
diff --git a/examples/agent/deep_research_agent/deep_research_agent.py b/examples/agent/deep_research_agent/deep_research_agent.py
index a3fc880f1..a76a27917 100644
--- a/examples/agent/deep_research_agent/deep_research_agent.py
+++ b/examples/agent/deep_research_agent/deep_research_agent.py
@@ -98,6 +98,7 @@ class DeepResearchAgent(ReActAgent):
sys_prompt: str = _DEEP_RESEARCH_AGENT_DEFAULT_SYS_PROMPT,
max_iters: int = 30,
max_depth: int = 3,
+ max_tool_results_words: int = 10000,
tmp_file_storage_dir: str = "tmp",
) -> None:
"""Initialize the Deep Research Agent.
@@ -125,6 +126,10 @@ class DeepResearchAgent(ReActAgent):
max_depth (int, optional):
The maximum depth of query expansion during deep searching.
Defaults to 3.
+ max_tool_results_words (int, optional):
+ The maximum number of words to keep from a tool's output before
+ truncation to fit the model's context.
+ Defaults to 10000.
tmp_file_storage_dir (str, optional):
The storage dir for generated files.
Default to 'tmp'
@@ -153,6 +158,7 @@ class DeepResearchAgent(ReActAgent):
max_iters=max_iters,
)
self.max_depth = max_depth
+ self.max_tool_results_words = max_tool_results_words
self.memory = memory
self.tmp_file_storage_dir = tmp_file_storage_dir
self.current_subtask = []
@@ -179,6 +185,7 @@ class DeepResearchAgent(ReActAgent):
self.user_query = None
# add functions into toolkit
+ self.toolkit.register_tool_function(self.generate_response)
self.toolkit.register_tool_function(self.reflect_failure)
self.toolkit.register_tool_function(
self.summarize_intermediate_results,
@@ -352,6 +359,7 @@ class DeepResearchAgent(ReActAgent):
]:
tool_res_msg.content[0]["output"] = truncate_search_result(
tool_res_msg.content[0]["output"],
+ self.max_tool_results_words,
)
# Update memory when an intermediate report is generated
@@ -629,6 +637,7 @@ class DeepResearchAgent(ReActAgent):
"output"
] = truncate_search_result(
extract_tool_res_msg.content[0]["output"],
+ self.max_tool_results_words,
)
# await self.memory.add(tool_res_msg)
await self.print(extract_tool_res_msg, True)
diff --git a/examples/agent/deep_research_agent/main.py b/examples/agent/deep_research_agent/main.py
index 7dca3b2b2..358537179 100644
--- a/examples/agent/deep_research_agent/main.py
+++ b/examples/agent/deep_research_agent/main.py
@@ -41,7 +41,7 @@ async def main(user_query: str) -> None:
sys_prompt="You are a helpful assistant named Friday.",
model=DashScopeChatModel(
api_key=os.environ.get("DASHSCOPE_API_KEY"),
- model_name="qwen-max",
+ model_name="qwen3-max",
enable_thinking=False,
stream=True,
),
@@ -49,6 +49,7 @@ async def main(user_query: str) -> None:
memory=InMemoryMemory(),
search_mcp_client=tavily_search_client,
tmp_file_storage_dir=agent_working_dir,
+ max_tool_results_words=10000,
)
user_name = "Bob"
msg = Msg(
diff --git a/examples/agent/deep_research_agent/utils.py b/examples/agent/deep_research_agent/utils.py
index 75395ddb3..2e39330ab 100644
--- a/examples/agent/deep_research_agent/utils.py
+++ b/examples/agent/deep_research_agent/utils.py
@@ -9,9 +9,6 @@ from pydantic import BaseModel
from agentscope.tool import Toolkit, ToolResponse
-TOOL_RESULTS_MAX_WORDS = 5000
-
-
def get_prompt_from_file(
file_path: str,
return_json: bool,
@@ -25,7 +22,10 @@ def get_prompt_from_file(
return prompt
-def truncate_by_words(sentence: str) -> str:
+def truncate_by_words(
+ sentence: str,
+ max_tool_results_words: int = 10000,
+) -> str:
"""Truncate too long sentences by words number"""
words = re.findall(
r"\w+|[^\w\s]",
@@ -38,7 +38,7 @@ def truncate_by_words(sentence: str) -> str:
for word in words:
if re.match(r"\w+", word):
word_count += 1
- if word_count > TOOL_RESULTS_MAX_WORDS:
+ if word_count > max_tool_results_words:
break
result.append(word)
@@ -55,6 +55,7 @@ def truncate_by_words(sentence: str) -> str:
def truncate_search_result(
res: list,
+ max_tool_results_words: int = 10000,
search_func: str = "tavily-search",
extract_function: str = "tavily-extract",
) -> list:
@@ -65,7 +66,10 @@ def truncate_search_result(
)
for i, val in enumerate(res):
- res[i]["text"] = truncate_by_words(val["text"])
+ res[i]["text"] = truncate_by_words(
+ val["text"],
+ max_tool_results_words,
+ )
return res