vlapparov commited on
Commit
e3d400c
·
verified ·
1 Parent(s): a768079

Create text_inspector_tool.py

Browse files
Files changed (1) hide show
  1. text_inspector_tool.py +122 -0
text_inspector_tool.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from smolagents import Tool
4
+ from smolagents.models import MessageRole, Model
5
+
6
+ from .mdconvert import MarkdownConverter
7
+
8
+
9
+ class TextInspectorTool(Tool):
10
+ name = "inspect_file_as_text"
11
+ description = """
12
+ You cannot load files yourself: instead call this tool to read a file as markdown text and ask questions about it.
13
+ This tool handles the following file extensions: [".html", ".htm", ".xlsx", ".pptx", ".wav", ".mp3", ".m4a", ".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 = model
32
+ self.text_limit = text_limit
33
+
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_content
42
+
43
+ if not question:
44
+ return result.text_content
45
+
46
+ if len(result.text_content) < 4000:
47
+ return "Document content: " + result.text_content
48
+
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
+ + question
69
+ + "\n\nDon't answer the question yourself! Just provide useful notes on the document",
70
+ }
71
+ ],
72
+ },
73
+ ]
74
+ return self.model(messages).content
75
+
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_content
84
+
85
+ if not question:
86
+ return result.text_content
87
+
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).content