File size: 2,106 Bytes
64d7fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from app.core.pipeline import rag_pipeline
from app.core.memory import conversation_memory
from app.utils.logger import logger
from typing import AsyncIterator, Optional


class ChatService:
    
    def __init__(self):
        self.pipeline = rag_pipeline
        self.memory = conversation_memory
    
    async def generate_response(
        self,
        message: str,
        session_id: Optional[str] = None,
        use_rag: bool = True,
        use_history: bool = True
    ) -> str:
        try:
            response = await self.pipeline.generate(
                query=message,
                session_id=session_id if use_history else None,
                use_context=use_rag
            )
            
            logger.info(f"Generated response for session: {session_id}")
            return response
            
        except Exception as e:
            logger.error(f"Chat service error: {str(e)}")
            raise
    
    async def stream_response(
        self,
        message: str,
        session_id: Optional[str] = None,
        use_rag: bool = True,
        use_history: bool = True
    ) -> AsyncIterator[str]:
        try:
            async for chunk in self.pipeline.stream(
                query=message,
                session_id=session_id if use_history else None,
                use_context=use_rag
            ):
                yield chunk
                
        except Exception as e:
            logger.error(f"Chat stream error: {str(e)}")
            raise
    
    def get_chat_history(self, session_id: str) -> list:
        try:
            return self.memory.get_messages(session_id)
        except Exception as e:
            logger.error(f"Get history error: {str(e)}")
            return []
    
    def clear_chat_history(self, session_id: str) -> bool:
        try:
            self.memory.clear(session_id)
            logger.info(f"Cleared history for session: {session_id}")
            return True
        except Exception as e:
            logger.error(f"Clear history error: {str(e)}")
            return False


chat_service = ChatService()