Ekstrakcija i validacija pomoću alata
Na ovoj stranici
Izgradi pouzdane cjevovode za ekstrakciju
Kombiniranjem strukturiranih izlaza s alatima (web pretraživanje, izvršavanje koda) možeš izgraditi robusne cjevovode za ekstrakciju i validaciju. Ova lekcija pokazuje kako arhitektonski oblikovati te cjevovode za produkcijsku upotrebu.
Uzorak: obogaćena ekstrakcija
Najčešći uzorak je obogaćivanje ekstrakcije podataka web pretraživanjem. Počinješ sa sirovim tekstom, a model prvo ekstrahira entitete, a zatim ih obogaćuje informacijama pronađenima na internetu.
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"}]
)
Uzorak: unakrsna validacija
Unakrsna validacija koristi web pretraživanje za verifikaciju informacija ekstrahiranih iz dokumenta:
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}")
Uzorak: izračun i strukturiranje
Kombiniraj izvršavanje koda za izračune i strukturirane izlaze za formatiranje:
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")
Arhitektura robusnog cjevovoda
Za produkcijsku upotrebu strukturiraj svoj cjevovod u korake:
# 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]
Svaki korak koristi zaseban poziv s prilagođenom shemom. Ovaj modularni pristup pojednostavljuje otklanjanje pogrešaka i nadzor.
Rukovanje pogreškama u produkciji
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
)
Ključne točke
- Obogaćena ekstrakcija kombinira web pretraživanje i strukturirane izlaze za potpune podatke
- Unakrsna validacija provjerava činjenice putem web pretraživanja
- Izvršavanje koda osigurava točne izračune u financijskim cjevovodima
- Strukturiraj proces u zasebne korake radi robusnosti i otklanjanja pogrešaka
- Uvijek planiraj rezervno rješenje za slučaj kvara alata