Repository navigation
Expand file tree
/
Copy pathloader.py
More file actions
310 lines (255 loc) · 9.38 KB
/
Copy pathloader.py
File metadata and controls
310 lines (255 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import asyncio
import json
import os
from collections import defaultdict
import agentops
import colorama
import ollama
import weave
from groq import AsyncGroq, Groq
from llama_index.core import Document, SimpleDirectoryReader
from llama_index.core.schema import ImageDocument
from llama_index.core.node_parser import TokenTextSplitter
from termcolor import colored
@agentops.record_function("get directory summaries")
async def get_dir_summaries(path: str):
doc_dicts = load_documents(path)
# metadata = process_metadata(doc_dicts)
summaries = await get_summaries(doc_dicts)
# Convert path to relative path
for summary in summaries:
summary["file_path"] = os.path.relpath(summary["file_path"], path)
return summaries
# [
# {
# file_path:
# file_name:
# file_size:
# content:
# summary:
# creation_date:
# last_modified_date:
# }
# ]
@agentops.record_function("load documents")
def load_documents(path: str):
reader = SimpleDirectoryReader(
input_dir=path,
recursive=True,
required_exts=[
".pdf",
# ".docx",
# ".py",
".txt",
# ".md",
".png",
".jpg",
".jpeg",
# ".ts",
],
)
splitter = TokenTextSplitter(chunk_size=6144)
documents = []
for docs in reader.iter_data():
# By default, llama index split files into multiple "documents"
if len(docs) > 1:
# So we first join all the document contexts, then truncate by token count
for d in docs:
# Some files will not have text and need to be handled
contents = splitter.split_text("\n".join(d.text))
if len(contents) > 0:
text = contents[0]
else:
text = ""
documents.append(Document(text=text, metadata=docs[0].metadata))
else:
documents.append(docs[0])
return documents
@agentops.record_tool("process_metadata")
def process_metadata(doc_dicts):
file_seen = set()
metadata_list = []
for doc in doc_dicts:
if doc["file_path"] not in file_seen:
file_seen.add(doc["file_path"])
metadata_list.append(doc)
return metadata_list
async def summarize_document(doc, client):
PROMPT = """
You will be provided with the contents of a file along with its metadata. Provide a summary of the contents. The purpose of the summary is to organize files based on their content. To this end provide a concise but informative summary. Make the summary as specific to the file as possible.
Write your response a JSON object with the following schema:
```json
{
"file_path": "path to the file including name",
"summary": "summary of the content"
}
```
""".strip()
max_retries = 5
attempt = 0
while attempt < max_retries:
try:
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": PROMPT},
{"role": "user", "content": json.dumps(doc)},
],
model="llama-3.1-70b-versatile",
response_format={"type": "json_object"},
temperature=0,
)
break
except Exception as e:
print("Error status {}".format(e.status_code))
attempt += 1
summary = json.loads(chat_completion.choices[0].message.content)
try:
# Print the filename in green
print(colored(summary["file_path"], "green"))
print(summary["summary"]) # Print the summary of the contents
# Print a separator line with spacing for readability
print("-" * 80 + "\n")
except KeyError as e:
print(e)
print(summary)
return summary
async def summarize_image_document(doc: ImageDocument, client):
PROMPT = """
You will be provided with an image along with its metadata. Provide a summary of the image contents. The purpose of the summary is to organize files based on their content. To this end provide a concise but informative summary. Make the summary as specific to the file as possible.
Write your response a JSON object with the following schema:
```json
{
"file_path": "path to the file including name",
"summary": "summary of the content"
}
```
""".strip()
client = ollama.AsyncClient()
chat_completion = await client.chat(
messages=[
# {"role": "system", "content": "Respond with one short sentence."},
{
"role": "user",
"content": "Summarize the contents of this image.",
"images": [doc.image_path],
},
],
model="moondream",
# format="json",
# stream=True,
options={"num_predict": 128},
)
summary = {
"file_path": doc.image_path,
"summary": chat_completion["message"]["content"],
}
# Print the filename in green
print(colored(summary["file_path"], "green"))
print(summary["summary"]) # Print the summary of the contents
# Print a separator line with spacing for readability
print("-" * 80 + "\n")
return summary
async def dispatch_summarize_document(doc, client):
if isinstance(doc, ImageDocument):
return await summarize_image_document(doc, client)
elif isinstance(doc, Document):
return await summarize_document({"content": doc.text, **doc.metadata}, client)
else:
raise ValueError("Document type not supported")
async def get_summaries(documents):
client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
summaries = await asyncio.gather(
*[dispatch_summarize_document(doc, client) for doc in documents]
)
return summaries
@agentops.record_function("merge")
def merge_summary_documents(summaries, metadata_list):
list_summaries = defaultdict(list)
for item in summaries:
list_summaries[item["file_path"]].append(item["summary"])
file_summaries = {
path: ". ".join(summaries) for path, summaries in list_summaries.items()
}
file_list = [
{"summary": file_summaries[file["file_path"]], **file} for file in metadata_list
]
return file_list
################################################################################################
# Non-async versions of the functions #
################################################################################################
def get_file_summary(path: str):
client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
reader = SimpleDirectoryReader(input_files=[path]).iter_data()
docs = next(reader)
splitter = TokenTextSplitter(chunk_size=6144)
text = splitter.split_text("\n".join([d.text for d in docs]))[0]
doc = Document(text=text, metadata=docs[0].metadata)
summary = dispatch_summarize_document_sync(doc, client)
return summary
def dispatch_summarize_document_sync(doc, client):
if isinstance(doc, ImageDocument):
return summarize_image_document_sync(doc, client)
elif isinstance(doc, Document):
return summarize_document_sync({"content": doc.text, **doc.metadata}, client)
else:
raise ValueError("Document type not supported")
def summarize_document_sync(doc, client):
PROMPT = """
You will be provided with the contents of a file along with its metadata. Provide a summary of the contents. The purpose of the summary is to organize files based on their content. To this end provide a concise but informative summary. Make the summary as specific to the file as possible.
Write your response a JSON object with the following schema:
```json
{
"file_path": "path to the file including name",
"summary": "summary of the content"
}
```
""".strip()
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": PROMPT},
{"role": "user", "content": json.dumps(doc)},
],
model="llama-3.1-70b-versatile",
response_format={"type": "json_object"},
temperature=0,
)
summary = json.loads(chat_completion.choices[0].message.content)
try:
# Print the filename in green
print(colored(summary["file_path"], "green"))
print(summary["summary"]) # Print the summary of the contents
# Print a separator line with spacing for readability
print("-" * 80 + "\n")
except KeyError as e:
print(e)
print(summary)
return summary
def summarize_image_document_sync(doc: ImageDocument, client):
client = ollama.Client()
chat_completion = client.chat(
messages=[
{
"role": "user",
"content": "Summarize the contents of this image.",
"images": [doc.image_path],
},
],
model="moondream",
# format="json",
# stream=True,
options={"num_predict": 128},
)
summary = {
"file_path": doc.image_path,
"summary": chat_completion["message"]["content"],
}
# Print the filename in green
print(colored(summary["file_path"], "green"))
print(summary["summary"]) # Print the summary of the contents
# Print a separator line with spacing for readability
print("-" * 80 + "\n")
return summary