{"id":457430,"date":"2025-04-25T21:00:31","date_gmt":"2025-04-25T21:00:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=457430"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=457430","title":{"rendered":"<span>RAG-\u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438: \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u043b\u043b\u0435\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043d\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/659\/5ee\/199\/6595ee19978aa59e36a0a75273f55996.png\" width=\"1536\" height=\"1024\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/659\/5ee\/199\/6595ee19978aa59e36a0a75273f55996.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/659\/5ee\/199\/6595ee19978aa59e36a0a75273f55996.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 RAG (Retrieval-Augmented Generation) \u043d\u0430 Python \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441 \u043e\u043f\u043e\u0440\u043e\u0439 \u043d\u0430 \u043d\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b: \u0421\u041d\u0438\u041f\u044b, \u0421\u041f, \u0413\u041e\u0421\u0422\u044b \u0438 \u0434\u0440\u0443\u0433\u0438\u0435. \u0413\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c Telegram-\u0431\u043e\u0442\u0435: <a href=\"https:\/\/t.me\/Pdflyx_bot\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/Pdflyx_bot<\/a> &#8212; \u0434\u0430\u043d\u043d\u0438\u0439 \u0431\u043e\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439, \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u0446\u0438\u0442\u0430\u0442\u044b \u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u044b\u043b\u0430 \u0432\u0437\u044f\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0444\u0435\u0440: \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0440\u0435\u0433\u043b\u0430\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043b\u044e\u0431\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0431\u0430\u0437 \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<h2>\u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 ChatGPT?  <\/h2>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f LLM-\u043c\u043e\u0434\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043e\u0431\u044a\u0451\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0435 \u043e\u043a\u043d\u043e ChatGPT \u2014 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e 8192 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 (20\u201330 \u0441\u0442\u0440\u0430\u043d\u0438\u0446). \u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0414\u0430\u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Gemini 2.5 Pro (1 \u043c\u043b\u043d \u0442\u043e\u043a\u0435\u043d\u043e\u0432), \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0442\u0435\u043d \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c RAG. \u042d\u0442\u0430 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043e\u0442\u0431\u0438\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0431\u043b\u0438\u0437\u043a\u0438\u0435 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u044f\u0433\u043a\u0438\u0439 \u043f\u043e\u0438\u0441\u043a (Soft Search), \u043c\u044b \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435 \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<h2>\u041e\u0431\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430:  <\/h2>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u043f\u043e\u0445\u043e\u0436\u0438\u0445 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u043c\u043f\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0437 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u0442\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430\u00a0 \u0438 \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 (\u0441 \u0446\u0438\u0442\u0430\u0442\u0430\u043c\u0438 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438).<\/p>\n<\/li>\n<\/ol>\n<h2>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f RAG-\u0441\u0438\u0441\u0442\u0435\u043c\u044b  <\/h2>\n<h2>1. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438.  <\/h2>\n<p> \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0431\u0430\u0437\u0443 FAISS (\u043f\u043e\u0434\u043e\u0439\u0434\u0443\u0442 \u0438 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b: Pinecone, AWS, Azure).  <\/p>\n<p><strong> \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:  <\/strong><\/p>\n<pre><code class=\"python\">import os from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from langchain.document_loaders import PyPDFLoader from langchain_openai import OpenAIEmbeddings from langchain_openai import ChatOpenAI<\/code><\/pre>\n<p><strong>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435: <\/strong><em> <\/em><\/p>\n<pre><code class=\"python\"># \u0412\u0430\u0448 API-\u043a\u043b\u044e\u0447 \u0434\u043b\u044f OpenAI OPENAI_API_KEY = \"Your_Open_API_Key\"  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0441 \u0432\u0430\u0448\u0438\u043c\u0438 PDF-\u0444\u0430\u0439\u043b\u0430\u043c\u0438 (\u0431\u0430\u0437\u0430 \u0437\u043d\u0430\u043d\u0438\u0439) folder_path = \"Path_To_Your_Knowledge_Base\"  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f (\u0438\u043b\u0438 \u0443\u0436\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f) FAISS-\u0438\u043d\u0434\u0435\u043a\u0441 vector_store_dir = \"Path_To_Your_Vector_Store\"  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c LLM \u0438 Embeddings (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 OpenAI) llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model_name=\"gpt-4o\") embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)  DOCS_IN_RETRIEVER = 15 RELEVANCE_THRESHOLD_DOCS = 0,7 RELEVANCE_THRESHOLD_PROMPT = 0,6<\/code><\/pre>\n<p><strong><em>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Vector Store.  <\/em><\/strong><\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e &#8212; \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 FAISS-\u0438\u043d\u0434\u0435\u043a\u0441 (vector_store) \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e:  <\/p>\n<pre><code class=\"python\">def save_vector_store(vector_store, vector_store_dir: str):    \"\"\"    :param vector_store: \u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 FAISS (VectorStore), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c    :param vector_store_dir: \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0438\u043d\u0434\u0435\u043a\u0441    \"\"\"     vector_store.save_local(vector_store_dir)    print(f\"Vector store \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0432: {vector_store_dir}\")<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f Vector Store, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u043e\u043c\u043f\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u0443\u044e \u0431\u0430\u0437\u0443.<\/p>\n<pre><code class=\"python\">def load_vector_store(vector_store_dir: str, embeddings):    # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 'index.faiss'    index_file = os.path.join(vector_store_dir, \"index.faiss\")    if not os.path.exists(index_file):        print(f\"\u0424\u0430\u0439\u043b {index_file} \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d. \u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c vector store.\")        return None    try:        vector_store = FAISS.load_local(vector_store_dir, embeddings, allow_dangerous_deserialization=True)        print(f\"Vector store \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0438\u0437: {vector_store_dir}\")        return vector_store    except Exception as e:        print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 vector store: {e}\")        return None <\/code><\/pre>\n<p> \u0418 \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f  \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f Vector Store &#8212; \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0447\u0430\u043d\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"python\">def load_and_index_documents(folder_path: str, vector_store_dir: str, embeddings) -&gt; bool:    vector_store = load_vector_store(vector_store_dir, embeddings)    if vector_store:        print(\"\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 vector store \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d.\")        return True     documents = []    if not os.path.isdir(folder_path):        print(f\"\u041f\u0430\u043f\u043a\u0430 {folder_path} \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430.\")        return False     for filename in os.listdir(folder_path):        if filename.lower().endswith(\".pdf\"):            pdf_path = os.path.join(folder_path, filename)            try:                loader = PyMuPDFLoader(pdf_path)                pdf_docs = loader.load()                documents.extend(pdf_docs)                print(f\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043e {len(pdf_docs)} \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438\u0437 {filename}\")            except Exception as e:                print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 {filename}: {e}\")     if not documents:        print(\"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 PDF-\u0444\u0430\u0439\u043b\u043e\u0432 \u0434\u043b\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.\")        return False     text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)    split_docs = text_splitter.split_documents(documents)    print(f\"\u0412\u0441\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e {len(split_docs)} \u0447\u0430\u043d\u043a\u043e\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f.\")     vector_store = FAISS.from_documents(split_docs, embeddings)    print(\"\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u0440\u043e\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 FAISS.\")     save_vector_store(vector_store, vector_store_dir)    return True<\/code><\/pre>\n<p> \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 PDF \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439.  <\/p>\n<h2>2. \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.  <\/h2>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u043a\u0440\u0430\u0439\u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0442\u0435\u043b\u044c\u043d\u0430, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u0438, \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0442\u0430\u043a \u0436\u0435 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u044d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<pre><code class=\"python\">def preprocess_user_prompt(user_prompt: str, chat_history: list, llm) -&gt; str:    \"\"\"    \u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u043f\u0440\u0435\u0434\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 LLM.    \"\"\"    instructions = (        \"Your task is to refine the user prompt below, preserving its meaning.\\n\"        \"Steps to follow:\\n\"        \"1. Identify the main question or request.\\n\"        \"2. If there are multiple tasks, list them.\\n\"        \"3. Keep the text concise and clear.\\n\\n\"        f\"User prompt:\\n{user_prompt}\\n\\n\"        \"Chat history:\\n\"        f\"{chat_history}\\n\"        \"-----\\n\"        \"Now, provide the improved prompt below:\\n\"    )    response = llm(instructions)    improved_prompt = response.content.strip()    return improved_prompt<\/code><\/pre>\n<h2>3. \u041f\u043e\u0438\u0441\u043a \u043f\u043e\u0445\u043e\u0436\u0438\u0445 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439.  <\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0448\u0430\u0433 \u2014 \u043d\u0430\u0439\u0442\u0438 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439 \u0442\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430  .<\/p>\n<pre><code class=\"python\">def retrieve_documents(    vector_store,    user_prompt: str,    k: int = 20,    metadata_filters: dict = None ):    \"\"\"    \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c\u0443 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 FAISS (similarity search).    \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 (Document, score).    \"\"\"    if not vector_store:        print(\"Vector store \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441.\")        return []      try:        if metadata_filters:            docs_with_scores = vector_store.similarity_search_with_score(                user_prompt,                k=DOCS_IN_RETRIEVER,                filter=metadata_filters            )        else:            docs_with_scores = vector_store.similarity_search_with_score(user_prompt, k=k)        return docs_with_scores    except Exception as e:        print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432: {e}\")        return [] <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 k=20 \u2014 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u0445 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446.<\/p>\n<p>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0433\u0440\u043e\u0437\u0438\u0442 \u0442\u0435\u043c, \u0447\u0442\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u0451\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u0442\u044c, \u0430 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0441\u043d\u0438\u0436\u0435\u043d\u0438\u044e \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u0438. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c k=(15-20).<\/p>\n<h2>4. \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.  <\/h2>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u2018k\u2019 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439, \u0434\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043d\u0438\u0445 \u0431\u043b\u0438\u0437\u043a\u0430 \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u0435\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u043c \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u0443\u044e \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 (\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b).<\/p>\n<pre><code class=\"python\">def compute_embeddings_similarity(embeddings, prompt: str, documents: list):    \"\"\"    \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0439 \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u0438    \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (prompt) \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.    \"\"\"    if not documents:        return []      try:        # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433 \u0434\u043b\u044f prompt        prompt_embedding = np.array(embeddings.embed_query(prompt))        relevance_scores = []          for doc in documents:            doc_embedding = np.array(embeddings.embed_query(doc.page_content))            # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432            if (prompt_embedding.size == 0 or doc_embedding.size == 0):                print(f\"Warning: invalid embedding for doc: {doc.metadata.get('source', 'Unknown')}\")                similarity = 0.0            else:                dot_product = np.dot(prompt_embedding, doc_embedding)                norm_prompt = np.linalg.norm(prompt_embedding)                norm_doc = np.linalg.norm(doc_embedding)                similarity = 0.0                # \u041a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u0430\u044f \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u044c = (A\u00b7B) \/ (|A| * |B|)                if norm_prompt &gt; 1e-9 and norm_doc &gt; 1e-9:                    similarity = dot_product \/ (norm_prompt * norm_doc)                # \u00ab\u0417\u0430\u0436\u0438\u043c\u0430\u0435\u043c\u00bb \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 [-1, 1] \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b \u043e\u0442 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0438\u0445 \u043f\u043e\u0433\u0440\u0435\u0448\u043d\u043e\u0441\u0442\u0435\u0439                similarity = np.clip(similarity, -1.0, 1.0)              relevance_scores.append((doc, similarity))          return relevance_scores      except Exception as e:        print(f\"Exception in compute_embeddings_similarity: {str(e)}\")        return [(doc, 0.0) for doc in documents] <\/code><\/pre>\n<p> \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u044d\u0442\u0430\u043f\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b.<\/p>\n<h2> 5. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043d \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u0435\u043d \u043a \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c  <\/h2>\n<p> \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u043d\u0430 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430, \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043e\u0442\u0441\u0435\u0447 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u0431\u0449\u0435\u0433\u043e \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0430 \u0438\u043b\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u043e \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u044e\u0449\u0438\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439.  <\/p>\n<pre><code class=\"python\">def is_prompt_relevant_to_documents(relevance_scores, relevance_threshold=RELEVANCE_THRESHOLD_PROMPT):     \"\"\"     \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430: \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c     \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u0446\u0435\u043d\u043a\u0435 (similarity) \u0441\u0440\u0435\u0434\u0438 \u0432\u0441\u0435\u0445.     \"\"\"     try:         if not relevance_scores:             return False          max_similarity = max((sim for _, sim in relevance_scores), default=0.0)         print(f\"Debug: max_similarity = {max_similarity:.4f}, \"               f\"threshold = {relevance_threshold}, \"               f\"is_relevant = {max_similarity &gt;= relevance_threshold}\")          return max_similarity &gt;= relevance_threshold     except Exception as e:         print(f\"Exception in is_prompt_relevant_to_documents: {str(e)}\")         return False <\/code><\/pre>\n<h2> 6. \u041f\u043e\u0441\u0442\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430 LLM  <\/h2>\n<p> \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 \u043f\u043e\u0441\u0442\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0435 &#8212; \u043f\u0435\u0440\u0435\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u0443\u0435\u043c \u043e\u0442\u0432\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0441\u0432\u044f\u0437\u0438 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.  <\/p>\n<pre><code class=\"python\">def postprocess_llm_response(     llm_response: str,     user_prompt: str,     context_str: str = \"\",     references: dict = None,     is_relevant: bool = False ) -&gt; tuple:     \"\"\"     \u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043f\u043e\u0441\u0442\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 LLM:     - \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u0441\u0442\u0438\u043b\u044c,     - \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\/\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0441\u044b\u043b\u043e\u043a (\u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e).     \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 (final_answer, processed_references).     \"\"\"     if references is None:         references = {}      if not is_relevant:         references = {}         context_str = \"\"      prompt_references = (         \"You are an advanced language model tasked with providing a final, \"         \"well-structured answer based on the given content.\\n\\n\"         \"### Provided Data\\n\"         f\"LLM raw response:\\n{llm_response}\\n\\n\"         f\"User prompt:\\n{user_prompt}\\n\\n\"         f\"Context:\\n{context_str}\\n\\n\"         f\"References:\\n{references}\\n\\n\"         f\"is_relevant: {is_relevant}\\n\"         \"-------------------------\\n\"         \"Please re-check clarity and, if references exist, list them at the end.\\n\"         \"Return the final improved answer now:\\n\"     )      # \u0412\u044b\u0437\u043e\u0432 llm \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e (\u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)     chain_response = llm(prompt_references)     final_answer = chain_response.content.strip()      return final_answer, references<\/code><\/pre>\n<h2> 7. \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u0437\u0430\u0434\u0430\u0435\u043c \u043e\u0431\u0449\u0438\u0439 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d RAG \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 LLM.<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c vector store (\u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u043c)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c &#8212; \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u043c \u0432\u043e\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u0418\u0449\u0435\u043c \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 vector store<\/p>\n<\/li>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 LLM<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u0442-\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043a \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044e LLM<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">async def generate_response(    prompt: str,    chat_history=None,    metadata_filters=None,    context=None ):     # 1. \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430\/\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 vector_store    load_and_index_documents(KNOWLEDGE_BASE_FOLDER, VECTOR_STORE_DIR, embeddings)    vector_store = load_vector_store(VECTOR_STORE_DIR, embeddings)    if not vector_store:        return \"Unable to load Vector Store.\", None, None     # 2. \u041f\u0440\u0435\u0434\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u043e\u043f\u0440\u043e\u0441    if chat_history is None:        chat_history = []    prepared_prompt = preprocess_user_prompt(prompt, chat_history, llm)     # 3. \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0438\u0437 FAISS    retrieved_docs_with_scores = retrieve_documents(        vector_store=vector_store,        user_prompt=prepared_prompt,        k=5,        metadata_filters=metadata_filters    )    retrieved_docs = [doc for doc, _ in retrieved_docs_with_scores]     # 4. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u0443\u044e \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u044c    relevance_scores = compute_embeddings_similarity(embeddings, prepared_prompt, retrieved_docs)     # 5. \u0424\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 RELEVANCE_THRESHOLD_DOCS    relevant_docs = [        doc for (doc, similarity) in relevance_scores        if similarity &gt;= RELEVANCE_THRESHOLD_DOCS    ]     # 6. \u0415\u0441\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u0448\u043b\u043e\u0441\u044c, \u0432\u044b\u0434\u0430\u0451\u043c \u00abfallback\u00bb-\u043e\u0442\u0432\u0435\u0442    if not relevant_docs:        fallback_answer = \"I couldn't find relevant information to answer your question.\"        final_answer, _, _ = postprocess_llm_response(            llm_response=fallback_answer,            user_prompt=prompt,            context_str=\"\",            references=None,            is_relevant=False        )        return parser_html(final_answer), None, None     # 7. \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u00ab\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u00bb \u0438\u0437 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432    context_str = \"\"    for doc in relevant_docs:        source = doc.metadata.get('source', 'Unknown')        page = doc.metadata.get('page', 'N\/A')        content = doc.page_content or 'N\/A'        context_str += f\"Source: {source}, Page: {page}\\nContent:\\n{content}\\n---\\n\"     # 8. \u00ab\u0421\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439\u00bb \u043f\u0440\u043e\u043c\u043f\u0442: \u0434\u0430\u0451\u043c \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442    system_prompt = (        \"You are an expert. Provide a concise answer based on the context:\\n\"        f\"{context_str}\\n\"        \"--- End Context ---\\n\"        \"If the user question isn't fully answered in the provided context, \"        \"use your best judgment while staying truthful.\\n\"    )    prompt_template = ChatPromptTemplate.from_messages([        (\"system\", system_prompt),        MessagesPlaceholder(variable_name=\"chat_history\"),        (\"user\", \"{input}\")    ])     # 9. \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u043c\u043f\u0442 \u0434\u043b\u044f LLM    chain_input = {        \"input\": prepared_prompt,        \"chat_history\": chat_history    }    chain_full_prompt = prompt_template.format(**chain_input)     # 10. \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c LLM    llm_result = llm(chain_full_prompt)    if hasattr(llm_result, \"content\"):        answer_text = llm_result.content    else:        answer_text = str(llm_result)     # 11. \u041e\u0446\u0435\u043d\u0438\u0432\u0430\u0435\u043c \u00ab\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e\u00bb \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c (RELEVANCE_THRESHOLD_PROMPT)    is_relevant = is_prompt_relevant_to_documents(relevance_scores)     # 12. \u0413\u043e\u0442\u043e\u0432\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0441\u044b\u043b\u043e\u043a    references = {}    for doc in relevant_docs:        filename = doc.metadata.get(\"source\", \"Unknown\")        page = doc.metadata.get(\"page\", \"N\/A\")        references.setdefault(filename, set()).add(page)     # 13. \u041f\u043e\u0441\u0442-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430    final_answer, processed_refs = postprocess_llm_response(        llm_response=answer_text,        user_prompt=prompt,        context_str=context_str,        references=references,        is_relevant=is_relevant    )     # 14. \u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442    if is_relevant:        final_text = final_answer + \"\\n---\\nAdditional references may be listed above.\"        source_files = list(processed_refs.keys()) if processed_refs else None    else:        final_text = final_answer        source_files = None     return parser_html(final_text), source_files  # ======================================================================== # \u041f\u0420\u0418\u041c\u0415\u0420 \u0418\u0421\u041f\u041e\u041b\u042c\u0417\u041e\u0412\u0410\u041d\u0418\u042f (\u0437\u0430\u043f\u0443\u0441\u043a \u0447\u0435\u0440\u0435\u0437 asyncio) # ======================================================================== if __name__ == \"__main__\":    async def main():        user_query = \"\u041a\u0430\u043a\u043e\u0439 \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u043e\u0432?\"        chat_hist = [\"User: \u041f\u0440\u0438\u0432\u0435\u0442!\"]          answer, sources, short_sum = await generate_response(            prompt=user_query,            chat_history=chat_hist        )        print(\"\u041e\u0422\u0412\u0415\u0422 \u041e\u0422 LLM:\\n\", answer)        print(\"\u0418\u0421\u041f\u041e\u041b\u042c\u0417\u041e\u0412\u0410\u041d\u041d\u042b\u0415 \u0418\u0421\u0422\u041e\u0427\u041d\u0418\u041a\u0418:\\n\", sources)     asyncio.run(main()) <\/code><\/pre>\n<p> \u041d\u0430 \u0444\u0438\u043d\u0430\u043b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043d\u0430\u0439\u0434\u0435\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.  <\/p>\n<h2> \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:  <\/h2>\n<pre><code class=\"python\">if __name__ == \"__main__\":     async def main():         user_query = \"\u041a\u0430\u043a\u043e\u0439 \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u043e\u0432?\"         chat_hist = [\"User: \u041f\u0440\u0438\u0432\u0435\u0442!\"]          answer, sources = await generate_response(             prompt=user_query,             chat_history=chat_hist         )         print(\"\u0412\u041e\u041f\u0420\u041e\u0421:\\n\", user_query )         print(\"\u041e\u0422\u0412\u0415\u0422 \u041e\u0422 LLM:\\n\", answer)         print(\"\u0418\u0421\u041f\u041e\u041b\u042c\u0417\u041e\u0412\u0410\u041d\u041d\u042b\u0415 \u0418\u0421\u0422\u041e\u0427\u041d\u0418\u041a\u0418:\\n\", sources)      asyncio.run(main())   <\/code><\/pre>\n<h2>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:  <\/h2>\n<p>\u0412\u041e\u041f\u0420\u041e\u0421: \u041a\u0430\u043a\u043e\u0439 \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u043e\u0432?<\/p>\n<p>\u041e\u0422\u0412\u0415\u0422 \u041e\u0422 LLM:<\/p>\n<p>\u0417\u0430\u0449\u0438\u0442\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u0434\u043b\u044f \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0433\u0440\u0430\u044e\u0442 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u0443\u044e \u0440\u043e\u043b\u044c \u0432 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0438 \u0434\u043e\u043b\u0433\u043e\u0432\u0435\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439. \u0412\u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u0438 \u0438\u0445 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u0413\u0438\u0434\u0440\u043e\u0438\u0437\u043e\u043b\u044f\u0446\u0438\u044f: \u2022 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u0417\u0430\u0449\u0438\u0442\u0430 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430 \u043e\u0442 \u043f\u0440\u043e\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u0432\u043b\u0430\u0433\u0438 \u0438\u0437 \u0433\u0440\u0443\u043d\u0442\u0430. \u2022 \u041f\u0440\u0438\u043c\u0435\u0440\u044b: \u0420\u0443\u043b\u043e\u043d\u043d\u044b\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u043d\u0430 \u0431\u0438\u0442\u0443\u043c\u043d\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435, \u043c\u0435\u043c\u0431\u0440\u0430\u043d\u044b, \u043e\u0431\u043c\u0430\u0437\u043e\u0447\u043d\u044b\u0435 \u0433\u0438\u0434\u0440\u043e\u0438\u0437\u043e\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u044b. \u2022 \u0424\u0430\u043a\u0442\u043e\u0440\u044b \u0432\u044b\u0431\u043e\u0440\u0430: \u0423\u0440\u043e\u0432\u0435\u043d\u044c \u0433\u0440\u0443\u043d\u0442\u043e\u0432\u044b\u0445 \u0432\u043e\u0434, \u0432\u043b\u0430\u0436\u043d\u043e\u0441\u0442\u044c \u0433\u0440\u0443\u043d\u0442\u0430, \u043a\u043b\u0438\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043f\u043b\u043e\u0438\u0437\u043e\u043b\u044f\u0446\u0438\u044f: \u2022 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u041f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u043c\u0435\u0440\u0437\u0430\u043d\u0438\u044f \u0438 \u0442\u0435\u043f\u043b\u043e\u043f\u043e\u0442\u0435\u0440\u044c \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442. \u2022 \u041f\u0440\u0438\u043c\u0435\u0440\u044b: \u041f\u0435\u043d\u043e\u043f\u043e\u043b\u0438\u0441\u0442\u0438\u0440\u043e\u043b, \u044d\u043a\u0441\u0442\u0440\u0443\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0435\u043d\u043e\u043f\u043e\u043b\u0438\u0441\u0442\u0438\u0440\u043e\u043b. \u2022 \u0424\u0430\u043a\u0442\u043e\u0440\u044b \u0432\u044b\u0431\u043e\u0440\u0430: \u041a\u043b\u0438\u043c\u0430\u0442 \u0440\u0435\u0433\u0438\u043e\u043d\u0430, \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0437\u0434\u0430\u043d\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u0410\u043d\u0442\u0438\u043a\u043e\u0440\u0440\u043e\u0437\u0438\u0439\u043d\u0430\u044f \u0437\u0430\u0449\u0438\u0442\u0430: \u2022 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u0417\u0430\u0449\u0438\u0442\u0430 \u0430\u0440\u043c\u0430\u0442\u0443\u0440\u044b \u0438 \u043c\u0435\u0442\u0430\u043b\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0431\u0435\u0442\u043e\u043d\u0435 \u043e\u0442 \u043a\u043e\u0440\u0440\u043e\u0437\u0438\u0438. \u2022 \u041f\u0440\u0438\u043c\u0435\u0440\u044b: \u0421\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0435 \u043f\u043e\u043a\u0440\u044b\u0442\u0438\u044f, \u0438\u043d\u0433\u0438\u0431\u0438\u0442\u043e\u0440\u044b \u043a\u043e\u0440\u0440\u043e\u0437\u0438\u0438. \u2022 \u0424\u0430\u043a\u0442\u043e\u0440\u044b \u0432\u044b\u0431\u043e\u0440\u0430: \u0425\u0438\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u043e\u0441\u0442\u0430\u0432 \u0433\u0440\u0443\u043d\u0442\u043e\u0432, \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0430\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u044b\u0445 \u0432\u0435\u0449\u0435\u0441\u0442\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0442\u0438\u0432\u043e\u043e\u0431\u043b\u0435\u0434\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0440\u044b: \u2022 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u041f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u0435\u0434\u044f\u043d\u044b\u0445 \u043b\u0438\u043d\u0437 \u0438 \u043f\u0443\u0447\u0435\u043d\u0438\u044f \u0433\u0440\u0443\u043d\u0442\u0430. \u2022 \u041f\u0440\u0438\u043c\u0435\u0440\u044b: \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0440\u043e\u0437\u043e\u0441\u0442\u043e\u0439\u043a\u0438\u0445 \u0433\u0440\u0443\u043d\u0442\u043e\u0432, \u0434\u043e\u0431\u0430\u0432\u043a\u0438 \u0432 \u0431\u0435\u0442\u043e\u043d. \u2022 \u0424\u0430\u043a\u0442\u043e\u0440\u044b \u0432\u044b\u0431\u043e\u0440\u0430: \u0422\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0443\u043d\u0442\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438, \u0442\u0438\u043f \u0438 \u0441\u043e\u0441\u0442\u0430\u0432 \u0433\u0440\u0443\u043d\u0442\u0430, \u043a\u043b\u0438\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0434\u043e\u043b\u0433\u043e\u0432\u0435\u0447\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438. \u042d\u0442\u0438 \u043c\u0435\u0440\u044b \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u0443\u044e \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u044e \u0438 \u043f\u0440\u043e\u0434\u043b\u0438\u0442\u044c \u0441\u0440\u043e\u043a \u0441\u043b\u0443\u0436\u0431\u044b \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u0418\u0421\u041f\u041e\u041b\u042c\u0417\u041e\u0412\u0410\u041d\u041d\u042b\u0415 \u0418\u0421\u0422\u041e\u0427\u041d\u0418\u041a\u0418:<\/p>\n<ol>\n<li>\n<p>\u0424\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u044b \u0421\u041f 45.13330.2017<\/p>\n<\/li>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0434\u0430\u043d\u0438\u0439 \u0438 \u0441\u043e\u043e\u0440\u0443\u0436\u0435\u043d\u0438\u0439 \u0421\u041f 22.13330<\/p>\n<\/li>\n<\/ol>\n<h2>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438  <\/h2>\n<p>\u041f\u0440\u0438 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0438 RAG \u0432 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b:<\/p>\n<ol>\n<li>\n<p>\u0422\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0439\u0442\u0435 \u043f\u043e\u0440\u043e\u0433 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 (RELEVANCE_THRESHOLD) &#8212; \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043d\u0438\u0437\u043a\u0438\u0439 \u043f\u043e\u0440\u043e\u0433 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a &#171;\u0448\u0443\u043c\u0443&#187; \u0432 \u043e\u0442\u0432\u0435\u0442\u0430\u0445, \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u0441\u0435\u0447\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u0440\u043e\u043c\u043f\u0442-\u0438\u043d\u0436\u0438\u043d\u0438\u0440\u0438\u043d\u0433 \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 &#8212; \u0447\u0451\u0442\u043a\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0434\u043b\u044f LLM \u043c\u043e\u0433\u0443\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0421\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0433\u043e\u0432 \u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0448\u0430\u0433\u043e\u0432, \u0442\u0435\u043c \u0432\u044b\u0448\u0435 \u0448\u0430\u043d\u0441 \u0447\u0442\u043e, \u043a\u0430\u043a\u0430\u044f \u043b\u0438\u0431\u043e \u0438\u0437 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>RAG-\u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0432\u0430\u0436\u043d\u0435\u0439\u0448\u0438\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u043d\u0438\u044f\u043c\u0438 \u0438 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043e\u0431\u044a\u0435\u043c\u0430\u043c\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u0440\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0442\u0432\u0435\u0442\u043e\u0432.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <\/h2>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043c\u043e\u0436\u0435\u0442\u0442 \u0432\u0430\u043c \u0432 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0438 RAG \u0441\u0438\u0441\u0442\u0435\u043c, \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u043f\u043e\u0436\u0435\u043b\u0430\u043d\u0438\u044f.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e RAG-\u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c\u0438. \u0413\u043b\u0430\u0432\u043d\u044b\u043c\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430\u043c\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f:<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0440\u0435\u043e\u0434\u043e\u043b\u0435\u043d\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/strong> &#8212; RAG \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043e\u0431\u044a\u0435\u043c\u0430\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u044e\u0449\u0438\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0435 \u043e\u043a\u043d\u043e LLM.<\/p>\n<\/li>\n<li>\n<p><strong>\u0422\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/strong> &#8212; \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0445 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432, \u0447\u0442\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438<\/strong> &#8212; \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b DOCS_IN_RETRIEVER, RELEVANCE_THRESHOLD_DOCS \u0438 RELEVANCE_THRESHOLD_PROMPT \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0442\u043e\u043d\u043a\u043e \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043f\u043e\u0434 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043d\u0443\u0436\u0434\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c<\/strong> &#8212; \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u044b\u0441\u044f\u0447\u0430\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0443\u0442\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/904418\/\"> https:\/\/habr.com\/ru\/articles\/904418\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 RAG (Retrieval-Augmented Generation) \u043d\u0430 Python \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441 \u043e\u043f\u043e\u0440\u043e\u0439 \u043d\u0430 \u043d\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b: \u0421\u041d\u0438\u041f\u044b, \u0421\u041f, \u0413\u041e\u0421\u0422\u044b \u0438 \u0434\u0440\u0443\u0433\u0438\u0435. \u0413\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c Telegram-\u0431\u043e\u0442\u0435: <a href=\"https:\/\/t.me\/Pdflyx_bot\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/Pdflyx_bot<\/a> &#8212; \u0434\u0430\u043d\u043d\u0438\u0439 \u0431\u043e\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439, \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u0446\u0438\u0442\u0430\u0442\u044b \u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u044b\u043b\u0430 \u0432\u0437\u044f\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0444\u0435\u0440: \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0440\u0435\u0433\u043b\u0430\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043b\u044e\u0431\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0431\u0430\u0437 \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<h2>\u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 ChatGPT?  <\/h2>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f LLM-\u043c\u043e\u0434\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043e\u0431\u044a\u0451\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0435 \u043e\u043a\u043d\u043e ChatGPT \u2014 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e 8192 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 (20\u201330 \u0441\u0442\u0440\u0430\u043d\u0438\u0446). \u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0414\u0430\u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Gemini 2.5 Pro (1 \u043c\u043b\u043d \u0442\u043e\u043a\u0435\u043d\u043e\u0432), \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0442\u0435\u043d \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c RAG. \u042d\u0442\u0430 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043e\u0442\u0431\u0438\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0431\u043b\u0438\u0437\u043a\u0438\u0435 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u044f\u0433\u043a\u0438\u0439 \u043f\u043e\u0438\u0441\u043a (Soft Search), \u043c\u044b \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435 \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<h2>\u041e\u0431\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430:  <\/h2>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u043f\u043e\u0445\u043e\u0436\u0438\u0445 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u043c\u043f\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0437 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u0442\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430\u00a0 \u0438 \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 (\u0441 \u0446\u0438\u0442\u0430\u0442\u0430\u043c\u0438 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438).<\/p>\n<\/li>\n<\/ol>\n<h2>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f RAG-\u0441\u0438\u0441\u0442\u0435\u043c\u044b  <\/h2>\n<h2>1. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438.  <\/h2>\n<p> \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0431\u0430\u0437\u0443 FAISS (\u043f\u043e\u0434\u043e\u0439\u0434\u0443\u0442 \u0438 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b: Pinecone, AWS, Azure).  <\/p>\n<p><strong> \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:  <\/strong><\/p>\n<pre><code class=\"python\">import os from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from langchain.document_loaders import PyPDFLoader from langchain_openai import OpenAIEmbeddings from langchain_openai import ChatOpenAI<\/code><\/pre>\n<p><strong>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435: <\/strong><em> <\/em><\/p>\n<pre><code class=\"python\"># \u0412\u0430\u0448 API-\u043a\u043b\u044e\u0447 \u0434\u043b\u044f OpenAI OPENAI_API_KEY = \"Your_Open_API_Key\"  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0441 \u0432\u0430\u0448\u0438\u043c\u0438 PDF-\u0444\u0430\u0439\u043b\u0430\u043c\u0438 (\u0431\u0430\u0437\u0430 \u0437\u043d\u0430\u043d\u0438\u0439) folder_path = \"Path_To_Your_Knowledge_Base\"  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f (\u0438\u043b\u0438 \u0443\u0436\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f) FAISS-\u0438\u043d\u0434\u0435\u043a\u0441 vector_store_dir = \"Path_To_Your_Vector_Store\"  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c LLM \u0438 Embeddings (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 OpenAI) llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model_name=\"gpt-4o\") embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)  DOCS_IN_RETRIEVER = 15 RELEVANCE_THRESHOLD_DOCS = 0,7 RELEVANCE_THRESHOLD_PROMPT = 0,6<\/code><\/pre>\n<p><strong><em>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Vector Store.  <\/em><\/strong><\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e &#8212; \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 FAISS-\u0438\u043d\u0434\u0435\u043a\u0441 (vector_store) \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e:  <\/p>\n<pre><code class=\"python\">def save_vector_store(vector_store, vector_store_dir: str):    \"\"\"    :param vector_store: \u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 FAISS (VectorStore), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c    :param vector_store_dir: \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0438\u043d\u0434\u0435\u043a\u0441    \"\"\"     vector_store.save_local(vector_store_dir)    print(f\"Vector store \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0432: {vector_store_dir}\")<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f Vector Store, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u043e\u043c\u043f\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u0443\u044e \u0431\u0430\u0437\u0443.<\/p>\n<pre><code class=\"python\">def load_vector_store(vector_store_dir: str, embeddings):    # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 'index.faiss'    index_file = os.path.join(vector_store_dir, \"index.faiss\")    if not os.path.exists(index_file):        print(f\"\u0424\u0430\u0439\u043b {index_file} \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d. \u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c vector store.\")        return None    try:        vector_store = FAISS.load_local(vector_store_dir, embeddings, allow_dangerous_deserialization=True)        print(f\"Vector store \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0438\u0437: {vector_store_dir}\")        return vector_store    except Exception as e:        print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 vector store: {e}\")        return None <\/code><\/pre>\n<p> \u0418 \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f  \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f Vector Store &#8212; \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0447\u0430\u043d\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"python\">def load_and_index_documents(folder_path: str, vector_store_dir: str, embeddings) -&gt; bool:    vector_store = load_vector_store(vector_store_dir, embeddings)    if vector_store:        print(\"\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 vector store \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d.\")        return True     documents = []    if not os.path.isdir(folder_path):        print(f\"\u041f\u0430\u043f\u043a\u0430 {folder_path} \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430.\")        return False     for filename in os.listdir(folder_path):        if filename.lower().endswith(\".pdf\"):            pdf_path = os.path.join(folder_path, filename)            try:                loader = PyMuPDFLoader(pdf_path)                pdf_docs = loader.load()                documents.extend(pdf_docs)                print(f\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043e {len(pdf_docs)} \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438\u0437 {filename}\")            except Exception as e:                print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 {filename}: {e}\")     if not documents:        print(\"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 PDF-\u0444\u0430\u0439\u043b\u043e\u0432 \u0434\u043b\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.\")        return False     text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)    split_docs = text_splitter.split_documents(documents)    print(f\"\u0412\u0441\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e {len(split_docs)} \u0447\u0430\u043d\u043a\u043e\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f.\")     vector_store = FAISS.from_documents(split_docs, embeddings)    print(\"\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u0440\u043e\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 FAISS.\")     save_vector_store(vector_store, vector_store_dir)    return True<\/code><\/pre>\n<p> \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 PDF \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439.  <\/p>\n<h2>2. \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.  <\/h2>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u043a\u0440\u0430\u0439\u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0442\u0435\u043b\u044c\u043d\u0430, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u0438, \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0442\u0430\u043a \u0436\u0435 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u044d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<pre><code class=\"python\">def preprocess_user_prompt(user_prompt: str, chat_history: list, llm) -&gt; str:    \"\"\"    \u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u043f\u0440\u0435\u0434\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 LLM.    \"\"\"    instructions = (        \"Your task is to refine the user prompt below, preserving its meaning.\\n\"        \"Steps to follow:\\n\"        \"1. Identify the main question or request.\\n\"        \"2. If there are multiple tasks, list them.\\n\"        \"3. Keep the text concise and clear.\\n\\n\"        f\"User prompt:\\n{user_prompt}\\n\\n\"        \"Chat history:\\n\"        f\"{chat_history}\\n\"        \"-----\\n\"        \"Now, provide the improved prompt below:\\n\"    )    response = llm(instructions)    improved_prompt = response.content.strip()    return improved_prompt<\/code><\/pre>\n<h2>3. \u041f\u043e\u0438\u0441\u043a \u043f\u043e\u0445\u043e\u0436\u0438\u0445 \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439.  <\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0448\u0430\u0433 \u2014 \u043d\u0430\u0439\u0442\u0438 \u0432 \u0431\u0430\u0437\u0435 \u0437\u043d\u0430\u043d\u0438\u0439 \u0442\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430  .<\/p>\n<pre><code class=\"python\">def retrieve_documents(    vector_store,    user_prompt: str,    k: int = 20,    metadata_filters: dict = None ):    \"\"\"    \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c\u0443 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 FAISS (similarity search).    \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 (Document, score).    \"\"\"    if not vector_store:        print(\"Vector store \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441.\")        return []      try:        if metadata_filters:            docs_with_scores = vector_store.similarity_search_with_score(                user_prompt,                k=DOCS_IN_RETRIEVER,                filter=metadata_filters            )        else:            docs_with_scores = vector_store.similarity_search_with_score(user_prompt, k=k)        return docs_with_scores    except Exception as e:        print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432: {e}\")        return [] <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 k=20 \u2014 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u0445 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446.<\/p>\n<p>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0433\u0440\u043e\u0437\u0438\u0442 \u0442\u0435\u043c, \u0447\u0442\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u0451\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u0442\u044c, \u0430 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0441\u043d\u0438\u0436\u0435\u043d\u0438\u044e \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u0438. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c k=(15-20).<\/p>\n<h2>4. \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.  <\/h2>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u2018k\u2019 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0437\u043d\u0430\u043d\u0438\u0439, \u0434\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043d\u0438\u0445 \u0431\u043b\u0438\u0437\u043a\u0430 \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u0435\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0435.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u043c \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u0443\u044e \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 (\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b).<\/p>\n<pre><code class=\"python\">def compute_embeddings_similarity(embeddings, prompt: str, documents: list):    \"\"\"    \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0439 \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u0438    \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (prompt) \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.    \"\"\"    if not documents:        return []      try:        # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433 \u0434\u043b\u044f prompt        prompt_embedding = np.array(embeddings.embed_query(prompt))        relevance_scores = []          for doc in documents:            doc_embedding = np.array(embeddings.embed_query(doc.page_content))            # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432            if (prompt_embedding.size == 0 or doc_embedding.size == 0):                print(f\"Warning: invalid embedding for doc: {doc.metadata.get('source', 'Unknown')}\")                similarity = 0.0            else:                dot_product = np.dot(prompt_embedding, doc_embedding)                norm_prompt = np.linalg.norm(prompt_embedding)                norm_doc = np.linalg.norm(doc_embedding)                similarity = 0.0                # \u041a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u0430\u044f \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u044c = (A\u00b7B) \/ (|A| * |B|)                if norm_prompt &gt; 1e-9 and norm_doc &gt; 1e-9:                    similarity = dot_product \/ (norm_prompt * norm_doc)                # \u00ab\u0417\u0430\u0436\u0438\u043c\u0430\u0435\u043c\u00bb \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 [-1, 1] \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b \u043e\u0442 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0438\u0445 \u043f\u043e\u0433\u0440\u0435\u0448\u043d\u043e\u0441\u0442\u0435\u0439                similarity = np.clip(similarity, -1.0, 1.0)              relevance_scores.append((doc, similarity))          return relevance_scores      except Exception as e:        print(f\"Exception in compute_embeddings_similarity: {str(e)}\")        return [(doc, 0.0) for doc in documents] <\/code><\/pre>\n<p> \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u044d\u0442\u0430\u043f\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b.<\/p>\n<h2> 5. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430<\/h><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-457430","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=457430"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457430\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=457430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=457430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=457430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}