import os import gradio as gr from context.dbloader import SQLiteContextLoader from…
- User
import os import gradio as gr from context.db_loader import SQLiteContextLoader from context.file_loader import FileContextLoader from models.gpt4o import ModelClient
class NeuroRavenApp: def init(self, loader_type='file'): self.system_prompt = None self.current_theme = None self.client = ModelClient()
code# Выбираем тип загрузчика: 'file' или 'database' if loader_type == 'file': self.context_loader = FileContextLoader() elif loader_type == 'database': self.context_loader = SQLiteContextLoader() self.conversation_themes = self.get_conversation_themes() if "Без контекста" not in self.conversation_themes: self.conversation_themes.append("Без контекста") @staticmethod def get_conversation_themes(docs_path='docs'): # Получаем список всех директорий в указанном пути return [name for name in os.listdir(docs_path) if os.path.isdir(os.path.join(docs_path, name))] def handle_query(self, message, history, context_theme, should_save): if context_theme != self.current_theme: context = self.context_loader.load_context(context_theme) if context: self.system_prompt = f"Отвечай на вопросы беря информацию из контекста: \n{context}" else: self.system_prompt = "You are a helpful assistant." self.current_theme = context_theme response = self.client.get_response( message=message, history=history, sys_prompt=self.system_prompt ) # Сохранение сообщения в базу данных, если чекбокс активирован if should_save and isinstance(self.context_loader, SQLiteContextLoader): if context_theme == "Без контекста": return "Невозможно сохранить сообщение для текущей темы..." self.context_loader.save_message(context_theme, message) return "Сообщение сохранено в контекст!" return response def launch_app(self): gr.ChatInterface( self.handle_query, chatbot=gr.Chatbot(height=500), textbox=gr.Textbox(placeholder="Напиши свой вопрос", container=False, scale=7), title="NeuroRaven", theme="soft", retry_btn=None, clear_btn="Clear", additional_inputs=[ gr.Dropdown(self.conversation_themes, label="Выбери тему контекста", value="Без контекста"), gr.Checkbox(label="Сохранить сообщение", value=False) ], css="footer{display:none !important}" ).launch(server_name="192.168.0.105", server_port=7846) - Assistant
To ensure that the context is always updated when there are additions or changes, you need to reload the context every time a query is handled. The key is to avoid storing the context in a way that would prevent updates from being reflected, and instead load it fresh each time the method
handle_queryis invoked.Currently, you are loading the context only when the theme changes and storing it in . To update the context every time, you should load the context directly when constructing the prompt for each query.