mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-07-13 06:32:57 +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:
|
||||
prompt = (
|
||||
f"Answer the query based on the documents below and quote the documents sections containing "
|
||||
f'the answer. Respond with one "{ANSWER_PAT}" section and one or more "{QUOTE_PAT}" sections. '
|
||||
f"For each quote, only include text exactly from the documents, don't include the source. "
|
||||
f"Answer the query based on the documents below and quote the documents segments containing the answer. "
|
||||
f'Respond with one "{ANSWER_PAT}" section and as many "{QUOTE_PAT}" sections as is relevant. '
|
||||
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'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.logging import setup_logger
|
||||
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 SentenceTransformer # type: ignore
|
||||
|
||||
@ -43,6 +45,7 @@ def semantic_retrival(
|
||||
)["data"][0]["embedding"]
|
||||
else:
|
||||
query_embedding = embedding_model.encode(query)
|
||||
try:
|
||||
hits = get_qdrant_client().search(
|
||||
collection_name=qdrant_collection,
|
||||
query_vector=query_embedding
|
||||
@ -51,6 +54,12 @@ def semantic_retrival(
|
||||
query_filter=None,
|
||||
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 = []
|
||||
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:
|
||||
text = text.lower()
|
||||
text = "".join(
|
||||
text.split()
|
||||
) # GPT models like to return cleaner spacing, not good for quote matching
|
||||
return text.replace(
|
||||
"*", ""
|
||||
) # GPT models sometimes like to cleanup bulletpoints represented by *
|
||||
|
||||
# GPT models like to return cleaner spacing, not good for quote matching
|
||||
text = "".join(text.split())
|
||||
|
||||
# GPT models sometimes like to clean up 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__":
|
||||
previous_query = None
|
||||
while True:
|
||||
try:
|
||||
keyword_search = False
|
||||
query = input(
|
||||
"\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:
|
||||
print("Answer: ?")
|
||||
if contents.get("quotes"):
|
||||
for ind, (quote, quote_info) in enumerate(contents["quotes"].items()):
|
||||
print(f"Quote {str(ind)}:\n{quote}")
|
||||
for ind, (quote, quote_info) in enumerate(
|
||||
contents["quotes"].items()
|
||||
):
|
||||
print(f"Quote {str(ind + 1)}:\n{quote}")
|
||||
print(f"Link: {quote_info['link']}")
|
||||
print(f"Source: {quote_info[SOURCE_TYPE]}")
|
||||
else:
|
||||
print("No quotes found")
|
||||
except Exception as e:
|
||||
print(f"Failed due to {e}, retrying")
|
||||
|
Reference in New Issue
Block a user