person_app.py 3.7 KB

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