Skip to content

Commit

Permalink
fix: summary of duckduckgo_search (#5488)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjlarry authored and XiaoLey committed Jun 22, 2024
1 parent 3c4075b commit 02a3ec3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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

0 comments on commit 02a3ec3

Please sign in to comment.