Appearance
Python walkthrough
Upload a document, wait for it to be ready, then ask a question and print the answer with its source files.
Before you start
Python 3.10 or later and the requests package. Create a Read & write API key in the console.
bash
python3 -m pip install requestsSet these environment variables. Keep the API key private.
bash
export LAB0_API_KEY='lab0_sk_replace_with_your_key'
export LAB0_BASE_URL='https://app.lab0.ai/app'
export LAB0_NAMESPACE='quickstart'Save a document
Save the following as plan.md, or download it:
md
# Plan
The rollout uses quillfeather checklists.The example uploads this file by default. Set LAB0_FILE_PATH to use another path, and change the prompt to match its contents.
Upload, wait and ask
Save this as quickstart.py, or download the example.
py
import json
import os
import time
from pathlib import Path
import requests
base = os.getenv("LAB0_BASE_URL", "https://app.lab0.ai/app").rstrip("/")
namespace = os.getenv("LAB0_NAMESPACE", "quickstart")
path = Path(os.getenv("LAB0_FILE_PATH", "plan.md"))
session = requests.Session()
session.headers["Authorization"] = f"Bearer {os.environ['LAB0_API_KEY']}"
def request(method, route, **kwargs):
response = session.request(method, base + route, timeout=200, **kwargs)
if not response.ok:
raise RuntimeError(f"HTTP {response.status_code}: {response.text}")
return response.json()
with path.open("rb") as document:
uploaded = request("POST", "/api/knowledge/files",
files={"file": (path.name, document)},
data={"namespace": namespace})
file_id = uploaded["id"]
for _ in range(90):
document = request("GET", f"/api/knowledge/files/{file_id}")
if document["status"] == "ready":
break
if document["status"] == "failed":
raise RuntimeError(document.get("error", "Processing failed"))
time.sleep(2)
else:
raise TimeoutError(f"Processing timed out; inspect file {file_id}")
answer = request("POST", "/api/knowledge/respond", json={
"prompt": "How is the rollout run?",
"namespace": namespace,
"file_ids": [file_id],
})
print(json.dumps(answer, indent=2))Run it from the directory containing plan.md:
bash
python3 quickstart.pyThe example stops on an HTTP error, a failed upload or a processing timeout. Once the file is ready, it asks “How is the rollout run?” using only the uploaded file.
Read the result
A successful response contains answer, sources and model. The document says that the rollout uses quillfeather checklists; verify that the answer reflects that and that sources identifies plan.md. Exact wording and the model name can vary.
The file stays in your namespace for later questions. See cleanup when you finish, or continue with streaming answers and the API reference.