RAG_AIEXP_01 / utils.py
MrSimple07's picture
complete new structure
ba52088
Raw
History Blame
6.79 kB
import logging
import sys
from llama_index.llms.google_genai import GoogleGenAI
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from sentence_transformers import CrossEncoder
from config import AVAILABLE_MODELS, DEFAULT_MODEL, GOOGLE_API_KEY
import time
from index_retriever import rerank_nodes
from utils import log_message, generate_sources_html
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def log_message(message):
logger.info(message)
print(message, flush=True)
sys.stdout.flush()
def get_llm_model(model_name):
try:
model_config = AVAILABLE_MODELS.get(model_name)
if not model_config:
log_message(f"Модель {model_name} не найдена, использую модель по умолчанию")
model_config = AVAILABLE_MODELS[DEFAULT_MODEL]
if not model_config.get("api_key"):
raise Exception(f"API ключ не найден для модели {model_name}")
if model_config["provider"] == "google":
return GoogleGenAI(
model=model_config["model_name"],
api_key=model_config["api_key"]
)
elif model_config["provider"] == "openai":
return OpenAI(
model=model_config["model_name"],
api_key=model_config["api_key"]
)
else:
raise Exception(f"Неподдерживаемый провайдер: {model_config['provider']}")
except Exception as e:
log_message(f"Ошибка создания модели {model_name}: {str(e)}")
return GoogleGenAI(model="gemini-2.0-flash", api_key=GOOGLE_API_KEY)
def get_embedding_model(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"):
return HuggingFaceEmbedding(model_name=model_name)
def get_reranker_model(model_name='cross-encoder/ms-marco-MiniLM-L-12-v2'):
return CrossEncoder(model_name)
def generate_sources_html(nodes, chunks_df=None):
html = "<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; max-height: 400px; overflow-y: auto;'>"
html += "<h3 style='color: #63b3ed; margin-top: 0;'>Источники:</h3>"
for i, node in enumerate(nodes):
metadata = node.metadata if hasattr(node, 'metadata') else {}
doc_type = metadata.get('type', 'text')
doc_id = metadata.get('document_id', 'unknown')
html += f"<div style='margin-bottom: 15px; padding: 15px; border: 1px solid #4a5568; border-radius: 8px; background-color: #1a202c;'>"
if doc_type == 'text':
html += f"<h4 style='margin: 0 0 10px 0; color: #63b3ed;'>📄 {doc_id}</h4>"
elif doc_type == 'table':
table_num = metadata.get('table_number', 'unknown')
if table_num and table_num != 'unknown':
if not table_num.startswith('№'):
table_num = f"№{table_num}"
html += f"<h4 style='margin: 0 0 10px 0; color: #68d391;'>📊 Таблица {table_num} - {doc_id}</h4>"
else:
html += f"<h4 style='margin: 0 0 10px 0; color: #68d391;'>📊 Таблица - {doc_id}</h4>"
elif doc_type == 'image':
image_num = metadata.get('image_number', 'unknown')
section = metadata.get('section', '')
if image_num and image_num != 'unknown':
if not str(image_num).startswith('№'):
image_num = f"№{image_num}"
html += f"<h4 style='margin: 0 0 10px 0; color: #fbb6ce;'>🖼️ Изображение {image_num} - {doc_id} ({section})</h4>"
else:
html += f"<h4 style='margin: 0 0 10px 0; color: #fbb6ce;'>🖼️ Изображение - {doc_id} ({section})</h4>"
if chunks_df is not None and 'file_link' in chunks_df.columns and doc_type == 'text':
doc_rows = chunks_df[chunks_df['document_id'] == doc_id]
if not doc_rows.empty:
file_link = doc_rows.iloc[0]['file_link']
html += f"<a href='{file_link}' target='_blank' style='color: #68d391; text-decoration: none; font-size: 14px; display: inline-block; margin-top: 10px;'>🔗 Ссылка на документ</a><br>"
html += "</div>"
html += "</div>"
return html
def answer_question(question, query_engine, reranker, current_model, chunks_df=None):
if query_engine is None:
return "<div style='background-color: #e53e3e; color: white; padding: 20px; border-radius: 10px;'>Система не инициализирована</div>", ""
try:
log_message(f"Получен вопрос: {question}")
log_message(f"Используется модель: {current_model}")
start_time = time.time()
log_message("Извлекаю релевантные узлы")
retrieved_nodes = query_engine.retriever.retrieve(question)
log_message(f"Извлечено {len(retrieved_nodes)} узлов")
log_message("Применяю переранжировку")
reranked_nodes = rerank_nodes(question, retrieved_nodes, reranker, top_k=10)
log_message(f"Отправляю запрос в LLM с {len(reranked_nodes)} узлами")
response = query_engine.query(question)
end_time = time.time()
processing_time = end_time - start_time
log_message(f"Обработка завершена за {processing_time:.2f} секунд")
sources_html = generate_sources_html(reranked_nodes, chunks_df)
answer_with_time = f"""<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; margin-bottom: 10px;'>
<h3 style='color: #63b3ed; margin-top: 0;'>Ответ (Модель: {current_model}):</h3>
<div style='line-height: 1.6; font-size: 16px;'>{response.response}</div>
<div style='margin-top: 15px; padding-top: 10px; border-top: 1px solid #4a5568; font-size: 14px; color: #a0aec0;'>
Время обработки: {processing_time:.2f} секунд
</div>
</div>"""
return answer_with_time, sources_html
except Exception as e:
log_message(f"Ошибка обработки вопроса: {str(e)}")
error_msg = f"<div style='background-color: #e53e3e; color: white; padding: 20px; border-radius: 10px;'>Ошибка обработки вопроса: {str(e)}</div>"
return error_msg, ""