-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from ElishaKay/document-reports
Research with local documents
- Loading branch information
Showing
26 changed files
with
1,825 additions
and
1,058 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,3 +1,4 @@ | ||
OPENAI_API_KEY= | ||
TAVILY_API_KEY= | ||
LANGCHAIN_API_KEY= | ||
LANGCHAIN_API_KEY= | ||
DOC_PATH=./my-docs |
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,10 +1,12 @@ | ||
version: '3' | ||
services: | ||
gpt-researcher: | ||
image: assafelovic/gpt-researcher | ||
image: kramer1346/gpt-researcher | ||
build: ./ | ||
environment: | ||
OPENAI_API_KEY: ${OPENAI_API_KEY} | ||
TAVILY_API_KEY: ${TAVILY_API_KEY} | ||
DOC_PATH: "=./my-docs" | ||
LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY} | ||
ports: | ||
- 8000:8000 |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .document import DocumentLoader | ||
|
||
__all__ = ['DocumentLoader'] |
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,65 @@ | ||
import asyncio | ||
import os | ||
|
||
from langchain_community.document_loaders import ( | ||
PyMuPDFLoader, | ||
TextLoader, | ||
UnstructuredCSVLoader, | ||
UnstructuredExcelLoader, | ||
UnstructuredMarkdownLoader, | ||
UnstructuredPowerPointLoader, | ||
UnstructuredWordDocumentLoader | ||
) | ||
|
||
|
||
class DocumentLoader: | ||
|
||
def __init__(self, path): | ||
self.path = path | ||
|
||
async def load(self) -> list: | ||
tasks = [] | ||
for root, dirs, files in os.walk(self.path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
file_name, file_extension_with_dot = os.path.splitext(file_path) | ||
file_extension = file_extension_with_dot.strip(".") | ||
tasks.append(self._load_document(file_path, file_extension)) | ||
|
||
docs = [] | ||
for pages in await asyncio.gather(*tasks): | ||
for page in pages: | ||
if page.page_content: | ||
docs.append({ | ||
"raw_content": page.page_content, | ||
"url": os.path.basename(page.metadata['source']) | ||
}) | ||
|
||
if not docs: | ||
raise ValueError("🤷 Failed to load any documents!") | ||
|
||
return docs | ||
|
||
async def _load_document(self, file_path: str, file_extension: str) -> list: | ||
try: | ||
loader_dict = { | ||
"pdf": PyMuPDFLoader(file_path), | ||
"txt": TextLoader(file_path), | ||
"doc": UnstructuredWordDocumentLoader(file_path), | ||
"docx": UnstructuredWordDocumentLoader(file_path), | ||
"pptx": UnstructuredPowerPointLoader(file_path), | ||
"csv": UnstructuredCSVLoader(file_path, mode="elements"), | ||
"xls": UnstructuredExcelLoader(file_path, mode="elements"), | ||
"xlsx": UnstructuredExcelLoader(file_path, mode="elements"), | ||
"md": UnstructuredMarkdownLoader(file_path) | ||
} | ||
|
||
loader = loader_dict.get(file_extension, None) | ||
if loader: | ||
data = loader.load() | ||
return data | ||
|
||
except Exception as e: | ||
print(f"Failed to load document : {file_path}") | ||
print(e) | ||
return [] |
Oops, something went wrong.