Spaces:
Building
Building
Update chat_handler.py
Browse files- 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:
|
211 |
-
"""
|
212 |
try:
|
213 |
-
#
|
214 |
-
project = next((p for p in cfg.projects if p.name == req.project_name), None)
|
215 |
if not project:
|
216 |
-
raise HTTPException(
|
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 |
-
#
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
-
#
|
237 |
-
session_store.
|
|
|
|
|
238 |
|
239 |
-
log(f"
|
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(
|
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(...)):
|