mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-09-26 20:08:38 +02:00
Prompt Tuning and minor QOA changes (#2)
This commit is contained in:
@@ -7,9 +7,9 @@ QUOTE_PAT = "Quote:"
|
|||||||
|
|
||||||
def generic_prompt_processor(question: str, documents: list[str]) -> str:
|
def generic_prompt_processor(question: str, documents: list[str]) -> str:
|
||||||
prompt = (
|
prompt = (
|
||||||
f"Answer the query based on the documents below and quote the documents sections containing "
|
f"Answer the query based on the documents below and quote the documents segments containing the answer. "
|
||||||
f'the answer. Respond with one "{ANSWER_PAT}" section and one or more "{QUOTE_PAT}" sections. '
|
f'Respond with one "{ANSWER_PAT}" section and as many "{QUOTE_PAT}" sections as is relevant. '
|
||||||
f"For each quote, only include text exactly from the documents, don't include the source. "
|
f'Start each quote with "{QUOTE_PAT}". Each quote should be a single continuous segment from a document. '
|
||||||
f'If the query cannot be answered based on the documents, say "{UNCERTAINTY_PAT}". '
|
f'If the query cannot be answered based on the documents, say "{UNCERTAINTY_PAT}". '
|
||||||
f'Each document is prefixed with "{DOC_SEP_PAT}".\n\n'
|
f'Each document is prefixed with "{DOC_SEP_PAT}".\n\n'
|
||||||
)
|
)
|
||||||
|
@@ -13,6 +13,8 @@ from danswer.configs.model_configs import QUERY_EMBEDDING_CONTEXT_SIZE
|
|||||||
from danswer.utils.clients import get_qdrant_client
|
from danswer.utils.clients import get_qdrant_client
|
||||||
from danswer.utils.logging import setup_logger
|
from danswer.utils.logging import setup_logger
|
||||||
from danswer.utils.timing import build_timing_wrapper
|
from danswer.utils.timing import build_timing_wrapper
|
||||||
|
from qdrant_client.http.exceptions import ResponseHandlingException
|
||||||
|
from qdrant_client.http.exceptions import UnexpectedResponse
|
||||||
from sentence_transformers import CrossEncoder # type: ignore
|
from sentence_transformers import CrossEncoder # type: ignore
|
||||||
from sentence_transformers import SentenceTransformer # type: ignore
|
from sentence_transformers import SentenceTransformer # type: ignore
|
||||||
|
|
||||||
@@ -43,6 +45,7 @@ def semantic_retrival(
|
|||||||
)["data"][0]["embedding"]
|
)["data"][0]["embedding"]
|
||||||
else:
|
else:
|
||||||
query_embedding = embedding_model.encode(query)
|
query_embedding = embedding_model.encode(query)
|
||||||
|
try:
|
||||||
hits = get_qdrant_client().search(
|
hits = get_qdrant_client().search(
|
||||||
collection_name=qdrant_collection,
|
collection_name=qdrant_collection,
|
||||||
query_vector=query_embedding
|
query_vector=query_embedding
|
||||||
@@ -51,6 +54,12 @@ def semantic_retrival(
|
|||||||
query_filter=None,
|
query_filter=None,
|
||||||
limit=num_hits,
|
limit=num_hits,
|
||||||
)
|
)
|
||||||
|
except ResponseHandlingException as e:
|
||||||
|
logger.exception(f'Qdrant querying failed due to: "{e}", is Qdrant set up?')
|
||||||
|
except UnexpectedResponse as e:
|
||||||
|
logger.exception(
|
||||||
|
f'Qdrant querying failed due to: "{e}", has ingestion been run?'
|
||||||
|
)
|
||||||
|
|
||||||
retrieved_chunks = []
|
retrieved_chunks = []
|
||||||
for hit in hits:
|
for hit in hits:
|
||||||
|
@@ -11,9 +11,14 @@ def clean_model_quote(quote: str, trim_length: int) -> str:
|
|||||||
|
|
||||||
def shared_precompare_cleanup(text: str) -> str:
|
def shared_precompare_cleanup(text: str) -> str:
|
||||||
text = text.lower()
|
text = text.lower()
|
||||||
text = "".join(
|
|
||||||
text.split()
|
# GPT models like to return cleaner spacing, not good for quote matching
|
||||||
) # GPT models like to return cleaner spacing, not good for quote matching
|
text = "".join(text.split())
|
||||||
return text.replace(
|
|
||||||
"*", ""
|
# GPT models sometimes like to clean up bulletpoints represented by *
|
||||||
) # GPT models sometimes like to cleanup bulletpoints represented by *
|
text = text.replace("*", "")
|
||||||
|
|
||||||
|
# GPT models sometimes like to edit the quoting, ie "Title: Contents" becomes Title: "Contents"
|
||||||
|
text = text.replace('"', "")
|
||||||
|
|
||||||
|
return text
|
||||||
|
@@ -9,6 +9,7 @@ from danswer.configs.constants import SOURCE_TYPE
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
previous_query = None
|
previous_query = None
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
keyword_search = False
|
keyword_search = False
|
||||||
query = input(
|
query = input(
|
||||||
"\n\nAsk any question:\n - prefix with -k for keyword search\n - input an empty string to "
|
"\n\nAsk any question:\n - prefix with -k for keyword search\n - input an empty string to "
|
||||||
@@ -50,9 +51,13 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
print("Answer: ?")
|
print("Answer: ?")
|
||||||
if contents.get("quotes"):
|
if contents.get("quotes"):
|
||||||
for ind, (quote, quote_info) in enumerate(contents["quotes"].items()):
|
for ind, (quote, quote_info) in enumerate(
|
||||||
print(f"Quote {str(ind)}:\n{quote}")
|
contents["quotes"].items()
|
||||||
|
):
|
||||||
|
print(f"Quote {str(ind + 1)}:\n{quote}")
|
||||||
print(f"Link: {quote_info['link']}")
|
print(f"Link: {quote_info['link']}")
|
||||||
print(f"Source: {quote_info[SOURCE_TYPE]}")
|
print(f"Source: {quote_info[SOURCE_TYPE]}")
|
||||||
else:
|
else:
|
||||||
print("No quotes found")
|
print("No quotes found")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed due to {e}, retrying")
|
||||||
|
Reference in New Issue
Block a user