PersonApp.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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,
  7. DEFAULT_PROMPT_WITH_HISTORY)
  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: BaseAppConfig instance to load as configuration.
  14. """
  15. def __init__(self, person, config: BaseAppConfig = None):
  16. self.person = person
  17. self.person_prompt = f"You are {person}. Whatever you say, you will always say in {person} style." # noqa:E501
  18. super().__init__(config)
  19. def add_person_template_to_config(self, default_prompt: str, config: ChatConfig = None):
  20. """
  21. This method checks if the config object contains a prompt template
  22. if yes it adds the person prompt to it and return the updated config
  23. else it creates a config object with the default prompt added to the person prompt
  24. :param default_prompt: it is the default prompt for query or chat methods
  25. :param config: Optional. The `ChatConfig` instance to use as
  26. configuration options.
  27. """
  28. template = Template(self.person_prompt + " " + default_prompt)
  29. if config:
  30. if config.template:
  31. # Add person prompt to custom user template
  32. config.template = Template(self.person_prompt + " " + config.template.template)
  33. else:
  34. # If no user template is present, use person prompt with the default template
  35. config.template = template
  36. else:
  37. # if no config is present at all, initialize the config with person prompt and default template
  38. config = QueryConfig(
  39. template=template,
  40. )
  41. return config
  42. class PersonApp(EmbedChainPersonApp, App):
  43. """
  44. The Person app.
  45. Extends functionality from EmbedChainPersonApp and App
  46. """
  47. def query(self, input_query, config: QueryConfig = None, dry_run=False):
  48. config = self.add_person_template_to_config(DEFAULT_PROMPT, config)
  49. return super().query(input_query, config, dry_run)
  50. def chat(self, input_query, config: ChatConfig = None, dry_run=False):
  51. config = self.add_person_template_to_config(DEFAULT_PROMPT_WITH_HISTORY, config)
  52. return super().chat(input_query, config, dry_run)
  53. class PersonOpenSourceApp(EmbedChainPersonApp, OpenSourceApp):
  54. """
  55. The Person app.
  56. Extends functionality from EmbedChainPersonApp and OpenSourceApp
  57. """
  58. def query(self, input_query, config: QueryConfig = None, dry_run=False):
  59. config = self.add_person_template_to_config(DEFAULT_PROMPT, config)
  60. return super().query(input_query, config, dry_run)
  61. def chat(self, input_query, config: ChatConfig = None, dry_run=False):
  62. config = self.add_person_template_to_config(DEFAULT_PROMPT_WITH_HISTORY, config)
  63. return super().chat(input_query, config, dry_run)