code: from typing import Union
  from llama_index.core import Prompt
  from llama_index.core.response_synthesizers import get_response_synthesizer, ResponseMode
  from llama_index.core.postprocessor import SimilarityPostprocessor
  from llama_index.core.llms import ChatMessage, MessageRole
  from llama_index.agent.openai import OpenAIAgent
  from llama_index.llms.openai import OpenAI
  from app.data.messages.qa import DocumentRequest
  from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer
  from app.data.models.mongodb import (
      LlamaIndexDocumentMeta,
      LlamaIndexDocumentMetaReadable,
      Message,
  )
  from app.utils.log_util import logger
  from app.utils import data_util
  from app.llama_index_server.chat_message_dao import ChatMessageDao
  from app.llama_index_server.index_storage import index_storage
  from app.llama_index_server.my_query_engine_tool import MyQueryEngineTool, MATCHED_MARK
  
  SIMILARITY_CUTOFF = 0.85
  PROMPT_TEMPLATE_FOR_QUERY_ENGINE = (
      "We have provided context information below. \n"
      "---------------------\n"
      "{context_str}"
      "\n---------------------\n"
      "Given this information, assume you are an experienced golf coach, if the question has anything to do with golf, "
      "please give short, simple, accurate, precise answer to the question, "
      "limited to 80 words maximum. If the question has nothing to do with golf at all, please answer "
      f"'{get_default_answer_id()}'.\n"
      "The question is: {query_str}\n"
  )
  SYSTEM_PROMPT_TEMPLATE_FOR_CHAT_ENGINE = (
      "Your are an expert Q&A system that can find relevant information using the tools at your disposal.\n"
      "The tools can access a set of typical questions a golf beginner might ask.\n"
      "If the user's query matches one of those typical questions, stop and return the matched question immediately.\n"
      "If the user's query doesn't match any of those typical questions, "
      "then you should act as an experienced golf coach, and firstly evaluate whether the question is relevant to golf.\n"
      f"if it is not golf relevant at all, please answer '{get_default_answer_id()},"
      "otherwise, please give short, simple, accurate, precise answer to the question, limited to 80 words maximum.\n"
      "You may need to combine the chat history to fully understand the query of the user.\n"
      "Remember you are only allowed to answer questions related to golf.\n"
  )
  chat_message_dao = ChatMessageDao()
  
  
  def get_local_query_engine():
      """
      strictly limited to local knowledge base. our local knowledge base is a list of standard questions which are indexed in vector store,
      while the standard answers are stored in mongodb through DocumentMetaDao.
      there is a one-to-one mapping between each standard question and a standard answer.
      we may update or optimize the standard answers in mongodb frequently, but usually we don't update the standard questions.
      if a query matches one of the standard questions, we can find the respective standard answer from mongodb.
      """
      index = index_storage.index()
      return index.as_query_engine(
          response_synthesizer=get_response_synthesizer(
              response_mode=ResponseMode.NO_TEXT
          ),
          node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=SIMILARITY_CUTOFF)],
      )
  
  
  def get_matched_question_from_local_query_engine(query_text):
      local_query_engine = get_local_query_engine()
      local_query_response = local_query_engine.query(query_text)
      if len(local_query_response.source_nodes) > 0:
          matched_node = local_query_response.source_nodes[0]
          matched_question = matched_node.text
          logger.debug(f"Found matched question from index: {matched_question}")
          return matched_question
      else:
          return None
  
  
  def get_doc_meta(text):
      matched_doc_id = data_util.get_doc_id(text)
      mongo = index_storage.mongo()
      doc_meta = mongo.find_one({"doc_id": matched_doc_id})
      doc_meta = LlamaIndexDocumentMeta(**doc_meta) if doc_meta else None
      return matched_doc_id, doc_meta
  
  
  def get_llm_query_engine():
      index = index_storage.index()
      qa_template = Prompt(PROMPT_TEMPLATE_FOR_QUERY_ENGINE)
      return index.as_query_engine(text_qa_template=qa_template)
  
  
  def query_index(query_text, only_for_meta=False) -> Union[Answer, LlamaIndexDocumentMeta, None]:
      data_util.assert_not_none(query_text, "query cannot be none")
      logger.info(f"Query test: {query_text}")
      # first search locally
      matched_question = get_matched_question_from_local_query_engine(query_text)
      if matched_question:
          matched_doc_id, doc_meta = get_doc_meta(matched_question)
          if doc_meta:
              logger.debug(f"An matched doc meta found from mongodb: {doc_meta}")
              doc_meta.query_timestamps.append(data_util.get_current_milliseconds())
              index_storage.mongo().upsert_one({"doc_id": matched_doc_id}, doc_meta)
              if only_for_meta:
                  return doc_meta
              else:
                  return Answer(
                      category=doc_meta.category,
                      question=query_text,
                      matched_question=matched_question,
                      source=Source.KNOWLEDGE_BASE if doc_meta.source == Source.KNOWLEDGE_BASE else Source.USER_ASKED,
                      answer=doc_meta.answer,
                  )
          else:
              # means the document meta has been removed from mongodb. for example by pruning
              logger.warning(f"'{matched_doc_id}' is not found in mongodb")
              if only_for_meta:
                  return None
      # if not found, turn to LLM
      llm_query_engine = get_llm_query_engine()
      response = llm_query_engine.query(query_text)
      # save the question-answer pair to index
      answer = Answer(
          category=None,
          question=query_text,
          source=index_storage.current_model,
          answer=str(response),
      )
      index_storage.add_doc(answer)
      return answer
  
  
  def delete_doc(doc_id):
      data_util.assert_not_none(doc_id, "doc_id cannot be none")
      logger.info(f"Delete document with doc id: {doc_id}")
      return index_storage.delete_doc(doc_id)
  
  
  def get_document(req: DocumentRequest):
      doc_meta = index_storage.mongo().find_one({"doc_id": req.doc_id})
      if doc_meta:
          return LlamaIndexDocumentMetaReadable(**doc_meta)
      elif req.fuzzy:
          doc_meta = query_index(req.doc_id, only_for_meta=True)
          if doc_meta:
              doc_meta.matched_question = doc_meta.question
              doc_meta.question = doc_meta.doc_id = req.doc_id
              return LlamaIndexDocumentMetaReadable(**doc_meta.model_dump())
          return None
  
  
  def cleanup_for_test():
      return index_storage.mongo().cleanup_for_test()
  
  
  def get_chat_engine(conversation_id: str, streaming: bool = False):
      local_query_engine = get_local_query_engine()
      query_engine_tools = [
          MyQueryEngineTool.from_defaults(
              query_engine=local_query_engine,
              name="local_query_engine",
              description="Queries from a knowledge base consists of typical questions that a golf beginner might ask",
          )
      ]
      chat_llm = OpenAI(
          temperature=0,
          model=index_storage.current_model,
          streaming=streaming,
          max_tokens=100,
      )
      chat_history = chat_message_dao.get_chat_history(conversation_id)
      chat_history = [ChatMessage(role=c.role, content=c.content) for c in chat_history]
      return OpenAIAgent.from_tools(
          tools=query_engine_tools,
          llm=chat_llm,
          chat_history=chat_history,
          verbose=True,
          system_prompt=SYSTEM_PROMPT_TEMPLATE_FOR_CHAT_ENGINE,
      )
  
  
  def get_response_text_from_chat(agent_chat_response):
      sources = agent_chat_response.sources
      if len(sources) > 0:
          source_content = sources[0].content
          if MATCHED_MARK in source_content:
              return source_content.replace(MATCHED_MARK, "").strip()
      return agent_chat_response.response
  
  
  def chat(query_text: str, conversation_id: str) -> Message:
      # we will not index chat messages in vector store, but will save them in mongodb
      data_util.assert_not_none(query_text, "query content cannot be none")
      user_message = ChatMessage(role=MessageRole.USER, content=query_text)
      # save immediately, since the following steps may take a while and throw exceptions
      chat_message_dao.save_chat_history(conversation_id, user_message)
      chat_engine = get_chat_engine(conversation_id)
      agent_chat_response = chat_engine.chat(query_text)
      response_text = get_response_text_from_chat(agent_chat_response)
      # todo: change the if condition to: response_text == get_default_answer_id()
      response_text = get_default_answer() if get_default_answer_id() in response_text else response_text
      matched_doc_id, doc_meta = get_doc_meta(response_text)
      if doc_meta:
          logger.debug(f"An matched doc meta found from mongodb: {doc_meta}")
          doc_meta.query_timestamps.append(data_util.get_current_milliseconds())
          index_storage.mongo().upsert_one({"doc_id": matched_doc_id}, doc_meta)
          bot_message = ChatMessage(role=MessageRole.ASSISTANT, content=doc_meta.answer)
      else:
          # means the chat engine cannot find a matched doc meta from mongodb
          logger.warning(f"'{matched_doc_id}' is not found in mongodb")
          bot_message = ChatMessage(role=MessageRole.ASSISTANT, content=response_text)
      chat_message_dao.save_chat_history(conversation_id, bot_message)
      return Message.from_chat_message(conversation_id, bot_message)
  
  
  async def stream_chat(content: str, conversation_id: str):
      # todo: need to use chat engine based on index. otherwise, the local database is not utilized
      # We only support using OpenAI's API
      client = OpenAI()
      user_message = ChatMessage(role=MessageRole.USER, content=content)
      chat_message_dao.save_chat_history(conversation_id, user_message)
      history = chat_message_dao.get_chat_history(conversation_id)
      messages = [dict(content=c.content, role=c.role) for c in history]
      messages = [
                     dict(
                         role=MessageRole.SYSTEM,
                         content=(
                             "assume you are an experienced golf coach, if the question has anything to do with golf, "
                             "please give short, simple, accurate, precise answer to the question, "
                             "limited to 80 words maximum. If the question has nothing to do with golf at all, please answer "
                             f"'{get_default_answer()}'."
                         )
                     ),
                 ] + messages
      completion = client.chat.completions.create(
          model=index_storage.current_model,
          messages=messages,
          temperature=0,
          stream=True  # again, we set stream=True
      )
      chunks = []
      for chunk in completion:
          finish_reason = chunk.choices[0].finish_reason
          content = chunk.choices[0].delta.content
          if finish_reason == "stop" or finish_reason == "length":
              # reached the end
              if content is not None:
                  bot_message = ChatMessage(role=MessageRole.ASSISTANT, content=content)
                  chat_message_dao.save_chat_history(conversation_id, bot_message)
              break
          if content is None:
              break
          chunks.append(content)
          logger.debug("Chunk message: %s", content)
          yield content
  
