person_app.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from string import Template
  2. from embedchain.apps.app import App
  3. from embedchain.apps.open_source_app import OpenSourceApp
  4. from embedchain.config import BaseLlmConfig, AppConfig
  5. from embedchain.config.llm.base import DEFAULT_PROMPT, DEFAULT_PROMPT_WITH_HISTORY
  6. from embedchain.helper.json_serializable import register_deserializable
  7. @register_deserializable
  8. class EmbedChainPersonApp:
  9. """
  10. Base class to create a person bot.
  11. This bot behaves and speaks like a person.
  12. :param person: name of the person, better if its a well known person.
  13. :param config: AppConfig instance to load as configuration.
  14. """
  15. def __init__(self, person: str, config: AppConfig = None):
  16. """Initialize a new person app
  17. :param person: Name of the person that's imitated.
  18. :type person: str
  19. :param config: Configuration class instance, defaults to None
  20. :type config: AppConfig, optional
  21. """
  22. self.person = person
  23. self.person_prompt = f"You are {person}. Whatever you say, you will always say in {person} style." # noqa:E501
  24. super().__init__(config)
  25. def add_person_template_to_config(self, default_prompt: str, config: BaseLlmConfig = None):
  26. """
  27. This method checks if the config object contains a prompt template
  28. if yes it adds the person prompt to it and return the updated config
  29. else it creates a config object with the default prompt added to the person prompt
  30. :param default_prompt: it is the default prompt for query or chat methods
  31. :type default_prompt: str
  32. :param config: _description_, defaults to None
  33. :type config: BaseLlmConfig, optional
  34. :return: The `ChatConfig` instance to use as configuration options.
  35. :rtype: _type_
  36. """
  37. template = Template(self.person_prompt + " " + default_prompt)
  38. if config:
  39. if config.template:
  40. # Add person prompt to custom user template
  41. config.template = Template(self.person_prompt + " " + config.template.template)
  42. else:
  43. # If no user template is present, use person prompt with the default template
  44. config.template = template
  45. else:
  46. # if no config is present at all, initialize the config with person prompt and default template
  47. config = BaseLlmConfig(
  48. template=template,
  49. )
  50. return config
  51. @register_deserializable
  52. class PersonApp(EmbedChainPersonApp, App):
  53. """
  54. The Person app.
  55. Extends functionality from EmbedChainPersonApp and App
  56. """
  57. def query(self, input_query, config: BaseLlmConfig = None, dry_run=False):
  58. config = self.add_person_template_to_config(DEFAULT_PROMPT, config)
  59. return super().query(input_query, config, dry_run, where=None)
  60. def chat(self, input_query, config: BaseLlmConfig = None, dry_run=False, where=None):
  61. config = self.add_person_template_to_config(DEFAULT_PROMPT_WITH_HISTORY, config)
  62. return super().chat(input_query, config, dry_run, where)
  63. @register_deserializable
  64. class PersonOpenSourceApp(EmbedChainPersonApp, OpenSourceApp):
  65. """
  66. The Person app.
  67. Extends functionality from EmbedChainPersonApp and OpenSourceApp
  68. """
  69. def query(self, input_query, config: BaseLlmConfig = None, dry_run=False):
  70. config = self.add_person_template_to_config(DEFAULT_PROMPT, config)
  71. return super().query(input_query, config, dry_run)
  72. def chat(self, input_query, config: BaseLlmConfig = None, dry_run=False):
  73. config = self.add_person_template_to_config(DEFAULT_PROMPT_WITH_HISTORY, config)
  74. return super().chat(input_query, config, dry_run)