mirror of
http://88.130.71.182:3000/BlitTech/contexta_be.git
synced 2026-06-12 23:23:21 +00:00
fixed bugs
This commit is contained in:
@@ -21,9 +21,9 @@ def _get_public_chatbot(chatbot_id: str, supabase) -> dict:
|
||||
|
||||
@router.post("/chat/{chatbot_id}", response_model=ChatResponse)
|
||||
async def chat(
|
||||
chatbot_id: str,
|
||||
message: ChatMessage,
|
||||
user=Depends(get_optional_user),
|
||||
chatbot_id: str,
|
||||
message: ChatMessage,
|
||||
user=Depends(get_optional_user),
|
||||
):
|
||||
supabase = get_supabase()
|
||||
chatbot = _get_public_chatbot(chatbot_id, supabase)
|
||||
@@ -97,9 +97,9 @@ async def chat(
|
||||
|
||||
@router.get("/chat/{chatbot_id}/history/{session_id}", response_model=List[MessageResponse])
|
||||
async def get_chat_history(
|
||||
chatbot_id: str,
|
||||
session_id: str,
|
||||
user=Depends(get_optional_user),
|
||||
chatbot_id: str,
|
||||
session_id: str,
|
||||
user=Depends(get_optional_user),
|
||||
):
|
||||
supabase = get_supabase()
|
||||
|
||||
@@ -114,7 +114,7 @@ async def get_chat_history(
|
||||
conv_id = conversation.data[0]["id"]
|
||||
messages = supabase.table("messages").select("*") \
|
||||
.eq("conversation_id", conv_id) \
|
||||
.order("created_at", asc=True) \
|
||||
.order("created_at", desc=False) \
|
||||
.execute()
|
||||
|
||||
return [
|
||||
@@ -140,7 +140,8 @@ async def get_analytics(chatbot_id: str, user=Depends(get_current_user)):
|
||||
if not company.data:
|
||||
raise HTTPException(status_code=404, detail="Company not found")
|
||||
|
||||
chatbot = supabase.table("chatbots").select("id").eq("id", chatbot_id).eq("company_id", company.data[0]["id"]).execute()
|
||||
chatbot = supabase.table("chatbots").select("id").eq("id", chatbot_id).eq("company_id",
|
||||
company.data[0]["id"]).execute()
|
||||
if not chatbot.data:
|
||||
raise HTTPException(status_code=404, detail="Chatbot not found")
|
||||
|
||||
@@ -159,11 +160,11 @@ async def get_analytics(chatbot_id: str, user=Depends(get_current_user)):
|
||||
# ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
def _get_or_create_conversation(
|
||||
chatbot_id: str,
|
||||
session_id: str,
|
||||
user_id: Optional[str],
|
||||
language: str,
|
||||
supabase,
|
||||
chatbot_id: str,
|
||||
session_id: str,
|
||||
user_id: Optional[str],
|
||||
language: str,
|
||||
supabase,
|
||||
) -> dict:
|
||||
existing = supabase.table("conversations").select("*") \
|
||||
.eq("chatbot_id", chatbot_id) \
|
||||
@@ -186,21 +187,29 @@ def _get_or_create_conversation(
|
||||
|
||||
|
||||
def _get_conversation_history(conversation_id: str, supabase) -> List[dict]:
|
||||
"""
|
||||
FIX: Changed from desc=True to desc=False (ascending/chronological order).
|
||||
|
||||
The conversation history MUST be in chronological order (oldest first)
|
||||
for the LLM to correctly understand the conversation flow.
|
||||
Previously, messages were returned newest-first, which reversed the
|
||||
conversation and confused the model.
|
||||
"""
|
||||
messages = supabase.table("messages").select("role, content") \
|
||||
.eq("conversation_id", conversation_id) \
|
||||
.order("created_at", desc=True) \
|
||||
.order("created_at", desc=False) \
|
||||
.limit(20) \
|
||||
.execute()
|
||||
return messages.data or []
|
||||
|
||||
|
||||
def _save_message(
|
||||
conversation_id: str,
|
||||
role: str,
|
||||
content: str,
|
||||
supabase,
|
||||
sources: Optional[list] = None,
|
||||
model: str = "",
|
||||
conversation_id: str,
|
||||
role: str,
|
||||
content: str,
|
||||
supabase,
|
||||
sources: Optional[list] = None,
|
||||
model: str = "",
|
||||
):
|
||||
supabase.table("messages").insert({
|
||||
"id": str(uuid.uuid4()),
|
||||
@@ -209,4 +218,4 @@ def _save_message(
|
||||
"content": content,
|
||||
"sources": sources,
|
||||
"model": model,
|
||||
}).execute()
|
||||
}).execute()
|
||||
Reference in New Issue
Block a user