from typing import Tuple, List, Optional from llama_index.core.schema import NodeWithScore import sys from initialize import app_state from utils import get_links_html, get_links_html_lp async def search_with_ai_action(legal_position_json: dict) -> Tuple[str, Optional[List[NodeWithScore]]]: try: if app_state.retriever_bm25 is None: raise ValueError("Retriever is not initialized") query_text = ( f"{legal_position_json['title']}: " f"{legal_position_json['text']}: " f"{legal_position_json['proceeding']}: " f"{legal_position_json['category']}" ) nodes = await app_state.retriever_bm25.aretrieve(query_text) sources_output = "\n **Результати пошуку (наявні правові позиції ВСУ):** \n\n" for index, node in enumerate(nodes, start=1): source_title = node.node.metadata.get('title') doc_ids = node.node.metadata.get('doc_id') lp_ids = node.node.metadata.get('lp_id') links = get_links_html(doc_ids) links_lp = get_links_html_lp(lp_ids) sources_output += f"\n[{index}] *{source_title}* {links_lp} 👉 Score: {node.score} {links}\n" return sources_output, nodes except Exception as e: error_message = f"Error during search: {str(e)}" print(error_message, file=sys.stderr) return error_message, None