code: """Chat service module."""
  
  from llama_index.core.memory import ChatMemoryBuffer
  from llama_index.core.base.llms.types import ChatMessage, MessageRole
  
  from app.api.database.models.message import MessageCreateModel
  from app.api.services.message_service import MessageService
  from app.api.services.ingest_service import ingest_service
  import llama_index.core
  
  llama_index.core.set_global_handler("simple")
  
  
  class ChatService:
      """Chat Service class for chat operations."""
  
      def __init__(self):
          self.message_service = MessageService()
  
      @staticmethod
      def chat(query: str):
          """Chat with the document."""
  
          chat_engine = ingest_service.index.as_query_engine(
              similarity_top_k=5, streaming=True, verbose=False
          )
          streaming_response = chat_engine.query(query)
  
          return streaming_response.response_gen
  
      def conversation(self, query: str, session_id: str):
          """Get answer from the chat engine."""
  
          history = self.message_service.get_messages_by_session_id(session_id)
          chat_history = []
          if history.messages:
              for message in history.messages:
                  chat_history.append(
                      ChatMessage(
                          message=message.message,
                          role=(
                              MessageRole.USER
                              if message.sender == "user"
                              else MessageRole.ASSISTANT
                          ),
                      )
                  )
  
          memory = ChatMemoryBuffer.from_defaults(
              chat_history=chat_history, token_limit=8000
          )
          chat_engine = ingest_service.index.as_chat_engine(
              chat_mode="context",
              memory=memory,
              similarity_top_k=5,
              verbose=False,
              system_prompt=(
                  """\
              You are a chatbot. You MUST NOT provide any information unless it is in the Context or previous messages or general conversation. If the user ask something you don't know, say that you cannot answer. \
              you MUST keep the answers short and simple. \
              """
              ),
          )
  
          response = chat_engine.stream_chat(message=query)
  
          for token in response.response_gen:
              yield token
  
          self.message_service.create_message(
              message=MessageCreateModel(
                  session_id=session_id,
                  message=query,
                  sender="user",
              )
          )
          self.message_service.create_message(
              message=MessageCreateModel(
                  session_id=session_id,
                  message=str(response),
                  sender="assistant",
              )
          )
  
apis: llama_index.core.memory.ChatMemoryBuffer.from_defaults llama_index.core.base.llms.types.ChatMessage
extract_api: [((532, 548), 'app.api.services.message_service.MessageService', 'MessageService', ([], {}), '()\n', (546, 548), False, 'from app.api.services.message_service import MessageService\n'), ((655, 746), 'app.api.services.ingest_service.ingest_service.index.as_query_engine', 'ingest_service.index.as_query_engine', ([], {'similarity_top_k': '(5)', 'streaming': '(True)', 'verbose': '(False)'}), '(similarity_top_k=5, streaming=True,\n    verbose=False)\n', (691, 746), False, 'from app.api.services.ingest_service import ingest_service\n'), ((1542, 1617), 'llama_index.core.memory.ChatMemoryBuffer.from_defaults', 'ChatMemoryBuffer.from_defaults', ([], {'chat_history': 'chat_history', 'token_limit': '(8000)'}), '(chat_history=chat_history, token_limit=8000)\n', (1572, 1617), False, 'from llama_index.core.memory import ChatMemoryBuffer\n'), ((1662, 2080), 'app.api.services.ingest_service.ingest_service.index.as_chat_engine', 'ingest_service.index.as_chat_engine', ([], {'chat_mode': '"""context"""', 'memory': 'memory', 'similarity_top_k': '(5)', 'verbose': '(False)', 'system_prompt': '"""            You are a chatbot. You MUST NOT provide any information unless it is in the Context or previous messages or general conversation. If the user ask something you don\'t know, say that you cannot answer.             you MUST keep the answers short and simple.             """'}), '(chat_mode=\'context\', memory=memory,\n    similarity_top_k=5, verbose=False, system_prompt=\n    "            You are a chatbot. You MUST NOT provide any information unless it is in the Context or previous messages or general conversation. If the user ask something you don\'t know, say that you cannot answer.             you MUST keep the answers short and simple.             "\n    )\n', (1697, 2080), False, 'from app.api.services.ingest_service import ingest_service\n'), ((2374, 2445), 'app.api.database.models.message.MessageCreateModel', 'MessageCreateModel', ([], {'session_id': 'session_id', 'message': 'query', 'sender': '"""user"""'}), "(session_id=session_id, message=query, sender='user')\n", (2392, 2445), False, 'from app.api.database.models.message import MessageCreateModel\n'), ((1208, 1327), 'llama_index.core.base.llms.types.ChatMessage', 'ChatMessage', ([], {'message': 'message.message', 'role': "(MessageRole.USER if message.sender == 'user' else MessageRole.ASSISTANT)"}), "(message=message.message, role=MessageRole.USER if message.\n    sender == 'user' else MessageRole.ASSISTANT)\n", (1219, 1327), False, 'from llama_index.core.base.llms.types import ChatMessage, MessageRole\n')]