Maximofn commited on
Commit
c61e41b
·
1 Parent(s): 33b440f

feat(EXCEPTION_HANDLING): :bug: Implement general exception handler and improve error responses for text generation and summarization

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -3,6 +3,7 @@ from pydantic import BaseModel
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import torch
5
  from functools import partial
 
6
 
7
  from langchain_core.messages import HumanMessage, AIMessage
8
  from langgraph.checkpoint.memory import MemorySaver
@@ -121,6 +122,14 @@ class SummaryRequest(BaseModel):
121
  # Create the FastAPI application
122
  app = FastAPI(title="LangChain FastAPI", description="API to generate text using LangChain and LangGraph - Máximo Fernández Núñez IriusRisk test challenge")
123
 
 
 
 
 
 
 
 
 
124
  # Welcome endpoint
125
  @app.get("/")
126
  async def api_home():
@@ -170,7 +179,14 @@ async def generate(request: QueryRequest):
170
  "thread_id": request.thread_id
171
  }
172
  except Exception as e:
173
- raise HTTPException(status_code=500, detail=f"Error generating text: {str(e)}")
 
 
 
 
 
 
 
174
 
175
  @app.post("/summarize")
176
  async def summarize(request: SummaryRequest):
@@ -216,7 +232,14 @@ async def summarize(request: SummaryRequest):
216
  "thread_id": request.thread_id
217
  }
218
  except Exception as e:
219
- raise HTTPException(status_code=500, detail=f"Error generating summary: {str(e)}")
 
 
 
 
 
 
 
220
 
221
  if __name__ == "__main__":
222
  import uvicorn
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import torch
5
  from functools import partial
6
+ from fastapi.responses import JSONResponse
7
 
8
  from langchain_core.messages import HumanMessage, AIMessage
9
  from langgraph.checkpoint.memory import MemorySaver
 
122
  # Create the FastAPI application
123
  app = FastAPI(title="LangChain FastAPI", description="API to generate text using LangChain and LangGraph - Máximo Fernández Núñez IriusRisk test challenge")
124
 
125
+ # Add general exception handler
126
+ @app.exception_handler(Exception)
127
+ async def general_exception_handler(request, exc):
128
+ return JSONResponse(
129
+ status_code=500,
130
+ content={"error": f"Error interno: {str(exc)}", "type": type(exc).__name__}
131
+ )
132
+
133
  # Welcome endpoint
134
  @app.get("/")
135
  async def api_home():
 
179
  "thread_id": request.thread_id
180
  }
181
  except Exception as e:
182
+ return JSONResponse(
183
+ status_code=500,
184
+ content={
185
+ "error": f"Error generando texto: {str(e)}",
186
+ "type": type(e).__name__,
187
+ "thread_id": request.thread_id
188
+ }
189
+ )
190
 
191
  @app.post("/summarize")
192
  async def summarize(request: SummaryRequest):
 
232
  "thread_id": request.thread_id
233
  }
234
  except Exception as e:
235
+ return JSONResponse(
236
+ status_code=500,
237
+ content={
238
+ "error": f"Error generando resumen: {str(e)}",
239
+ "type": type(e).__name__,
240
+ "thread_id": request.thread_id
241
+ }
242
+ )
243
 
244
  if __name__ == "__main__":
245
  import uvicorn