ciyidogan commited on
Commit
ec93388
Β·
verified Β·
1 Parent(s): cd2c11f

Update chat_handler.py

Browse files
Files changed (1) hide show
  1. chat_handler.py +31 -33
chat_handler.py CHANGED
@@ -205,51 +205,49 @@ class SessionRequest(BaseModel):
205
  class ChatRequest(BaseModel):
206
  message: str
207
 
 
 
 
 
208
  # ───────────────────────── API ENDPOINTS ───────────────────────── #
209
- @router.post("/start_session")
210
- async def start_session(req: SessionRequest):
211
- """Initialize a new chat session"""
212
  try:
213
- # Verify project exists and has published version
214
- project = next((p for p in cfg.projects if p.name == req.project_name), None)
215
  if not project:
216
- raise HTTPException(status_code=404, detail=f"Project '{req.project_name}' not found")
217
-
218
- if not project.enabled:
219
- raise HTTPException(status_code=400, detail=f"Project '{req.project_name}' is disabled")
220
-
221
- # Find published version
222
- published_version = next((v for v in project.versions if v.published), None)
223
- if not published_version:
224
- raise HTTPException(status_code=400, detail=f"Project '{req.project_name}' has no published version")
225
-
226
- # Create session
227
- session = Session(
228
- project_name=req.project_name,
229
- version_id=published_version.id,
230
- test_mode=req.test_mode
231
- )
232
 
233
- # Store version config in session
234
- session.version_config = published_version
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
- # Store session
237
- session_store.add(session)
 
 
238
 
239
- log(f"πŸ”‘ New session created: {session.session_id[:8]}... for project '{req.project_name}' v{published_version.id}")
240
 
241
- return {
242
- "session_id": session.session_id,
243
- "project_name": req.project_name,
244
- "version": published_version.id,
245
- "greeting": "Merhaba! Size nasΔ±l yardΔ±mcΔ± olabilirim?"
246
- }
247
 
248
  except HTTPException:
249
  raise
250
  except Exception as e:
251
  log(f"❌ Session creation error: {e}")
252
- raise HTTPException(status_code=500, detail=str(e))
253
 
254
  @router.post("/chat")
255
  async def chat(req: ChatRequest, x_session_id: str = Header(...)):
 
205
  class ChatRequest(BaseModel):
206
  message: str
207
 
208
+ class StartRequest(BaseModel):
209
+ project_name: str
210
+ version_number: Optional[int] = None # Opsiyonel, belirtilmezse published olan en bΓΌyΓΌk version no'yu kullan
211
+
212
  # ───────────────────────── API ENDPOINTS ───────────────────────── #
213
+ @router.post("/start_session", response_model=ChatResponse)
214
+ async def start_session(req: StartRequest):
215
+ """Create new session"""
216
  try:
217
+ # Validate project exists
218
+ project = next((p for p in cfg.projects if p.name == req.project_name and p.enabled), None)
219
  if not project:
220
+ raise HTTPException(404, f"Project '{req.project_name}' not found or disabled")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
+ # Find version
223
+ if req.version_number:
224
+ # Specific version requested
225
+ version = next((v for v in project.versions if v.id == req.version_number), None)
226
+ if not version:
227
+ raise HTTPException(404, f"Version {req.version_number} not found for project '{req.project_name}'")
228
+ else:
229
+ # Find published version with highest version number
230
+ published_versions = [v for v in project.versions if v.published]
231
+ if not published_versions:
232
+ raise HTTPException(404, f"No published version for project '{req.project_name}'")
233
+
234
+ # Sort by version number (id) and get the highest
235
+ version = max(published_versions, key=lambda v: v.id)
236
 
237
+ # Create session with version config
238
+ session = session_store.create_session(req.project_name, version)
239
+ greeting = "Hoş geldiniz! Size nasıl yardımcı olabilirim?"
240
+ session.add_turn("assistant", greeting)
241
 
242
+ log(f"βœ… Session created for project '{req.project_name}' version {version.id} (highest published)")
243
 
244
+ return ChatResponse(session_id=session.session_id, answer=greeting)
 
 
 
 
 
245
 
246
  except HTTPException:
247
  raise
248
  except Exception as e:
249
  log(f"❌ Session creation error: {e}")
250
+ raise HTTPException(500, f"Session creation failed: {str(e)}")
251
 
252
  @router.post("/chat")
253
  async def chat(req: ChatRequest, x_session_id: str = Header(...)):