attachments 0.2.1__tar.gz → 0.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- attachments-0.2.3/PKG-INFO +166 -0
- attachments-0.2.3/README.md +127 -0
- {attachments-0.2.1 → attachments-0.2.3}/pyproject.toml +1 -1
- attachments-0.2.3/src/attachments.egg-info/PKG-INFO +166 -0
- attachments-0.2.1/PKG-INFO +0 -284
- attachments-0.2.1/README.md +0 -245
- attachments-0.2.1/src/attachments.egg-info/PKG-INFO +0 -284
- {attachments-0.2.1 → attachments-0.2.3}/LICENSE +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/setup.cfg +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/__init__.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/audio_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/config.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/core.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/detectors.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/exceptions.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/image_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/office_contact_sheet.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/parsers.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/renderers.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments/utils.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments.egg-info/SOURCES.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments.egg-info/dependency_links.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments.egg-info/requires.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/src/attachments.egg-info/top_level.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_audio_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_base.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_core_functionality.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_document_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_html_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_image_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_indexing_slicing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_markdown_rendering.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_office_contact_sheet.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_pdf_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_pptx_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.3}/tests/test_simple.py +0 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: attachments
|
|
3
|
+
Version: 0.2.3
|
|
4
|
+
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
+
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: Pillow
|
|
15
|
+
Requires-Dist: python-dotenv
|
|
16
|
+
Requires-Dist: pytest
|
|
17
|
+
Requires-Dist: beautifulsoup4
|
|
18
|
+
Requires-Dist: html2text
|
|
19
|
+
Requires-Dist: markdownify
|
|
20
|
+
Requires-Dist: lxml
|
|
21
|
+
Requires-Dist: python-magic
|
|
22
|
+
Requires-Dist: mammoth
|
|
23
|
+
Requires-Dist: PyMuPDF
|
|
24
|
+
Requires-Dist: python-pptx
|
|
25
|
+
Requires-Dist: python-docx
|
|
26
|
+
Requires-Dist: odfpy
|
|
27
|
+
Requires-Dist: pillow-heif
|
|
28
|
+
Requires-Dist: pydub
|
|
29
|
+
Requires-Dist: SpeechRecognition
|
|
30
|
+
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
+
Requires-Dist: pypdfium2
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest; extra == "dev"
|
|
34
|
+
Requires-Dist: flake8; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy; extra == "dev"
|
|
36
|
+
Requires-Dist: black; extra == "dev"
|
|
37
|
+
Requires-Dist: filelock; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Attachments
|
|
41
|
+
## Easiest way to give context to LLMs
|
|
42
|
+
|
|
43
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
+
|
|
45
|
+
## Very quickstart
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
> pip install attachments
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from attachments import Attachments
|
|
55
|
+
|
|
56
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
57
|
+
|
|
58
|
+
prompt_ready_context = str(a)
|
|
59
|
+
images_ready_for_llm = a.images
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
That is really just it!
|
|
63
|
+
|
|
64
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
65
|
+
|
|
66
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
67
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
68
|
+
|
|
69
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
70
|
+
|
|
71
|
+
## How to give attachments to openai llms?
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
from attachments import Attachments
|
|
76
|
+
|
|
77
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
78
|
+
|
|
79
|
+
prompt = f"""
|
|
80
|
+
Analyze the following documents:
|
|
81
|
+
{pdf_attachment}
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
85
|
+
[{"type": "input_text", "text": prompt}]
|
|
86
|
+
|
|
87
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
88
|
+
response.output_text
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## How to give attachments to anthropic llms?
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import anthropic
|
|
95
|
+
from attachments import Attachments
|
|
96
|
+
|
|
97
|
+
pptx_file = Attachments(
|
|
98
|
+
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx")
|
|
99
|
+
|
|
100
|
+
prompt = f"""
|
|
101
|
+
Analyze the following documents:
|
|
102
|
+
{pptx_file}
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
content = [
|
|
106
|
+
{
|
|
107
|
+
"type": "image",
|
|
108
|
+
"source": {
|
|
109
|
+
"type": "base64",
|
|
110
|
+
"media_type": image.split(";")[0].split(":")[1], # this is ugly but it works
|
|
111
|
+
"data": image.split(",")[1] # this is ugly but it works
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for image in pptx_file.images
|
|
115
|
+
] + [{"type": "text", "text": prompt}]
|
|
116
|
+
|
|
117
|
+
message = anthropic.Anthropic().messages.create(
|
|
118
|
+
max_tokens=8192,
|
|
119
|
+
model="claude-3-5-haiku-20241022",
|
|
120
|
+
messages=[{"role": "user", "content": content}])
|
|
121
|
+
print(message.content)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## Usage
|
|
126
|
+
|
|
127
|
+
### Basic Initialization
|
|
128
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from attachments import Attachments
|
|
132
|
+
|
|
133
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
134
|
+
a = Attachments(
|
|
135
|
+
"docs/report.pdf",
|
|
136
|
+
"images/diagram.png[resize:400xauto]",
|
|
137
|
+
"https://www.example.com/article.html",
|
|
138
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# The library will download URLs, process files, and extract content.
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Indexing Attachments
|
|
145
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
146
|
+
```python
|
|
147
|
+
first_attachment = a[0]
|
|
148
|
+
first_two_attachments = a[0:2]
|
|
149
|
+
|
|
150
|
+
print(f"Selected attachment: {first_attachment}")
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Page/Slide Selection
|
|
154
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
155
|
+
```python
|
|
156
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
157
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
158
|
+
|
|
159
|
+
# Process the first three slides and the last slide of a presentation
|
|
160
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
161
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Supported File Types
|
|
165
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
166
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Attachments
|
|
2
|
+
## Easiest way to give context to LLMs
|
|
3
|
+
|
|
4
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
5
|
+
|
|
6
|
+
## Very quickstart
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
> pip install attachments
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from attachments import Attachments
|
|
16
|
+
|
|
17
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
18
|
+
|
|
19
|
+
prompt_ready_context = str(a)
|
|
20
|
+
images_ready_for_llm = a.images
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
That is really just it!
|
|
24
|
+
|
|
25
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
26
|
+
|
|
27
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
28
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
29
|
+
|
|
30
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
31
|
+
|
|
32
|
+
## How to give attachments to openai llms?
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from openai import OpenAI
|
|
36
|
+
from attachments import Attachments
|
|
37
|
+
|
|
38
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
39
|
+
|
|
40
|
+
prompt = f"""
|
|
41
|
+
Analyze the following documents:
|
|
42
|
+
{pdf_attachment}
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
46
|
+
[{"type": "input_text", "text": prompt}]
|
|
47
|
+
|
|
48
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
49
|
+
response.output_text
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## How to give attachments to anthropic llms?
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import anthropic
|
|
56
|
+
from attachments import Attachments
|
|
57
|
+
|
|
58
|
+
pptx_file = Attachments(
|
|
59
|
+
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx")
|
|
60
|
+
|
|
61
|
+
prompt = f"""
|
|
62
|
+
Analyze the following documents:
|
|
63
|
+
{pptx_file}
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
content = [
|
|
67
|
+
{
|
|
68
|
+
"type": "image",
|
|
69
|
+
"source": {
|
|
70
|
+
"type": "base64",
|
|
71
|
+
"media_type": image.split(";")[0].split(":")[1], # this is ugly but it works
|
|
72
|
+
"data": image.split(",")[1] # this is ugly but it works
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for image in pptx_file.images
|
|
76
|
+
] + [{"type": "text", "text": prompt}]
|
|
77
|
+
|
|
78
|
+
message = anthropic.Anthropic().messages.create(
|
|
79
|
+
max_tokens=8192,
|
|
80
|
+
model="claude-3-5-haiku-20241022",
|
|
81
|
+
messages=[{"role": "user", "content": content}])
|
|
82
|
+
print(message.content)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Usage
|
|
87
|
+
|
|
88
|
+
### Basic Initialization
|
|
89
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from attachments import Attachments
|
|
93
|
+
|
|
94
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
95
|
+
a = Attachments(
|
|
96
|
+
"docs/report.pdf",
|
|
97
|
+
"images/diagram.png[resize:400xauto]",
|
|
98
|
+
"https://www.example.com/article.html",
|
|
99
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# The library will download URLs, process files, and extract content.
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Indexing Attachments
|
|
106
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
107
|
+
```python
|
|
108
|
+
first_attachment = a[0]
|
|
109
|
+
first_two_attachments = a[0:2]
|
|
110
|
+
|
|
111
|
+
print(f"Selected attachment: {first_attachment}")
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Page/Slide Selection
|
|
115
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
116
|
+
```python
|
|
117
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
118
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
119
|
+
|
|
120
|
+
# Process the first three slides and the last slide of a presentation
|
|
121
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
122
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Supported File Types
|
|
126
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
127
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: attachments
|
|
3
|
+
Version: 0.2.3
|
|
4
|
+
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
+
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: Pillow
|
|
15
|
+
Requires-Dist: python-dotenv
|
|
16
|
+
Requires-Dist: pytest
|
|
17
|
+
Requires-Dist: beautifulsoup4
|
|
18
|
+
Requires-Dist: html2text
|
|
19
|
+
Requires-Dist: markdownify
|
|
20
|
+
Requires-Dist: lxml
|
|
21
|
+
Requires-Dist: python-magic
|
|
22
|
+
Requires-Dist: mammoth
|
|
23
|
+
Requires-Dist: PyMuPDF
|
|
24
|
+
Requires-Dist: python-pptx
|
|
25
|
+
Requires-Dist: python-docx
|
|
26
|
+
Requires-Dist: odfpy
|
|
27
|
+
Requires-Dist: pillow-heif
|
|
28
|
+
Requires-Dist: pydub
|
|
29
|
+
Requires-Dist: SpeechRecognition
|
|
30
|
+
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
+
Requires-Dist: pypdfium2
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest; extra == "dev"
|
|
34
|
+
Requires-Dist: flake8; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy; extra == "dev"
|
|
36
|
+
Requires-Dist: black; extra == "dev"
|
|
37
|
+
Requires-Dist: filelock; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Attachments
|
|
41
|
+
## Easiest way to give context to LLMs
|
|
42
|
+
|
|
43
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
+
|
|
45
|
+
## Very quickstart
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
> pip install attachments
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from attachments import Attachments
|
|
55
|
+
|
|
56
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
57
|
+
|
|
58
|
+
prompt_ready_context = str(a)
|
|
59
|
+
images_ready_for_llm = a.images
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
That is really just it!
|
|
63
|
+
|
|
64
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
65
|
+
|
|
66
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
67
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
68
|
+
|
|
69
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
70
|
+
|
|
71
|
+
## How to give attachments to openai llms?
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
from attachments import Attachments
|
|
76
|
+
|
|
77
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
78
|
+
|
|
79
|
+
prompt = f"""
|
|
80
|
+
Analyze the following documents:
|
|
81
|
+
{pdf_attachment}
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
85
|
+
[{"type": "input_text", "text": prompt}]
|
|
86
|
+
|
|
87
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
88
|
+
response.output_text
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## How to give attachments to anthropic llms?
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import anthropic
|
|
95
|
+
from attachments import Attachments
|
|
96
|
+
|
|
97
|
+
pptx_file = Attachments(
|
|
98
|
+
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx")
|
|
99
|
+
|
|
100
|
+
prompt = f"""
|
|
101
|
+
Analyze the following documents:
|
|
102
|
+
{pptx_file}
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
content = [
|
|
106
|
+
{
|
|
107
|
+
"type": "image",
|
|
108
|
+
"source": {
|
|
109
|
+
"type": "base64",
|
|
110
|
+
"media_type": image.split(";")[0].split(":")[1], # this is ugly but it works
|
|
111
|
+
"data": image.split(",")[1] # this is ugly but it works
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for image in pptx_file.images
|
|
115
|
+
] + [{"type": "text", "text": prompt}]
|
|
116
|
+
|
|
117
|
+
message = anthropic.Anthropic().messages.create(
|
|
118
|
+
max_tokens=8192,
|
|
119
|
+
model="claude-3-5-haiku-20241022",
|
|
120
|
+
messages=[{"role": "user", "content": content}])
|
|
121
|
+
print(message.content)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## Usage
|
|
126
|
+
|
|
127
|
+
### Basic Initialization
|
|
128
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from attachments import Attachments
|
|
132
|
+
|
|
133
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
134
|
+
a = Attachments(
|
|
135
|
+
"docs/report.pdf",
|
|
136
|
+
"images/diagram.png[resize:400xauto]",
|
|
137
|
+
"https://www.example.com/article.html",
|
|
138
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# The library will download URLs, process files, and extract content.
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Indexing Attachments
|
|
145
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
146
|
+
```python
|
|
147
|
+
first_attachment = a[0]
|
|
148
|
+
first_two_attachments = a[0:2]
|
|
149
|
+
|
|
150
|
+
print(f"Selected attachment: {first_attachment}")
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Page/Slide Selection
|
|
154
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
155
|
+
```python
|
|
156
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
157
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
158
|
+
|
|
159
|
+
# Process the first three slides and the last slide of a presentation
|
|
160
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
161
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Supported File Types
|
|
165
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
166
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|