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 my_logging import log_message from config import PROMPT_SIMPLE_POISK 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": # Fix: Remove image_config parameter or set it properly return GoogleGenAI( model=model_config["model_name"], api_key=model_config["api_key"], # Don't pass image_config=None ) 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)}") # Fix: Also apply to fallback model 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 format_context_for_llm(nodes): context_parts = [] for node in nodes: metadata = node.metadata if hasattr(node, 'metadata') else {} doc_id = metadata.get('document_id', 'Неизвестный документ') section_info = "" # Handle section information with proper hierarchy if metadata.get('section_path'): section_path = metadata['section_path'] section_text = metadata.get('section_text', '') parent_section = metadata.get('parent_section', '') parent_title = metadata.get('parent_title', '') level = metadata.get('level', '') if level in ['subsection', 'sub_subsection', 'sub_sub_subsection'] and parent_section and parent_title: # For subsections: раздел X (Title), пункт X.X if section_text: section_info = f"раздел {parent_section} ({parent_title}), пункт {section_path} ({section_text})" else: section_info = f"раздел {parent_section} ({parent_title}), пункт {section_path}" elif section_text: # For main sections: раздел X (Title) section_info = f"раздел {section_path} ({section_text})" else: section_info = f"раздел {section_path}" elif metadata.get('section_id'): section_id = metadata['section_id'] section_text = metadata.get('section_text', '') level = metadata.get('level', '') parent_section = metadata.get('parent_section', '') parent_title = metadata.get('parent_title', '') if level in ['subsection', 'sub_subsection', 'sub_sub_subsection'] and parent_section and parent_title: if section_text: section_info = f"раздел {parent_section} ({parent_title}), пункт {section_id} ({section_text})" else: section_info = f"раздел {parent_section} ({parent_title}), пункт {section_id}" elif section_text: section_info = f"раздел {section_id} ({section_text})" else: section_info = f"раздел {section_id}" # Override with table/image info if applicable if metadata.get('type') == 'table' and metadata.get('table_number'): table_num = metadata['table_number'] if not str(table_num).startswith('№'): table_num = f"№{table_num}" table_title = metadata.get('table_title', '') # Include section context for tables base_section = "" if metadata.get('section_path'): base_section = f", раздел {metadata['section_path']}" elif metadata.get('section_id'): base_section = f", раздел {metadata['section_id']}" if table_title: section_info = f"Таблица {table_num} ({table_title}){base_section}" else: section_info = f"Таблица {table_num}{base_section}" if metadata.get('type') == 'image' and metadata.get('image_number'): image_num = metadata['image_number'] if not str(image_num).startswith('№'): image_num = f"№{image_num}" image_title = metadata.get('image_title', '') # Include section context for images base_section = "" if metadata.get('section_path'): base_section = f", раздел {metadata['section_path']}" elif metadata.get('section_id'): base_section = f", раздел {metadata['section_id']}" if image_title: section_info = f"Рисунок {image_num} ({image_title}){base_section}" else: section_info = f"Рисунок {image_num}{base_section}" context_text = node.text if hasattr(node, 'text') else str(node) if section_info: formatted_context = f"[ИСТОЧНИК: {section_info}, документ {doc_id}]\n{context_text}\n" else: formatted_context = f"[ИСТОЧНИК: документ {doc_id}]\n{context_text}\n" context_parts.append(formatted_context) return "\n".join(context_parts) def generate_sources_html(nodes, chunks_df=None): html = "
{table_title}
" else: html += f"{image_title}
" if section and section != 'unknown': html += f"Раздел: {section}
" else: html += f"