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