diff --git a/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py b/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py index bf963f3442fd6c..442f29f33dfcef 100644 --- a/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py +++ b/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py @@ -2,9 +2,20 @@ from duckduckgo_search import DDGS +from core.model_runtime.entities.message_entities import SystemPromptMessage from core.tools.entities.tool_entities import ToolInvokeMessage from core.tools.tool.builtin_tool import BuiltinTool +SUMMARY_PROMPT = """ +User's query: +{query} + +Here is the search engine result: +{content} + +Please summarize the result in a few sentences. +""" + class DuckDuckGoSearchTool(BuiltinTool): """ @@ -25,5 +36,12 @@ def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMe results = [res.get("body") for res in response] results = "\n".join(results) if require_summary: - results = self.summary(user_id=user_id, content=results) + results = self.summary_results(user_id=user_id, content=results, query=query) return self.create_text_message(text=results) + + def summary_results(self, user_id: str, content: str, query: str) -> str: + prompt = SUMMARY_PROMPT.format(query=query, content=content) + summary = self.invoke_model(user_id=user_id, prompt_messages=[ + SystemPromptMessage(content=prompt), + ], stop=[]) + return summary.message.content