PersonApp.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. class PersonApp(EmbedChainPersonApp, App):
  20. """
  21. The Person app.
  22. Extends functionality from EmbedChainPersonApp and App
  23. """
  24. def query(self, input_query, config: QueryConfig = None):
  25. self.template = Template(self.person_prompt + " " + DEFAULT_PROMPT)
  26. query_config = QueryConfig(
  27. template=self.template,
  28. )
  29. return super().query(input_query, query_config)
  30. def chat(self, input_query, config: ChatConfig = None):
  31. self.template = Template(self.person_prompt + " " + DEFAULT_PROMPT_WITH_HISTORY)
  32. chat_config = ChatConfig(
  33. template=self.template,
  34. )
  35. return super().chat(input_query, chat_config)
  36. class PersonOpenSourceApp(EmbedChainPersonApp, OpenSourceApp):
  37. """
  38. The Person app.
  39. Extends functionality from EmbedChainPersonApp and OpenSourceApp
  40. """
  41. def query(self, input_query, config: QueryConfig = None):
  42. query_config = QueryConfig(
  43. template=self.template,
  44. )
  45. return super().query(input_query, query_config)
  46. def chat(self, input_query, config: ChatConfig = None):
  47. chat_config = ChatConfig(
  48. template=self.template,
  49. )
  50. return super().chat(input_query, chat_config)