-
Notifications
You must be signed in to change notification settings - Fork 54
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
Aryn connectors for reading and writing docsets #1147
Changes from 3 commits
6ae1b4c
f242546
192ec9d
7bdfd50
5bf54de
c0895ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import json | ||
import os | ||
from dataclasses import dataclass | ||
from typing import Optional, Any | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from sycamore.connectors.base_reader import BaseDBReader | ||
from sycamore.data import Document, Element | ||
from sycamore.data.element import create_element | ||
from sycamore.utils.aryn_config import ArynConfig | ||
|
||
|
||
@dataclass | ||
class ArynClientParams(BaseDBReader.ClientParams): | ||
def __init__(self, aryn_url: str, api_key: str, **kwargs): | ||
self.aryn_url = aryn_url | ||
assert self.aryn_url is not None, "Aryn URL is required" | ||
self.api_key = api_key | ||
assert self.api_key is not None, "API key is required" | ||
self.kwargs = kwargs | ||
|
||
|
||
@dataclass | ||
class ArynQueryParams(BaseDBReader.QueryParams): | ||
def __init__(self, docset_id: str): | ||
self.docset_id = docset_id | ||
|
||
|
||
class ArynQueryResponse(BaseDBReader.QueryResponse): | ||
def __init__(self, docs: list[dict[str, Any]]): | ||
self.docs = docs | ||
|
||
def to_docs(self, query_params: "BaseDBReader.QueryParams") -> list[Document]: | ||
docs = [] | ||
for doc in self.docs: | ||
elements = doc.get("elements", []) | ||
_doc = Document(**doc) | ||
_doc.data["elements"] = [create_element(**element) for element in elements] | ||
docs.append(_doc) | ||
|
||
return docs | ||
|
||
|
||
class ArynClient(BaseDBReader.Client): | ||
def __init__(self, client_params: ArynClientParams, **kwargs): | ||
self.aryn_url = client_params.aryn_url | ||
self.api_key = client_params.api_key | ||
self.kwargs = kwargs | ||
|
||
def read_records(self, query_params: "BaseDBReader.QueryParams") -> "ArynQueryResponse": | ||
assert isinstance(query_params, ArynQueryParams) | ||
headers = { | ||
"Authorization": f"Bearer {self.api_key}" | ||
} | ||
response: Response = requests.post( | ||
f"{self.aryn_url}/docsets/{query_params.docset_id}/read", stream=True, | ||
headers=headers) | ||
assert response.status_code == 200 | ||
docs = [] | ||
print(f"Reading from docset: {query_params.docset_id}") | ||
for chunk in response.iter_lines(): | ||
# print(f"\n{chunk}\n") | ||
doc = json.loads(chunk) | ||
docs.append(doc) | ||
|
||
return ArynQueryResponse(docs) | ||
|
||
def check_target_presence(self, query_params: "BaseDBReader.QueryParams") -> bool: | ||
return True | ||
|
||
@classmethod | ||
def from_client_params(cls, params: "BaseDBReader.ClientParams") -> "ArynClient": | ||
assert isinstance(params, ArynClientParams) | ||
return cls(params) | ||
|
||
|
||
class ArynReader(BaseDBReader): | ||
Client = ArynClient | ||
Record = ArynQueryResponse | ||
ClientParams = ArynClientParams | ||
QueryParams = ArynQueryParams |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import os | ||
from dataclasses import dataclass | ||
from typing import Optional, Mapping | ||
|
||
import requests | ||
|
||
from sycamore.connectors.base_writer import BaseDBWriter | ||
from sycamore.data import Document | ||
from sycamore.utils.aryn_config import ArynConfig | ||
|
||
|
||
@dataclass | ||
class ArynWriterClientParams(BaseDBWriter.ClientParams): | ||
def __init__(self, aryn_url: str, api_key: str, **kwargs): | ||
self.aryn_url = aryn_url | ||
assert self.aryn_url is not None, "Aryn URL is required" | ||
self.api_key = api_key | ||
assert self.api_key is not None, "API key is required" | ||
self.kwargs = kwargs | ||
|
||
|
||
@dataclass | ||
class ArynWriterTargetParams(BaseDBWriter.TargetParams): | ||
def __init__(self, docset_id: Optional[str] = None): | ||
self.docset_id = docset_id | ||
|
||
def compatible_with(self, other: "BaseDBWriter.TargetParams") -> bool: | ||
return True | ||
|
||
|
||
class ArynWriterRecord(BaseDBWriter.Record): | ||
def __init__(self, doc: Document): | ||
self.doc = doc | ||
|
||
@classmethod | ||
def from_doc(cls, document: Document, target_params: "BaseDBWriter.TargetParams") -> "ArynWriterRecord": | ||
return cls(document) | ||
|
||
|
||
class ArynWriterClient(BaseDBWriter.Client): | ||
def __init__(self, client_params: ArynWriterClientParams, **kwargs): | ||
self.aryn_url = client_params.aryn_url | ||
self.api_key = client_params.api_key | ||
self.kwargs = kwargs | ||
|
||
@classmethod | ||
def from_client_params(cls, params: "BaseDBWriter.ClientParams") -> "BaseDBWriter.Client": | ||
assert isinstance(params, ArynWriterClientParams) | ||
return cls(params) | ||
|
||
def write_many_records(self, records: list["BaseDBWriter.Record"], target_params: "BaseDBWriter.TargetParams"): | ||
assert isinstance(target_params, ArynWriterTargetParams) | ||
docset_id = target_params.docset_id | ||
name = "" | ||
|
||
headers = { | ||
"Authorization": f"Bearer {self.api_key}" | ||
} | ||
|
||
for record in records: | ||
assert isinstance(record, ArynWriterRecord) | ||
doc = record.doc | ||
files: Mapping = {"doc": doc.serialize()} | ||
res = requests.post(url=f"{self.aryn_url}/docsets/write", | ||
params={"docset_id": docset_id}, | ||
files=files, headers=headers) | ||
|
||
def create_target_idempotent(self, target_params: "BaseDBWriter.TargetParams"): | ||
pass | ||
|
||
def get_existing_target_params(self, target_params: "BaseDBWriter.TargetParams") -> "BaseDBWriter.TargetParams": | ||
pass | ||
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. Is it a huge lift to add docset-creation functionality to this? 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. If there are multiple writer tasks, will each one attempt to create a docset? Since |
||
|
||
|
||
class ArynWriter(BaseDBWriter): | ||
Client = ArynWriterClient | ||
Record = ArynWriterRecord | ||
ClientParams = ArynWriterClientParams | ||
TargetParams = ArynWriterTargetParams |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,14 @@ | |
from pyarrow.filesystem import FileSystem | ||
|
||
from sycamore.connectors.doc_reconstruct import DocumentReconstructor | ||
from sycamore.connectors.aryn.ArynReader import ArynClientParams, ArynQueryParams | ||
from sycamore.context import context_params | ||
from sycamore.plan_nodes import Node | ||
from sycamore import Context, DocSet | ||
from sycamore.data import Document | ||
from sycamore.connectors.file import ArrowScan, BinaryScan, DocScan, PandasScan, JsonScan, JsonDocumentScan | ||
from sycamore.connectors.file.file_scan import FileMetadataProvider | ||
from sycamore.utils.aryn_config import ArynConfig | ||
from sycamore.utils.import_utils import requires_modules | ||
|
||
|
||
|
@@ -632,3 +634,23 @@ def qdrant(self, client_params: dict, query_params: dict, **kwargs) -> DocSet: | |
**kwargs, | ||
) | ||
return DocSet(self._context, wr) | ||
|
||
@context_params | ||
def aryn(self, docset_id: str, **kwargs) -> 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. What params come from the context? 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. I don't think I need this. I'll remove it. |
||
""" | ||
Reads the contents of a Aryn collection into a DocSet. | ||
|
||
Args: | ||
kwargs: Keyword arguments to pass to the underlying execution engine. | ||
""" | ||
from sycamore.connectors.aryn.ArynReader import ( | ||
ArynReader, | ||
ArynClientParams, | ||
ArynQueryParams, | ||
) | ||
|
||
api_key = ArynConfig.get_aryn_api_key() | ||
aryn_url = ArynConfig.get_aryn_url() | ||
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. should be able to pass these in as params here too I think 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. ok |
||
|
||
dr = ArynReader(client_params=ArynClientParams(aryn_url, api_key), query_params=ArynQueryParams(docset_id), **kwargs) | ||
return DocSet(self._context, dr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
import logging | ||
from typing import Any, Callable, Optional, Union, TYPE_CHECKING | ||
|
||
import requests | ||
from pyarrow.fs import FileSystem | ||
|
||
from sycamore.connectors.aryn.ArynReader import ArynClientParams | ||
from sycamore.connectors.aryn.ArynWriter import ArynWriterTargetParams | ||
from sycamore.context import Context, ExecMode, context_params | ||
from sycamore.connectors.common import HostAndPort | ||
from sycamore.connectors.file.file_writer import default_doc_to_bytes, default_filename, FileWriter, JsonWriter | ||
from sycamore.data import Document | ||
from sycamore.executor import Execution | ||
from sycamore.plan_nodes import Node | ||
from sycamore.docset import DocSet | ||
from sycamore.utils.aryn_config import ArynConfig | ||
from sycamore.utils.import_utils import requires_modules | ||
|
||
from mypy_boto3_s3.client import S3Client | ||
|
@@ -800,6 +804,38 @@ def json( | |
|
||
self._maybe_execute(node, True) | ||
|
||
def aryn( | ||
self, | ||
docset_id: Optional[str] = None, | ||
create_new_docset: Optional[bool] = False, | ||
name: Optional[str] = None, | ||
**kwargs, | ||
) -> Optional["DocSet"]: | ||
""" | ||
Writes all documents of a DocSet to Aryn. | ||
""" | ||
|
||
from sycamore.connectors.aryn.ArynWriter import ( | ||
ArynWriter, | ||
ArynWriterClientParams, | ||
ArynWriterTargetParams, | ||
) | ||
|
||
api_key = ArynConfig.get_aryn_api_key() | ||
aryn_url = ArynConfig.get_aryn_url() | ||
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. these guys too 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. ok |
||
if docset_id is None and create_new_docset and name is not None: | ||
headers = { | ||
"Authorization": f"Bearer {api_key}" | ||
} | ||
res = requests.post(url=f"{aryn_url}/docsets", data={"name": name}, headers=headers) | ||
docset_id = res.json()["docset_id"] | ||
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. I guess docstore allows multiple docsets with the same name? 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. Yes, only IDs are unique. |
||
|
||
client_params = ArynWriterClientParams(aryn_url, api_key) | ||
target_params = ArynWriterTargetParams(docset_id) | ||
ds = ArynWriter(self.plan, client_params=client_params, target_params=target_params, **kwargs) | ||
|
||
return self._maybe_execute(ds, True) | ||
|
||
def _maybe_execute(self, node: Node, execute: bool) -> Optional[DocSet]: | ||
ds = DocSet(self.context, node) | ||
if not execute: | ||
|
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.
Some day I feel like we should turn these lists that we're passing around into iterators/generators (all the way to the ray ds construction) but not right now and that applies to all the readers