添加媒体分析工具,tavily_search
Browse files112345672 unmodified lines101112131415161718192043 unmodified lines64656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612765128129130131from smolagents import DuckDuckGoSearchTool, Tool,toolfrom smolagents import DuckDuckGoSearchTool, Tool, toolimport wikipediaapiimport osimport requests
class 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 lines except 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 Image with 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 result except Exception as e: return f"Error analyzing file: {str(e)}"
my_tool_list = [ WikipediaSearchTool(), DuckDuckGoSearchTool() DuckDuckGoSearchTool(), tavily_search, analyze_media_file,]my_tools.py+66-2