Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

rewrite.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. from abc import ABC
  17. from api.db import LLMType
  18. from api.db.services.llm_service import LLMBundle
  19. from agent.component import GenerateParam, Generate
  20. class RewriteQuestionParam(GenerateParam):
  21. """
  22. Define the QuestionRewrite component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.temperature = 0.9
  27. self.prompt = ""
  28. self.loop = 1
  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. if not hasattr(self, "_loop"):
  79. setattr(self, "_loop", 0)
  80. if self._loop >= self._param.loop:
  81. self._loop = 0
  82. raise Exception("Sorry! Nothing relevant found.")
  83. self._loop += 1
  84. conv = self._canvas.get_history(4)
  85. conv = "\n".join(conv)
  86. chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
  87. ans = chat_mdl.chat(self._param.get_prompt(conv), [{"role": "user", "content": "Output: "}],
  88. self._param.gen_conf())
  89. self._canvas.history.pop()
  90. self._canvas.history.append(("user", ans))
  91. print(ans, ":::::::::::::::::::::::::::::::::")
  92. return RewriteQuestion.be_output(ans)