Sources-API-Skript
Wenn Sie Dateien programmatisch hochladen möchten, können Sie die Sources-API verwenden. Damit können Sie Dateien auf dieselbe Weise hochladen wie über die Benutzeroberfläche. Die API-Dokumentation finden Sie hier.
Bitte führen Sie das folgende Skript im Sinne der Fair-Use-Richtlinie nicht täglich aus, sondern höchstens einmal pro Woche und nur, wenn Sie neue Dateien hochzuladen haben. Das Skript löscht alle bestehenden Quellen und lädt alle Dateien erneut hoch, was Last auf der Datenbank und Kosten für die für die Neuindexierung erforderlichen Embedding-API-Aufrufe verursacht.
Das folgende Skript ist ein Beispiel dafür, wie Sie Dateien mit der Sources-API in LoyJoy hochladen. Das Skript ist in Python geschrieben und verwendet die Bibliothek requests. Das Skript lädt alle PDF- und HTML-Dateien aus den angegebenen Verzeichnissen hoch. Es liest außerdem eine CSV-Datei mit der docid und der URL der Quelle. Die URL wird verwendet, um die Quelle mit einer internen URL zu verknüpfen, z. B. SharePoint oder Moodle. Das Skript löscht außerdem alle bestehenden Quellen, bevor es die neuen Dateien hochlädt. Das Skript ist für Windows geschrieben, kann aber leicht an andere Betriebssysteme angepasst werden.
#!/usr/bin/env python3
# pip3 install requests
import glob
import os
import requests
import csv
import time
AUTH_TOKEN = ""
CSV_FILE = "X:/.../urls.csv"
HTML_DIR = "X:/.../html/"
PDF_DIR = "X:/.../pdf/"
API_URL = "https://app-stable.loyjoy.com/api/sources/upload"
# If you are behind a proxy, you can set the proxy here
PROXY = { "http": "http://proxy-username:proxy-password@localhost:8080", "https": "http://proxy-username:proxy-password@localhost:8080" }
# Optional: Read a CSV containing the docid and the URL of the source
csv_content = []
with open(CSV_FILE, "r") as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=";", quotechar='"', fieldnames=["key", "value"])
for row in csv_reader:
csv_content.append(row)
# Delete all existing sources
req = requests.delete(API_URL,
headers={"Authorization": AUTH_TOKEN},
proxies=PROXY)
docs = 9999
max_tries = 10
try_count = 0
while docs > 0 and try_count < max_tries:
req = requests.get(API_URL,
headers={"Authorization": AUTH_TOKEN},
proxies=PROXY)
# fetch number of sources in LoyJoy, while deletion is still in progress
docs=len(req.json())
try_count += 1
print("Waiting for 60 seconds")
time.sleep(60)
# Upload PDF files
paths = sorted(glob.glob(PDF_DIR + "**/*.pdf", recursive=True))
for pdf in paths:
for row in csv_content:
if row["value"] == pdf[pdf.rfind("_")+1:pdf.find(".pdf")]:
docid = row["key"]
if "docid" in locals():
# Optional: Create URL so that the chat can link the sources to internal URLs, e.g. SharePoint or Moodle
url = requests.utils.quote(f"https://localhost:8443/pdf/{docid}")
optional_folder = "some_folder"
req = requests.post(f"{API_URL}?url={url}&folder={optional_folder}",
files={"file": (os.path.basename(pdf), open(pdf, "rb"), "application/pdf")},
headers={"Authorization": AUTH_TOKEN},
proxies=PROXY)
del docid
else:
req = requests.post(f"{API_URL}",
files={"file": (os.path.basename(pdf), open(pdf, "rb"), "application/pdf")},
headers={"Authorization": AUTH_TOKEN},
proxies=PROXY)
if req.status_code == 200:
print("Uploaded: {}".format(pdf))
else:
print("Error: {}".format(pdf))
print(req.status_code)
print(req.text)
# Upload HTML files
paths = sorted(glob.glob(HTML_DIR + "**/*.html", recursive=True))
for html in paths:
docid = os.path.splitext(os.path.basename(html))[0]
# Optional: Create URL so that the AI agent can link to the source
url = requests.utils.quote(f"https://localhost:8443/html/{docid}")
optional_folder = "some_folder"
req = requests.post(f"{API_URL}?url={url}&folder={optional_folder}",
files={"file": (os.path.basename(html), open(html, "rb"), "text/html")},
headers={"Authorization": AUTH_TOKEN},
proxies=PROXY)
if req.status_code == 200:
print("Uploaded: {}".format(html))
else:
print("Error: {}".format(html))
print(req.status_code)
print(req.text)
print("Upload finished")