apis: llama_index.agent.openai.OpenAIAgent.from_tools llama_index.core.Prompt llama_index.core.response_synthesizers.get_response_synthesizer llama_index.llms.openai.OpenAI llama_index.core.llms.ChatMessage llama_index.core.postprocessor.SimilarityPostprocessor
extract_api: [((2418, 2434), 'app.llama_index_server.chat_message_dao.ChatMessageDao', 'ChatMessageDao', ([], {}), '()\n', (2432, 2434), False, 'from app.llama_index_server.chat_message_dao import ChatMessageDao\n'), ((3036, 3057), 'app.llama_index_server.index_storage.index_storage.index', 'index_storage.index', ([], {}), '()\n', (3055, 3057), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((3825, 3851), 'app.utils.data_util.get_doc_id', 'data_util.get_doc_id', (['text'], {}), '(text)\n', (3845, 3851), False, 'from app.utils import data_util\n'), ((3864, 3885), 'app.llama_index_server.index_storage.index_storage.mongo', 'index_storage.mongo', ([], {}), '()\n', (3883, 3885), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((4094, 4115), 'app.llama_index_server.index_storage.index_storage.index', 'index_storage.index', ([], {}), '()\n', (4113, 4115), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((4134, 4174), 'llama_index.core.Prompt', 'Prompt', (['PROMPT_TEMPLATE_FOR_QUERY_ENGINE'], {}), '(PROMPT_TEMPLATE_FOR_QUERY_ENGINE)\n', (4140, 4174), False, 'from llama_index.core import Prompt\n'), ((4341, 4402), 'app.utils.data_util.assert_not_none', 'data_util.assert_not_none', (['query_text', '"""query cannot be none"""'], {}), "(query_text, 'query cannot be none')\n", (4366, 4402), False, 'from app.utils import data_util\n'), ((4407, 4447), 'app.utils.log_util.logger.info', 'logger.info', (['f"""Query test: {query_text}"""'], {}), "(f'Query test: {query_text}')\n", (4418, 4447), False, 'from app.utils.log_util import logger\n'), ((5915, 5944), 'app.llama_index_server.index_storage.index_storage.add_doc', 'index_storage.add_doc', (['answer'], {}), '(answer)\n', (5936, 5944), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((5993, 6051), 'app.utils.data_util.assert_not_none', 'data_util.assert_not_none', (['doc_id', '"""doc_id cannot be none"""'], {}), "(doc_id, 'doc_id cannot be none')\n", (6018, 6051), False, 'from app.utils import data_util\n'), ((6056, 6109), 'app.utils.log_util.logger.info', 'logger.info', (['f"""Delete document with doc id: {doc_id}"""'], {}), "(f'Delete document with doc id: {doc_id}')\n", (6067, 6109), False, 'from app.utils.log_util import logger\n'), ((6121, 6153), 'app.llama_index_server.index_storage.index_storage.delete_doc', 'index_storage.delete_doc', (['doc_id'], {}), '(doc_id)\n', (6145, 6153), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((7158, 7256), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'temperature': '(0)', 'model': 'index_storage.current_model', 'streaming': 'streaming', 'max_tokens': '(100)'}), '(temperature=0, model=index_storage.current_model, streaming=\n    streaming, max_tokens=100)\n', (7164, 7256), False, 'from llama_index.llms.openai import OpenAI\n'), ((7459, 7626), 'llama_index.agent.openai.OpenAIAgent.from_tools', 'OpenAIAgent.from_tools', ([], {'tools': 'query_engine_tools', 'llm': 'chat_llm', 'chat_history': 'chat_history', 'verbose': '(True)', 'system_prompt': 'SYSTEM_PROMPT_TEMPLATE_FOR_CHAT_ENGINE'}), '(tools=query_engine_tools, llm=chat_llm, chat_history\n    =chat_history, verbose=True, system_prompt=\n    SYSTEM_PROMPT_TEMPLATE_FOR_CHAT_ENGINE)\n', (7481, 7626), False, 'from llama_index.agent.openai import OpenAIAgent\n'), ((8133, 8202), 'app.utils.data_util.assert_not_none', 'data_util.assert_not_none', (['query_text', '"""query content cannot be none"""'], {}), "(query_text, 'query content cannot be none')\n", (8158, 8202), False, 'from app.utils import data_util\n'), ((8222, 8276), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.USER', 'content': 'query_text'}), '(role=MessageRole.USER, content=query_text)\n', (8233, 8276), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((9513, 9568), 'app.data.models.mongodb.Message.from_chat_message', 'Message.from_chat_message', (['conversation_id', 'bot_message'], {}), '(conversation_id, bot_message)\n', (9538, 9568), False, 'from app.data.models.mongodb import LlamaIndexDocumentMeta, LlamaIndexDocumentMetaReadable, Message\n'), ((9782, 9790), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {}), '()\n', (9788, 9790), False, 'from llama_index.llms.openai import OpenAI\n'), ((9810, 9861), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.USER', 'content': 'content'}), '(role=MessageRole.USER, content=content)\n', (9821, 9861), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((1415, 1438), 'app.data.models.qa.get_default_answer_id', 'get_default_answer_id', ([], {}), '()\n', (1436, 1438), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n'), ((2086, 2109), 'app.data.models.qa.get_default_answer_id', 'get_default_answer_id', ([], {}), '()\n', (2107, 2109), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n'), ((3645, 3715), 'app.utils.log_util.logger.debug', 'logger.debug', (['f"""Found matched question from index: {matched_question}"""'], {}), "(f'Found matched question from index: {matched_question}')\n", (3657, 3715), False, 'from app.utils.log_util import logger\n'), ((3959, 3993), 'app.data.models.mongodb.LlamaIndexDocumentMeta', 'LlamaIndexDocumentMeta', ([], {}), '(**doc_meta)\n', (3981, 3993), False, 'from app.data.models.mongodb import LlamaIndexDocumentMeta, LlamaIndexDocumentMetaReadable, Message\n'), ((6298, 6340), 'app.data.models.mongodb.LlamaIndexDocumentMetaReadable', 'LlamaIndexDocumentMetaReadable', ([], {}), '(**doc_meta)\n', (6328, 6340), False, 'from app.data.models.mongodb import LlamaIndexDocumentMeta, LlamaIndexDocumentMetaReadable, Message\n'), ((6892, 7104), 'app.llama_index_server.my_query_engine_tool.MyQueryEngineTool.from_defaults', 'MyQueryEngineTool.from_defaults', ([], {'query_engine': 'local_query_engine', 'name': '"""local_query_engine"""', 'description': '"""Queries from a knowledge base consists of typical questions that a golf beginner might ask"""'}), "(query_engine=local_query_engine, name=\n    'local_query_engine', description=\n    'Queries from a knowledge base consists of typical questions that a golf beginner might ask'\n    )\n", (6923, 7104), False, 'from app.llama_index_server.my_query_engine_tool import MyQueryEngineTool, MATCHED_MARK\n'), ((7381, 7424), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'c.role', 'content': 'c.content'}), '(role=c.role, content=c.content)\n', (7392, 7424), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((8711, 8731), 'app.data.models.qa.get_default_answer', 'get_default_answer', ([], {}), '()\n', (8729, 8731), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n'), ((8879, 8946), 'app.utils.log_util.logger.debug', 'logger.debug', (['f"""An matched doc meta found from mongodb: {doc_meta}"""'], {}), "(f'An matched doc meta found from mongodb: {doc_meta}')\n", (8891, 8946), False, 'from app.utils.log_util import logger\n'), ((9127, 9191), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.ASSISTANT', 'content': 'doc_meta.answer'}), '(role=MessageRole.ASSISTANT, content=doc_meta.answer)\n', (9138, 9191), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((9286, 9347), 'app.utils.log_util.logger.warning', 'logger.warning', (['f"""\'{matched_doc_id}\' is not found in mongodb"""'], {}), '(f"\'{matched_doc_id}\' is not found in mongodb")\n', (9300, 9347), False, 'from app.utils.log_util import logger\n'), ((9370, 9432), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.ASSISTANT', 'content': 'response_text'}), '(role=MessageRole.ASSISTANT, content=response_text)\n', (9381, 9432), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((11412, 11454), 'app.utils.log_util.logger.debug', 'logger.debug', (['"""Chunk message: %s"""', 'content'], {}), "('Chunk message: %s', content)\n", (11424, 11454), False, 'from app.utils.log_util import logger\n'), ((3121, 3181), 'llama_index.core.response_synthesizers.get_response_synthesizer', 'get_response_synthesizer', ([], {'response_mode': 'ResponseMode.NO_TEXT'}), '(response_mode=ResponseMode.NO_TEXT)\n', (3145, 3181), False, 'from llama_index.core.response_synthesizers import get_response_synthesizer, ResponseMode\n'), ((4679, 4746), 'app.utils.log_util.logger.debug', 'logger.debug', (['f"""An matched doc meta found from mongodb: {doc_meta}"""'], {}), "(f'An matched doc meta found from mongodb: {doc_meta}')\n", (4691, 4746), False, 'from app.utils.log_util import logger\n'), ((5465, 5526), 'app.utils.log_util.logger.warning', 'logger.warning', (['f"""\'{matched_doc_id}\' is not found in mongodb"""'], {}), '(f"\'{matched_doc_id}\' is not found in mongodb")\n', (5479, 5526), False, 'from app.utils.log_util import logger\n'), ((6211, 6232), 'app.llama_index_server.index_storage.index_storage.mongo', 'index_storage.mongo', ([], {}), '()\n', (6230, 6232), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((6696, 6717), 'app.llama_index_server.index_storage.index_storage.mongo', 'index_storage.mongo', ([], {}), '()\n', (6715, 6717), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((8735, 8758), 'app.data.models.qa.get_default_answer_id', 'get_default_answer_id', ([], {}), '()\n', (8756, 8758), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n'), ((8988, 9024), 'app.utils.data_util.get_current_milliseconds', 'data_util.get_current_milliseconds', ([], {}), '()\n', (9022, 9024), False, 'from app.utils import data_util\n'), ((3234, 3294), 'llama_index.core.postprocessor.SimilarityPostprocessor', 'SimilarityPostprocessor', ([], {'similarity_cutoff': 'SIMILARITY_CUTOFF'}), '(similarity_cutoff=SIMILARITY_CUTOFF)\n', (3257, 3294), False, 'from llama_index.core.postprocessor import SimilarityPostprocessor\n'), ((4792, 4828), 'app.utils.data_util.get_current_milliseconds', 'data_util.get_current_milliseconds', ([], {}), '()\n', (4826, 4828), False, 'from app.utils import data_util\n'), ((5016, 5236), 'app.data.models.qa.Answer', 'Answer', ([], {'category': 'doc_meta.category', 'question': 'query_text', 'matched_question': 'matched_question', 'source': '(Source.KNOWLEDGE_BASE if doc_meta.source == Source.KNOWLEDGE_BASE else\n    Source.USER_ASKED)', 'answer': 'doc_meta.answer'}), '(category=doc_meta.category, question=query_text, matched_question=\n    matched_question, source=Source.KNOWLEDGE_BASE if doc_meta.source ==\n    Source.KNOWLEDGE_BASE else Source.USER_ASKED, answer=doc_meta.answer)\n', (5022, 5236), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n'), ((9034, 9055), 'app.llama_index_server.index_storage.index_storage.mongo', 'index_storage.mongo', ([], {}), '()\n', (9053, 9055), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((11171, 11227), 'llama_index.core.llms.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.ASSISTANT', 'content': 'content'}), '(role=MessageRole.ASSISTANT, content=content)\n', (11182, 11227), False, 'from llama_index.core.llms import ChatMessage, MessageRole\n'), ((4842, 4863), 'app.llama_index_server.index_storage.index_storage.mongo', 'index_storage.mongo', ([], {}), '()\n', (4861, 4863), False, 'from app.llama_index_server.index_storage import index_storage\n'), ((10564, 10584), 'app.data.models.qa.get_default_answer', 'get_default_answer', ([], {}), '()\n', (10582, 10584), False, 'from app.data.models.qa import Source, Answer, get_default_answer_id, get_default_answer\n')]