Skip to main content

🎯 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:​

  1. ❌ Don't call LLM yourself
    Let QWED handle it internally

  2. βœ… Call QWED directly
    Use natural language queries

  3. πŸ”’ 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.