Spaces:
Build error
Build error
Initial code
Browse files- app.py +68 -52
- config/gradio_config.json +14 -0
- config/meta_prompt.txt +0 -0
- requirements.txt +2 -1
- utils/response_manager.py +80 -0
app.py
CHANGED
@@ -1,64 +1,80 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
"""
|
5 |
-
|
|
|
6 |
"""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
+
from utils.response_manager import ResponseManager # Import the ResponseManager class
|
|
|
5 |
"""
|
6 |
+
This script sets up a Gradio interface to host an AI chatbot using RAG (Retrieval-Augmented Generation)
|
7 |
+
to provide responses to user queries. Response API from OpenAI is used for both retrieval and generation of responses.
|
8 |
"""
|
9 |
+
# Vector store ID for the retrieval of knowledge base documents
|
10 |
+
# Load the vector store ID from the environment variable
|
11 |
+
vector_store_id = os.getenv('VECTOR_STORE_ID')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Check if the VECTOR_STORE_ID environment variable is set
|
14 |
+
if not vector_store_id:
|
15 |
+
raise ValueError("VECTOR_STORE_ID environment variable is not set.")
|
|
|
|
|
16 |
|
17 |
+
# Initialize the ResponseManager with the vector store ID
|
18 |
+
response_manager = ResponseManager(vector_store_id)
|
19 |
|
20 |
+
# Set parameters for the response generation
|
21 |
+
model = "gpt-4o-mini" # Set the model to be used for response generation
|
22 |
+
temperature=0 # Set the temperature for response generation
|
23 |
+
max_output_tokens=800 # Set the maximum number of output tokens
|
24 |
+
max_num_results=7 # Set the maximum number of knowledge base documents to return for retrieval
|
25 |
|
26 |
+
# Load the configuration for Gradio GUI interface from the JSON file
|
27 |
+
with open('config/gradio_config.json', 'r') as config_file:
|
28 |
+
config = json.load(config_file)
|
29 |
+
# Check if the configuration file is loaded successfully
|
30 |
+
if not config:
|
31 |
+
raise ValueError("Failed to load the configuration file.")
|
32 |
+
# Extract the configuration parameters
|
33 |
+
title = config["chatbot_title"]
|
34 |
+
description = config["chatbot_description"]
|
35 |
+
chatbot_input_label = config["chatbot_input_label"]
|
36 |
+
chatbot_input_placeholder = config["chatbot_input_placeholder"]
|
37 |
+
chatbot_output_label = config["chatbot_output_label"]
|
38 |
+
chatbot_output_placeholder = config["chatbot_output_placeholder"]
|
39 |
+
chatbot_submit_button = config["chatbot_submit_button"]
|
40 |
+
chatbot_reset_button = config["chatbot_reset_button"]
|
41 |
|
42 |
+
# Check if the configuration parameters are set correctly
|
43 |
+
if not all([header_message, title, description,
|
44 |
+
chatbot_input_label, chatbot_input_placeholder,
|
45 |
+
chatbot_output_label, chatbot_output_placeholder,
|
46 |
+
chatbot_submit_button, chatbot_reset_button]):
|
47 |
+
raise ValueError("One or more configuration parameters are missing or empty.")
|
48 |
|
49 |
+
# Define the chatbot function to handle user queries and generate responses
|
50 |
+
def chatbot(query: str) -> str:
|
51 |
+
"""
|
52 |
+
Function to handle the chatbot interaction.
|
53 |
+
:param query: The user query to respond to.
|
54 |
+
:return: The response text from the chatbot.
|
55 |
+
"""
|
56 |
+
try:
|
57 |
+
if query.strip():
|
58 |
+
response = response_manager.create_response(query, model, temperature, max_output_tokens, max_num_results)
|
59 |
+
if not response:
|
60 |
+
return "Sorry, I couldn't generate a response at this time. Please try again later."
|
61 |
+
# Return the response from the AI model
|
62 |
+
return response
|
63 |
+
else:
|
64 |
+
return "Please enter a valid query."
|
65 |
+
except Exception as e:
|
66 |
+
return str(e)
|
67 |
|
68 |
+
# Create a Gradio GUI interface
|
69 |
+
inputs = gr.Textbox(lines=7, label=chatbot_input_label, placeholder=chatbot_input_placeholder)
|
70 |
+
outputs = gr.Textbox(label=chatbot_output_label, placeholder=chatbot_output_placeholder)
|
71 |
+
iface = gr.Interface(fn=chatbot,
|
72 |
+
inputs=inputs,
|
73 |
+
outputs=outputs,
|
74 |
+
title=title,
|
75 |
+
description=description,
|
76 |
+
theme="default",
|
77 |
+
live=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
+
iface.launch()
|
config/gradio_config.json
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"chatbot_header_message": "Ask anything about the Harvey Mudd College CIS services",
|
3 |
+
"chatbot_title": "AI assistant for the Computing and Information Services (CIS) Helpdesk at Harvey Mudd College (HMC)",
|
4 |
+
"chatbot_description": "This is an AI chatbot for HMC CIS services",
|
5 |
+
"chatbot_input_label": "Type your question here",
|
6 |
+
"chatbot_input_placeholder": "What would you like to know?",
|
7 |
+
"chatbot_output_label": "Response",
|
8 |
+
"chatbot_output_placeholder": "The AI assistant will respond here",
|
9 |
+
"chatbot_submit_button": "Ask",
|
10 |
+
"chatbot_reset_button": "Reset"
|
11 |
+
}
|
12 |
+
// This JSON file contains configuration settings for a Gradio chatbot interface.
|
13 |
+
// It includes settings for the header message, title, description, input and output labels,
|
14 |
+
// placeholders, and button labels.
|
config/meta_prompt.txt
ADDED
File without changes
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
openai==1.66.3
|
utils/response_manager.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
"""
|
4 |
+
A module to manage responses from the OpenAI Response API for an IT Helpdesk assistant
|
5 |
+
at Harvey Mudd College. This module initializes the OpenAI client and provides a method
|
6 |
+
to create responses using RAG (Retrieval-Augmented Generation) to user queries. It uses
|
7 |
+
a vector store for retrieval of knowledge base documents and generates responses using
|
8 |
+
the specified OpenAI model. The module also loads a developer message from a text file
|
9 |
+
to prompt engineer responses from the AI model.
|
10 |
+
"""
|
11 |
+
|
12 |
+
# Load the OpenAI API key from the environment variable
|
13 |
+
# If the API key is not set, raise an error.
|
14 |
+
if "OPENAI_API_KEY" not in os.environ:
|
15 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set.")
|
16 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
17 |
+
|
18 |
+
class ResponseManager:
|
19 |
+
"""
|
20 |
+
A class to manage responses from the OpenAI API for an IT Helpdesk assistant.
|
21 |
+
This class initializes the OpenAI client and provides a method to create responses
|
22 |
+
to user queries using the specified OpenAI model.
|
23 |
+
"""
|
24 |
+
def __init__(self, vector_store_id):
|
25 |
+
"""
|
26 |
+
Initialize the ResponseManager with a vector store ID.
|
27 |
+
:param vector_store_id: The ID of the vector store to use for file search.
|
28 |
+
"""
|
29 |
+
# Initialize the OpenAI client
|
30 |
+
# Note: The OpenAI client is initialized with the API key set in the environment variable
|
31 |
+
# This is a placeholder for the actual OpenAI client initialization
|
32 |
+
# In a real-world scenario, you would use the appropriate OpenAI client library
|
33 |
+
# For example, if using the OpenAI Python library, you would do:
|
34 |
+
self.client = openai.OpenAI(api_key=api_key)
|
35 |
+
self.vector_store_id = vector_store_id
|
36 |
+
self.previous_response_id = None
|
37 |
+
|
38 |
+
# Load the meta prompt from the text file
|
39 |
+
# This message is used to provide context for the AI model
|
40 |
+
meta_prompt_file = 'config/meta_prompt.txt'
|
41 |
+
if not os.path.exists(developer_message_file):
|
42 |
+
raise FileNotFoundError(f"Meta prompt file '{meta_prompt_file}' not found.")
|
43 |
+
with open(meta_prompt_file, 'r') as file:
|
44 |
+
self.meta_prompt_file = file.read().strip()
|
45 |
+
|
46 |
+
def create_response(self, query, model: str= "gpt-4o-mini",
|
47 |
+
temperature=0, max_output_tokens=800,
|
48 |
+
max_num_results=7):
|
49 |
+
"""
|
50 |
+
Create a response to a user query using the OpenAI API.
|
51 |
+
:param query: The user query to respond to.
|
52 |
+
:param model: The OpenAI model to use (default is "gpt-4o-mini").
|
53 |
+
:param temperature: The temperature for the response (default is 0).
|
54 |
+
:param max_output_tokens: The maximum number of output tokens (default is 800).
|
55 |
+
:param max_num_results: The maximum number of search results to return (default is 7).
|
56 |
+
:param verbose: Whether to print the response (default is False).
|
57 |
+
:return: The response text from the OpenAI API.
|
58 |
+
"""
|
59 |
+
if self.previous_response_id is None:
|
60 |
+
input=[{"role": "developer", "content": self.developer_message},
|
61 |
+
{"role": "user", "content": query}]
|
62 |
+
else:
|
63 |
+
input=[{"role": "user", "content": query}]
|
64 |
+
|
65 |
+
response = self.client.responses.create(
|
66 |
+
model=model,
|
67 |
+
previous_response_id=self.previous_response_id,
|
68 |
+
input=input,
|
69 |
+
tools=[{
|
70 |
+
"type": "file_search",
|
71 |
+
"vector_store_ids": [self.vector_store_id], # ["<vector_store_id>"]
|
72 |
+
"max_num_results": max_num_results}
|
73 |
+
],
|
74 |
+
temperature=temperature,
|
75 |
+
max_output_tokens = max_output_tokens,
|
76 |
+
# include=["output[*].file_search_call.search_results"]
|
77 |
+
)
|
78 |
+
self.previous_response_id = response.id
|
79 |
+
|
80 |
+
return response.output_text
|