CoolFace
Apppublic

Akjava/open_Deep-Research-DuckDuckGo

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
4likes
text_inspector_tool.py123 linesDownload Raw Back to scripts
1from typing import Optional2 3from smolagents import Tool4from smolagents.models import MessageRole, Model5 6from .mdconvert import MarkdownConverter7 8 9class TextInspectorTool(Tool):10    name = "inspect_file_as_text"11    description = """12You cannot load files yourself: instead call this tool to read a file as markdown text and ask questions about it.13This tool handles the following file extensions: [".html", ".htm", ".xlsx", ".pptx", ".wav", ".mp3", ".flac", ".pdf", ".docx"], and all other types of text files. IT DOES NOT HANDLE IMAGES."""14 15    inputs = {16        "file_path": {17            "description": "The path to the file you want to read as text. Must be a '.something' file, like '.pdf'. If it is an image, use the visualizer tool instead! DO NOT use this tool for an HTML webpage: use the web_search tool instead!",18            "type": "string",19        },20        "question": {21            "description": "[Optional]: Your question, as a natural language sentence. Provide as much context as possible. Do not pass this parameter if you just want to directly return the content of the file.",22            "type": "string",23            "nullable": True,24        },25    }26    output_type = "string"27    md_converter = MarkdownConverter()28 29    def __init__(self, model: Model, text_limit: int):30        super().__init__()31        self.model = model32        self.text_limit = text_limit33 34    def forward_initial_exam_mode(self, file_path, question):35        result = self.md_converter.convert(file_path)36 37        if file_path[-4:] in [".png", ".jpg"]:38            raise Exception("Cannot use inspect_file_as_text tool with images: use visualizer instead!")39 40        if ".zip" in file_path:41            return result.text_content42 43        if not question:44            return result.text_content45 46        if len(result.text_content) < 4000:47            return "Document content: " + result.text_content48 49        messages = [50            {51                "role": MessageRole.SYSTEM,52                "content": [53                    {54                        "type": "text",55                        "text": "Here is a file:\n### "56                        + str(result.title)57                        + "\n\n"58                        + result.text_content[: self.text_limit],59                    }60                ],61            },62            {63                "role": MessageRole.USER,64                "content": [65                    {66                        "type": "text",67                        "text": "Now please write a short, 5 sentence caption for this document, that could help someone asking this question: "68                        + question69                        + "\n\nDon't answer the question yourself! Just provide useful notes on the document",70                    }71                ],72            },73        ]74        return self.model(messages).content75 76    def forward(self, file_path, question: Optional[str] = None) -> str:77        result = self.md_converter.convert(file_path)78 79        if file_path[-4:] in [".png", ".jpg"]:80            raise Exception("Cannot use inspect_file_as_text tool with images: use visualizer instead!")81 82        if ".zip" in file_path:83            return result.text_content84 85        if not question:86            return result.text_content87 88        messages = [89            {90                "role": MessageRole.SYSTEM,91                "content": [92                    {93                        "type": "text",94                        "text": "You will have to write a short caption for this file, then answer this question:"95                        + question,96                    }97                ],98            },99            {100                "role": MessageRole.USER,101                "content": [102                    {103                        "type": "text",104                        "text": "Here is the complete file:\n### "105                        + str(result.title)106                        + "\n\n"107                        + result.text_content[: self.text_limit],108                    }109                ],110            },111            {112                "role": MessageRole.USER,113                "content": [114                    {115                        "type": "text",116                        "text": "Now answer the question below. Use these three headings: '1. Short answer', '2. Extremely detailed answer', '3. Additional Context on the document and question asked'."117                        + question,118                    }119                ],120            },121        ]122        return self.model(messages).content123