11from uuid import UUID
22from typing import List
3- from datetime import timedelta
43from sqlalchemy .orm import Session
54from fastapi import HTTPException , status
65from langchain_core .messages .human import HumanMessage
76
87from src .util .configuration import Config
98from src .agentflow .agents .hub import AgentHub
109from src .db .models import Conversation , QueryUsage
10+ from src .agentflow .utils .shared_tools import init_llm
1111from src .user .services .user_service import UserService
1212from src .agent .services .memory_service import MemoryService
1313from src .project .services .project_service import ProjectService
@@ -31,7 +31,7 @@ def __init__(self):
3131 self .user_service = UserService ()
3232
3333 def create_conversation (
34- self , db_session : Session , name : str , project_id : UUID , user_id : UUID
34+ self , db_session : Session , name : str , project_id : UUID , user_id : UUID , conversation_id : UUID = None
3535 ) -> Conversation :
3636 """
3737 Creates a new conversation.
@@ -41,6 +41,7 @@ def create_conversation(
4141 name (str): The name of the conversation.
4242 project_id (UUID): The ID of the project the conversation belongs to.
4343 user_id (UUID): The ID of the user creating the conversation.
44+ conversation_id (UUID): The ID of the conversation (optional).
4445
4546 Returns:
4647 Conversation: The created conversation instance.
@@ -53,6 +54,11 @@ def create_conversation(
5354
5455 conversation = Conversation (
5556 name = name , project_id = project_id , user_id = user_id )
57+
58+ # conversation id is generated in frontend
59+ if conversation_id :
60+ conversation .id = conversation_id
61+
5662 return self .repository .create (db_session , conversation )
5763
5864 def get_conversation (
@@ -258,9 +264,46 @@ def get_history(
258264 memory = self .memory_service .get_memory (conversation_id )
259265
260266 history = []
267+ generated_id = 0
261268 for message in memory .messages :
262269 history .append ({
270+ "id" : generated_id ,
263271 "role" : "human" if isinstance (message , HumanMessage ) else "ai" ,
264272 "content" : message .content ,
265273 })
274+ generated_id += 1
266275 return history
276+
277+ def rename_title (self , db_session : Session , conversation_id : UUID , user_id : UUID , message : str ) -> str :
278+ """
279+ Renames a conversation title using the LLM to generate a title.
280+
281+ Args:
282+ db_session (Session): The database session.
283+ conversation_id (UUID): The ID of the conversation to rename.
284+ user_id (UUID): The ID of the user renaming the conversation.
285+ message (str): The message to generate a title for.
286+
287+ Returns:
288+ str: The new title of the conversation.
289+ """
290+ config = Config .get_config ()
291+ self .llm = init_llm (service = config ["llm" ]["provider" ],
292+ model_name = config ["llm" ]["model" ],
293+ api_key = config ["llm" ]["api_key" ],
294+ stream = False ,
295+ callbacks = None )
296+ messages = [
297+ (
298+ "system" ,
299+ "You should generate a title (maximum 5 words) for this message" ,
300+ ),
301+ ("human" , f"{ message } " ),
302+ ]
303+
304+ title = self .llm .invoke (messages )
305+
306+ self .repository .update (db_session , conversation_id , {
307+ "name" : title .content }, user_id )
308+
309+ return title .content
0 commit comments