-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add document for Docset readers and writers. #196
Open
bohou-aryn
wants to merge
1
commit into
main
Choose a base branch
from
readerwriterdoc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. _Ref-Readers: | ||
|
||
Readers | ||
=========== | ||
|
||
.. automodule:: sycamore.scans.reader | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. _Ref-Writers: | ||
|
||
Writers | ||
=========== | ||
|
||
.. automodule:: sycamore.writers.writer | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from sycamore.scans.file_scan import BinaryScan, FileScan, JsonScan | ||
from sycamore.scans.materialized_scan import ArrowScan, DocScan, MaterializedScan, PandasScan | ||
from sycamore.scans.reader import DocSetReader | ||
|
||
__all__ = ["ArrowScan", "BinaryScan", "DocScan", "FileScan", "JsonScan", "MaterializedScan", "PandasScan"] | ||
__all__ = ["DocSetReader"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,17 @@ | |
|
||
from sycamore import Context, DocSet | ||
from sycamore.data import Document | ||
from sycamore.scans import ArrowScan, BinaryScan, DocScan, PandasScan, JsonScan | ||
from sycamore.scans.file_scan import FileMetadataProvider | ||
from sycamore.scans.materialized_scan import ArrowScan, DocScan, PandasScan | ||
from sycamore.scans.file_scan import BinaryScan, FileMetadataProvider, JsonScan | ||
|
||
|
||
class DocSetReader: | ||
""" | ||
Read data from different kinds of sources into DocSet. | ||
|
||
DocSetReader is exposed through sycamore context read API. | ||
""" | ||
|
||
def __init__(self, context: Context): | ||
self._context = context | ||
|
||
|
@@ -23,6 +29,18 @@ def binary( | |
metadata_provider: Optional[FileMetadataProvider] = None, | ||
**resource_args | ||
) -> DocSet: | ||
""" | ||
Scan data file into raw bytes | ||
|
||
For each file, BinaryScan creates one Document, we use BinaryScan to process | ||
unstructured data format like PDF or HTML. | ||
|
||
Examples: | ||
>>> import sycamore | ||
>>> import pyarrow as pa | ||
>>> context = sycamore.init() | ||
>>> docset = context.read.binary("s3://bucket/prefix", "pdf") | ||
""" | ||
scan = BinaryScan( | ||
paths, | ||
binary_format=binary_format, | ||
|
@@ -61,6 +79,16 @@ def json( | |
document_body_field: Optional[str] = None, | ||
**resource_args | ||
) -> DocSet: | ||
""" | ||
Scan JSON or JSONL data file into DocSet | ||
|
||
We currently handle each JSON file by reading binary and then parsing it into Document. | ||
Examples: | ||
>>> import sycamore | ||
>>> import pyarrow as pa | ||
>>> context = sycamore.init() | ||
>>> docset = context.read.json("s3://bucket/prefix") | ||
""" | ||
json_scan = JsonScan( | ||
paths, | ||
properties=properties, | ||
|
@@ -71,13 +99,43 @@ def json( | |
return DocSet(self._context, json_scan) | ||
|
||
def arrow(self, tables: Union[Table, bytes, list[Union[Table, bytes]]]) -> DocSet: | ||
""" | ||
Scan arrow data into a DocSet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrow should be capitalized |
||
|
||
Examples: | ||
>>> import sycamore | ||
>>> import pyarrow as pa | ||
>>> context = sycamore.init() | ||
>>> table = pa.table({"x": [1]}) | ||
>>> docset = context.read.arrow(table) | ||
""" | ||
scan = ArrowScan(tables) | ||
return DocSet(self._context, scan) | ||
|
||
def document(self, docs: list[Document]) -> DocSet: | ||
""" | ||
Scan a list of Documents into a DocSet | ||
|
||
Examples: | ||
>>> import sycamore | ||
>>> from sycamore.data import Document | ||
>>> context = sycamore.init() | ||
>>> documents = [Document()] | ||
>>> docset = context.read.document(documents) | ||
""" | ||
scan = DocScan(docs) | ||
return DocSet(self._context, scan) | ||
|
||
def pandas(self, dfs: Union[DataFrame, list[DataFrame]]) -> DocSet: | ||
""" | ||
Scan a list of Documents into a DocSet | ||
|
||
Examples: | ||
>>> import sycamore | ||
>>> from pandas import DataFrame | ||
>>> context = sycamore.init() | ||
>>> df = DataFrame({"doc_id": 1, "type": "hello, world!"}) | ||
>>> docset = context.read.pandas(df) | ||
""" | ||
scan = PandasScan(dfs) | ||
return DocSet(self._context, scan) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from sycamore.writers.opensearch import OpenSearchWriter | ||
from sycamore.writers.writer import DocSetWriter | ||
|
||
__all__ = ["OpenSearchWriter"] | ||
__all__ = ["DocSetWriter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,25 @@ | |
|
||
|
||
class DocSetWriter: | ||
""" | ||
Write DocSet into different targets. | ||
""" | ||
|
||
def __init__(self, context: Context, plan: Node): | ||
self.context = context | ||
self.plan = plan | ||
|
||
def opensearch( | ||
self, *, os_client_args: dict, index_name: str, index_settings: Optional[dict] = None, **resource_args | ||
) -> None: | ||
from sycamore.writers import OpenSearchWriter | ||
"""Write a docset into opensearch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DocSet caps? OpenSearch caps |
||
Args: | ||
os_client_args: opensearch client arguments like host address, port number etc. | ||
index_name: opensearch index name | ||
index_settings: index setting such as number of shards, index mapping | ||
resource_args: Additional resource-related arguments that can be passed | ||
""" | ||
from sycamore.writers.opensearch import OpenSearchWriter | ||
|
||
os = OpenSearchWriter( | ||
self.plan, index_name, os_client_args=os_client_args, index_settings=index_settings, **resource_args | ||
|
@@ -42,6 +53,12 @@ def files( | |
Defaults to using text_representation if available, or binary_representation | ||
if not. | ||
resource_args: Arguments to pass to the underlying execution environment. | ||
|
||
Example | ||
>>> import sycamore | ||
>>> context = sycamore.init() | ||
>>> docset = context.read.json("s3://bucket/prefix1") | ||
>>> docset.write.files("s3://bucket/prefix2") | ||
""" | ||
file_writer = FileWriter( | ||
self.plan, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sycamore should be capitalized