Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. import re
  18. from abc import ABC
  19. from api.db import LLMType
  20. from api.db.services.llm_service import LLMBundle
  21. from agent.component import GenerateParam, Generate
  22. class KeywordExtractParam(GenerateParam):
  23. """
  24. Define the KeywordExtract component parameters.
  25. """
  26. def __init__(self):
  27. super().__init__()
  28. self.top_n = 1
  29. def check(self):
  30. super().check()
  31. self.check_positive_integer(self.top_n, "Top N")
  32. def get_prompt(self):
  33. self.prompt = """
  34. - Role: You're a question analyzer.
  35. - Requirements:
  36. - Summarize user's question, and give top %s important keyword/phrase.
  37. - Use comma as a delimiter to separate keywords/phrases.
  38. - Answer format: (in language of user's question)
  39. - keyword:
  40. """ % self.top_n
  41. return self.prompt
  42. class KeywordExtract(Generate, ABC):
  43. component_name = "KeywordExtract"
  44. def _run(self, history, **kwargs):
  45. query = self.get_input()
  46. query = str(query["content"][0]) if "content" in query else ""
  47. chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
  48. ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": query}],
  49. self._param.gen_conf())
  50. ans = re.sub(r".*keyword:", "", ans).strip()
  51. logging.debug(f"ans: {ans}")
  52. return KeywordExtract.be_output(ans)
  53. def debug(self, **kwargs):
  54. return self._run([], **kwargs)