-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat:create base sdk * fix: restructure repo * fix: separate package * fix : remove comments * add: pyproject * fix: rebase branch * fix: release please & sdk dep * delete general.py * fix: remove unused files * change bearer to api key & reformat imports * fix: API key optional
- Loading branch information
Showing
22 changed files
with
625 additions
and
345 deletions.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
LLAMA_CLOUD_API_KEY=llx-1234567890 | ||
OPENAI_API_KEY = sk-1234567890 | ||
OPENAI_API_KEY=sk-1234567890 | ||
MEGAPARSE_API_KEY=MyMegaParseKey |
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 @@ | ||
*.md | ||
/output | ||
/input | ||
.env | ||
|
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,8 @@ | ||
# from .Converter import MegaParse | ||
"""My library with optional components.""" | ||
|
||
# __all__ = ["MegaParse"] | ||
__version__ = "0.1.0" | ||
|
||
# Import only SDK components by default | ||
from megaparse import sdk | ||
|
||
__all__ = ["sdk"] |
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,4 @@ | ||
## MegaParse SDK | ||
|
||
What if you just don't care about the code and just want to call a service that does everythink for you ? | ||
We thought of that (since we do to), just use our SDK. |
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,3 @@ | ||
from megaparse.sdk.src import MegaParseSDK | ||
|
||
__all__ = ["MegaParseSDK"] |
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,31 @@ | ||
import asyncio | ||
import os | ||
|
||
from megaparse.sdk import MegaParseSDK | ||
|
||
|
||
async def main(): | ||
api_key = str(os.getenv("MEGAPARSE_API_KEY")) | ||
megaparse = MegaParseSDK(api_key) | ||
|
||
url = "https://www.quivr.com" | ||
|
||
# Upload a URL | ||
url_response = await megaparse.url.upload(url) | ||
print(f"\n----- URL Response : {url} -----\n") | ||
print(url_response) | ||
|
||
file_path = "megaparse/sdk/pdf/MegaFake_report.pdf" | ||
# Upload a file | ||
response = await megaparse.file.upload( | ||
file_path=file_path, | ||
method="unstructured", # type: ignore # unstructured, llama_parser, megaparse_vision | ||
strategy="auto", | ||
) | ||
print(f"\n----- File Response : {file_path} -----\n") | ||
print(response) | ||
await megaparse.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Binary file not shown.
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,30 @@ | ||
[project] | ||
name = "megaparse-sdk" | ||
version = "0.1.0" | ||
description = "Megaparse SDK" | ||
dependencies = [ | ||
"python-dotenv>=1.0.0", | ||
"pycryptodome>=3.21.0", | ||
"psutil>=6.1.0", | ||
"llama-parse>=0.4.0", | ||
"httpx>=0.27.0", | ||
|
||
] | ||
|
||
readme = "README.md" | ||
requires-python = "< 3.12" | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.rye] | ||
managed = true | ||
dev-dependencies = [] | ||
universal = true | ||
|
||
[tool.hatch.metadata] | ||
allow-direct-references = true | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["megaparse_sdk"] |
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,13 @@ | ||
from .client import MegaParseClient | ||
from .endpoints.file_upload import FileUpload | ||
from .endpoints.url_upload import URLUpload | ||
|
||
|
||
class MegaParseSDK: | ||
def __init__(self, api_key: str): | ||
self.client = MegaParseClient(api_key) | ||
self.file = FileUpload(self.client) | ||
self.url = URLUpload(self.client) | ||
|
||
async def close(self): | ||
await self.client.close() |
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,26 @@ | ||
from typing import Any | ||
|
||
import httpx | ||
|
||
|
||
class MegaParseClient: | ||
def __init__(self, api_key: str | None = None): | ||
self.base_url = "https://megaparse.tooling.quivr.app" # to define once in production # to define once in production | ||
|
||
self.api_key = api_key | ||
if self.api_key: | ||
self.session = httpx.AsyncClient( | ||
headers={"x-api-key": self.api_key}, timeout=60 | ||
) | ||
else: | ||
self.session = httpx.AsyncClient(timeout=60) | ||
|
||
async def request(self, method: str, endpoint: str, **kwargs: Any) -> Any: | ||
url = f"{self.base_url}{endpoint}" | ||
client = self.session | ||
response = await client.request(method, url, **kwargs) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
async def close(self): | ||
await self.session.aclose() |
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 @@ | ||
|
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,34 @@ | ||
from typing import Optional | ||
|
||
from httpx import Response | ||
from llama_parse.utils import Language | ||
|
||
from megaparse.sdk.src.client import MegaParseClient | ||
from megaparse.sdk.utils.type import ParserType, StrategyEnum | ||
|
||
|
||
class FileUpload: | ||
def __init__(self, client: MegaParseClient): | ||
self.client = client | ||
|
||
async def upload( | ||
self, | ||
file_path: str, | ||
method: ParserType = ParserType.UNSTRUCTURED, | ||
strategy: str = StrategyEnum.AUTO, | ||
check_table: bool = False, | ||
language: Language = Language.ENGLISH, | ||
parsing_instruction: Optional[str] = None, | ||
model_name: str = "gpt-4o", | ||
) -> Response: | ||
with open(file_path, "rb") as file: | ||
files = {"file": (file_path, file)} | ||
data = { | ||
"method": method, | ||
"strategy": strategy, | ||
"check_table": check_table, | ||
"language": language.value, | ||
"parsing_instruction": parsing_instruction, | ||
"model_name": model_name, | ||
} | ||
return await self.client.request("POST", "/v1/file", files=files, data=data) |
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,11 @@ | ||
from megaparse.sdk.src.client import MegaParseClient | ||
|
||
|
||
class URLUpload: | ||
def __init__(self, client: MegaParseClient): | ||
self.client = client | ||
|
||
async def upload(self, url: str): | ||
endpoint = f"/v1/url?url={url}" | ||
headers = {"accept": "application/json"} | ||
return await self.client.request("POST", endpoint, headers=headers, data="") |
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,17 @@ | ||
from enum import Enum | ||
|
||
|
||
class ParserType(str, Enum): | ||
"""Parser type enumeration.""" | ||
|
||
UNSTRUCTURED = "unstructured" | ||
LLAMA_PARSER = "llama_parser" | ||
MEGAPARSE_VISION = "megaparse_vision" | ||
|
||
|
||
class StrategyEnum(str, Enum): | ||
"""Method to use for the conversion""" | ||
|
||
FAST = "fast" | ||
AUTO = "auto" | ||
HI_RES = "hi_res" |
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.