Requirements#
Python 3.11 or newer. No additional packages. Set WEIGHTSAPI_API_KEY to your own key. AI access requires sign-in, one confirmed top-up of at least USD 100, an authorized API key and enough available credit for the request. Each later top-up also has a USD 100 minimum; smaller remaining balances stay usable when they cover the request. Credit requires transaction verification. Check service status for model availability before sending traffic.
Run the recipe#
import os, json, urllib.request
BASE = os.environ.get("WEIGHTSAPI_BASE_URL", "https://weightsapi.com/v1")
KEY = os.environ["WEIGHTSAPI_API_KEY"]
def chat(messages, model="Qwen/Qwen3-32B", **options):
payload = dict(model=model, messages=messages, max_tokens=512, **options)
req = urllib.request.Request(BASE+"/chat/completions", json.dumps(payload).encode(),
{"Authorization": "Bearer "+KEY, "Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=120) as response:
return json.load(response)
documents = [
{"id":"S1","text":"A KV cache reuses attention keys and values from previous tokens."},
{"id":"S2","text":"Prefill processes the prompt; decode generates new output tokens."},
{"id":"S3","text":"Longer contexts increase KV cache memory usage."}]
question = "How does caching affect generation?"
terms = set(question.lower().split())
ranked = sorted(documents, key=lambda d: len(terms & set(d["text"].lower().split())), reverse=True)[:2]
context = "\n".join("["+d["id"]+"] "+d["text"] for d in ranked)
r = chat([{"role":"system","content":"Answer only from the sources. Cite [S1], [S2], etc. Say when evidence is missing."},
{"role":"user","content":context+"\nQuestion: "+question}])
print(r["choices"][0]["message"]["content"])
print("Retrieved sources:", [d["id"] for d in ranked])
Before production#
This small example uses lexical ranking and embedded sample sources so it runs independently. Replace the corpus and ranking with your retriever, and verify citations actually support each claim.