Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

rewrite.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import logging
  17. from abc import ABC
  18. from api.db import LLMType
  19. from api.db.services.llm_service import LLMBundle
  20. from agent.component import GenerateParam, Generate
  21. class RewriteQuestionParam(GenerateParam):
  22. """
  23. Define the QuestionRewrite component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.temperature = 0.9
  28. self.prompt = ""
  29. def check(self):
  30. super().check()
  31. def get_prompt(self, conv):
  32. self.prompt = """
  33. You are an expert at query expansion to generate a paraphrasing of a question.
  34. I can't retrieval relevant information from the knowledge base by using user's question directly.
  35. You need to expand or paraphrase user's question by multiple ways such as using synonyms words/phrase,
  36. writing the abbreviation in its entirety, adding some extra descriptions or explanations,
  37. changing the way of expression, translating the original question into another language (English/Chinese), etc.
  38. And return 5 versions of question and one is from translation.
  39. Just list the question. No other words are needed.
  40. """
  41. return f"""
  42. Role: A helpful assistant
  43. Task: Generate a full user question that would follow the conversation.
  44. Requirements & Restrictions:
  45. - Text generated MUST be in the same language of the original user's question.
  46. - If the user's latest question is completely, don't do anything, just return the original question.
  47. - DON'T generate anything except a refined question.
  48. ######################
  49. -Examples-
  50. ######################
  51. # Example 1
  52. ## Conversation
  53. USER: What is the name of Donald Trump's father?
  54. ASSISTANT: Fred Trump.
  55. USER: And his mother?
  56. ###############
  57. Output: What's the name of Donald Trump's mother?
  58. ------------
  59. # Example 2
  60. ## Conversation
  61. USER: What is the name of Donald Trump's father?
  62. ASSISTANT: Fred Trump.
  63. USER: And his mother?
  64. ASSISTANT: Mary Trump.
  65. User: What's her full name?
  66. ###############
  67. Output: What's the full name of Donald Trump's mother Mary Trump?
  68. ######################
  69. # Real Data
  70. ## Conversation
  71. {conv}
  72. ###############
  73. """
  74. return self.prompt
  75. class RewriteQuestion(Generate, ABC):
  76. component_name = "RewriteQuestion"
  77. def _run(self, history, **kwargs):
  78. hist = self._canvas.get_history(self._param.message_history_window_size)
  79. conv = []
  80. for m in hist:
  81. if m["role"] not in ["user", "assistant"]:
  82. continue
  83. conv.append("{}: {}".format(m["role"].upper(), m["content"]))
  84. conv = "\n".join(conv)
  85. chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
  86. ans = chat_mdl.chat(self._param.get_prompt(conv), [{"role": "user", "content": "Output: "}],
  87. self._param.gen_conf())
  88. self._canvas.history.pop()
  89. self._canvas.history.append(("user", ans))
  90. logging.debug(ans)
  91. return RewriteQuestion.be_output(ans)