Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2 firecrawl tools : Scrape and Search #6016

Merged
merged 22 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/core/tools/provider/builtin/firecrawl/tools/scrape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from typing import Any, Union

from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.provider.builtin.firecrawl.firecrawl_appx import FirecrawlApp
from core.tools.tool.builtin_tool import BuiltinTool


class ScrapeTool(BuiltinTool):
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
app = FirecrawlApp(api_key=self.runtime.credentials['firecrawl_api_key'], base_url=self.runtime.credentials['base_url'])

crawl_result = app.scrape_url(
url=tool_parameters['url'],
wait=True
)

if isinstance(crawl_result, dict):
result_message = json.dumps(crawl_result, ensure_ascii=False, indent=4)
else:
result_message = str(crawl_result)

if not crawl_result:
return self.create_text_message("Scrape request failed.")

return self.create_text_message(result_message)
23 changes: 23 additions & 0 deletions api/core/tools/provider/builtin/firecrawl/tools/scrape.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
identity:
name: scrape
author: ahasasjeb
label:
en_US: Scrape
zh_Hans: 抓取
description:
human:
en_US: Extract data from a single URL.
zh_Hans: 从单个URL抓取数据。
llm: This tool is designed to scrape URL and output the content in Markdown format.
parameters:
- name: url
type: string
required: true
label:
en_US: URL to scrape
zh_Hans: 要抓取的URL
human_description:
en_US: The URL of the website to scrape and extract data from.
zh_Hans: 要抓取并提取数据的网站URL。
llm_description: The URL of the website that needs to be crawled. This is a required parameter.
form: llm
26 changes: 26 additions & 0 deletions api/core/tools/provider/builtin/firecrawl/tools/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from typing import Any, Union

from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.provider.builtin.firecrawl.firecrawl_appx import FirecrawlApp
from core.tools.tool.builtin_tool import BuiltinTool


class SearchTool(BuiltinTool):
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
app = FirecrawlApp(api_key=self.runtime.credentials['firecrawl_api_key'], base_url=self.runtime.credentials['base_url'])

crawl_result = app.search(
query=tool_parameters['keyword'],
wait=True
)

if isinstance(crawl_result, dict):
result_message = json.dumps(crawl_result, ensure_ascii=False, indent=4)
else:
result_message = str(crawl_result)

if not crawl_result:
return self.create_text_message("Search request failed.")

return self.create_text_message(result_message)
23 changes: 23 additions & 0 deletions api/core/tools/provider/builtin/firecrawl/tools/search.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
identity:
name: search
author: ahasasjeb
label:
en_US: Search
zh_Hans: 搜索
description:
human:
en_US: Search, and output in Markdown format
zh_Hans: 搜索,并且以Markdown格式输出
llm: This tool can perform online searches and convert the results to Markdown format.
parameters:
- name: keyword
type: string
required: true
label:
en_US: keyword
zh_Hans: 关键词
human_description:
en_US: Input keywords to use Firecrawl API for search.
zh_Hans: 输入关键词即可使用Firecrawl API进行搜索。
llm_description: Efficiently extract keywords from user text.
form: llm