添加媒体分析工具,tavily_search
Browse files123452 unmodified lines891011121314151643 unmodified lines6061626364656667from smolagents import DuckDuckGoSearchTool, Tool,toolimport wikipediaapiclass WikipediaSearchTool(Tool):name = "wikipedia_search"2 unmodified lines"query": {"type": "string", "description": "维基百科搜索关键词,例如人名/专名"}}output_type = "string"def __init__(self, lang="en"):super().__init__()self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="celum")def forward(self, query: str):page = self.wiki.page(query)if not page.exists():43 unmodified linesexcept Exception as e:return f"Tavily search error: {str(e)}"my_tool_list = [WikipediaSearchTool(),DuckDuckGoSearchTool()tavily_search,]12345672 unmodified lines101112131415161718192043 unmodified lines646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131from smolagents import DuckDuckGoSearchTool, Tool, toolimport wikipediaapiimport osimport requestsclass WikipediaSearchTool(Tool):name = "wikipedia_search"2 unmodified lines"query": {"type": "string", "description": "维基百科搜索关键词,例如人名/专名"}}output_type = "string"def __init__(self, lang="en"):super().__init__()self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="celum")def forward(self, query: str):page = self.wiki.page(query)if not page.exists():43 unmodified linesexcept Exception as e:return f"Tavily search error: {str(e)}"# 添加媒体分析工具@tooldef analyze_media_file(file_path: str) -> str:"""分析媒体文件并返回基本信息Args:file_path: 文件路径Returns:文件信息和分析结果"""if not os.path.exists(file_path):return f"File not found: {file_path}"try:file_size = os.path.getsize(file_path)file_ext = os.path.splitext(file_path)[1].lower()result = f"File: {os.path.basename(file_path)}\n"result += f"Size: {file_size} bytes\n"result += f"Extension: {file_ext}\n"# 根据文件类型进行不同的分析if file_ext in ['.png', '.jpg', '.jpeg', '.gif', '.bmp']:try:from PIL import Imagewith Image.open(file_path) as img:result += f"Image dimensions: {img.size[0]}x{img.size[1]}\n"result += f"Image mode: {img.mode}\n"result += f"Image format: {img.format}\n"# 检测是否可能是棋盘if abs(img.size[0] - img.size[1]) < 50:result += "Note: Square aspect ratio - possibly a chess board\n"except Exception as e:result += f"Image analysis error: {str(e)}\n"elif file_ext in ['.mp4', '.avi', '.mov', '.mkv']:result += "Video file detected\n"elif file_ext in ['.mp3', '.wav', '.m4a', '.flac']:result += "Audio file detected\n"elif file_ext in ['.pdf']:result += "PDF file detected\n"elif file_ext in ['.txt', '.csv', '.json']:try:with open(file_path, 'r', encoding='utf-8') as f:content = f.read()result += f"Text length: {len(content)} characters\n"result += f"Line count: {len(content.splitlines())}\n"preview = content[:200].replace('\n', ' ')result += f"Content preview: {preview}...\n"except Exception as e:result += f"Text analysis error: {str(e)}\n"return resultexcept Exception as e:return f"Error analyzing file: {str(e)}"my_tool_list = [WikipediaSearchTool(),DuckDuckGoSearchTool(),tavily_search,analyze_media_file,]
my_tools.py+66-2