Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

keyword.py 2.1KB

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 re
  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 agent.settings import DEBUG
  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. q = ""
  46. for r, c in self._canvas.history[::-1]:
  47. if r == "user":
  48. q += c
  49. break
  50. chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
  51. ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": q}],
  52. self._param.gen_conf())
  53. ans = re.sub(r".*keyword:", "", ans).strip()
  54. if DEBUG: print(ans, ":::::::::::::::::::::::::::::::::")
  55. return KeywordExtract.be_output(ans)