OpenWebUI integration
This commit is contained in:
52
scripts/backfill_openwebui.py
Executable file
52
scripts/backfill_openwebui.py
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
import os, sys
|
||||
from pathlib import Path
|
||||
import requests, orjson
|
||||
|
||||
OWUI_URL = os.getenv("OPENWEBUI_URL", "").rstrip("/")
|
||||
OWUI_KEY = os.getenv("OPENWEBUI_API_KEY", "")
|
||||
OWUI_KB = os.getenv("OPENWEBUI_KB_NAME", "Homelab Library")
|
||||
|
||||
LIB = Path(os.getenv("LIBRARY_ROOT", "./library"))
|
||||
TRN = Path(os.getenv("TRANSCRIPT_ROOT", "./transcripts"))
|
||||
|
||||
def headers():
|
||||
return {"Authorization": f"Bearer {OWUI_KEY}"} if OWUI_KEY else {}
|
||||
|
||||
def get_or_create_kb():
|
||||
if not OWUI_URL or not OWUI_KEY:
|
||||
print("OpenWebUI not configured.")
|
||||
sys.exit(1)
|
||||
r = requests.get(f"{OWUI_URL}/api/v1/knowledge/list", headers=headers(), timeout=15)
|
||||
r.raise_for_status()
|
||||
for kb in r.json().get("data", []):
|
||||
if kb.get("name") == OWUI_KB:
|
||||
return kb["id"]
|
||||
r = requests.post(f"{OWUI_URL}/api/v1/knowledge/create",
|
||||
headers={**headers(), "Content-Type":"application/json"},
|
||||
data=orjson.dumps({"name": OWUI_KB, "description": "All local content indexed by podx"}))
|
||||
r.raise_for_status()
|
||||
return r.json()["data"]["id"]
|
||||
|
||||
def upload_and_attach(path: Path, kb_id: str):
|
||||
with open(path, "rb") as f:
|
||||
r = requests.post(f"{OWUI_URL}/api/v1/files/", headers=headers(), files={"file": (path.name, f)}, timeout=60*10)
|
||||
r.raise_for_status()
|
||||
file_id = r.json()["data"]["id"]
|
||||
r = requests.post(f"{OWUI_URL}/api/v1/knowledge/{kb_id}/file/add",
|
||||
headers={**headers(), "Content-Type":"application/json"},
|
||||
data=orjson.dumps({"file_id": file_id}), timeout=60)
|
||||
r.raise_for_status()
|
||||
print(f"Uploaded {path}")
|
||||
|
||||
def main():
|
||||
kb_id = get_or_create_kb()
|
||||
# transcripts
|
||||
for txt in TRN.glob("*.txt"):
|
||||
upload_and_attach(txt, kb_id)
|
||||
# web snapshots
|
||||
for txt in LIB.glob("web/**/*.txt"):
|
||||
upload_and_attach(txt, kb_id)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user