참고소스 수정본
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"""Create a custom data loader to transform content into text."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
from fsspec import AbstractFileSystem
|
||||
|
||||
from neo4j_graphrag.experimental.components.data_loader import DataLoader
|
||||
from neo4j_graphrag.experimental.components.types import DocumentInfo, LoadedDocument
|
||||
|
||||
|
||||
class MyLoader(DataLoader):
|
||||
async def run(
|
||||
self,
|
||||
filepath: Union[str, Path],
|
||||
metadata: Optional[Dict[str, str]] = None,
|
||||
fs: Optional[Union[AbstractFileSystem, str]] = None,
|
||||
) -> LoadedDocument:
|
||||
# Implement logic here; use ``fs`` when reading from non-local storage.
|
||||
_ = fs
|
||||
return LoadedDocument(
|
||||
text="<extracted text>",
|
||||
document_info=DocumentInfo(
|
||||
path=str(filepath),
|
||||
metadata=metadata,
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Use the PdfLoader component to extract text from a PDF file."""
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from neo4j_graphrag.experimental.components.data_loader import PdfLoader
|
||||
|
||||
root_dir = Path(__file__).parents[4]
|
||||
file_path = root_dir / "data" / "Harry Potter and the Chamber of Secrets Summary.pdf"
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
loader = PdfLoader()
|
||||
document = await loader.run(filepath=file_path)
|
||||
print(document.text[:200])
|
||||
print(document.document_info)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Use the PdfLoader component to extract text from a remote PDF file."""
|
||||
|
||||
import asyncio
|
||||
|
||||
from neo4j_graphrag.experimental.components.data_loader import PdfLoader
|
||||
|
||||
url = "https://raw.githubusercontent.com/neo4j/neo4j-graphrag-python/c166afc4d5abc56a5686f3da46a97ed7c07da19d/examples/data/Harry%20Potter%20and%20the%20Chamber%20of%20Secrets%20Summary.pdf"
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
loader = PdfLoader()
|
||||
document = await loader.run(filepath=url, fs="http")
|
||||
print(document.text[:100])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user