-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ae1b4c
Add Aryn reader and writer
austintlee f242546
Add new reader and writer for Aryn
austintlee 192ec9d
Remove old tests
austintlee 7bdfd50
Address reviewer comments
austintlee 5bf54de
Fix mypy
austintlee c0895ed
Fix lint
austintlee 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from sycamore.connectors.base_reader import BaseDBReader | ||
from sycamore.data import Document | ||
from sycamore.data.element import create_element | ||
|
||
|
||
@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 |
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,74 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, Mapping | ||
|
||
import requests | ||
|
||
from sycamore.connectors.base_writer import BaseDBWriter | ||
from sycamore.data import Document | ||
|
||
|
||
@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 | ||
|
||
headers = {"Authorization": f"Bearer {self.api_key}"} | ||
|
||
for record in records: | ||
assert isinstance(record, ArynWriterRecord) | ||
doc = record.doc | ||
files: Mapping = {"doc": doc.serialize()} | ||
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"): | ||
pass | ||
|
||
|
||
class ArynWriter(BaseDBWriter): | ||
Client = ArynWriterClient | ||
Record = ArynWriterRecord | ||
ClientParams = ArynWriterClientParams | ||
TargetParams = ArynWriterTargetParams |
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
Empty file.
Empty file.
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
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.
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