mirror of
https://github.com/open-webui/open-webui
synced 2025-06-14 02:11:03 +00:00
Some checks are pending
Deploy to HuggingFace Spaces / check-secret (push) Waiting to run
Deploy to HuggingFace Spaces / deploy (push) Blocked by required conditions
Create and publish Docker images with specific build args / build-main-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-main-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / build-cuda-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-cuda-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / build-ollama-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-ollama-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / merge-main-images (push) Blocked by required conditions
Create and publish Docker images with specific build args / merge-cuda-images (push) Blocked by required conditions
Create and publish Docker images with specific build args / merge-ollama-images (push) Blocked by required conditions
Python CI / Format Backend (3.11.x) (push) Waiting to run
Python CI / Format Backend (3.12.x) (push) Waiting to run
Frontend Build / Format & Build Frontend (push) Waiting to run
Frontend Build / Frontend Unit Tests (push) Waiting to run
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import requests
|
|
import logging
|
|
from typing import Iterator, List, Union
|
|
|
|
from langchain_core.document_loaders import BaseLoader
|
|
from langchain_core.documents import Document
|
|
from open_webui.env import SRC_LOG_LEVELS
|
|
|
|
log = logging.getLogger(__name__)
|
|
log.setLevel(SRC_LOG_LEVELS["RAG"])
|
|
|
|
|
|
class ExternalDocumentLoader(BaseLoader):
|
|
def __init__(
|
|
self,
|
|
file_path,
|
|
url: str,
|
|
api_key: str,
|
|
mime_type=None,
|
|
**kwargs,
|
|
) -> None:
|
|
self.url = url
|
|
self.api_key = api_key
|
|
|
|
self.file_path = file_path
|
|
self.mime_type = mime_type
|
|
|
|
def load(self) -> list[Document]:
|
|
with open(self.file_path, "rb") as f:
|
|
data = f.read()
|
|
|
|
headers = {}
|
|
if self.mime_type is not None:
|
|
headers["Content-Type"] = self.mime_type
|
|
|
|
if self.api_key is not None:
|
|
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
|
|
url = self.url
|
|
if url.endswith("/"):
|
|
url = url[:-1]
|
|
|
|
r = requests.put(f"{url}/process", data=data, headers=headers)
|
|
|
|
if r.ok:
|
|
res = r.json()
|
|
|
|
if res:
|
|
return [
|
|
Document(
|
|
page_content=res.get("page_content"),
|
|
metadata=res.get("metadata"),
|
|
)
|
|
]
|
|
else:
|
|
raise Exception("Error loading document: No content returned")
|
|
else:
|
|
raise Exception(f"Error loading document: {r.status_code} {r.text}")
|