Aasher commited on
Commit
fc7c411
·
1 Parent(s): db5367c

feat(chat): add background task for updating chat title from message content

Browse files
Files changed (2) hide show
  1. api/routers/chats.py +8 -4
  2. db/crud/chat.py +31 -2
api/routers/chats.py CHANGED
@@ -1,7 +1,7 @@
1
  import uuid
2
  from typing import List
3
 
4
- from fastapi import APIRouter, Depends, HTTPException, status
5
  from sqlmodel.ext.asyncio.session import AsyncSession
6
 
7
  # --- LangChain Imports ---
@@ -110,6 +110,7 @@ async def post_message_and_get_response(
110
  *,
111
  chat_id: uuid.UUID,
112
  message_in: MessageCreate,
 
113
  user_id: uuid.UUID = Depends(get_current_user),
114
  db: AsyncSession = Depends(get_db)
115
  ):
@@ -149,7 +150,10 @@ async def post_message_and_get_response(
149
 
150
  # 8. If this was the first message, generate and set a title for the chat
151
  if is_first_user_message:
152
- new_title = await generate_chat_title(message_in.content)
153
- await chat_crud.update_chat_title(db=db, chat=chat, chat_update=ChatUpdate(title=new_title))
154
-
 
 
 
155
  return final_ai_message
 
1
  import uuid
2
  from typing import List
3
 
4
+ from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
5
  from sqlmodel.ext.asyncio.session import AsyncSession
6
 
7
  # --- LangChain Imports ---
 
110
  *,
111
  chat_id: uuid.UUID,
112
  message_in: MessageCreate,
113
+ background_tasks: BackgroundTasks,
114
  user_id: uuid.UUID = Depends(get_current_user),
115
  db: AsyncSession = Depends(get_db)
116
  ):
 
150
 
151
  # 8. If this was the first message, generate and set a title for the chat
152
  if is_first_user_message:
153
+ background_tasks.add_task(
154
+ chat_crud.update_chat_title_from_message,
155
+ db=db,
156
+ chat_id=chat.id,
157
+ message_content=message_in.content
158
+ )
159
  return final_ai_message
db/crud/chat.py CHANGED
@@ -4,10 +4,12 @@ from typing import Optional
4
 
5
  from sqlmodel import Session, select
6
  from sqlmodel.ext.asyncio.session import AsyncSession
7
- from sqlalchemy.orm import selectinload
8
  from db.models.chat import Chat
9
  from db.schemas.chat import ChatUpdate
10
 
 
 
11
  async def create_chat(db: AsyncSession, user_id: uuid.UUID) -> Chat:
12
  """Creates a new, empty chat for a specific user asynchronously."""
13
  db_chat = Chat(user_id=user_id)
@@ -18,7 +20,12 @@ async def create_chat(db: AsyncSession, user_id: uuid.UUID) -> Chat:
18
 
19
  async def get_chats_by_user(db: AsyncSession, user_id: uuid.UUID) -> list[Chat]:
20
  """Retrieves all chats for a specific user, sorted by the most recently updated."""
21
- statement = select(Chat).where(Chat.user_id == user_id).order_by(Chat.updated_at.desc())
 
 
 
 
 
22
  result = await db.exec(statement)
23
  return result.all()
24
 
@@ -41,6 +48,28 @@ async def update_chat_title(db: AsyncSession, chat: Chat, chat_update: ChatUpdat
41
  await db.refresh(chat)
42
  return chat
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  async def delete_chat(db: AsyncSession, chat: Chat) -> None:
45
  """Deletes a chat from the database."""
46
  await db.delete(chat)
 
4
 
5
  from sqlmodel import Session, select
6
  from sqlmodel.ext.asyncio.session import AsyncSession
7
+ from sqlalchemy.orm import selectinload, raiseload
8
  from db.models.chat import Chat
9
  from db.schemas.chat import ChatUpdate
10
 
11
+ from workflow.title_generator import generate_chat_title
12
+
13
  async def create_chat(db: AsyncSession, user_id: uuid.UUID) -> Chat:
14
  """Creates a new, empty chat for a specific user asynchronously."""
15
  db_chat = Chat(user_id=user_id)
 
20
 
21
  async def get_chats_by_user(db: AsyncSession, user_id: uuid.UUID) -> list[Chat]:
22
  """Retrieves all chats for a specific user, sorted by the most recently updated."""
23
+ statement = (
24
+ select(Chat)
25
+ .where(Chat.user_id == user_id)
26
+ .order_by(Chat.updated_at.desc())
27
+ .options(raiseload(Chat.messages)) # Proactively block loading of messages
28
+ )
29
  result = await db.exec(statement)
30
  return result.all()
31
 
 
48
  await db.refresh(chat)
49
  return chat
50
 
51
+ async def update_chat_title_from_message(db: AsyncSession, chat_id: uuid.UUID, message_content: str):
52
+ """
53
+ Generates a title from a message and updates the chat record.
54
+ Designed to be run as a background task.
55
+ """
56
+ try:
57
+ chat = await db.get(Chat, chat_id)
58
+ if not chat or chat.title != "New Chat":
59
+ # Chat doesn't exist or has already been titled, so we exit.
60
+ return
61
+
62
+ new_title = await generate_chat_title(message_content)
63
+ if new_title:
64
+ chat.title = new_title
65
+ db.add(chat)
66
+ await db.commit()
67
+ print(f"INFO: Background task updated title for chat {chat_id} to '{new_title}'")
68
+ except Exception as e:
69
+ print(f"ERROR: Background task failed for chat {chat_id}: {e}")
70
+ finally:
71
+ await db.close() # Ensure the session is closed.
72
+
73
  async def delete_chat(db: AsyncSession, chat: Chat) -> None:
74
  """Deletes a chat from the database."""
75
  await db.delete(chat)