Spaces:
Build error
Build error
Update functions.py
Browse files- functions.py +30 -13
functions.py
CHANGED
|
@@ -7,8 +7,7 @@ import os
|
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
COMPLETIONS_MODEL = "gpt-4o-mini"# "gpt-3.5-turbo-instruct" used earlier .
|
| 10 |
-
|
| 11 |
-
EMBEDDING_MODEL = "text-embedding-3-large"
|
| 12 |
|
| 13 |
COMPLETIONS_API_PARAMS = {
|
| 14 |
# We use temperature of 0.0 because it gives the most predictable, factual answer.
|
|
@@ -90,13 +89,26 @@ def answer_query_with_context(
|
|
| 90 |
document_embeddings: dict[(str, str), np.array],
|
| 91 |
show_prompt: bool = False
|
| 92 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
query,
|
| 96 |
-
document_embeddings,
|
| 97 |
-
df
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
response = openai.ChatCompletion.create(
|
| 101 |
model = COMPLETIONS_MODEL, # "gpt-3.5-turbo",
|
| 102 |
messages=[
|
|
@@ -105,10 +117,15 @@ def answer_query_with_context(
|
|
| 105 |
]
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
end_message
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
return reply
|
| 114 |
-
|
|
|
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
COMPLETIONS_MODEL = "gpt-4o-mini"# "gpt-3.5-turbo-instruct" used earlier .
|
| 10 |
+
EMBEDDING_MODEL = "text-embedding-3-large" # "text-embedding-3-small"
|
|
|
|
| 11 |
|
| 12 |
COMPLETIONS_API_PARAMS = {
|
| 13 |
# We use temperature of 0.0 because it gives the most predictable, factual answer.
|
|
|
|
| 89 |
document_embeddings: dict[(str, str), np.array],
|
| 90 |
show_prompt: bool = False
|
| 91 |
) -> str:
|
| 92 |
+
"""
|
| 93 |
+
Generates a response to a user's query using the most relevant service description.
|
| 94 |
+
|
| 95 |
+
This function constructs a prompt by attaching the most relevant service description to the user's query,
|
| 96 |
+
sends this prompt to the language model to generate a response, and appends additional service-related
|
| 97 |
+
information to this response.
|
| 98 |
+
|
| 99 |
+
Parameters:
|
| 100 |
+
query (str): The user's input question.
|
| 101 |
+
df (pd.DataFrame): A DataFrame containing service descriptions and links.
|
| 102 |
+
document_embeddings (dict): A dictionary mapping service identifiers to their embeddings.
|
| 103 |
+
show_prompt (bool, optional): If True, displays the constructed prompt (for debugging). Defaults to False.
|
| 104 |
+
|
| 105 |
+
Returns:
|
| 106 |
+
str: The final response from the chatbot, including the generated answer and additional service details.
|
| 107 |
+
"""
|
| 108 |
+
# Construct the prompt and retrieve the service link
|
| 109 |
+
prompt, link = construct_prompt(query, document_embeddings, df)
|
| 110 |
|
| 111 |
+
# Get the response from the language model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
response = openai.ChatCompletion.create(
|
| 113 |
model = COMPLETIONS_MODEL, # "gpt-3.5-turbo",
|
| 114 |
messages=[
|
|
|
|
| 117 |
]
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Append additional service-related information
|
| 121 |
+
end_message = (
|
| 122 |
+
f"\n\nPlease check out the relevant HMC service catalogue for more details: {link} "
|
| 123 |
+
"\n\nIf not satisfied with the answer, please email helpdesk@hmc.edu, call 909.607.7777, "
|
| 124 |
+
"or visit the Helpdesk located on the Sprague first floor. "
|
| 125 |
+
"Helpdesk representatives are also available for a remote chat session during normal hours (Monday - Friday, "
|
| 126 |
+
"8:00 AM - 5:00 PM PST) via https://helpdesk.hmc.edu"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
reply = response + end_message
|
| 130 |
|
| 131 |
return reply
|
|
|