PersonApp.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from string import Template
  2. from embedchain.apps.App import App
  3. from embedchain.apps.OpenSourceApp import OpenSourceApp
  4. from embedchain.config import ChatConfig, QueryConfig
  5. from embedchain.config.apps.BaseAppConfig import BaseAppConfig
  6. from embedchain.config.QueryConfig import DEFAULT_PROMPT, DEFAULT_PROMPT_WITH_HISTORY
  7. class EmbedChainPersonApp:
  8. """
  9. Base class to create a person bot.
  10. This bot behaves and speaks like a person.
  11. :param person: name of the person, better if its a well known person.
  12. :param config: BaseAppConfig instance to load as configuration.
  13. """
  14. def __init__(self, person, config: BaseAppConfig = None):
  15. self.person = person
  16. self.person_prompt = f"You are {person}. Whatever you say, you will always say in {person} style." # noqa:E501
  17. if config is None:
  18. config = BaseAppConfig()
  19. super().__init__(config)
  20. class PersonApp(EmbedChainPersonApp, App):
  21. """
  22. The Person app.
  23. Extends functionality from EmbedChainPersonApp and App
  24. """
  25. def query(self, input_query, config: QueryConfig = None):
  26. self.template = Template(self.person_prompt + " " + DEFAULT_PROMPT)
  27. query_config = QueryConfig(
  28. template=self.template,
  29. )
  30. return super().query(input_query, query_config)
  31. def chat(self, input_query, config: ChatConfig = None):
  32. self.template = Template(self.person_prompt + " " + DEFAULT_PROMPT_WITH_HISTORY)
  33. chat_config = ChatConfig(
  34. template=self.template,
  35. )
  36. return super().chat(input_query, chat_config)
  37. class PersonOpenSourceApp(EmbedChainPersonApp, OpenSourceApp):
  38. """
  39. The Person app.
  40. Extends functionality from EmbedChainPersonApp and OpenSourceApp
  41. """
  42. def query(self, input_query, config: QueryConfig = None):
  43. query_config = QueryConfig(
  44. template=self.template,
  45. )
  46. return super().query(input_query, query_config)
  47. def chat(self, input_query, config: ChatConfig = None):
  48. chat_config = ChatConfig(
  49. template=self.template,
  50. )
  51. return super().chat(input_query, chat_config)