Extrakcia a validácia pomocou nástrojov
Na tejto strane
Budovanie spoľahlivých extrakčných pipeline
Kombináciou štruktúrovaných výstupov s nástrojmi (webové vyhľadávanie, vykonávanie kódu) môžeš vytvoriť robustné pipeline na extrakciu a validáciu dát. Táto lekcia ti ukáže, ako tieto pipeline navrhnúť pre nasadenie v produkčnom prostredí.
Vzorec: obohatená extrakcia
Najbežnejším vzorcom je obohatenie extrakcie dát pomocou webového vyhľadávania. Začneš so surovým textom, model extrahuje entity a následne ich obohatí o informácie nájdené 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"}]
)
Vzorec: krížová validácia
Krížová validácia využíva webové vyhľadávanie na overenie informácií extrahovaných z dokumentu:
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}")
Vzorec: výpočty a štruktúrovanie
Skombinuj vykonávanie kódu pre výpočty so štruktúrovanými výstupmi pre formátovanie:
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")
Architektúra robustného pipeline
Pre nasadenie v produkčnom prostredí rozdeľ svoj pipeline do jednotlivých krokov:
# 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]
Každý krok využíva samostatné volanie s prispôsobenou schémou. Tento modulárny prístup zjednodušuje ladenie a monitorovanie.
Spracovanie chýb v produkčnom prostredí
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
)
Kľúčové poznatky
- Obohatená extrakcia kombinuje webové vyhľadávanie so štruktúrovanými výstupmi pre úplné dáta.
- Krížová validácia overuje fakty pomocou webového vyhľadávania.
- Vykonávanie kódu zabezpečuje presné výpočty vo finančných pipeline.
- Rozdeľ proces na samostatné kroky pre väčšiu robustnosť a jednoduchšie ladenie.
- Vždy plánuj záložné riešenie pre prípad, že nástroje zlyhajú.