Introduction
Imagine you ask a smart assistant a question, but it doesn't understand or can't answer. How does it respond so you still get some help? This is where fallback and error handling come in to keep conversations smooth and useful.
Jump into concepts and practice - no test required
Imagine talking to a helpful store assistant who sometimes doesn’t know the answer. Instead of leaving you hanging, they say, 'Let me check with a colleague' or 'Can you tell me more?' This keeps the conversation friendly and useful.
┌───────────────┐
│ User Input │
└──────┬────────┘
│
▼
┌───────────────┐
│ AI Processing │
└──────┬────────┘
│
▼
┌───────────────┐ ┌───────────────┐
│ Success │ │ Error Detected │
└──────┬────────┘ └──────┬────────┘
│ │
▼ ▼
┌───────────────┐ ┌───────────────┐
│ Provide Answer│ │ Fallback Reply│
└───────────────┘ └───────────────┘def get_response(input_text):
try:
return model.generate(input_text)
except Exception:
return "Sorry, I can't process that right now."
print(get_response('Hello'))model.generate raises an exception, what prints?model.generate. If it raises an exception, the except block returns the fallback string.try:
result = model.predict(data)
except:
result = fallback()
print(result)fallback() is not defined or imported, calling it causes a NameError, which is the main issue.