feat: Add person bot (#182)
This commit introduces a new bot type called Person. It behaves and speaks like a particular Person. For this two app types: PersonApp and PersonOpenSourceApp
This commit is contained in:
@@ -1 +1 @@
|
|||||||
from .embedchain import App, OpenSourceApp
|
from .embedchain import App, OpenSourceApp, PersonApp, PersonOpenSourceApp
|
||||||
@@ -2,8 +2,7 @@ from embedchain.config.BaseConfig import BaseConfig
|
|||||||
from string import Template
|
from string import Template
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
DEFAULT_PROMPT = """
|
||||||
DEFAULT_PROMPT_TEMPLATE = Template("""
|
|
||||||
Use the following pieces of context to answer the query at the end.
|
Use the following pieces of context to answer the query at the end.
|
||||||
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
||||||
|
|
||||||
@@ -12,7 +11,9 @@ DEFAULT_PROMPT_TEMPLATE = Template("""
|
|||||||
Query: $query
|
Query: $query
|
||||||
|
|
||||||
Helpful Answer:
|
Helpful Answer:
|
||||||
""")
|
"""
|
||||||
|
|
||||||
|
DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
|
||||||
query_re = re.compile(r"\$\{*query\}*")
|
query_re = re.compile(r"\$\{*query\}*")
|
||||||
context_re = re.compile(r"\$\{*context\}*")
|
context_re = re.compile(r"\$\{*context\}*")
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from langchain.docstore.document import Document
|
|||||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||||
from langchain.memory import ConversationBufferMemory
|
from langchain.memory import ConversationBufferMemory
|
||||||
from embedchain.config import InitConfig, AddConfig, QueryConfig, ChatConfig
|
from embedchain.config import InitConfig, AddConfig, QueryConfig, ChatConfig
|
||||||
|
from embedchain.config.QueryConfig import DEFAULT_PROMPT
|
||||||
|
|
||||||
from embedchain.loaders.youtube_video import YoutubeVideoLoader
|
from embedchain.loaders.youtube_video import YoutubeVideoLoader
|
||||||
from embedchain.loaders.pdf_file import PdfFileLoader
|
from embedchain.loaders.pdf_file import PdfFileLoader
|
||||||
@@ -388,3 +389,58 @@ class OpenSourceApp(EmbedChain):
|
|||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class EmbedChainPersonApp:
|
||||||
|
"""
|
||||||
|
Base class to create a person bot.
|
||||||
|
This bot behaves and speaks like a person.
|
||||||
|
|
||||||
|
:param person: name of the person, better if its a well known person.
|
||||||
|
:param config: InitConfig instance to load as configuration.
|
||||||
|
"""
|
||||||
|
def __init__(self, person, config: InitConfig = None):
|
||||||
|
self.person = person
|
||||||
|
self.person_prompt = f"You are {person}. Whatever you say, you will always say in {person} style."
|
||||||
|
self.template = Template(
|
||||||
|
self.person_prompt + " " + DEFAULT_PROMPT
|
||||||
|
)
|
||||||
|
if config is None:
|
||||||
|
config = InitConfig()
|
||||||
|
super().__init__(config)
|
||||||
|
|
||||||
|
|
||||||
|
class PersonApp(EmbedChainPersonApp, App):
|
||||||
|
"""
|
||||||
|
The Person app.
|
||||||
|
Extends functionality from EmbedChainPersonApp and App
|
||||||
|
"""
|
||||||
|
def query(self, input_query, config: QueryConfig = None):
|
||||||
|
query_config = QueryConfig(
|
||||||
|
template=self.template,
|
||||||
|
)
|
||||||
|
return super().query(input_query, query_config)
|
||||||
|
|
||||||
|
def chat(self, input_query, config: ChatConfig = None):
|
||||||
|
chat_config = ChatConfig(
|
||||||
|
template = self.template,
|
||||||
|
)
|
||||||
|
return super().chat(input_query, chat_config)
|
||||||
|
|
||||||
|
|
||||||
|
class PersonOpenSourceApp(EmbedChainPersonApp, OpenSourceApp):
|
||||||
|
"""
|
||||||
|
The Person app.
|
||||||
|
Extends functionality from EmbedChainPersonApp and OpenSourceApp
|
||||||
|
"""
|
||||||
|
def query(self, input_query, config: QueryConfig = None):
|
||||||
|
query_config = QueryConfig(
|
||||||
|
template=self.template,
|
||||||
|
)
|
||||||
|
return super().query(input_query, query_config)
|
||||||
|
|
||||||
|
def chat(self, input_query, config: ChatConfig = None):
|
||||||
|
chat_config = ChatConfig(
|
||||||
|
template = self.template,
|
||||||
|
)
|
||||||
|
return super().chat(input_query, chat_config)
|
||||||
Reference in New Issue
Block a user