Below is an instruction that describes a task. Write a response that appropriately completes the request.
import json
import gradio as gr
from context.db_loader import SQLiteContextLoader
import constants
from helpers import load_json_file
class NeuroRavenApp:
def init(self):
self.system_prompt = None
self.current_theme = None
self.client = self.load_model_client()
self.context_loader = SQLiteContextLoader()
self.conversation_themes = self.get_conversation_themes()
if constants.NO_CONTEXT not in self.conversation_themes:
self.conversation_themes.append(constants.NO_CONTEXT)
code
Assistant
@staticmethod
def get_conversation_themes():
themes = load_json_file(constants.CONVERSATION_THEMES_JSON)
return themes
def handle_query(self, message, history, context_theme, should_save):
if should_save and isinstance(self.context_loader, SQLiteContextLoader):
if context_theme == constants.NO_CONTEXT:
yield constants.ERROR_CANNOT_SAVE_MESSAGE
self.context_loader.save_message(context_theme, message)
self.current_theme = None
yield constants.MESSAGE_SAVED_TO_CONTEXT
if context_theme != self.current_theme:
context = self.context_loader.load_context(context_theme)
if context:
self.system_prompt = constants.ANSWER_USING_CONTEXT_PROMPT.format(context=context)
else:
self.system_prompt = constants.DEFAULT_SYSTEM_PROMPT
self.current_theme = context_theme
response_generator = self.client.get_response(
message=message,
history=history,
sys_prompt=self.system_prompt
)
# Проверяем, поддерживает ли модель стриминг
if not self.client.supports_streaming:
yield response_generator # Возвращаем response_generator если модель поддерживает стрим
else:
# Генерация ответов по мере их поступления для остальных моделей
for response in response_generator:
yield response # Передаем каждый ответ в Gradio
def launch_app(self):
gr.ChatInterface(
fn=self.handle_query,
chatbot=gr.Chatbot(height=500),
textbox=gr.Textbox(
placeholder=constants.INPUT_PLACEHOLDER,
container=False,
scale=7
),
title="RenDocs",
theme="soft",
retry_btn=None,
clear_btn=constants.CLEAR_BUTTON_LABEL,
additional_inputs=[
gr.Dropdown(
self.conversation_themes,
label=constants.SELECT_CONTEXT_THEME_LABEL,
value=constants.NO_CONTEXT
),
gr.Checkbox(
label=constants.SAVE_MESSAGE_LABEL,
value=False
)
],
css="footer{display:none !important}"
).launch(server_name=constants.SERVER_NAME, server_port=constants.SERVER_PORT)
@staticmethod
def load_model_client():
with open("config.json") as config_file:
config = json.load(config_file)
model_name = config.get("model", "mini_4o") # Значение по умолчанию
if model_name == "gemma":
from models.gemma_model import ModelClient
elif model_name == "lms":
from models.lm_studio_model import ModelClient
elif model_name == "mini_4o":
from models.mini_4o_model import ModelClient
else:
raise ValueError(f"Unknown model name: {model_name}")
return ModelClient()
if name == "main":
app = NeuroRavenApp()
app.launch_app()
Как мне добавить очередь запросов
Чтобы добавить очередь запросов в ваше приложение NeuroRavenApp, вам нужно создать очередь, в которую будут помещаться входящие сообщения, и реализовать механизм обработки этих сообщений по одному. Преимущества этого подхода заключаются в том, что это позволяет управлять потоками данных и останавливать перегрузку сервера. Вам, возможно, придется использовать модули, такие как queue для создания очереди, и многопоточность или асинхронное программирование для обработки сообщений.
Вот пример, как вы можете изменить ваш код, чтобы добавить обработку очереди:
python
import json
import gradio as gr
import queue
import threading
from context.db_loader import SQLiteContextLoader
import constants
from helpers import load_json_file
class NeuroRavenApp:
def __init__(self):
self.system_prompt = None
self.current_theme = None
self.client = self.load_model_client()
self.context_loader = SQLiteContextLoader()
self.conversation_themes = self.get_conversation_themes()
self.query_queue = queue.Queue()
self.is_processing = False # Флаг для отслеживания состояния обработки запросов
if constants.NO_CONTEXT not in self.conversation_themes:
self.conversation_themes.append(constants.NO_CONTEXT)
# Запуск потока для обработки очереди
threading.Thread(target=self.process_queue, daemon=True).start()
@staticmethod
def get_conversation_themes():
themes = load_json_file(constants.CONVERSATION_THEMES_JSON)
return themes
def handle_query(self, message, history, context_theme, should_save):
# Поставить запрос в очередь
self.query_queue.put((message, history, context_theme, should_save))
return "Ваш запрос был принят и находится в очереди." # Подтверждение
def process_queue(self):
while True:
message, history, context_theme, should_save = self.query_queue.get()
response = self.process_message(message, history, context_theme, should_save)
self.query_queue.task_done() # Удаление задачи из очереди
# Здесь можно добавить логику для отправки ответа обратно в интерфейс Gradio
def process_message(self, message, history, context_theme, should_save):
# Здесь логика обработки отдельного сообщения
if should_save and isinstance(self.context_loader, SQLiteContextLoader):
if context_theme == constants.NO_CONTEXT:
return constants.ERROR_CANNOT_SAVE_MESSAGE
self.context_loader.save_message(context_theme, message)
self.current_theme = None
return constants.MESSAGE_SAVED_TO_CONTEXT
if context_theme != self.current_theme:
context = self.context_loader.load_context(context_theme)
if context:
self.system_prompt = constants.ANSWER_USING_CONTEXT_PROMPT.format(context=context)
else:
self.system_prompt = constants.DEFAULT_SYSTEM_PROMPT
self.current_theme = context_theme
response_generator = self.client.get_response(
message=message,
history=history,
sys_prompt=self.system_prompt
)
# Проверяем, поддерживает ли модель стриминг
if not self.client.supports_streaming:
return list(response_generator) # Возвращаем массив ответов если модель не поддерживает стрим
else:
responses = []
for response in response_generator:
responses.append(response) # Сохраняем каждый ответ
return responses # Возвращаем все ответы
def launch_app(self):
gr.ChatInterface(
fn=self.handle_query,
chatbot=gr.Chatbot(height=500),
textbox=gr.Textbox(
placeholder=constants.INPUT_PLACEHOLDER,
container=False,
scale=7
),
title="RenDocs",
theme="soft",
retry_btn=None,
clear_btn=constants.CLEAR_BUTTON_LABEL,
additional_inputs=[
gr.Dropdown(
self.conversation_themes,
label=constants.SELECT_CONTEXT_THEME_LABEL,
value=constants.NO_CONTEXT
),
gr.Checkbox(
label=constants.SAVE_MESSAGE_LABEL,
value=False
)
],
css="footer{display:none !important}"
).launch(server_name=constants.SERVER_NAME, server_port=constants.SERVER_PORT)
@staticmethod
def load_model_client():
with open("config.json") as config_file:
config = json.load(config_file)
model_name = config.get("model", "mini_4o") # Значение по умолчанию
if model_name == "gemma":
from models.gemma_model import ModelClient
elif model_name == "lms":
from models.lm_studio_model import ModelClient
elif model_name == "mini_4o":
from models.mini_4o_model import ModelClient
else:
raise ValueError(f"Unknown model name: {model_name}")
return ModelClient()
if __name__ == "__main__":
app = NeuroRavenApp()
app.launch_app()
Замечания:
Очередь запросов: Мы использовали стандартный модуль queue для создания очереди, которая хранит сообщения от пользователей.
Поток обработки: Создан новый поток, который будет обрабатывать запросы по мере их поступления из очереди.
Обработка сообщений: Логика обработки сообщений теперь вынесена в отдельный метод process_message, чтобы отделить обработку запроса от его получения.
Интерфейс Gradio: Теперь функция handle_query просто помещает запрос в очередь и возвращает подтверждение.
Этот код должен помочь вам управлять очередью запросов и их обработкой в вашем приложении.