π― QWED Integration Guide
TL;DR: Don't call your LLM yourself. Let QWED handle it. β
β οΈ Common Mistakeβ
Most users think:
β DON'T DO THIS:β
# β WRONG!
import openai
from qwed import QWEDClient
# Calling LLM yourself
response = openai.ChatCompletion.create(...)
# Then trying to verify
qwed.verify(response.content) # TOO LATE!
Why this fails:
- π« No control over LLM prompts
- π« No DSL enforcement
- π« Vulnerable to prompt injection
- π« Can't guarantee structured output
β Correct Approachβ
β DO THIS:β
# β
CORRECT!
from qwed import QWEDClient
qwed = QWEDClient(api_key="qwed_...")
# Just call QWED directly
result = qwed.verify("Is 2+2 equal to 4?")
print(result.verified) # True β
Why this works:
- β QWED controls LLM internally
- β Structured prompts ensure DSL output
- β Formal verification layer active
- β 100% deterministic results
π How QWED Really Worksβ
Step-by-Step:β
1οΈβ£ Your Code
β
βββ "Is 15% of 200 equal to 30?"
β
βΌ
2οΈβ£ QWED API Gateway
β
βββ Sends to LLM (with special prompts)
β βββ LLM extracts: "15% Γ 200 = 30"
β βββ Returns structured data
β
βββ Sends to Formal Verifiers
β βββ SymPy calculates: 0.15 Γ 200 = 30
β βββ Verification: β
MATCH
β
βΌ
3οΈβ£ Deterministic Result
β
βββ {verified: true, evidence: {...}}
π Quick Start Examplesβ
1οΈβ£ Math Verificationβ
from qwed import QWEDClient
client = QWEDClient(api_key="your_key")
# β
Natural language input
result = client.verify("Is 2+2 equal to 5?")
# What happens inside QWED:
# π LLM extracts: "2+2=5"
# π¬ SymPy verifies: 2+2 = 4 (not 5!)
# β Returns: verified=False
print(result.verified) # False
print(result.reason) # "Expected 4, got 5"
print(result.evidence) # {"calculated": 4, "claimed": 5}
Visual Flow:
User Query β QWED β [LLM: "2+2=5"] β [SymPy: 4β 5] β β Failed
2οΈβ£ Code Securityβ
dangerous_code = """
def get_user(username):
query = f"SELECT * FROM users WHERE name='{username}'"
return db.execute(query)
"""
result = client.verify_code(dangerous_code, language="python")
# What happens inside QWED:
# π LLM identifies: String interpolation in SQL
# π¬ AST parser finds: User input in query
# π« Security engine: SQL INJECTION RISK
# β Returns: blocked=True
print(result.blocked) # True π«
print(result.vulnerabilities) # ["SQL Injection"]
print(result.severity) # "HIGH"
Visual Flow:
Code β QWED β [LLM: Detects SQL] β [AST: f-string in query] β π« BLOCKED
π¨ Visual Comparisonβ
Traditional LLM Call:β
βββββββββββββββ
β Your App β
ββββββββ¬βββββββ
β "Calculate 2+2"
βΌ
βββββββββββββββ
β GPT-4 API β π² Random output
ββββββββ¬βββββββ
β "2 + 2 = 5" β WRONG!
βΌ
βββββββββββββββ
β Your App β π₯ Uses wrong answer
βββββββββββββββ
QWED Call:β
βββββββββββββββ
β Your App β
ββββββββ¬βββββββ
β "Calculate 2+2"
βΌ
βββββββββββββββββββββββββββββββ
β QWED API β
β ββββββββ βββββββββββ β
β β LLM ββββββββΆβ SymPy β β
β ββββββββ ββββββ¬βββββ β
β "2+2=4" β Verify β
β βΌ β
β β
VERIFIED β
ββββββββββββββββββββ¬ββββββββββββ
β "4" β
βΌ
βββββββββββββββ
β Your App β β
Correct!
βββββββββββββββ
π Understanding the Security Modelβ
The Trust Boundary:β
ββββββββββββββββββββββββββββββββββββββββ
β UNTRUSTED ZONE β
β ββββββββββββββββββββββββββββββββββ β
β β LLM (OpenAI/Anthropic/etc) β β
β β β’ Can hallucinate β β
β β β’ Non-deterministic β β
β β β’ Prompt-injectable β β
β ββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββ€ββββββββββββββββββββββ
β Structured Output (DSL)
βΌ
ββββββββββββββββββββββββββββββββββββββββ
β TRUSTED ZONE β
β ββββββββββββββββββββββββββββββββββ β
β β Formal Verifiers β β
β β β’ SymPy (Math) β β
β β β’ Z3 (Logic) β β
β β β’ AST (Code) β β
β β β’ SQLGlot (SQL) β β
β ββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββ
Key Point: QWED ensures LLM output passes through the trust boundary via formal verification.
π― Do's and Don'tsβ
β DO:β
# β
Call QWED directly
result = qwed.verify("Calculate 15% of 200")
# β
Use natural language
result = qwed.verify("Is the square root of 16 equal to 4?")
# β
Let QWED handle LLM internally
result = qwed.verify_code(untrusted_code, language="python")
# β
Trust the verification results
if result.verified:
use_output(result.value)
β DON'T:β
# β Call LLM yourself first
llm_output = openai.chat(...)
qwed.verify(llm_output) # TOO LATE!
# β Try to bypass QWED's LLM
result = qwed.verify_math("2+2", skip_llm=True) # No such option
# β Mix QWED calls with direct LLM calls
llm_result = gpt4.complete(...)
qwed_result = qwed.verify(...) # Inconsistent!
# β Assume LLM output is correct
value = llm.generate("Calculate...")
use_value_directly(value) # DANGEROUS!
π Quick Summaryβ
Remember These 3 Things:β
-
β Don't call LLM yourself
Let QWED handle it internally -
β Call QWED directly
Use natural language queries -
π Trust the verification
QWED uses formal methods, not guessing
One-Line Integration:β
result = QWEDClient(api_key="...").verify("Your question here")
That's it! π
See Full Integration Guide for framework integrations, debugging, and advanced usage.