Resolve conflicts (#208)

This commit is contained in:
Deshraj Yadav
2023-07-10 21:50:05 -07:00
committed by GitHub
parent 6936d6983d
commit 9ca836520f
32 changed files with 396 additions and 207 deletions

View File

@@ -1,4 +1,5 @@
from typing import Callable, Optional
from embedchain.config.BaseConfig import BaseConfig
@@ -6,27 +7,36 @@ class ChunkerConfig(BaseConfig):
"""
Config for the chunker used in `add` method
"""
def __init__(self,
chunk_size: Optional[int] = 4000,
chunk_overlap: Optional[int] = 200,
length_function: Optional[Callable[[str], int]] = len):
def __init__(
self,
chunk_size: Optional[int] = 4000,
chunk_overlap: Optional[int] = 200,
length_function: Optional[Callable[[str], int]] = len,
):
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.length_function = length_function
class LoaderConfig(BaseConfig):
"""
Config for the chunker used in `add` method
"""
def __init__(self):
pass
class AddConfig(BaseConfig):
"""
Config for the `add` method.
"""
def __init__(self,
chunker: Optional[ChunkerConfig] = None,
loader: Optional[LoaderConfig] = None):
def __init__(
self,
chunker: Optional[ChunkerConfig] = None,
loader: Optional[LoaderConfig] = None,
):
self.loader = loader
self.chunker = chunker

View File

@@ -2,6 +2,7 @@ class BaseConfig:
"""
Base config.
"""
def __init__(self):
pass

View File

@@ -1,8 +1,10 @@
from embedchain.config.QueryConfig import QueryConfig
from string import Template
from embedchain.config.QueryConfig import QueryConfig
DEFAULT_PROMPT = """
You are a chatbot having a conversation with a human. You are given chat history and context.
You are a chatbot having a conversation with a human. You are given chat
history and context.
You need to answer the query considering context, chat history and your knowledge base. If you don't know the answer or the answer is neither contained in the context nor in history, then simply say "I don't know".
$context
@@ -12,35 +14,41 @@ DEFAULT_PROMPT = """
Query: $query
Helpful Answer:
"""
""" # noqa:E501
DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
class ChatConfig(QueryConfig):
"""
Config for the `chat` method, inherits from `QueryConfig`.
"""
def __init__(self, template: Template = None, stream: bool = False):
"""
Initializes the ChatConfig instance.
:param template: Optional. The `Template` instance to use as a template for prompt.
:param stream: Optional. Control if response is streamed back to the user
:raises ValueError: If the template is not valid as template should contain $context and $query and $history
:param template: Optional. The `Template` instance to use as a
template for prompt.
:param stream: Optional. Control if response is streamed back to the
user
:raises ValueError: If the template is not valid as template should
contain $context and $query and $history
"""
if template is None:
template = DEFAULT_PROMPT_TEMPLATE
# History is set as 0 to ensure that there is always a history, that way, there don't have to be two templates.
# Having two templates would make it complicated because the history is not user controlled.
# History is set as 0 to ensure that there is always a history, that
# way, there don't have to be two templates.
# Having two templates would make it complicated because the history
# is not user controlled.
super().__init__(template, history=[0], stream=stream)
def set_history(self, history):
"""
Chat history is not user provided and not set at initialization time
:param history: (string) history to set
"""
self.history = history
return

View File

@@ -1,8 +1,9 @@
import os
import logging
import os
from embedchain.config.BaseConfig import BaseConfig
class InitConfig(BaseConfig):
"""
Config to initialize an embedchain `App` instance.
@@ -10,7 +11,8 @@ class InitConfig(BaseConfig):
def __init__(self, log_level=None, ef=None, db=None):
"""
:param log_level: Optional. (String) Debug level ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
:param log_level: Optional. (String) Debug level
['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
:param ef: Optional. Embedding function to use.
:param db: Optional. (Vector) database to use for embeddings.
"""
@@ -19,16 +21,18 @@ class InitConfig(BaseConfig):
# Embedding Function
if ef is None:
from chromadb.utils import embedding_functions
self.ef = embedding_functions.OpenAIEmbeddingFunction(
api_key=os.getenv("OPENAI_API_KEY"),
organization_id=os.getenv("OPENAI_ORGANIZATION"),
model_name="text-embedding-ada-002"
model_name="text-embedding-ada-002",
)
else:
self.ef = ef
if db is None:
from embedchain.vectordb.chroma_db import ChromaDB
self.db = ChromaDB(ef=self.ef)
else:
self.db = db
@@ -44,9 +48,10 @@ class InitConfig(BaseConfig):
if debug_level is not None:
level = getattr(logging, debug_level.upper(), None)
if not isinstance(level, int):
raise ValueError(f'Invalid log level: {debug_level}')
raise ValueError(f"Invalid log level: {debug_level}")
logging.basicConfig(format="%(asctime)s [%(name)s] [%(levelname)s] %(message)s",
level=level)
logging.basicConfig(
format="%(asctime)s [%(name)s] [%(levelname)s] %(message)s", level=level
)
self.logger = logging.getLogger(__name__)
return

View File

@@ -1,6 +1,7 @@
from embedchain.config.BaseConfig import BaseConfig
from string import Template
import re
from string import Template
from embedchain.config.BaseConfig import BaseConfig
DEFAULT_PROMPT = """
Use the following pieces of context to answer the query at the end.
@@ -11,7 +12,7 @@ DEFAULT_PROMPT = """
Query: $query
Helpful Answer:
"""
""" # noqa:E501
DEFAULT_PROMPT_WITH_HISTORY = """
Use the following pieces of context to answer the query at the end.
@@ -25,7 +26,7 @@ DEFAULT_PROMPT_WITH_HISTORY = """
Query: $query
Helpful Answer:
"""
""" # noqa:E501
DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE = Template(DEFAULT_PROMPT_WITH_HISTORY)
@@ -38,14 +39,17 @@ class QueryConfig(BaseConfig):
"""
Config for the `query` method.
"""
def __init__(self, template: Template = None, history = None, stream: bool = False):
def __init__(self, template: Template = None, history=None, stream: bool = False):
"""
Initializes the QueryConfig instance.
:param template: Optional. The `Template` instance to use as a template for prompt.
:param template: Optional. The `Template` instance to use as a
template for prompt.
:param history: Optional. A list of strings to consider as history.
:param stream: Optional. Control if response is streamed back to the user
:raises ValueError: If the template is not valid as template should contain $context and $query (and optionally $history).
:param stream: Optional. Control if response is streamed back to user
:raises ValueError: If the template is not valid as template should
contain $context and $query (and optionally $history).
"""
if not history:
self.history = None
@@ -67,12 +71,13 @@ class QueryConfig(BaseConfig):
if self.history is None:
raise ValueError("`template` should have `query` and `context` keys")
else:
raise ValueError("`template` should have `query`, `context` and `history` keys")
raise ValueError(
"`template` should have `query`, `context` and `history` keys"
)
if not isinstance(stream, bool):
raise ValueError("`stream` should be bool")
self.stream = stream
def validate_template(self, template: Template):
"""
@@ -82,9 +87,12 @@ class QueryConfig(BaseConfig):
:return: Boolean, valid (true) or invalid (false)
"""
if self.history is None:
return (re.search(query_re, template.template) \
and re.search(context_re, template.template))
return re.search(query_re, template.template) and re.search(
context_re, template.template
)
else:
return (re.search(query_re, template.template) \
return (
re.search(query_re, template.template)
and re.search(context_re, template.template)
and re.search(history_re, template.template))
and re.search(history_re, template.template)
)

View File

@@ -1,5 +1,5 @@
from .BaseConfig import BaseConfig
from .AddConfig import AddConfig
from .BaseConfig import BaseConfig
from .ChatConfig import ChatConfig
from .InitConfig import InitConfig
from .QueryConfig import QueryConfig
from .QueryConfig import QueryConfig