Spaces:
Sleeping
Sleeping
import os | |
import pandas as pd | |
import numpy as np | |
from utils.openai_api import get_response | |
from utils.document_retrieval import select_document_section_by_query_similarity | |
from utils.file_utils import load_file | |
# Load the prompts from the files | |
meta_prompt_file_path = '/home/user/app/prompts/meta_prompt.txt' | |
end_message_file_path = '/home/user/app/prompts/end_message.txt' | |
META_PROMPT = load_file(meta_prompt_file_path) | |
end_message = load_file(end_message_file_path) | |
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], | |
META_PROMPT=META_PROMPT, | |
end_message=end_message | |
) -> 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) | |
print(end_message) | |
return response + end_message | |