EsempioscriptintermediateEseguibileschema-validation
Sistema di Ticket
Esempio rieseguibile intermediate di tipo script che usa instructor, openai.
Fatti chiave
- Livello
- intermediate
- Runtime
- Python • API OpenAI
- Pattern
- Flusso ispezionabile con confini di sistema visibili
- Interazione
- Sandbox live • Script
- Aggiornato
- 14 marzo 2026
Naviga questo esempio
Libreria
Sfoglia gli esempiRiapri la libreria completa per confrontare pattern vicini e percorsi collegati.Interazione
Esegui ora nel sandboxProva l'interazione direttamente nella superficie guidata di questo esempio.Sorgente
Apri codice completoLeggi l'implementazione reale, i punti evidenziati e i requisiti runtime.MCP
Chiama via MCPUsa la stessa risorsa dentro agenti, export deterministici e setup MCP.
04-ticket-system.py
python
import instructor
from pydantic import BaseModel, Field
from openai import OpenAI
from enum import Enum
# --------------------------------------------------------------
# Ticket System Example with Structured Output
# --------------------------------------------------------------
# Patch the OpenAI client
client = instructor.from_openai(OpenAI())
class TicketCategory(str, Enum):
"""Enumeration of categories for incoming tickets."""
GENERAL = "general"
ORDER = "order"
BILLING = "billing"
class CustomerSentiment(str, Enum):
"""Enumeration of customer sentiment labels."""
NEGATIVE = "negative"
NEUTRAL = "neutral"
POSITIVE = "positive"
class Ticket(BaseModel):
reply: str = Field(description="Your reply that we send to the customer.")
category: TicketCategory
confidence: float = Field(ge=0, le=1)
sentiment: CustomerSentiment
def process_ticket(customer_message: str) -> Ticket:
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Ticket,
max_retries=3,
messages=[
{
"role": "system",
"content": "Analyze the incoming customer message and predict the values for the ticket.",
},
{"role": "user", "content": customer_message},
],
)
return reply
# --------------------------------------------------------------
# Billing Issue Example
# --------------------------------------------------------------
ticket = process_ticket("Hi there, I have a question about my bill. Can you help me?")
assert ticket.category == TicketCategory.BILLING
ticket.reply
ticket.category
ticket.confidence
ticket.sentiment
# --------------------------------------------------------------
# Order-Related Example
# --------------------------------------------------------------
ticket = process_ticket("I would like to place an order.")
assert ticket.category == TicketCategory.ORDER