Support Video Proxy and TED Embedding (#10819)

This commit is contained in:
Tao Wang 2024-11-19 01:49:14 -08:00 committed by GitHub
parent 1cc7dc6360
commit 9f195df103
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 33 deletions

View File

@ -1,4 +1,4 @@
from typing import Any from typing import Any, ClassVar
from duckduckgo_search import DDGS from duckduckgo_search import DDGS
@ -11,6 +11,17 @@ class DuckDuckGoVideoSearchTool(BuiltinTool):
Tool for performing a video search using DuckDuckGo search engine. Tool for performing a video search using DuckDuckGo search engine.
""" """
IFRAME_TEMPLATE: ClassVar[str] = """
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; \
max-width: 100%; border-radius: 8px;">
<iframe
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
src="{src}"
frameborder="0"
allowfullscreen>
</iframe>
</div>"""
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> list[ToolInvokeMessage]: def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> list[ToolInvokeMessage]:
query_dict = { query_dict = {
"keywords": tool_parameters.get("query"), "keywords": tool_parameters.get("query"),
@ -26,6 +37,9 @@ class DuckDuckGoVideoSearchTool(BuiltinTool):
# Remove None values to use API defaults # Remove None values to use API defaults
query_dict = {k: v for k, v in query_dict.items() if v is not None} query_dict = {k: v for k, v in query_dict.items() if v is not None}
# Get proxy URL from parameters
proxy_url = tool_parameters.get("proxy_url", "").strip()
response = DDGS().videos(**query_dict) response = DDGS().videos(**query_dict)
# Create HTML result with embedded iframes # Create HTML result with embedded iframes
@ -36,20 +50,21 @@ class DuckDuckGoVideoSearchTool(BuiltinTool):
title = res.get("title", "") title = res.get("title", "")
embed_html = res.get("embed_html", "") embed_html = res.get("embed_html", "")
description = res.get("description", "") description = res.get("description", "")
content_url = res.get("content", "")
# Modify iframe to be responsive # Handle TED.com videos
if embed_html: if not embed_html and "ted.com/talks" in content_url:
# Replace fixed dimensions with responsive wrapper and iframe embed_url = content_url.replace("www.ted.com", "embed.ted.com")
embed_html = """ if proxy_url:
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; \ embed_url = f"{proxy_url}{embed_url}"
max-width: 100%; border-radius: 8px;"> embed_html = self.IFRAME_TEMPLATE.format(src=embed_url)
<iframe
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" # Original YouTube/other platform handling
src="{src}" elif embed_html:
frameborder="0" embed_url = res.get("embed_url", "")
allowfullscreen> if proxy_url and embed_url:
</iframe> embed_url = f"{proxy_url}{embed_url}"
</div>""".format(src=res.get("embed_url", "")) embed_html = self.IFRAME_TEMPLATE.format(src=embed_url)
markdown_result += f"{title}\n\n" markdown_result += f"{title}\n\n"
markdown_result += f"{embed_html}\n\n" markdown_result += f"{embed_html}\n\n"

View File

@ -1,40 +1,43 @@
identity: identity:
name: ddgo_video name: ddgo_video
author: Assistant author: Tao Wang
label: label:
en_US: DuckDuckGo Video Search en_US: DuckDuckGo Video Search
zh_Hans: DuckDuckGo 视频搜索 zh_Hans: DuckDuckGo 视频搜索
description: description:
human: human:
en_US: Perform video searches on DuckDuckGo and get results with embedded videos. en_US: Search and embedded videos.
zh_Hans: 在 DuckDuckGo 上进行视频搜索并获取可嵌入视频结果。 zh_Hans: 搜索并嵌入视频
llm: Perform video searches on DuckDuckGo and get results with embedded videos. llm: Search videos on duckduckgo and embed videos in iframe
parameters: parameters:
- name: query - name: query
type: string
required: true
label: label:
en_US: Query String en_US: Query String
zh_Hans: 查询语句 zh_Hans: 查询语句
type: string
required: true
human_description: human_description:
en_US: Search Query en_US: Search Query
zh_Hans: 搜索查询语句 zh_Hans: 搜索查询语句
llm_description: Key words for searching llm_description: Key words for searching
form: llm form: llm
- name: max_results - name: max_results
label:
en_US: Max Results
zh_Hans: 最大结果数量
type: number type: number
required: true required: true
default: 3 default: 3
minimum: 1 minimum: 1
maximum: 10 maximum: 10
label:
en_US: Max Results
zh_Hans: 最大结果数量
human_description: human_description:
en_US: The max results (1-10). en_US: The max results (1-10)
zh_Hans: 最大结果数量1-10 zh_Hans: 最大结果数量1-10
form: form form: form
- name: timelimit - name: timelimit
label:
en_US: Result Time Limit
zh_Hans: 结果时间限制
type: select type: select
required: false required: false
options: options:
@ -54,14 +57,14 @@ parameters:
label: label:
en_US: Current Year en_US: Current Year
zh_Hans: 今年 zh_Hans: 今年
label:
en_US: Result Time Limit
zh_Hans: 结果时间限制
human_description: human_description:
en_US: Use when querying results within a specific time range only. en_US: Query results within a specific time range only
zh_Hans: 只查询一定时间范围内的结果时使用 zh_Hans: 只查询一定时间范围内的结果时使用
form: form form: form
- name: duration - name: duration
label:
en_US: Video Duration
zh_Hans: 视频时长
type: select type: select
required: false required: false
options: options:
@ -77,10 +80,18 @@ parameters:
label: label:
en_US: Long (>20 minutes) en_US: Long (>20 minutes)
zh_Hans: 长视频(>20分钟 zh_Hans: 长视频(>20分钟
label:
en_US: Video Duration
zh_Hans: 视频时长
human_description: human_description:
en_US: Filter videos by duration en_US: Filter videos by duration
zh_Hans: 按时长筛选视频 zh_Hans: 按时长筛选视频
form: form form: form
- name: proxy_url
label:
en_US: Proxy URL
zh_Hans: 视频代理地址
type: string
required: false
default: ""
human_description:
en_US: Proxy URL
zh_Hans: 视频代理地址
form: form