Spaces:
Build error
Build error
Update functions.py
Browse files- functions.py +31 -7
functions.py
CHANGED
|
@@ -47,18 +47,41 @@ def select_document_section_by_query_similarity(query: str, contexts: dict[(str,
|
|
| 47 |
|
| 48 |
return document_similarities[0]
|
| 49 |
|
| 50 |
-
def construct_prompt(query: str, context_embeddings: dict, df: pd.DataFrame) -> str:
|
| 51 |
"""
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
-
|
|
|
|
| 55 |
|
|
|
|
| 56 |
service_description = df.loc[chosen_service].description.replace("\n", " ")
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
question = f"\n\nQ: {query}"
|
| 60 |
-
message = introduction
|
|
|
|
|
|
|
| 61 |
link = df.loc[chosen_service].link
|
|
|
|
| 62 |
return message, link
|
| 63 |
|
| 64 |
def answer_query_with_context(
|
|
@@ -87,4 +110,5 @@ def answer_query_with_context(
|
|
| 87 |
end_message += """Helpdesk representatives are also available for a remote chat session during normal hours on Monday - Friday, 8:00 AM - 5:00 PM PST via https://helpdesk.hmc.edu"""
|
| 88 |
reply = response["choices"][0]["message"]["content"] + end_message
|
| 89 |
|
| 90 |
-
return reply
|
|
|
|
|
|
| 47 |
|
| 48 |
return document_similarities[0]
|
| 49 |
|
| 50 |
+
def construct_prompt(query: str, context_embeddings: dict, df: pd.DataFrame) -> tuple[str, str]:
|
| 51 |
"""
|
| 52 |
+
Constructs a prompt for the language model based on the most relevant service description.
|
| 53 |
+
|
| 54 |
+
This function identifies the most relevant service by comparing the query with precomputed
|
| 55 |
+
document embeddings. It then formats the prompt to include an introduction, the service
|
| 56 |
+
description as context, and the user's question.
|
| 57 |
+
|
| 58 |
+
Parameters:
|
| 59 |
+
query (str): The user's input question.
|
| 60 |
+
context_embeddings (dict): A dictionary mapping service identifiers to their embeddings.
|
| 61 |
+
df (pd.DataFrame): A DataFrame containing service descriptions and links.
|
| 62 |
+
|
| 63 |
+
Returns:
|
| 64 |
+
tuple[str, str]: A tuple containing the formatted prompt and the associated service link.
|
| 65 |
"""
|
| 66 |
+
# Select the most relevant service based on the query
|
| 67 |
+
_, chosen_service = select_document_section_by_query_similarity(query, context_embeddings)
|
| 68 |
|
| 69 |
+
# Format the service description and clean up newline characters
|
| 70 |
service_description = df.loc[chosen_service].description.replace("\n", " ")
|
| 71 |
+
|
| 72 |
+
# Construct the introduction and the full prompt
|
| 73 |
+
introduction = (
|
| 74 |
+
"Answer the question as truthfully as possible using the provided context. "
|
| 75 |
+
"If the answer is not contained within the text below, say: "
|
| 76 |
+
"'I could not find an answer to your question, please reach out to Helpdesk.'"
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
question = f"\n\nQ: {query}"
|
| 80 |
+
message = f"{introduction}\n* \n\nContext:\n{service_description}{question}"
|
| 81 |
+
|
| 82 |
+
# Get the relevant service link
|
| 83 |
link = df.loc[chosen_service].link
|
| 84 |
+
|
| 85 |
return message, link
|
| 86 |
|
| 87 |
def answer_query_with_context(
|
|
|
|
| 110 |
end_message += """Helpdesk representatives are also available for a remote chat session during normal hours on Monday - Friday, 8:00 AM - 5:00 PM PST via https://helpdesk.hmc.edu"""
|
| 111 |
reply = response["choices"][0]["message"]["content"] + end_message
|
| 112 |
|
| 113 |
+
return reply
|
| 114 |
+
|