요구 사항#
Python 3.11 이상. 추가 패키지 불필요. WEIGHTSAPI_API_KEY를 자신의 키로 설정하세요. AI 접근에는 로그인, 최소 100 USD의 확인된 충전 1회, 승인된 API 키, 요청에 충분한 사용 가능 크레딧이 필요합니다. 이후 각 충전도 최소 100 USD이며, 요청을 충당할 때 더 적은 잔액도 계속 사용할 수 있습니다. 크레딧은 거래 검증이 필요합니다. 트래픽을 보내기 전에 모델 가용성은 서비스 상태를 확인하세요.
레시피 실행#
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])
프로덕션 전#
이 작은 예제는 독립적으로 실행되도록 어휘 순위와 내장된 샘플 출처를 사용합니다. 말뭉치와 순위를 자신의 리트리버로 교체하고, 인용이 실제로 각 주장을 뒷받침하는지 검증하세요.