camel-ai 0.1.1__py3-none-any.whl
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.
Potentially problematic release.
This version of camel-ai might be problematic. Click here for more details.
- camel/__init__.py +30 -0
- camel/agents/__init__.py +40 -0
- camel/agents/base.py +29 -0
- camel/agents/chat_agent.py +539 -0
- camel/agents/critic_agent.py +179 -0
- camel/agents/embodied_agent.py +138 -0
- camel/agents/role_assignment_agent.py +117 -0
- camel/agents/task_agent.py +382 -0
- camel/agents/tool_agents/__init__.py +20 -0
- camel/agents/tool_agents/base.py +40 -0
- camel/agents/tool_agents/hugging_face_tool_agent.py +203 -0
- camel/configs.py +159 -0
- camel/embeddings/__init__.py +20 -0
- camel/embeddings/base.py +65 -0
- camel/embeddings/openai_embedding.py +74 -0
- camel/functions/__init__.py +27 -0
- camel/functions/base_io_functions.py +261 -0
- camel/functions/math_functions.py +61 -0
- camel/functions/openai_function.py +88 -0
- camel/functions/search_functions.py +309 -0
- camel/functions/unstructured_io_fuctions.py +616 -0
- camel/functions/weather_functions.py +136 -0
- camel/generators.py +263 -0
- camel/human.py +130 -0
- camel/memories/__init__.py +28 -0
- camel/memories/base.py +75 -0
- camel/memories/chat_history_memory.py +111 -0
- camel/memories/context_creators/__init__.py +18 -0
- camel/memories/context_creators/base.py +72 -0
- camel/memories/context_creators/score_based.py +130 -0
- camel/memories/records.py +92 -0
- camel/messages/__init__.py +38 -0
- camel/messages/base.py +223 -0
- camel/messages/func_message.py +106 -0
- camel/models/__init__.py +26 -0
- camel/models/base_model.py +110 -0
- camel/models/model_factory.py +59 -0
- camel/models/open_source_model.py +144 -0
- camel/models/openai_model.py +103 -0
- camel/models/stub_model.py +106 -0
- camel/prompts/__init__.py +38 -0
- camel/prompts/ai_society.py +121 -0
- camel/prompts/base.py +227 -0
- camel/prompts/code.py +111 -0
- camel/prompts/evaluation.py +40 -0
- camel/prompts/misalignment.py +84 -0
- camel/prompts/prompt_templates.py +117 -0
- camel/prompts/role_description_prompt_template.py +53 -0
- camel/prompts/solution_extraction.py +44 -0
- camel/prompts/task_prompt_template.py +56 -0
- camel/prompts/translation.py +42 -0
- camel/responses/__init__.py +18 -0
- camel/responses/agent_responses.py +42 -0
- camel/societies/__init__.py +20 -0
- camel/societies/babyagi_playing.py +254 -0
- camel/societies/role_playing.py +456 -0
- camel/storages/__init__.py +23 -0
- camel/storages/key_value_storages/__init__.py +23 -0
- camel/storages/key_value_storages/base.py +57 -0
- camel/storages/key_value_storages/in_memory.py +51 -0
- camel/storages/key_value_storages/json.py +97 -0
- camel/terminators/__init__.py +23 -0
- camel/terminators/base.py +44 -0
- camel/terminators/response_terminator.py +118 -0
- camel/terminators/token_limit_terminator.py +55 -0
- camel/types/__init__.py +54 -0
- camel/types/enums.py +176 -0
- camel/types/openai_types.py +39 -0
- camel/utils/__init__.py +47 -0
- camel/utils/commons.py +243 -0
- camel/utils/python_interpreter.py +435 -0
- camel/utils/token_counting.py +220 -0
- camel_ai-0.1.1.dist-info/METADATA +311 -0
- camel_ai-0.1.1.dist-info/RECORD +75 -0
- camel_ai-0.1.1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
|
|
15
|
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class UnstructuredModules:
|
|
19
|
+
r"""A class to handle various functionalities provided by the
|
|
20
|
+
Unstructured library, including version checking, parsing, cleaning,
|
|
21
|
+
extracting, staging, chunking data, and integrating with cloud
|
|
22
|
+
services like S3 and Azure for data connection.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
UNSTRUCTURED_MIN_VERSION (str): The minimum required version of
|
|
26
|
+
the Unstructured library.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
UNSTRUCTURED_MIN_VERSION = "0.10.30" # Define the minimum version
|
|
30
|
+
|
|
31
|
+
def __init__(self):
|
|
32
|
+
r"""Initializes the UnstructuredModules class and ensures the
|
|
33
|
+
installed version of Unstructured library meets the minimum
|
|
34
|
+
requirements.
|
|
35
|
+
"""
|
|
36
|
+
self.ensure_unstructured_version(self.UNSTRUCTURED_MIN_VERSION)
|
|
37
|
+
|
|
38
|
+
def ensure_unstructured_version(self, min_version: str) -> None:
|
|
39
|
+
r"""Validates that the installed 'Unstructured' library version
|
|
40
|
+
satisfies the specified minimum version requirement. This function is
|
|
41
|
+
essential for ensuring compatibility with features that depend on a
|
|
42
|
+
certain version of the 'Unstructured' package.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
min_version (str): The minimum version required, specified in
|
|
46
|
+
`'major.minor.patch'` format.
|
|
47
|
+
|
|
48
|
+
Raises:
|
|
49
|
+
ImportError: If the 'Unstructured' package is not available in the
|
|
50
|
+
environment.
|
|
51
|
+
ValueError: If the current `'Unstructured'` version is older than
|
|
52
|
+
the required minimum version.
|
|
53
|
+
|
|
54
|
+
Notes:
|
|
55
|
+
Uses the 'packaging.version' module to parse and compare version
|
|
56
|
+
strings.
|
|
57
|
+
"""
|
|
58
|
+
from packaging import version
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
from unstructured.__version__ import __version__
|
|
62
|
+
|
|
63
|
+
except ImportError as e:
|
|
64
|
+
raise ImportError("Package `unstructured` not installed.") from e
|
|
65
|
+
|
|
66
|
+
# Use packaging.version to compare versions
|
|
67
|
+
min_ver = version.parse(min_version)
|
|
68
|
+
installed_ver = version.parse(__version__)
|
|
69
|
+
|
|
70
|
+
if installed_ver < min_ver:
|
|
71
|
+
raise ValueError(f"Require `unstructured>={min_version}`, "
|
|
72
|
+
f"you have {__version__}.")
|
|
73
|
+
|
|
74
|
+
def parse_file_or_url(
|
|
75
|
+
self,
|
|
76
|
+
input_path: str,
|
|
77
|
+
) -> Union[Any, List[Any]]:
|
|
78
|
+
r"""Loads a file or a URL and parses its contents as unstructured data.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
input_path (str): Path to the file or URL to be parsed.
|
|
82
|
+
Returns:
|
|
83
|
+
List[Any]: The elements after parsing the file or URL, could be a
|
|
84
|
+
dict, list, etc., depending on the content. If return_str is
|
|
85
|
+
True, returns a tuple with a string representation of the
|
|
86
|
+
elements and the elements themselves.
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
FileNotFoundError: If the file does not exist
|
|
90
|
+
at the path specified.
|
|
91
|
+
Exception: For any other issues during file or URL parsing.
|
|
92
|
+
|
|
93
|
+
Notes:
|
|
94
|
+
By default we use the basic "unstructured" library,
|
|
95
|
+
if you are processing document types beyond the basics,
|
|
96
|
+
you can install the necessary extras like:
|
|
97
|
+
`pip install "unstructured[docx,pptx]"` or
|
|
98
|
+
`pip install "unstructured[all-docs]"`.
|
|
99
|
+
Available document types:
|
|
100
|
+
"csv", "doc", "docx", "epub", "image", "md", "msg", "odt",
|
|
101
|
+
"org", "pdf", "ppt", "pptx", "rtf", "rst", "tsv", "xlsx".
|
|
102
|
+
|
|
103
|
+
References:
|
|
104
|
+
https://unstructured-io.github.io/unstructured/
|
|
105
|
+
"""
|
|
106
|
+
import os
|
|
107
|
+
from urllib.parse import urlparse
|
|
108
|
+
|
|
109
|
+
# Check if the input is a URL
|
|
110
|
+
parsed_url = urlparse(input_path)
|
|
111
|
+
is_url = all([parsed_url.scheme, parsed_url.netloc])
|
|
112
|
+
|
|
113
|
+
if is_url:
|
|
114
|
+
# Handling URL
|
|
115
|
+
from unstructured.partition.html import partition_html
|
|
116
|
+
|
|
117
|
+
try:
|
|
118
|
+
elements = partition_html(url=input_path)
|
|
119
|
+
return elements
|
|
120
|
+
except Exception as e:
|
|
121
|
+
raise Exception("Failed to parse the URL.") from e
|
|
122
|
+
|
|
123
|
+
else:
|
|
124
|
+
# Handling file
|
|
125
|
+
from unstructured.partition.auto import partition
|
|
126
|
+
|
|
127
|
+
# Check if the file exists
|
|
128
|
+
if not os.path.exists(input_path):
|
|
129
|
+
raise FileNotFoundError(
|
|
130
|
+
f"The file {input_path} was not found.")
|
|
131
|
+
|
|
132
|
+
# Read the file
|
|
133
|
+
try:
|
|
134
|
+
with open(input_path, "rb") as f:
|
|
135
|
+
elements = partition(file=f)
|
|
136
|
+
return elements
|
|
137
|
+
except Exception as e:
|
|
138
|
+
raise Exception(
|
|
139
|
+
"Failed to parse the unstructured file.") from e
|
|
140
|
+
|
|
141
|
+
def clean_text_data(
|
|
142
|
+
self,
|
|
143
|
+
text: str,
|
|
144
|
+
clean_options: Optional[List[Tuple[str, Dict[str, Any]]]] = None,
|
|
145
|
+
) -> str:
|
|
146
|
+
r"""Cleans text data using a variety of cleaning functions provided by
|
|
147
|
+
the `'unstructured'` library.
|
|
148
|
+
|
|
149
|
+
This function applies multiple text cleaning utilities by calling the
|
|
150
|
+
`'unstructured'` library's cleaning bricks for operations like
|
|
151
|
+
replacing unicode quotes, removing extra whitespace, dashes, non-ascii
|
|
152
|
+
characters, and more.
|
|
153
|
+
|
|
154
|
+
If no cleaning options are provided, a default set of cleaning
|
|
155
|
+
operations is applied. These defaults including operations
|
|
156
|
+
"replace_unicode_quotes", "clean_non_ascii_chars",
|
|
157
|
+
"group_broken_paragraphs", and "clean_extra_whitespace".
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
text (str): The text to be cleaned.
|
|
161
|
+
clean_options (dict): A dictionary specifying which
|
|
162
|
+
cleaning options to apply. The keys should
|
|
163
|
+
match the names of the cleaning functions,
|
|
164
|
+
and the values should be dictionaries
|
|
165
|
+
containing the parameters for each
|
|
166
|
+
function. Supported types:
|
|
167
|
+
'clean_extra_whitespace',
|
|
168
|
+
'clean_bullets',
|
|
169
|
+
'clean_ordered_bullets',
|
|
170
|
+
'clean_postfix',
|
|
171
|
+
'clean_prefix',
|
|
172
|
+
'clean_dashes',
|
|
173
|
+
'clean_trailing_punctuation',
|
|
174
|
+
'clean_non_ascii_chars',
|
|
175
|
+
'group_broken_paragraphs',
|
|
176
|
+
'remove_punctuation',
|
|
177
|
+
'replace_unicode_quotes',
|
|
178
|
+
'bytes_string_to_string',
|
|
179
|
+
'translate_text'.
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
str: The cleaned text.
|
|
183
|
+
|
|
184
|
+
Raises:
|
|
185
|
+
AttributeError: If a cleaning option does not correspond to a
|
|
186
|
+
valid cleaning function in 'unstructured'.
|
|
187
|
+
|
|
188
|
+
Notes:
|
|
189
|
+
The 'options' dictionary keys must correspond to valid cleaning
|
|
190
|
+
brick names from the 'unstructured' library.
|
|
191
|
+
Each brick's parameters must be provided in a nested dictionary
|
|
192
|
+
as the value for the key.
|
|
193
|
+
|
|
194
|
+
References:
|
|
195
|
+
https://unstructured-io.github.io/unstructured/
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
from unstructured.cleaners.core import (
|
|
199
|
+
bytes_string_to_string,
|
|
200
|
+
clean_bullets,
|
|
201
|
+
clean_dashes,
|
|
202
|
+
clean_extra_whitespace,
|
|
203
|
+
clean_non_ascii_chars,
|
|
204
|
+
clean_ordered_bullets,
|
|
205
|
+
clean_postfix,
|
|
206
|
+
clean_prefix,
|
|
207
|
+
clean_trailing_punctuation,
|
|
208
|
+
group_broken_paragraphs,
|
|
209
|
+
remove_punctuation,
|
|
210
|
+
replace_unicode_quotes,
|
|
211
|
+
)
|
|
212
|
+
from unstructured.cleaners.translate import translate_text
|
|
213
|
+
|
|
214
|
+
cleaning_functions = {
|
|
215
|
+
"clean_extra_whitespace": clean_extra_whitespace,
|
|
216
|
+
"clean_bullets": clean_bullets,
|
|
217
|
+
"clean_ordered_bullets": clean_ordered_bullets,
|
|
218
|
+
"clean_postfix": clean_postfix,
|
|
219
|
+
"clean_prefix": clean_prefix,
|
|
220
|
+
"clean_dashes": clean_dashes,
|
|
221
|
+
"clean_trailing_punctuation": clean_trailing_punctuation,
|
|
222
|
+
"clean_non_ascii_chars": clean_non_ascii_chars,
|
|
223
|
+
"group_broken_paragraphs": group_broken_paragraphs,
|
|
224
|
+
"remove_punctuation": remove_punctuation,
|
|
225
|
+
"replace_unicode_quotes": replace_unicode_quotes,
|
|
226
|
+
"bytes_string_to_string": bytes_string_to_string,
|
|
227
|
+
"translate_text": translate_text,
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
# Define default clean options if none are provided
|
|
231
|
+
if clean_options is None:
|
|
232
|
+
clean_options = [
|
|
233
|
+
("replace_unicode_quotes", {}),
|
|
234
|
+
("clean_non_ascii_chars", {}),
|
|
235
|
+
("group_broken_paragraphs", {}),
|
|
236
|
+
("clean_extra_whitespace", {}),
|
|
237
|
+
]
|
|
238
|
+
|
|
239
|
+
cleaned_text = text
|
|
240
|
+
for func_name, params in clean_options:
|
|
241
|
+
if func_name in cleaning_functions:
|
|
242
|
+
cleaned_text = cleaning_functions[func_name](cleaned_text,
|
|
243
|
+
**params)
|
|
244
|
+
else:
|
|
245
|
+
raise ValueError(
|
|
246
|
+
f"'{func_name}' is not a valid function in 'unstructured'."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
return cleaned_text
|
|
250
|
+
|
|
251
|
+
def extract_data_from_text(self, text: str, extract_type: str,
|
|
252
|
+
**kwargs) -> Any:
|
|
253
|
+
r"""Extracts various types of data from text using functions from
|
|
254
|
+
unstructured.cleaners.extract.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
text (str): Text to extract data from.
|
|
258
|
+
extract_type (str): Type of data to extract. Supported types:
|
|
259
|
+
'extract_datetimetz',
|
|
260
|
+
'extract_email_address',
|
|
261
|
+
'extract_ip_address',
|
|
262
|
+
'extract_ip_address_name',
|
|
263
|
+
'extract_mapi_id',
|
|
264
|
+
'extract_ordered_bullets',
|
|
265
|
+
'extract_text_after',
|
|
266
|
+
'extract_text_before',
|
|
267
|
+
'extract_us_phone_number'.
|
|
268
|
+
**kwargs: Additional keyword arguments for specific
|
|
269
|
+
extraction functions.
|
|
270
|
+
|
|
271
|
+
Returns:
|
|
272
|
+
Any: The extracted data, type depends on extract_type.
|
|
273
|
+
|
|
274
|
+
References:
|
|
275
|
+
https://unstructured-io.github.io/unstructured/
|
|
276
|
+
"""
|
|
277
|
+
|
|
278
|
+
from unstructured.cleaners.extract import (
|
|
279
|
+
extract_datetimetz,
|
|
280
|
+
extract_email_address,
|
|
281
|
+
extract_ip_address,
|
|
282
|
+
extract_ip_address_name,
|
|
283
|
+
extract_mapi_id,
|
|
284
|
+
extract_ordered_bullets,
|
|
285
|
+
extract_text_after,
|
|
286
|
+
extract_text_before,
|
|
287
|
+
extract_us_phone_number,
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
extraction_functions = {
|
|
291
|
+
"extract_datetimetz": extract_datetimetz,
|
|
292
|
+
"extract_email_address": extract_email_address,
|
|
293
|
+
"extract_ip_address": extract_ip_address,
|
|
294
|
+
"extract_ip_address_name": extract_ip_address_name,
|
|
295
|
+
"extract_mapi_id": extract_mapi_id,
|
|
296
|
+
"extract_ordered_bullets": extract_ordered_bullets,
|
|
297
|
+
"extract_text_after": extract_text_after,
|
|
298
|
+
"extract_text_before": extract_text_before,
|
|
299
|
+
"extract_us_phone_number": extract_us_phone_number,
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if extract_type not in extraction_functions:
|
|
303
|
+
raise ValueError(f"Unsupported extract_type: {extract_type}")
|
|
304
|
+
|
|
305
|
+
return extraction_functions[extract_type](text, **kwargs)
|
|
306
|
+
|
|
307
|
+
def stage_elements(self, elements: List[Any], stage_type: str,
|
|
308
|
+
**kwargs) -> Union[str, List[Dict], Any]:
|
|
309
|
+
r"""Stages elements for various platforms based on the
|
|
310
|
+
specified staging type.
|
|
311
|
+
|
|
312
|
+
This function applies multiple staging utilities to format data
|
|
313
|
+
for different NLP annotation and machine learning tools. It uses
|
|
314
|
+
the 'unstructured.staging' module's functions for operations like
|
|
315
|
+
converting to CSV, DataFrame, dictionary, or formatting for
|
|
316
|
+
specific platforms like Prodigy, etc.
|
|
317
|
+
|
|
318
|
+
Args:
|
|
319
|
+
elements (List[Any]): List of Element objects to be staged.
|
|
320
|
+
stage_type (str): Type of staging to perform. Supported types:
|
|
321
|
+
'convert_to_csv',
|
|
322
|
+
'convert_to_dataframe',
|
|
323
|
+
'convert_to_dict',
|
|
324
|
+
'dict_to_elements',
|
|
325
|
+
'stage_csv_for_prodigy',
|
|
326
|
+
'stage_for_prodigy',
|
|
327
|
+
'stage_for_argilla',
|
|
328
|
+
'stage_for_baseplate',
|
|
329
|
+
'stage_for_datasaur',
|
|
330
|
+
'stage_for_label_box',
|
|
331
|
+
'stage_for_label_studio',
|
|
332
|
+
'stage_for_weaviate'.
|
|
333
|
+
**kwargs: Additional keyword arguments specific to
|
|
334
|
+
the staging type.
|
|
335
|
+
|
|
336
|
+
Returns:
|
|
337
|
+
Union[str, List[Dict], Any]: Staged data in the
|
|
338
|
+
format appropriate for the specified staging type.
|
|
339
|
+
|
|
340
|
+
Raises:
|
|
341
|
+
ValueError: If the staging type is not supported or a required
|
|
342
|
+
argument is missing.
|
|
343
|
+
References:
|
|
344
|
+
https://unstructured-io.github.io/unstructured/
|
|
345
|
+
"""
|
|
346
|
+
|
|
347
|
+
from unstructured.staging import (
|
|
348
|
+
argilla,
|
|
349
|
+
base,
|
|
350
|
+
baseplate,
|
|
351
|
+
datasaur,
|
|
352
|
+
label_box,
|
|
353
|
+
label_studio,
|
|
354
|
+
prodigy,
|
|
355
|
+
weaviate,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
staging_functions = {
|
|
359
|
+
"convert_to_csv":
|
|
360
|
+
base.convert_to_csv,
|
|
361
|
+
"convert_to_dataframe":
|
|
362
|
+
base.convert_to_dataframe,
|
|
363
|
+
"convert_to_dict":
|
|
364
|
+
base.convert_to_dict,
|
|
365
|
+
"dict_to_elements":
|
|
366
|
+
base.dict_to_elements,
|
|
367
|
+
"stage_csv_for_prodigy":
|
|
368
|
+
lambda els, **kw: prodigy.stage_csv_for_prodigy(
|
|
369
|
+
els, kw.get('metadata', [])),
|
|
370
|
+
"stage_for_prodigy":
|
|
371
|
+
lambda els, **kw: prodigy.stage_for_prodigy(
|
|
372
|
+
els, kw.get('metadata', [])),
|
|
373
|
+
"stage_for_argilla":
|
|
374
|
+
lambda els, **kw: argilla.stage_for_argilla(els, **kw),
|
|
375
|
+
"stage_for_baseplate":
|
|
376
|
+
baseplate.stage_for_baseplate,
|
|
377
|
+
"stage_for_datasaur":
|
|
378
|
+
lambda els, **kw: datasaur.stage_for_datasaur(
|
|
379
|
+
els, kw.get('entities', [])),
|
|
380
|
+
"stage_for_label_box":
|
|
381
|
+
lambda els, **kw: label_box.stage_for_label_box(els, **kw),
|
|
382
|
+
"stage_for_label_studio":
|
|
383
|
+
lambda els, **kw: label_studio.stage_for_label_studio(els, **kw),
|
|
384
|
+
"stage_for_weaviate":
|
|
385
|
+
weaviate.stage_for_weaviate,
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if stage_type not in staging_functions:
|
|
389
|
+
raise ValueError(f"Unsupported stage type: {stage_type}")
|
|
390
|
+
|
|
391
|
+
return staging_functions[stage_type](elements, **kwargs)
|
|
392
|
+
|
|
393
|
+
def chunk_elements(self, elements: List[Any], chunk_type: str,
|
|
394
|
+
**kwargs) -> List[Any]:
|
|
395
|
+
r"""Chunks elements by titles.
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
elements (List[Any]): List of Element objects to be chunked.
|
|
399
|
+
chunk_type (str): Type chunk going to apply. Supported types:
|
|
400
|
+
'chunk_by_title'.
|
|
401
|
+
**kwargs: Additional keyword arguments for chunking.
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
List[Dict]: List of chunked sections.
|
|
405
|
+
|
|
406
|
+
References:
|
|
407
|
+
https://unstructured-io.github.io/unstructured/
|
|
408
|
+
"""
|
|
409
|
+
|
|
410
|
+
from unstructured.chunking.title import chunk_by_title
|
|
411
|
+
|
|
412
|
+
chunking_functions = {
|
|
413
|
+
"chunk_by_title": chunk_by_title,
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if chunk_type not in chunking_functions:
|
|
417
|
+
raise ValueError(f"Unsupported chunk type: {chunk_type}")
|
|
418
|
+
|
|
419
|
+
# Format chunks into a list of dictionaries (or your preferred format)
|
|
420
|
+
return chunking_functions[chunk_type](elements, **kwargs)
|
|
421
|
+
|
|
422
|
+
def run_s3_ingest(self, s3_url: str, output_dir: str,
|
|
423
|
+
num_processes: int = 2, anonymous: bool = True) -> None:
|
|
424
|
+
r"""Processes documents from an S3 bucket and stores structured
|
|
425
|
+
outputs locally.
|
|
426
|
+
|
|
427
|
+
Args:
|
|
428
|
+
s3_url (str): The URL of the S3 bucket.
|
|
429
|
+
output_dir (str): Local directory to store the processed outputs.
|
|
430
|
+
num_processes (int, optional): Number of processes to use.
|
|
431
|
+
(default: :obj:`2`)
|
|
432
|
+
anonymous (bool, optional): Flag to run anonymously if
|
|
433
|
+
required. (default: :obj:`True`)
|
|
434
|
+
|
|
435
|
+
Notes:
|
|
436
|
+
You need to install the necessary extras by using:
|
|
437
|
+
`pip install "unstructured[s3]"`.
|
|
438
|
+
|
|
439
|
+
References:
|
|
440
|
+
https://unstructured-io.github.io/unstructured/
|
|
441
|
+
"""
|
|
442
|
+
|
|
443
|
+
from unstructured.ingest.interfaces import (
|
|
444
|
+
FsspecConfig,
|
|
445
|
+
PartitionConfig,
|
|
446
|
+
ProcessorConfig,
|
|
447
|
+
ReadConfig,
|
|
448
|
+
)
|
|
449
|
+
from unstructured.ingest.runner import S3Runner
|
|
450
|
+
|
|
451
|
+
runner = S3Runner(
|
|
452
|
+
processor_config=ProcessorConfig(
|
|
453
|
+
verbose=True,
|
|
454
|
+
output_dir=output_dir,
|
|
455
|
+
num_processes=num_processes,
|
|
456
|
+
),
|
|
457
|
+
read_config=ReadConfig(),
|
|
458
|
+
partition_config=PartitionConfig(),
|
|
459
|
+
fsspec_config=FsspecConfig(remote_url=s3_url),
|
|
460
|
+
)
|
|
461
|
+
runner.run(anonymous=anonymous)
|
|
462
|
+
|
|
463
|
+
def run_azure_ingest(self, azure_url: str, output_dir: str,
|
|
464
|
+
account_name: str, num_processes: int = 2) -> None:
|
|
465
|
+
"""
|
|
466
|
+
Processes documents from an Azure storage container and stores
|
|
467
|
+
structured outputs locally.
|
|
468
|
+
|
|
469
|
+
Args:
|
|
470
|
+
azure_url (str): The URL of the Azure storage container.
|
|
471
|
+
output_dir (str): Local directory to store the processed outputs.
|
|
472
|
+
account_name (str): Azure account name for accessing the container.
|
|
473
|
+
num_processes (int, optional): Number of processes to use.
|
|
474
|
+
(default: :obj:`2`)
|
|
475
|
+
|
|
476
|
+
Notes:
|
|
477
|
+
You need to install the necessary extras by using:
|
|
478
|
+
`pip install "unstructured[azure]"`.
|
|
479
|
+
|
|
480
|
+
References:
|
|
481
|
+
https://unstructured-io.github.io/unstructured/
|
|
482
|
+
"""
|
|
483
|
+
from unstructured.ingest.interfaces import (
|
|
484
|
+
FsspecConfig,
|
|
485
|
+
PartitionConfig,
|
|
486
|
+
ProcessorConfig,
|
|
487
|
+
ReadConfig,
|
|
488
|
+
)
|
|
489
|
+
from unstructured.ingest.runner import AzureRunner
|
|
490
|
+
|
|
491
|
+
runner = AzureRunner(
|
|
492
|
+
processor_config=ProcessorConfig(
|
|
493
|
+
verbose=True,
|
|
494
|
+
output_dir=output_dir,
|
|
495
|
+
num_processes=num_processes,
|
|
496
|
+
),
|
|
497
|
+
read_config=ReadConfig(),
|
|
498
|
+
partition_config=PartitionConfig(),
|
|
499
|
+
fsspec_config=FsspecConfig(remote_url=azure_url),
|
|
500
|
+
)
|
|
501
|
+
runner.run(account_name=account_name)
|
|
502
|
+
|
|
503
|
+
def run_github_ingest(self, repo_url: str, git_branch: str,
|
|
504
|
+
output_dir: str, num_processes: int = 2) -> None:
|
|
505
|
+
r"""Processes documents from a GitHub repository and stores
|
|
506
|
+
structured outputs locally.
|
|
507
|
+
|
|
508
|
+
Args:
|
|
509
|
+
repo_url (str): URL of the GitHub repository.
|
|
510
|
+
git_branch (str): Git branch name to process.
|
|
511
|
+
output_dir (str): Local directory to store the processed outputs.
|
|
512
|
+
num_processes (int, optional): Number of processes to use.
|
|
513
|
+
(default: :obj:`2`)
|
|
514
|
+
|
|
515
|
+
Notes:
|
|
516
|
+
You need to install the necessary extras by using:
|
|
517
|
+
`pip install "unstructured[github]"`.
|
|
518
|
+
|
|
519
|
+
References:
|
|
520
|
+
https://unstructured-io.github.io/unstructured/
|
|
521
|
+
"""
|
|
522
|
+
from unstructured.ingest.interfaces import (
|
|
523
|
+
PartitionConfig,
|
|
524
|
+
ProcessorConfig,
|
|
525
|
+
ReadConfig,
|
|
526
|
+
)
|
|
527
|
+
from unstructured.ingest.runner import GithubRunner
|
|
528
|
+
|
|
529
|
+
runner = GithubRunner(
|
|
530
|
+
processor_config=ProcessorConfig(
|
|
531
|
+
verbose=True,
|
|
532
|
+
output_dir=output_dir,
|
|
533
|
+
num_processes=num_processes,
|
|
534
|
+
),
|
|
535
|
+
read_config=ReadConfig(),
|
|
536
|
+
partition_config=PartitionConfig(),
|
|
537
|
+
)
|
|
538
|
+
runner.run(url=repo_url, git_branch=git_branch)
|
|
539
|
+
|
|
540
|
+
def run_slack_ingest(self, channels: List[str], token: str,
|
|
541
|
+
start_date: str, end_date: str, output_dir: str,
|
|
542
|
+
num_processes: int = 2) -> None:
|
|
543
|
+
r"""Processes documents from specified Slack channels and stores
|
|
544
|
+
structured outputs locally.
|
|
545
|
+
|
|
546
|
+
Args:
|
|
547
|
+
channels (List[str]): List of Slack channel IDs.
|
|
548
|
+
token (str): Slack API token.
|
|
549
|
+
start_date (str): Start date for fetching data.
|
|
550
|
+
end_date (str): End date for fetching data.
|
|
551
|
+
output_dir (str): Local directory to store the processed outputs.
|
|
552
|
+
num_processes (int, optional): Number of processes to use.
|
|
553
|
+
(default: :obj:`2`)
|
|
554
|
+
|
|
555
|
+
Notes:
|
|
556
|
+
You need to install the necessary extras by using:
|
|
557
|
+
`pip install "unstructured[slack]"`.
|
|
558
|
+
|
|
559
|
+
References:
|
|
560
|
+
https://unstructured-io.github.io/unstructured/
|
|
561
|
+
"""
|
|
562
|
+
from unstructured.ingest.interfaces import (
|
|
563
|
+
PartitionConfig,
|
|
564
|
+
ProcessorConfig,
|
|
565
|
+
ReadConfig,
|
|
566
|
+
)
|
|
567
|
+
from unstructured.ingest.runner import SlackRunner
|
|
568
|
+
|
|
569
|
+
runner = SlackRunner(
|
|
570
|
+
processor_config=ProcessorConfig(
|
|
571
|
+
verbose=True,
|
|
572
|
+
output_dir=output_dir,
|
|
573
|
+
num_processes=num_processes,
|
|
574
|
+
),
|
|
575
|
+
read_config=ReadConfig(),
|
|
576
|
+
partition_config=PartitionConfig(),
|
|
577
|
+
)
|
|
578
|
+
runner.run(channels=channels, token=token, start_date=start_date,
|
|
579
|
+
end_date=end_date)
|
|
580
|
+
|
|
581
|
+
def run_discord_ingest(self, channels: List[str], token: str,
|
|
582
|
+
output_dir: str, num_processes: int = 2) -> None:
|
|
583
|
+
r"""Processes messages from specified Discord channels and stores
|
|
584
|
+
structured outputs locally.
|
|
585
|
+
|
|
586
|
+
Args:
|
|
587
|
+
channels (List[str]): List of Discord channel IDs.
|
|
588
|
+
token (str): Discord bot token.
|
|
589
|
+
output_dir (str): Local directory to store the processed outputs.
|
|
590
|
+
num_processes (int, optional): Number of processes to use.
|
|
591
|
+
(default: :obj:`2`)
|
|
592
|
+
|
|
593
|
+
Notes:
|
|
594
|
+
You need to install the necessary extras by using:
|
|
595
|
+
`pip install "unstructured[discord]"`.
|
|
596
|
+
|
|
597
|
+
References:
|
|
598
|
+
https://unstructured-io.github.io/unstructured/
|
|
599
|
+
"""
|
|
600
|
+
from unstructured.ingest.interfaces import (
|
|
601
|
+
PartitionConfig,
|
|
602
|
+
ProcessorConfig,
|
|
603
|
+
ReadConfig,
|
|
604
|
+
)
|
|
605
|
+
from unstructured.ingest.runner import DiscordRunner
|
|
606
|
+
|
|
607
|
+
runner = DiscordRunner(
|
|
608
|
+
processor_config=ProcessorConfig(
|
|
609
|
+
verbose=True,
|
|
610
|
+
output_dir=output_dir,
|
|
611
|
+
num_processes=num_processes,
|
|
612
|
+
),
|
|
613
|
+
read_config=ReadConfig(),
|
|
614
|
+
partition_config=PartitionConfig(),
|
|
615
|
+
)
|
|
616
|
+
runner.run(channels=channels, token=token)
|