Ga naar hoofdinhoud
Nederlands— Kies een taal
Menu

Extractie en validatie met tools

Les 10 van 12 Bijgewerkt op 30 juli 2026 Ontwikkelaar
Op deze pagina
  1. Bouw betrouwbare extractiepijplijnen
  2. Patroon: verrijkte extractie
  3. Patroon: kruisvalidatie
  4. Patroon: berekening en structurering
  5. Architectuur van een robuuste pijplijn
  6. Foutafhandeling in productie
  7. Belangrijkste punten

Bouw betrouwbare extractiepijplijnen

Door gestructureerde uitvoer te combineren met tools (zoals webzoekopdrachten en code-uitvoering), kunt u robuuste pijplijnen voor extractie en validatie bouwen. Deze les laat zien hoe u deze pijplijnen architectonisch ontwerpt voor productieomgevingen.

Patroon: verrijkte extractie

Het meest voorkomende patroon is het verrijken van data-extractie met webzoekopdrachten. U begint met ruwe tekst, waarna het model entiteiten extraheert en deze verrijkt met informatie die online wordt gevonden.

from pydantic import BaseModel

class EntrepriseEnrichie(BaseModel):
    nom: str
    secteur: str
    pays_siege: str
    site_web: str | None
    description_courte: str
    nombre_employes_estime: str | None
    technologies_utilisees: list[str]

class ExtractionEnrichie(BaseModel):
    entreprises: list[EntrepriseEnrichie]
    relations: list[str]
    contexte: str

response, parsed = client.chat.parse(
    model="grok-4.20-0309-reasoning",
    messages=[{
        "role": "user",
        "content": """Extract and enrich the companies mentioned in this text:

        'Mistral AI announced a partnership with Scaleway to
        host its models in France. Meanwhile, Hugging Face
        continues to collaborate with OVHcloud on open source
        infrastructure.'"""
    }],
    response_format=ExtractionEnrichie,
    tools=[{"type": "web_search"}]
)

Patroon: kruisvalidatie

Kruisvalidatie gebruikt webzoekopdrachten om informatie die uit een document is gehaald, te verifiëren:

class FactCheck(BaseModel):
    affirmation: str
    verifie: bool
    source: str | None
    correction: str | None
    confiance: float

class RapportValidation(BaseModel):
    document_original: str
    verifications: list[FactCheck]
    fiabilite_globale: float
    resume: str

response, parsed = client.chat.parse(
    model="grok-4.20-0309-reasoning",
    messages=[{
        "role": "user",
        "content": """Verify the facts in this text:
        'France has 68 million inhabitants. Paris is the
        largest city in Europe. France's GDP is the 7th
        largest worldwide.'"""
    }],
    response_format=RapportValidation,
    tools=[{"type": "web_search"}]
)

for v in parsed.verifications:
    status = "Verified" if v.verifie else "Unverified"
    print(f"[{status}] {v.affirmation}")
    if v.correction:
        print(f"  → Correction: {v.correction}")

Patroon: berekening en structurering

Combineer code-uitvoering voor berekeningen met gestructureerde uitvoer voor opmaak:

class LigneFacture(BaseModel):
    description: str
    quantite: int
    prix_unitaire_ht: float
    montant_ht: float
    tva: float
    montant_ttc: float

class Facture(BaseModel):
    lignes: list[LigneFacture]
    total_ht: float
    total_tva: float
    total_ttc: float

response, parsed = client.chat.parse(
    model="grok-4.20-0309-reasoning",
    messages=[{
        "role": "user",
        "content": """Calculate the invoice:
        - 3x Pro License at 299 EUR excl. VAT (VAT 20%)
        - 1x Training at 1500 EUR excl. VAT (VAT 20%)
        - 12x Monthly Support at 49 EUR excl. VAT (VAT 20%)"""
    }],
    response_format=Facture,
    tools=[{"type": "code_interpreter"}]
)

print(f"Total excl. VAT: {parsed.total_ht} EUR")
print(f"Total incl. VAT: {parsed.total_ttc} EUR")

Architectuur van een robuuste pijplijn

Voor gebruik in productieomgevingen structureert u uw pijplijn in stappen:

# Step 1: Raw extraction
class ExtractionBrute(BaseModel):
    entites: list[str]
    dates: list[str]
    montants: list[str]

# Step 2: Enrichment
class EntiteEnrichie(BaseModel):
    nom: str
    type: str
    details: str

# Step 3: Validation
class ResultatValide(BaseModel):
    entites: list[EntiteEnrichie]
    score_qualite: float
    alertes: list[str]

Elke stap gebruikt een aparte aanroep met een op maat gemaakt schema. Deze modulaire aanpak vereenvoudigt foutopsporing en monitoring.

Foutafhandeling in productie

try:
    response, parsed = client.chat.parse(
        model="grok-4.20-0309-reasoning",
        messages=messages,
        response_format=MonSchema,
        tools=[{"type": "web_search"}]
    )
except Exception as e:
    # Fallback without web search
    response, parsed = client.chat.parse(
        model="grok-4.20-0309-reasoning",
        messages=messages,
        response_format=MonSchema
    )

Belangrijkste punten

  • Verrijkte extractie combineert webzoekopdrachten en gestructureerde uitvoer voor volledige data
  • Kruisvalidatie verifieert feiten met webzoekopdrachten
  • Code-uitvoering zorgt voor exacte berekeningen in financiële pijplijnen
  • Structureer in afzonderlijke stappen voor robuustheid en foutopsporing
  • Voorzie altijd een noodoplossing voor het geval tools falen