Udtræk og validering med værktøjer
På denne side
Byg pålidelige udtrækspipelines
Ved at kombinere strukturerede output med værktøjer (websøgning, kodeeksekvering) kan du bygge robuste pipelines til udtræk og validering. Denne lektion viser dig, hvordan du arkitekterer disse pipelines til produktionsbrug.
Mønster: beriget udtræk
Det mest almindelige mønster er at berige dataudtræk med websøgning. Du starter med rå tekst, hvor modellen udtrækker enheder og derefter beriger dem med information fundet online.
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"}]
)
Mønster: krydsvalidering
Krydsvalidering bruger websøgning til at verificere information udtrukket fra et dokument:
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}")
Mønster: beregning og strukturering
Kombinér kodeeksekvering til beregninger og strukturerede output til formatering:
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")
Arkitektur for en robust pipeline
Til produktionsbrug skal du strukturere din pipeline i trin:
# 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]
Hvert trin bruger et separat kald med et skræddersyet skema. Denne modulære tilgang forenkler fejlsøgning og overvågning.
Håndtering af fejl i produktion
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
)
Vigtige pointer
- Beriget udtræk kombinerer websøgning + strukturerede output for komplette data
- Krydsvalidering verificerer fakta med websøgning
- Kodeeksekvering sikrer nøjagtige beregninger i økonomiske pipelines
- Strukturér i separate trin for robusthed og fejlsøgning
- Planlæg altid en nødplan, hvis værktøjer fejler