Spaces:
Runtime error
Runtime error
update tests
Browse files- buster/apps/gradio_app.py +0 -2
- buster/busterbot.py +5 -4
- tests/test_chatbot.py +18 -18
buster/apps/gradio_app.py
CHANGED
|
@@ -56,8 +56,6 @@ def chat(question, history, bot_source):
|
|
| 56 |
cfg = available_configs.get(bot_source)
|
| 57 |
buster.update_cfg(cfg)
|
| 58 |
|
| 59 |
-
# formatting hack for code blocks to render properly every time
|
| 60 |
-
# answer = answer.replace("```", "\n```\n")
|
| 61 |
response = buster.process_input(question)
|
| 62 |
|
| 63 |
# formatted_sources = source_formatter(sources)
|
|
|
|
| 56 |
cfg = available_configs.get(bot_source)
|
| 57 |
buster.update_cfg(cfg)
|
| 58 |
|
|
|
|
|
|
|
| 59 |
response = buster.process_input(question)
|
| 60 |
|
| 61 |
# formatted_sources = source_formatter(sources)
|
buster/busterbot.py
CHANGED
|
@@ -17,6 +17,7 @@ logging.basicConfig(level=logging.INFO)
|
|
| 17 |
@dataclass(slots=True)
|
| 18 |
class Response:
|
| 19 |
completion: Completion
|
|
|
|
| 20 |
matched_documents: pd.DataFrame | None = None
|
| 21 |
|
| 22 |
|
|
@@ -177,7 +178,7 @@ class Buster:
|
|
| 177 |
logger.warning("No documents found...")
|
| 178 |
completion = Completion(text="No documents found.")
|
| 179 |
matched_documents = pd.DataFrame(columns=matched_documents.columns)
|
| 180 |
-
response = Response(completion=completion, matched_documents=matched_documents)
|
| 181 |
return response
|
| 182 |
|
| 183 |
# prepare the prompt
|
|
@@ -186,17 +187,17 @@ class Buster:
|
|
| 186 |
logger.info(f"GPT Response:\n{completion.text}")
|
| 187 |
|
| 188 |
# check for relevance
|
| 189 |
-
|
| 190 |
completion_text=completion.text,
|
| 191 |
engine=self.cfg.embedding_model,
|
| 192 |
unk_embedding=self.unk_embedding,
|
| 193 |
unk_threshold=self.cfg.unknown_threshold,
|
| 194 |
)
|
| 195 |
-
if not
|
| 196 |
matched_documents = pd.DataFrame(columns=matched_documents.columns)
|
| 197 |
# answer generated was the chatbot saying it doesn't know how to answer
|
| 198 |
# uncomment override completion with unknown prompt
|
| 199 |
# completion = Completion(text=self.cfg.unknown_prompt)
|
| 200 |
|
| 201 |
-
response = Response(completion=completion, matched_documents=matched_documents)
|
| 202 |
return response
|
|
|
|
| 17 |
@dataclass(slots=True)
|
| 18 |
class Response:
|
| 19 |
completion: Completion
|
| 20 |
+
is_relevant: bool
|
| 21 |
matched_documents: pd.DataFrame | None = None
|
| 22 |
|
| 23 |
|
|
|
|
| 178 |
logger.warning("No documents found...")
|
| 179 |
completion = Completion(text="No documents found.")
|
| 180 |
matched_documents = pd.DataFrame(columns=matched_documents.columns)
|
| 181 |
+
response = Response(completion=completion, matched_documents=matched_documents, is_relevant=False)
|
| 182 |
return response
|
| 183 |
|
| 184 |
# prepare the prompt
|
|
|
|
| 187 |
logger.info(f"GPT Response:\n{completion.text}")
|
| 188 |
|
| 189 |
# check for relevance
|
| 190 |
+
is_relevant = self.check_response_relevance(
|
| 191 |
completion_text=completion.text,
|
| 192 |
engine=self.cfg.embedding_model,
|
| 193 |
unk_embedding=self.unk_embedding,
|
| 194 |
unk_threshold=self.cfg.unknown_threshold,
|
| 195 |
)
|
| 196 |
+
if not is_relevant:
|
| 197 |
matched_documents = pd.DataFrame(columns=matched_documents.columns)
|
| 198 |
# answer generated was the chatbot saying it doesn't know how to answer
|
| 199 |
# uncomment override completion with unknown prompt
|
| 200 |
# completion = Completion(text=self.cfg.unknown_prompt)
|
| 201 |
|
| 202 |
+
response = Response(completion=completion, matched_documents=matched_documents, is_relevant=is_relevant)
|
| 203 |
return response
|
tests/test_chatbot.py
CHANGED
|
@@ -4,9 +4,8 @@ from pathlib import Path
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
from buster.busterbot import Buster, BusterConfig
|
| 8 |
-
from buster.completers.base import Completer
|
| 9 |
-
from buster.formatter.base import Response
|
| 10 |
from buster.retriever import Retriever
|
| 11 |
from buster.utils import get_retriever_from_extension
|
| 12 |
|
|
@@ -26,8 +25,8 @@ class MockCompleter(Completer):
|
|
| 26 |
def complete(self):
|
| 27 |
return
|
| 28 |
|
| 29 |
-
def generate_response(self, user_input,
|
| 30 |
-
return
|
| 31 |
|
| 32 |
|
| 33 |
class MockRetriever(Retriever):
|
|
@@ -93,9 +92,9 @@ def test_chatbot_mock_data(tmp_path, monkeypatch):
|
|
| 93 |
filepath = tmp_path / "not_a_real_file.tar.gz"
|
| 94 |
retriever = MockRetriever(filepath)
|
| 95 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 96 |
-
|
| 97 |
-
assert isinstance(
|
| 98 |
-
assert
|
| 99 |
|
| 100 |
|
| 101 |
def test_chatbot_real_data__chatGPT():
|
|
@@ -122,8 +121,8 @@ def test_chatbot_real_data__chatGPT():
|
|
| 122 |
)
|
| 123 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 124 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 125 |
-
|
| 126 |
-
assert isinstance(
|
| 127 |
|
| 128 |
|
| 129 |
def test_chatbot_real_data__chatGPT_OOD():
|
|
@@ -136,7 +135,7 @@ def test_chatbot_real_data__chatGPT_OOD():
|
|
| 136 |
completer_cfg={
|
| 137 |
"name": "ChatGPT",
|
| 138 |
"text_before_prompt": (
|
| 139 |
-
"""You are a
|
| 140 |
"""Make sure to format your answers in Markdown format, including code block and snippets. """
|
| 141 |
"""Do not include any links to urls or hyperlinks in your answers. """
|
| 142 |
"""If you do not know the answer to a question, or if it is completely irrelevant to the library usage, let the user know you cannot answer. """
|
|
@@ -156,9 +155,9 @@ def test_chatbot_real_data__chatGPT_OOD():
|
|
| 156 |
)
|
| 157 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 158 |
buster = Buster(cfg=buster_cfg, retriever=retriever)
|
| 159 |
-
|
| 160 |
-
assert isinstance(
|
| 161 |
-
assert
|
| 162 |
|
| 163 |
|
| 164 |
def test_chatbot_real_data__GPT():
|
|
@@ -166,13 +165,13 @@ def test_chatbot_real_data__GPT():
|
|
| 166 |
unknown_prompt="This doesn't seem to be related to the huggingface library. I am not sure how to answer.",
|
| 167 |
embedding_model="text-embedding-ada-002",
|
| 168 |
top_k=3,
|
| 169 |
-
thresh=0
|
| 170 |
max_words=3000,
|
| 171 |
response_format="slack",
|
| 172 |
completer_cfg={
|
| 173 |
"name": "GPT3",
|
| 174 |
"text_before_prompt": (
|
| 175 |
-
"""You are a
|
| 176 |
"""Make sure to format your answers in Markdown format, including code block and snippets.\n"""
|
| 177 |
"""Do not include any links to urls or hyperlinks in your answers.\n\n"""
|
| 178 |
"""Now answer the following question:\n"""
|
|
@@ -190,5 +189,6 @@ def test_chatbot_real_data__GPT():
|
|
| 190 |
)
|
| 191 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 192 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 193 |
-
|
| 194 |
-
assert isinstance(
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from buster.busterbot import Buster, BusterConfig, Response
|
| 8 |
+
from buster.completers.base import Completer, Completion
|
|
|
|
| 9 |
from buster.retriever import Retriever
|
| 10 |
from buster.utils import get_retriever_from_extension
|
| 11 |
|
|
|
|
| 25 |
def complete(self):
|
| 26 |
return
|
| 27 |
|
| 28 |
+
def generate_response(self, user_input, system_prompt) -> Completion:
|
| 29 |
+
return Completion(self.expected_answer)
|
| 30 |
|
| 31 |
|
| 32 |
class MockRetriever(Retriever):
|
|
|
|
| 92 |
filepath = tmp_path / "not_a_real_file.tar.gz"
|
| 93 |
retriever = MockRetriever(filepath)
|
| 94 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 95 |
+
response = buster.process_input("What is a transformer?")
|
| 96 |
+
assert isinstance(response.completion.text, str)
|
| 97 |
+
assert response.completion.text.startswith(gpt_expected_answer)
|
| 98 |
|
| 99 |
|
| 100 |
def test_chatbot_real_data__chatGPT():
|
|
|
|
| 121 |
)
|
| 122 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 123 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 124 |
+
response = buster.process_input("What is a transformer?")
|
| 125 |
+
assert isinstance(response.completion.text, str)
|
| 126 |
|
| 127 |
|
| 128 |
def test_chatbot_real_data__chatGPT_OOD():
|
|
|
|
| 135 |
completer_cfg={
|
| 136 |
"name": "ChatGPT",
|
| 137 |
"text_before_prompt": (
|
| 138 |
+
"""You are a chatbot assistant answering technical questions about huggingface transformers, a library to train transformers in python. """
|
| 139 |
"""Make sure to format your answers in Markdown format, including code block and snippets. """
|
| 140 |
"""Do not include any links to urls or hyperlinks in your answers. """
|
| 141 |
"""If you do not know the answer to a question, or if it is completely irrelevant to the library usage, let the user know you cannot answer. """
|
|
|
|
| 155 |
)
|
| 156 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 157 |
buster = Buster(cfg=buster_cfg, retriever=retriever)
|
| 158 |
+
response = buster.process_input("What is a good recipe for brocolli soup?")
|
| 159 |
+
assert isinstance(response.completion.text, str)
|
| 160 |
+
assert response.is_relevant is False
|
| 161 |
|
| 162 |
|
| 163 |
def test_chatbot_real_data__GPT():
|
|
|
|
| 165 |
unknown_prompt="This doesn't seem to be related to the huggingface library. I am not sure how to answer.",
|
| 166 |
embedding_model="text-embedding-ada-002",
|
| 167 |
top_k=3,
|
| 168 |
+
thresh=0, # ensures documents aren't empty
|
| 169 |
max_words=3000,
|
| 170 |
response_format="slack",
|
| 171 |
completer_cfg={
|
| 172 |
"name": "GPT3",
|
| 173 |
"text_before_prompt": (
|
| 174 |
+
"""You are a chatbot assistant answering technical questions about huggingface transformers, a library to train transformers in python.\n"""
|
| 175 |
"""Make sure to format your answers in Markdown format, including code block and snippets.\n"""
|
| 176 |
"""Do not include any links to urls or hyperlinks in your answers.\n\n"""
|
| 177 |
"""Now answer the following question:\n"""
|
|
|
|
| 189 |
)
|
| 190 |
retriever = get_retriever_from_extension(DOCUMENTS_FILE)(DOCUMENTS_FILE)
|
| 191 |
buster = Buster(cfg=hf_transformers_cfg, retriever=retriever)
|
| 192 |
+
response = buster.process_input("What is a transformer?")
|
| 193 |
+
assert isinstance(response.completion.text, str)
|
| 194 |
+
assert response.is_relevant is True
|