Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import numpy as np | |
| from mistral_hf_api import get_response | |
| from document_retrieval import select_document_section_by_query_similarity | |
| from file_utils import load_file | |
| # Load the prompt from a file | |
| META_PROMPT = load_file("meta_prompt.txt") | |
| def construct_prompt(query: str, context_embeddings: dict, df: pd.DataFrame) -> tuple[str, str]: | |
| """ | |
| Constructs a prompt for the language model based on the most relevant service description. | |
| This function identifies the most relevant service by comparing the query with precomputed | |
| document embeddings. It then formats the prompt to include an introduction, the service | |
| description as context, and the user's question. | |
| Parameters: | |
| query (str): The user's input question. | |
| context_embeddings (dict): A dictionary mapping service identifiers to their embeddings. | |
| df (pd.DataFrame): A DataFrame containing service descriptions and links. | |
| Returns: | |
| tuple[str, str]: A tuple containing the formatted prompt and the associated service link. | |
| """ | |
| # Select the most relevant service based on the query | |
| _, chosen_service = select_document_section_by_query_similarity(query, context_embeddings) | |
| # Format the service description and clean up newline characters | |
| service_description = df.loc[chosen_service].description.replace("\n", " ") | |
| # Construct the introduction and the full prompt | |
| introduction = ( | |
| "Answer the question as truthfully as possible using the provided context. " | |
| "If the answer is not contained within the text below, say: " | |
| "'I could not find an answer to your question, please reach out to Helpdesk.'" | |
| ) | |
| question = f"\n\nQ: {query}" | |
| message = f"{introduction}\n* \n\nContext:\n{service_description}{question}" | |
| # Get the relevant service link | |
| link = df.loc[chosen_service].link | |
| return message, link | |
| def answer_query_with_context( | |
| query: str, | |
| df: pd.DataFrame, | |
| document_embeddings: dict[(str, str), np.array], | |
| show_prompt: bool = False | |
| ) -> str: | |
| """ | |
| Generates a response to a user's query using the most relevant service description. | |
| This function constructs a prompt by attaching the most relevant service description to the user's query, | |
| sends this prompt to the language model to generate a response, and appends additional service-related | |
| information to this response. | |
| Parameters: | |
| query (str): The user's input question. | |
| df (pd.DataFrame): A DataFrame containing service descriptions and links. | |
| document_embeddings (dict): A dictionary mapping service identifiers to their embeddings. | |
| show_prompt (bool, optional): If True, displays the constructed prompt (for debugging). Defaults to False. | |
| Returns: | |
| str: The final response from the chatbot, including the generated answer and additional service details. | |
| """ | |
| # Construct the prompt and retrieve the service link | |
| prompt, link = construct_prompt(query, document_embeddings, df) | |
| # Get the response from the language model | |
| messages=[ | |
| {"role": "system", "content": META_PROMPT}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| response = get_response(messages) | |
| # Append additional service-related information | |
| end_message = ( | |
| f"\n\nPlease check out the relevant HMC service catalogue for more details: {link} " | |
| "\n\nIf not satisfied with the answer, please email helpdesk@hmc.edu, call 909.607.7777, " | |
| "or visit the Helpdesk located on the Sprague first floor. " | |
| "Helpdesk representatives are also available for a remote chat session during normal hours (Monday - Friday, " | |
| "8:00 AM - 5:00 PM PST) via https://helpdesk.hmc.edu" | |
| ) | |
| reply = response + end_message | |
| return reply | |