Spaces:
Sleeping
Sleeping
Create chatbot_interface4.py
Browse files- utils/chatbot_interface4.py +170 -0
utils/chatbot_interface4.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
from typing import Optional
|
5 |
+
import gradio as gr
|
6 |
+
from utils.response_manager import ResponseManager
|
7 |
+
|
8 |
+
class ChatbotInterface:
|
9 |
+
def __init__(self,
|
10 |
+
config_path: str = 'config/gradio_config.json',
|
11 |
+
model: str = "gpt-4o-mini",
|
12 |
+
temperature: float = 0,
|
13 |
+
max_output_tokens: int = 800,
|
14 |
+
max_num_results: int = 15,
|
15 |
+
vector_store_id: Optional[str] = None,
|
16 |
+
api_key: Optional[str] = None,
|
17 |
+
meta_prompt_file: Optional[str] = None):
|
18 |
+
"""
|
19 |
+
Initialize the ChatbotInterface with configuration and custom parameters for ResponseManager.
|
20 |
+
:param config_path: Path to the configuration JSON file.
|
21 |
+
:param model: The OpenAI model to use (default: 'gpt-4o-mini').
|
22 |
+
:param temperature: The temperature for response generation (default: 0).
|
23 |
+
:param max_output_tokens: The maximum number of output tokens (default: 800).
|
24 |
+
:param max_num_results: The maximum number of search results to return (default: 15).
|
25 |
+
:param vector_store_id: The ID of the vector store to use for file search.
|
26 |
+
:param api_key: The OpenAI API key for authentication.
|
27 |
+
:param meta_prompt_file: Path to the meta prompt file .
|
28 |
+
"""
|
29 |
+
# Parameters for UI
|
30 |
+
self.config = self.load_config(config_path)
|
31 |
+
self.title = self.config["chatbot_title"]
|
32 |
+
self.description = self.config["chatbot_description"]
|
33 |
+
self.input_placeholder = self.config["chatbot_input_placeholder"]
|
34 |
+
self.output_label = self.config["chatbot_output_label"]
|
35 |
+
|
36 |
+
# Parameters for ResponseManager class
|
37 |
+
self.model = model
|
38 |
+
self.temperature = temperature
|
39 |
+
self.max_output_tokens = max_output_tokens
|
40 |
+
self.max_num_results = max_num_results
|
41 |
+
self.vector_store_id = vector_store_id
|
42 |
+
self.api_key = api_key
|
43 |
+
self.meta_prompt_file = meta_prompt_file
|
44 |
+
|
45 |
+
|
46 |
+
@staticmethod
|
47 |
+
def load_config(config_path: str) -> dict:
|
48 |
+
"""
|
49 |
+
Load the configuration for Gradio GUI interface from the JSON file.
|
50 |
+
:param config_path: Path to the configuration JSON file.
|
51 |
+
:return: Configuration dictionary.
|
52 |
+
"""
|
53 |
+
logging.info(f"Loading configuration from {config_path}...")
|
54 |
+
if not os.path.exists(config_path):
|
55 |
+
logging.error(f"Configuration file not found: {config_path}")
|
56 |
+
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
57 |
+
|
58 |
+
with open(config_path, 'r') as config_file:
|
59 |
+
config = json.load(config_file)
|
60 |
+
|
61 |
+
required_keys = [
|
62 |
+
"chatbot_title",
|
63 |
+
"chatbot_description",
|
64 |
+
"chatbot_input_placeholder",
|
65 |
+
"chatbot_output_label"
|
66 |
+
]
|
67 |
+
|
68 |
+
for key in required_keys:
|
69 |
+
if key not in config:
|
70 |
+
logging.error(f"Missing required configuration key: {key}")
|
71 |
+
raise ValueError(f"Missing required configuration key: {key}")
|
72 |
+
|
73 |
+
logging.info("Configuration loaded successfully.")
|
74 |
+
return config
|
75 |
+
|
76 |
+
|
77 |
+
def create_interface(self) -> gr.Blocks:
|
78 |
+
"""
|
79 |
+
Create the Gradio Blocks interface that displays a single container including both
|
80 |
+
the text input and a small arrow submit button. The interface will clear the text input
|
81 |
+
after each message is submitted.
|
82 |
+
"""
|
83 |
+
logging.info("Creating Gradio interface...")
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
# Title and description area.
|
87 |
+
gr.Markdown(f"## {self.title}\n{self.description}")
|
88 |
+
|
89 |
+
# Chatbot output area.
|
90 |
+
chatbot_output = gr.Chatbot(label=self.output_label, type="messages")
|
91 |
+
|
92 |
+
# Session-specific states
|
93 |
+
conversation_state = gr.State([])
|
94 |
+
response_manager_state = gr.State(None)
|
95 |
+
|
96 |
+
# Row area.
|
97 |
+
with gr.Row(elem_id="input-container", equal_height=True):
|
98 |
+
reset = gr.ClearButton(
|
99 |
+
value="Clear history 🔄",
|
100 |
+
variant="secondary",
|
101 |
+
elem_id="reset-button",
|
102 |
+
size="lg"
|
103 |
+
)
|
104 |
+
user_input = gr.Textbox(
|
105 |
+
lines=1,
|
106 |
+
show_label=False, # Hide label for a unified look.
|
107 |
+
elem_id="chat-input",
|
108 |
+
placeholder=self.input_placeholder,
|
109 |
+
scale=500,
|
110 |
+
)
|
111 |
+
|
112 |
+
|
113 |
+
# Initialization function for session-specific response manager
|
114 |
+
def init_response_manager():
|
115 |
+
try:
|
116 |
+
rm = ResponseManager(
|
117 |
+
model=self.model,
|
118 |
+
temperature=self.temperature,
|
119 |
+
max_output_tokens=self.max_output_tokens,
|
120 |
+
max_num_results=self.max_num_results,
|
121 |
+
vector_store_id=self.vector_store_id,
|
122 |
+
api_key=self.api_key,
|
123 |
+
meta_prompt_file=self.meta_prompt_file
|
124 |
+
)
|
125 |
+
|
126 |
+
logging.info(
|
127 |
+
"ChatbotInterface initialized with the following parameters:\n"
|
128 |
+
f" - Model: {self.model}\n"
|
129 |
+
f" - Temperature: {self.temperature}\n"
|
130 |
+
f" - Max Output Tokens: {self.max_output_tokens}\n"
|
131 |
+
f" - Max Number of Results: {self.max_num_results}\n"
|
132 |
+
)
|
133 |
+
|
134 |
+
rm.reset_conversation()
|
135 |
+
return rm
|
136 |
+
except Exception as e:
|
137 |
+
logging.error(f"Failed to initialize ResponseManager: {e}")
|
138 |
+
raise
|
139 |
+
|
140 |
+
# Reset function updated to reset ResponseManager
|
141 |
+
def reset_output():
|
142 |
+
response_manager = init_response_manager()
|
143 |
+
return [], response_manager, "" # Returns [chatbot_output, response_manager_state, user_input]
|
144 |
+
|
145 |
+
# Process input now uses session-specific ResponseManager
|
146 |
+
def process_input(user_message, chat_history, response_manager):
|
147 |
+
updated_history = response_manager.generate_response(user_message, chat_history)
|
148 |
+
return updated_history, updated_history, response_manager, "" # Returns [chatbot_output, conversation_state, response_manager_state, user_input]
|
149 |
+
|
150 |
+
# Initialize ResponseManager on load
|
151 |
+
demo.load(
|
152 |
+
fn=init_response_manager,
|
153 |
+
inputs=None,
|
154 |
+
outputs=response_manager_object
|
155 |
+
)
|
156 |
+
|
157 |
+
reset.click(
|
158 |
+
fn=reset_output,
|
159 |
+
inputs=None,
|
160 |
+
outputs=[conversation_state, response_manager_object, user_input]
|
161 |
+
)
|
162 |
+
|
163 |
+
user_input.submit(
|
164 |
+
fn=process_input,
|
165 |
+
inputs=[user_input, conversation_state, response_manager_object],
|
166 |
+
outputs=[chatbot_output, conversation_state, response_manager_object, user_input]
|
167 |
+
)
|
168 |
+
|
169 |
+
logging.info("Gradio interface created successfully.")
|
170 |
+
return demo
|