You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. from rag.utils import num_tokens_from_string, encoder
  22. class RelevantParam(GenerateParam):
  23. """
  24. Define the Relevant component parameters.
  25. """
  26. def __init__(self):
  27. super().__init__()
  28. self.prompt = ""
  29. self.yes = ""
  30. self.no = ""
  31. def check(self):
  32. super().check()
  33. self.check_empty(self.yes, "[Relevant] 'Yes'")
  34. self.check_empty(self.no, "[Relevant] 'No'")
  35. def get_prompt(self):
  36. self.prompt = """
  37. You are a grader assessing relevance of a retrieved document to a user question.
  38. It does not need to be a stringent test. The goal is to filter out erroneous retrievals.
  39. If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant.
  40. Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.
  41. No other words needed except 'yes' or 'no'.
  42. """
  43. return self.prompt
  44. class Relevant(Generate, ABC):
  45. component_name = "Relevant"
  46. def _run(self, history, **kwargs):
  47. q = ""
  48. for r, c in self._canvas.history[::-1]:
  49. if r == "user":
  50. q = c
  51. break
  52. ans = self.get_input()
  53. ans = " - ".join(ans["content"]) if "content" in ans else ""
  54. if not ans:
  55. return Relevant.be_output(self._param.no)
  56. ans = "Documents: \n" + ans
  57. ans = f"Question: {q}\n" + ans
  58. chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
  59. if num_tokens_from_string(ans) >= chat_mdl.max_length - 4:
  60. ans = encoder.decode(encoder.encode(ans)[:chat_mdl.max_length - 4])
  61. ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": ans}],
  62. self._param.gen_conf())
  63. logging.debug(ans)
  64. if ans.lower().find("yes") >= 0:
  65. return Relevant.be_output(self._param.yes)
  66. if ans.lower().find("no") >= 0:
  67. return Relevant.be_output(self._param.no)
  68. assert False, f"Relevant component got: {ans}"
  69. def debug(self, **kwargs):
  70. return self._run([], **kwargs)