Spaces:
Sleeping
Sleeping
Commit
·
cfa5d53
1
Parent(s):
65b8488
file filename fileuri for every chat
Browse files- api_routes_v2.py +91 -11
api_routes_v2.py
CHANGED
|
@@ -444,6 +444,9 @@ class Message(BaseModel):
|
|
| 444 |
role: str
|
| 445 |
content: str
|
| 446 |
timestamp: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
|
| 449 |
class ChatRequest(BaseModel):
|
|
@@ -460,6 +463,9 @@ class ChatResponse(BaseModel):
|
|
| 460 |
chat_id: str
|
| 461 |
state: str
|
| 462 |
history: List[Message]
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
|
| 465 |
# ========================
|
|
@@ -491,17 +497,38 @@ def _get_session_or_init(chat_id: str) -> Dict[str, Any]:
|
|
| 491 |
def _normalize_history_for_api(chat_id: str) -> List[Message]:
|
| 492 |
"""
|
| 493 |
Return history in Gradio-like format: [{role, content, timestamp}]
|
|
|
|
| 494 |
"""
|
| 495 |
session = session_manager.get_session(chat_id) or {}
|
| 496 |
raw_messages = session.get("messages") or []
|
| 497 |
history: List[Message] = []
|
|
|
|
| 498 |
for m in raw_messages:
|
| 499 |
role = m.get("role") or m.get("sender") or "assistant"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
content = m.get("content") or m.get("text") or ""
|
| 501 |
ts = m.get("timestamp")
|
|
|
|
| 502 |
if not isinstance(content, str):
|
| 503 |
content = json.dumps(content, ensure_ascii=False)
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
return history
|
| 506 |
|
| 507 |
|
|
@@ -534,15 +561,24 @@ def _assistant_response_payload(
|
|
| 534 |
) -> ChatResponse:
|
| 535 |
# Persist assistant message
|
| 536 |
_add_and_mirror_message(chat_id, "assistant", friendly_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
# Return full history in role/content shape
|
| 538 |
history = _normalize_history_for_api(chat_id)
|
|
|
|
| 539 |
return ChatResponse(
|
| 540 |
assistant_response=friendly_response,
|
| 541 |
api_response=api_data,
|
| 542 |
intent=intent,
|
| 543 |
chat_id=chat_id,
|
| 544 |
state=state,
|
| 545 |
-
history=history
|
|
|
|
|
|
|
|
|
|
| 546 |
)
|
| 547 |
|
| 548 |
|
|
@@ -629,8 +665,37 @@ def upload_stream_to_s3(chat_id: str, file: UploadFile) -> str:
|
|
| 629 |
)
|
| 630 |
|
| 631 |
s3_uri = f"s3://{S3_BUCKET}/{key}"
|
| 632 |
-
|
| 633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
return s3_uri
|
| 635 |
|
| 636 |
|
|
@@ -1895,24 +1960,39 @@ def get_chat_history(chat_id: str):
|
|
| 1895 |
chat_id: Chat identifier
|
| 1896 |
|
| 1897 |
Returns:
|
| 1898 |
-
Chat history with session ID
|
| 1899 |
"""
|
| 1900 |
s = session_manager.get_session(chat_id)
|
| 1901 |
if not s:
|
| 1902 |
raise HTTPException(status_code=404, detail="Chat not found")
|
| 1903 |
|
| 1904 |
-
# Get the normalized history
|
| 1905 |
history = [m.dict() for m in _normalize_history_for_api(chat_id)]
|
| 1906 |
|
| 1907 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1908 |
return {
|
| 1909 |
-
"session_id": chat_id,
|
| 1910 |
-
"chat_id": chat_id,
|
| 1911 |
"history": history,
|
| 1912 |
"count": len(history),
|
| 1913 |
"state": s.get("state", "initial"),
|
| 1914 |
-
"created_at":
|
| 1915 |
-
"updated_at":
|
|
|
|
|
|
|
|
|
|
| 1916 |
}
|
| 1917 |
|
| 1918 |
|
|
|
|
| 444 |
role: str
|
| 445 |
content: str
|
| 446 |
timestamp: Optional[str] = None
|
| 447 |
+
file: Optional[bool] = None
|
| 448 |
+
fileName: Optional[str] = None
|
| 449 |
+
fileUrl: Optional[str] = None
|
| 450 |
|
| 451 |
|
| 452 |
class ChatRequest(BaseModel):
|
|
|
|
| 463 |
chat_id: str
|
| 464 |
state: str
|
| 465 |
history: List[Message]
|
| 466 |
+
file: Optional[bool] = None
|
| 467 |
+
fileName: Optional[str] = None
|
| 468 |
+
fileUrl: Optional[str] = None
|
| 469 |
|
| 470 |
|
| 471 |
# ========================
|
|
|
|
| 497 |
def _normalize_history_for_api(chat_id: str) -> List[Message]:
|
| 498 |
"""
|
| 499 |
Return history in Gradio-like format: [{role, content, timestamp}]
|
| 500 |
+
FILTERS OUT system messages as requested
|
| 501 |
"""
|
| 502 |
session = session_manager.get_session(chat_id) or {}
|
| 503 |
raw_messages = session.get("messages") or []
|
| 504 |
history: List[Message] = []
|
| 505 |
+
|
| 506 |
for m in raw_messages:
|
| 507 |
role = m.get("role") or m.get("sender") or "assistant"
|
| 508 |
+
|
| 509 |
+
# Skip system messages
|
| 510 |
+
if role == "system":
|
| 511 |
+
continue
|
| 512 |
+
|
| 513 |
content = m.get("content") or m.get("text") or ""
|
| 514 |
ts = m.get("timestamp")
|
| 515 |
+
|
| 516 |
if not isinstance(content, str):
|
| 517 |
content = json.dumps(content, ensure_ascii=False)
|
| 518 |
+
|
| 519 |
+
# Check if this message has file information
|
| 520 |
+
file_data = m.get("file_data") or {}
|
| 521 |
+
|
| 522 |
+
message = Message(
|
| 523 |
+
role=role,
|
| 524 |
+
content=content,
|
| 525 |
+
timestamp=ts,
|
| 526 |
+
file=file_data.get("has_file", False),
|
| 527 |
+
fileName=file_data.get("file_name"),
|
| 528 |
+
fileUrl=file_data.get("file_url")
|
| 529 |
+
)
|
| 530 |
+
history.append(message)
|
| 531 |
+
|
| 532 |
return history
|
| 533 |
|
| 534 |
|
|
|
|
| 561 |
) -> ChatResponse:
|
| 562 |
# Persist assistant message
|
| 563 |
_add_and_mirror_message(chat_id, "assistant", friendly_response)
|
| 564 |
+
|
| 565 |
+
# Get file metadata from session
|
| 566 |
+
session = session_manager.get_session(chat_id) or {}
|
| 567 |
+
file_metadata = session.get("file_metadata", {})
|
| 568 |
+
|
| 569 |
# Return full history in role/content shape
|
| 570 |
history = _normalize_history_for_api(chat_id)
|
| 571 |
+
|
| 572 |
return ChatResponse(
|
| 573 |
assistant_response=friendly_response,
|
| 574 |
api_response=api_data,
|
| 575 |
intent=intent,
|
| 576 |
chat_id=chat_id,
|
| 577 |
state=state,
|
| 578 |
+
history=history,
|
| 579 |
+
file=file_metadata.get("has_file", False),
|
| 580 |
+
fileName=file_metadata.get("file_name"),
|
| 581 |
+
fileUrl=file_metadata.get("file_url")
|
| 582 |
)
|
| 583 |
|
| 584 |
|
|
|
|
| 665 |
)
|
| 666 |
|
| 667 |
s3_uri = f"s3://{S3_BUCKET}/{key}"
|
| 668 |
+
|
| 669 |
+
# Store file metadata in session
|
| 670 |
+
session_manager.update_session(chat_id, {
|
| 671 |
+
"current_file": s3_uri,
|
| 672 |
+
"state": "initial",
|
| 673 |
+
"file_metadata": {
|
| 674 |
+
"has_file": True,
|
| 675 |
+
"file_name": file.filename,
|
| 676 |
+
"file_url": s3_uri,
|
| 677 |
+
"uploaded_at": datetime.utcnow().isoformat() + "Z"
|
| 678 |
+
}
|
| 679 |
+
})
|
| 680 |
+
|
| 681 |
+
# Create a user message with file metadata (instead of system message)
|
| 682 |
+
file_message = {
|
| 683 |
+
"role": "user",
|
| 684 |
+
"content": f"Uploaded file: {file.filename}",
|
| 685 |
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
| 686 |
+
"file_data": {
|
| 687 |
+
"has_file": True,
|
| 688 |
+
"file_name": file.filename,
|
| 689 |
+
"file_url": s3_uri
|
| 690 |
+
}
|
| 691 |
+
}
|
| 692 |
+
|
| 693 |
+
# Get existing messages and add the file message
|
| 694 |
+
s = session_manager.get_session(chat_id) or {}
|
| 695 |
+
msgs = list(s.get("messages", []))
|
| 696 |
+
msgs.append(file_message)
|
| 697 |
+
session_manager.update_session(chat_id, {"messages": msgs})
|
| 698 |
+
|
| 699 |
return s3_uri
|
| 700 |
|
| 701 |
|
|
|
|
| 1960 |
chat_id: Chat identifier
|
| 1961 |
|
| 1962 |
Returns:
|
| 1963 |
+
Chat history with session ID and file metadata
|
| 1964 |
"""
|
| 1965 |
s = session_manager.get_session(chat_id)
|
| 1966 |
if not s:
|
| 1967 |
raise HTTPException(status_code=404, detail="Chat not found")
|
| 1968 |
|
| 1969 |
+
# Get the normalized history (system messages filtered out)
|
| 1970 |
history = [m.dict() for m in _normalize_history_for_api(chat_id)]
|
| 1971 |
|
| 1972 |
+
# Get file metadata
|
| 1973 |
+
file_metadata = s.get("file_metadata", {})
|
| 1974 |
+
|
| 1975 |
+
# Format datetime objects for JSON serialization
|
| 1976 |
+
created_at = s.get("created_at")
|
| 1977 |
+
updated_at = s.get("updated_at")
|
| 1978 |
+
|
| 1979 |
+
if isinstance(created_at, datetime):
|
| 1980 |
+
created_at = created_at.isoformat()
|
| 1981 |
+
if isinstance(updated_at, datetime):
|
| 1982 |
+
updated_at = updated_at.isoformat()
|
| 1983 |
+
|
| 1984 |
+
# Return with session_id and file metadata
|
| 1985 |
return {
|
| 1986 |
+
"session_id": chat_id,
|
| 1987 |
+
"chat_id": chat_id,
|
| 1988 |
"history": history,
|
| 1989 |
"count": len(history),
|
| 1990 |
"state": s.get("state", "initial"),
|
| 1991 |
+
"created_at": created_at,
|
| 1992 |
+
"updated_at": updated_at,
|
| 1993 |
+
"file": file_metadata.get("has_file", False),
|
| 1994 |
+
"fileName": file_metadata.get("file_name"),
|
| 1995 |
+
"fileUrl": file_metadata.get("file_url")
|
| 1996 |
}
|
| 1997 |
|
| 1998 |
|