Pipeline multi-strumento
In questa pagina
Orchestrazione degli strumenti in sequenza
L’agente meteo della lezione precedente utilizzava strumenti indipendenti. In una pipeline multi-strumento, i risultati di uno strumento alimentano la chiamata successiva. Il modello orchestra la sequenza automaticamente: il tuo ruolo è fornire gli strumenti ed eseguire le chiamate.
Architettura della pipeline
Una pipeline tipica segue questo schema:
Ricerca → Analisi → Azione → Notifica
Ogni passo produce dati consumati dal successivo. Il modello gestisce questa orchestrazione naturalmente grazie al contesto della conversazione.
Esempio: pipeline di intelligence competitiva
Immagina una pipeline che monitora i prezzi dei concorrenti e genera un report:
tools = [
{
"type": "function",
"function": {
"name": "scrape_competitor_prices",
"description": "Retrieve current prices from a competitor "
"for a list of products. Returns an array "
"of {product, price, availability}.",
"parameters": {
"type": "object",
"properties": {
"competitor": {
"type": "string",
"description": "Competitor name"
},
"products": {
"type": "array",
"items": {"type": "string"},
"description": "List of products to check"
}
},
"required": ["competitor", "products"]
}
}
},
{
"type": "function",
"function": {
"name": "compare_with_our_prices",
"description": "Compare competitor prices with our prices. "
"Returns percentage gaps and adjustment "
"recommendations.",
"parameters": {
"type": "object",
"properties": {
"competitor_prices": {
"type": "array",
"items": {
"type": "object",
"properties": {
"product": {"type": "string"},
"price": {"type": "number"}
}
}
}
},
"required": ["competitor_prices"]
}
}
},
{
"type": "function",
"function": {
"name": "generate_report",
"description": "Generate a PDF report with the benchmark "
"analysis results. Returns the report URL.",
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string"},
"data": {"type": "object"},
"format": {
"type": "string",
"enum": ["pdf", "csv", "json"]
}
},
"required": ["title", "data"]
}
}
},
{
"type": "function",
"function": {
"name": "send_notification",
"description": "Send an email or Slack notification "
"with a summary and a link to the report.",
"parameters": {
"type": "object",
"properties": {
"channel": {
"type": "string",
"enum": ["email", "slack"]
},
"recipient": {"type": "string"},
"message": {"type": "string"},
"attachment_url": {"type": "string"}
},
"required": ["channel", "recipient", "message"]
}
}
}
]
Esecuzione della pipeline
Quando l’utente chiede: «Analizza i prezzi di CompetitorX per i nostri 5 prodotti principali e invia il report al team vendite», il modello orchestra:
Turn 1 : scrape_competitor_prices(competitor="CompetitorX", products=[...])
Turn 2 : compare_with_our_prices(competitor_prices=[results from turn 1])
Turn 3 : generate_report(title="CompetitorX Price Monitoring", data={...})
Turn 4 : send_notification(channel="slack", recipient="#sales", ...)
Ogni turno utilizza i risultati del precedente. Il modello passa naturalmente i dati da uno strumento all’altro.
Combinazione di strumenti server e client in una pipeline
Le pipeline più potenti combinano strumenti server e client:
tools = [
{"type": "web_search"}, # Server: web search
{
"type": "function",
"function": {
"name": "extract_data",
"description": "Extract structured data from raw text",
"parameters": { ... }
}
},
{
"type": "function",
"function": {
"name": "save_to_crm",
"description": "Save extracted data to the CRM",
"parameters": { ... }
}
}
]
Flusso: il modello esegue una ricerca web (automatica lato server), poi chiama la tua funzione di estrazione (lato client), infine salva nel CRM (lato client).
Pattern: fan-out / fan-in
Alcune pipeline richiedono un pattern fan-out (chiamate parallele multiple) seguito da un fan-in (aggregazione dei risultati):
┌→ scrape_site_A ─┐
Request ─┤→ scrape_site_B ─┤→ aggregate_results → generate_report
└→ scrape_site_C ─┘
Il modello gestisce questo scenario naturalmente con chiamate parallele:
Turn 1 : [scrape_site_A, scrape_site_B, scrape_site_C] (parallel)
Turn 2 : aggregate_results(data=[result_A, result_B, result_C])
Turn 3 : generate_report(aggregated_data={...})
Gestione degli errori in una pipeline
Una pipeline robusta gestisce gli errori a ogni passo:
def execute_with_retry(name, args, max_retries=2):
for attempt in range(max_retries + 1):
try:
result = execute_function(name, args)
if "error" not in result:
return result
except Exception:
pass
if attempt < max_retries:
time.sleep(1) # Wait before retry
return {
"error": f"Failed after {max_retries + 1} attempts",
"function": name,
"partial_data": None
}
Quando un passo fallisce, il modello può:
- Riprovare con parametri aggiustati
- Saltare il passo e continuare con dati parziali
- Informare l’utente e richiedere istruzioni
Punti chiave
- Una pipeline multi-strumento concatena i risultati di uno strumento al successivo
- Il modello orchestra la sequenza automaticamente grazie al contesto
- Il pattern fan-out/fan-in combina chiamate parallele e aggregazione
- Combina strumenti server (web_search) e client (le tue funzioni) per pipeline ibride
- Gestisci gli errori a ogni passo con retry e fallback
- Ogni strumento nella pipeline deve avere una responsabilità singola e chiara