{"id":463273,"date":"2025-06-13T15:00:24","date_gmt":"2025-06-13T15:00:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=463273"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=463273","title":{"rendered":"<span>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 RAG: \u043e\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0441 LangGraph. \u0427\u0430\u0441\u0442\u044c 2<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u0431\u044a\u044f\u0441\u043d\u044e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f RAG (Retrieval-Augmented Generation), \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u0435\u0451 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a <strong><em>LangGraph\u00a0<\/em><\/strong>\u2014 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u044b \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b \u0432\u00a0<a href=\"https:\/\/habr.com\/ru\/articles\/907844\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a><\/p>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u0430\u0441 \u0436\u0434\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439  \u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0447\u0438\u0442\u044b\u0432\u0430\u0439\u0442\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430. <\/p>\n<h2>\u041a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d RAG<\/h2>\n<p>\u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f RAG \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u044f (<em>Indexing<\/em>)<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b <\/p>\n<\/li>\n<li>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f <\/p>\n<\/li>\n<li>\n<p>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f (<em>Retrieval and Generation<\/em>)<\/p>\n<ul>\n<li>\n<p>\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430 <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u042d\u0442\u0430\u043f \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 (Indexing)<\/h2>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u043e\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043f\u044f\u0442\u0438 \u0447\u0430\u0441\u0442\u0435\u0439: <\/p>\n<ol>\n<li>\n<p><strong>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (Load)<\/strong><\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>Document Loader<\/p>\n<\/li>\n<li>\n<p>PyPDF<\/p>\n<\/li>\n<li>\n<p>CSVLoader <\/p>\n<\/li>\n<li>\n<p>\u0438 \u0442.\u0434.<\/p>\n<p>\u0412\u044b\u0431\u043e\u0440 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u042f  \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  <em>Document Loader<\/em>, \u0442\u0430\u043a \u043a\u0430\u043a \u0431\u0443\u0434\u0443 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b<\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u0447\u0430\u0441\u0442\u0438. \u042d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0443 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430<\/p>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 LLM \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u043c \u043e\u043a\u043d\u043e\u043c \u0432 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0439\u0442\u0435 \u044d\u0442\u043e\u0442 \u0448\u0430\u0433. <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f (<em>Emdeddings<\/em>)<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430, \u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f embedding \u043c\u043e\u0434\u0435\u043b\u0438. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <\/p>\n<p><code><em>\"cointegrated\/LaBSE-en-ru\" \u0441 HuggingFace<\/em><\/code><\/p>\n<\/li>\n<li>\n<p>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 (Store)<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0411\u0414 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, FAISS, Chroma)<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ff9\/b8c\/1b7\/ff9b8c1b7da753ab51cf7848b8f74014.png\" alt=\"\u041f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c\" title=\"\u041f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c\" width=\"2583\" height=\"1299\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/ff9\/b8c\/1b7\/ff9b8c1b7da753ab51cf7848b8f74014.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ff9\/b8c\/1b7\/ff9b8c1b7da753ab51cf7848b8f74014.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c. Retrieval and generation  <\/p>\n<ol>\n<li>\n<p>Retrieve. \u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0440\u0435\u0442\u0440\u0438\u0432\u0435\u0440\u044b. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/876844\/\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>Generation. \u0417\u0430\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0448\u0430\u0433, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 prompt \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043e\u0442\u0432\u0435\u0442.  <\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/86e\/129\/561\/86e129561749ac63a51de93f98281ff5.png\" alt=\"\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c\" title=\"\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c\" width=\"2532\" height=\"1299\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/86e\/129\/561\/86e129561749ac63a51de93f98281ff5.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/86e\/129\/561\/86e129561749ac63a51de93f98281ff5.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<\/li>\n<\/ol>\n<p><strong><em>\u0411\u043e\u043b\u044c\u0448\u0435 \u043e  AI \u0438 NLP \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u043c\u043e\u0435\u043c \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u043c \u043a\u0430\u043d\u0430\u043b\u0435:<\/em><\/strong><\/p>\n<p><a href=\"https:\/\/t.me\/Viacheslav_Talks\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/Viacheslav_Talks<\/a><\/p>\n<h2>\u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 <\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c langchain, langgraph<\/p>\n<pre><code class=\"python\">!pip install langgraph langchain langchain_core langchain_community<\/code><\/pre>\n<pre><code class=\"python\">!pip install --quiet -U langchain_openai langchain_core langgraph langgraph-prebuilt <\/code><\/pre>\n<p>\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438<\/p>\n<pre><code class=\"python\">!pip install langchain-gigachat<\/code><\/pre>\n<pre><code class=\"python\">!pip install langchain_huggingface<\/code><\/pre>\n<h2>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c<\/h2>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u043e \u0420\u043e\u0441\u0441\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 4000 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. <\/p>\n<p>\u0424\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e <a href=\"https:\/\/drive.google.com\/file\/d\/1eVDiR0aQhWRuxUXpVlFJzqTbvoDfWJFB\/view?usp=sharing\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<pre><code class=\"python\">from langchain_community.document_loaders import TextLoader from pprint import pprint   loader  = TextLoader(\"\/content\/Ruusai.txt\") documents  = loader.load() #\u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0431\u044c\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 Document<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c <em>RecursiveCharacterTextSplitter <\/em>\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438: <\/p>\n<ul>\n<li>\n<p><em>chunk_size<\/em>=1000 (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435)<\/p>\n<\/li>\n<li>\n<p><em>chunk_overlap<\/em>=100 (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435)<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>InMemoryVectorStore <\/em>\u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u044b. <\/p>\n<pre><code class=\"python\">from langchain_core.vectorstores import InMemoryVectorStore from langchain_huggingface import HuggingFaceEmbeddings import os   HF_TOKEN = '\u0412\u0410\u0428 \u0422\u041e\u041a\u0415\u041d' os.environ['HF_TOKEN'] = HF_TOKEN embeddings_model_name  = \"cointegrated\/LaBSE-en-ru\"  model_embed = HuggingFaceEmbeddings(     model_name=embeddings_model_name ) vec_store  = InMemoryVectorStore(model_embed)  _ = vec_store.add_documents(chunks) #\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u0440\u0430\u043c\u0433\u0435\u043d\u0442\u044b \u0432 \u0431\u0430\u0437\u0443<\/code><\/pre>\n<h2>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>GigaChat<\/em>.<\/p>\n<pre><code class=\"python\">from langchain_gigachat import GigaChat  llm  = GigaChat(                 verify_ssl_certs=False,                  credentials=\"\u0412\u0410\u0429 \u041a\u041b\u042e\u0427\",                 model=\"GigaChat-2\" )<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430. \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e 3 \u043f\u043e\u043b\u044f: <\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">from typing import TypedDict   class State(TypedDict):   question: str   context: list[str]   answer: str <\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0433\u0440\u0430\u0444\u0430. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u0432\u0443\u0445 \u0443\u0437\u043b\u043e\u0432, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c <em>RAG<\/em>:  <\/p>\n<ol>\n<li>\n<p><strong>Retrieval Node<\/strong>\u00a0&#8212; \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p><strong>Generation Node<\/strong>\u00a0&#8212; \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">from langchain_core.documents import Document from langchain_core.prompts import ChatPromptTemplate   def retrieve(state: State):   retrieved_docs: List[Document]  = vec_store.similarity_search_with_score(state[\"question\"])   retrieved_content  = [doc[0].page_content for doc in retrieved_docs]   return {\"context\": retrieved_content}   def generate_answer(state: State):    system_prompt  = \"\"\"   \u0422\u044b  - \u0443\u043c\u043d\u044b\u0439 \u0430\u0441\u0441\u0438\u0441\u0442\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.   \u0414\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442.    \u041d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442: {context}   \"\"\"    prompt  = ChatPromptTemplate.from_messages(       [           (\"system\", system_prompt),           (\"human\", state[\"question\"])       ]   )    chain  = prompt | llm   answer  = chain.invoke({\"context\": state[\"context\"]})   return {\"answer\": answer}<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END   builder  = StateGraph(State) builder.add_node(\"retrieve\", retrieve) builder.add_node(\"generate_answer\", generate_answer)  builder.add_edge(START, \"retrieve\") builder.add_edge(\"retrieve\", \"generate_answer\") builder.add_edge(\"generate_answer\", END)<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/p>\n<pre><code class=\"python\">from IPython.display import Image, display   graph  = builder.compile() display(Image(graph.get_graph().draw_mermaid_png()))<\/code><\/pre>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a8a\/237\/b95\/a8a237b95e35287f53403a0cf25ccbfd.png\" width=\"170\" height=\"333\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/a8a\/237\/b95\/a8a237b95e35287f53403a0cf25ccbfd.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a8a\/237\/b95\/a8a237b95e35287f53403a0cf25ccbfd.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"python\">from pprint import pprint   question  = \"\u0432 \u043a\u0430\u043a\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0420\u043e\u0441\u0441\u0438\u044f?\"  answer  = graph.invoke({\"question\": question}) pprint(answer)<\/code><\/pre>\n<pre><code>content='\u0420\u043e\u0441\u0441\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445:\\n- \u041e\u041e\u041d\\n- G20\\n- \u0415\u0410\u042d\u0421\\n- \u0421\u041d\u0413\\n- \u041e\u0414\u041a\u0411\\n- \u0412\u0422\u041e\\n- \u041e\u0411\u0421\u0415\\n- \u0428\u041e\u0421\\n- \u0410\u0422\u042d\u0421\\n- \u0411\u0420\u0418\u041a\u0421\\n- \u041c\u041e\u041a\\n\\n\u0438 \u0434\u0440\u0443\u0433\u0438\u0445.' additional_kwargs={} response_metadata={'token_usage': {'prompt_tokens': 524, 'completion_tokens': 56, 'total_tokens': 580, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': 'c60ef6da-fc1a-424b-83a4-b99d441bcf22', 'x-session-id': '5dc8944f-3355-44e1-a57b-e91728509bca', 'x-client-id': None}, 'finish_reason': 'stop'} id='c60ef6da-fc1a-424b-83a4-b99d441bcf22' usage_metadata={'output_tokens': 56, 'input_tokens': 524, 'total_tokens': 580, 'input_token_details': {'cache_read': 2}}  {'answer': AIMessage(content='\u0420\u043e\u0441\u0441\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445:\\n- \u041e\u041e\u041d\\n- G20\\n- \u0415\u0410\u042d\u0421\\n- \u0421\u041d\u0413\\n- \u041e\u0414\u041a\u0411\\n- \u0412\u0422\u041e\\n- \u041e\u0411\u0421\u0415\\n- \u0428\u041e\u0421\\n- \u0410\u0422\u042d\u0421\\n- \u0411\u0420\u0418\u041a\u0421\\n- \u041c\u041e\u041a\\n\\n\u0438 \u0434\u0440\u0443\u0433\u0438\u0445.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 524, 'completion_tokens': 56, 'total_tokens': 580, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': 'c60ef6da-fc1a-424b-83a4-b99d441bcf22', 'x-session-id': '5dc8944f-3355-44e1-a57b-e91728509bca', 'x-client-id': None}, 'finish_reason': 'stop'}, id='c60ef6da-fc1a-424b-83a4-b99d441bcf22', usage_metadata={'output_tokens': 56, 'input_tokens': 524, 'total_tokens': 580, 'input_token_details': {'cache_read': 2}}),  'context': ['\u0420\u043e\u0441\u0441\u0438\u044f \u2014 \u043c\u043d\u043e\u0433\u043e\u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u043e \u0441 \u0448\u0438\u0440\u043e\u043a\u0438\u043c \u044d\u0442\u043d\u043e\u043a\u0443\u043b\u044c\u0442\u0443\u0440\u043d\u044b\u043c '              '\u043c\u043d\u043e\u0433\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435\u043c[20]. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0438 \u043d\u0430\u0441\u0435\u043b\u0435\u043d\u0438\u044f '              '\u0420\u043e\u0441\u0441\u0438\u0438 2020\u20142021 \u0433\u043e\u0434\u0430, \u0432 \u0441\u0442\u0440\u0430\u043d\u0435 \u0436\u0438\u0432\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u0438 \u0441\u0432\u044b\u0448\u0435 190 '              ...........................................................<\/code><\/pre>\n<pre><code class=\"python\">question  = \"\u043a\u0430\u043a\u0438\u043c\u0438 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044f\u043c\u0438 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u0418\u043b\u043e\u043d \u041c\u0430\u0441\u043a?\"  answer  = graph.invoke({\"question\": question}) pprint(answer)<\/code><\/pre>\n<pre><code class=\"python\"> content='\u0418\u0437 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441.' additional_kwargs={} response_metadata={'token_usage': {'prompt_tokens': 58, 'completion_tokens': 13, 'total_tokens': 71, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': '3537302b-ae38-4e82-97cb-affde8772260', 'x-session-id': '27a801e4-4f7e-4e20-a4c1-f62a88bcc8c6', 'x-client-id': None}, 'finish_reason': 'stop'} id='3537302b-ae38-4e82-97cb-affde8772260' usage_metadata={'output_tokens': 13, 'input_tokens': 58, 'total_tokens': 71, 'input_token_details': {'cache_read': 2}}  {'answer': AIMessage(content='\u0418\u0437 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 58, 'completion_tokens': 13, 'total_tokens': 71, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': '3537302b-ae38-4e82-97cb-affde8772260', 'x-session-id': '27a801e4-4f7e-4e20-a4c1-f62a88bcc8c6', 'x-client-id': None}, 'finish_reason': 'stop'}, id='3537302b-ae38-4e82-97cb-affde8772260', usage_metadata={'output_tokens': 13, 'input_tokens': 58, 'total_tokens': 71, 'input_token_details': {'cache_read': 2}}),  'context': [],<\/code><\/pre>\n<h2>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438<\/h2>\n<p>\u042f \u0440\u0435\u0448\u0438\u043b \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 <em>TavilySearch<\/em>.<\/p>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 \u0433\u0440\u0430\u0444\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432\u044b\u0448\u0435. \u0415\u0441\u043b\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043d\u0435 \u0431\u044b\u043b\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u0435, \u0442\u043e \u0430\u0433\u0435\u043d\u0442 \u0441\u043f\u0440\u043e\u0441\u0438\u0442 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435. <\/p>\n<p> \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"python\">!pip install langchain_tavily<\/code><\/pre>\n<pre><code class=\"python\">tavily_api_key  = \"\u0412\u0410\u0428 \u041a\u041b\u042e\u0427. \u0415\u0413\u041e \u041c\u041e\u0416\u041d\u041e \u041f\u041e\u041b\u0423\u0427\u0418\u0422\u042c \u0411\u0415\u0421\u041f\u041b\u0410\u0422\u041d\u041e \u041d\u0410 \u0421\u0410\u0419\u0422\u0415 TAVILY\" os.environ[\"TAVILY_API_KEY\"] = tavily_api_key <\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u044f \u0441\u043e\u0437\u0434\u0430\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044e \u0440\u0430\u0431\u043e\u0442\u0443<\/p>\n<pre><code class=\"python\">from langchain_tavily import TavilySearch  search_tool  = TavilySearch(     max_results = 5 )  answer_tool  = search_tool.invoke(\"\u0420\u043e\u0441\u0441\u0438\u044f\") content  = [ans[\"content\"] for ans in answer_tool[\"results\"]] pprint(content)<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0438 \u0443\u0437\u043b\u044b. \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435<\/p>\n<\/li>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0440\u043e\u0433 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e \u0440\u0430\u0432\u043d\u044b\u043c 0.4 <\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">from typing import TypedDict from langgraph.types import interrupt, Command   class State(TypedDict):   question: str   context: list[str]   answer: str<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438 <\/p>\n<pre><code class=\"python\">builder  = StateGraph(State) builder.add_node(\"retrieve\", retrieve) builder.add_node(\"generate_answer\", generate_answer) builder.add_node(\"web_search\", web_search)  builder.add_edge(START, \"retrieve\") #\u0434\u043b\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 builder.add_conditional_edges(     \"retrieve\",     use_search,     {         \"web_search\": \"web_search\",         \"generate_answer\": \"generate_answer\"     } )  builder.add_edge(\"web_search\", \"generate_answer\") builder.add_edge(\"generate_answer\", END)<\/code><\/pre>\n<pre><code class=\"python\">graph  = builder.compile() display(Image(graph.get_graph().draw_mermaid_png()))<\/code><\/pre>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/83a\/011\/da1\/83a011da19bc755f26ed15378d242956.png\" width=\"198\" height=\"432\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/83a\/011\/da1\/83a011da19bc755f26ed15378d242956.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/83a\/011\/da1\/83a011da19bc755f26ed15378d242956.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u0412 <em>use_search <\/em>\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <em>interrupt<\/em>. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f interrupt \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0433\u0440\u0430\u0444\u0430 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 <em>input<\/em>, \u043d\u043e \u0441 \u043e\u0434\u043d\u0438\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c. <em>Input <\/em>\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441 \u043c\u0435\u0441\u0442\u0430 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f. <em>Interrupt <\/em>\u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 <strong>\u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0437\u043b\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435<\/strong>. <\/p>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/strong>. \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0443\u0436\u043d\u043e \u0435\u0441\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430 \u0441 <em>interrupt <\/em>\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u0448\u0430\u0433\u0438. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>InMemorySaver<\/em>. <\/p>\n<\/li>\n<li>\n<p>\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u0430\u043c\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043e\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u044b\u0437\u0432\u0430\u043d\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0437\u0430\u043d\u043e\u0432\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0433\u0440\u0430\u0444 \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u043c.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">checkpointer = InMemorySaver() graph  = builder.compile(checkpointer=checkpointer)  config = {\"configurable\": {\"thread_id\": 1}} result = graph.invoke({\"question\": \"\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0431\u044c\u0435\u043a\u0442\u043e\u0432 \u0432 \u0440\u043e\u0441\u0441\u0438\u0438?\"}, config=config)  if result.get(\"__interrupt__\", None):   age  = input(\"\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u044b. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 (accept)?\")   final_result  = graph.invoke(Command(resume=str(age)), config=config)   pprint(final_result)<\/code><\/pre>\n<pre><code class=\"python\">content='\u0412 \u0441\u043e\u0441\u0442\u0430\u0432 \u0420\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u043e\u0439 \u0424\u0435\u0434\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u044f\u0442 89 \u0441\u0443\u0431\u044a\u0435\u043a\u0442\u043e\u0432.'.....<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044f \u0437\u0430\u043a\u043e\u043d\u0447\u0443 \u0441\u0442\u0430\u0442\u044c\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435!<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0431\u044b\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0438 \u0432\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u0447\u0442\u043e \u0442\u043e \u043d\u043e\u0432\u043e\u0435, \u043f\u043e\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435. <\/p>\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\/918048\/\"> https:\/\/habr.com\/ru\/articles\/918048\/<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u0431\u044a\u044f\u0441\u043d\u044e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f RAG (Retrieval-Augmented Generation), \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u0435\u0451 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a <strong><em>LangGraph\u00a0<\/em><\/strong>\u2014 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u044b \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b \u0432\u00a0<a href=\"https:\/\/habr.com\/ru\/articles\/907844\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a><\/p>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u0430\u0441 \u0436\u0434\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439  \u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0447\u0438\u0442\u044b\u0432\u0430\u0439\u0442\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430. <\/p>\n<h2>\u041a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d RAG<\/h2>\n<p>\u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f RAG \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u044f (<em>Indexing<\/em>)<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b <\/p>\n<\/li>\n<li>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f <\/p>\n<\/li>\n<li>\n<p>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f (<em>Retrieval and Generation<\/em>)<\/p>\n<ul>\n<li>\n<p>\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430 <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u042d\u0442\u0430\u043f \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 (Indexing)<\/h2>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u043e\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043f\u044f\u0442\u0438 \u0447\u0430\u0441\u0442\u0435\u0439: <\/p>\n<ol>\n<li>\n<p><strong>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (Load)<\/strong><\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>Document Loader<\/p>\n<\/li>\n<li>\n<p>PyPDF<\/p>\n<\/li>\n<li>\n<p>CSVLoader <\/p>\n<\/li>\n<li>\n<p>\u0438 \u0442.\u0434.<\/p>\n<p>\u0412\u044b\u0431\u043e\u0440 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u042f  \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  <em>Document Loader<\/em>, \u0442\u0430\u043a \u043a\u0430\u043a \u0431\u0443\u0434\u0443 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b<\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u0447\u0430\u0441\u0442\u0438. \u042d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0443 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430<\/p>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 LLM \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u043c \u043e\u043a\u043d\u043e\u043c \u0432 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0439\u0442\u0435 \u044d\u0442\u043e\u0442 \u0448\u0430\u0433. <\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f (<em>Emdeddings<\/em>)<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430, \u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f embedding \u043c\u043e\u0434\u0435\u043b\u0438. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <\/p>\n<p><code><em>\"cointegrated\/LaBSE-en-ru\" \u0441 HuggingFace<\/em><\/code><\/p>\n<\/li>\n<li>\n<p>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 (Store)<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0411\u0414 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, FAISS, Chroma)<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c. Retrieval and generation  <\/p>\n<ol>\n<li>\n<p>Retrieve. \u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0440\u0435\u0442\u0440\u0438\u0432\u0435\u0440\u044b. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/876844\/\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>Generation. \u0417\u0430\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0448\u0430\u0433, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 prompt \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043e\u0442\u0432\u0435\u0442.  <\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<\/li>\n<\/ol>\n<p><strong><em>\u0411\u043e\u043b\u044c\u0448\u0435 \u043e  AI \u0438 NLP \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u043c\u043e\u0435\u043c \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u043c \u043a\u0430\u043d\u0430\u043b\u0435:<\/em><\/strong><\/p>\n<p><a href=\"https:\/\/t.me\/Viacheslav_Talks\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/Viacheslav_Talks<\/a><\/p>\n<h2>\u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 <\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c langchain, langgraph<\/p>\n<pre><code class=\"python\">!pip install langgraph langchain langchain_core langchain_community<\/code><\/pre>\n<pre><code class=\"python\">!pip install --quiet -U langchain_openai langchain_core langgraph langgraph-prebuilt <\/code><\/pre>\n<p>\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438<\/p>\n<pre><code class=\"python\">!pip install langchain-gigachat<\/code><\/pre>\n<pre><code class=\"python\">!pip install langchain_huggingface<\/code><\/pre>\n<h2>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c<\/h2>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u043e \u0420\u043e\u0441\u0441\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 4000 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. <\/p>\n<p>\u0424\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e <a href=\"https:\/\/drive.google.com\/file\/d\/1eVDiR0aQhWRuxUXpVlFJzqTbvoDfWJFB\/view?usp=sharing\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<pre><code class=\"python\">from langchain_community.document_loaders import TextLoader from pprint import pprint   loader  = TextLoader(\"\/content\/Ruusai.txt\") documents  = loader.load() #\u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0431\u044c\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 Document<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c <em>RecursiveCharacterTextSplitter <\/em>\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438: <\/p>\n<ul>\n<li>\n<p><em>chunk_size<\/em>=1000 (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435)<\/p>\n<\/li>\n<li>\n<p><em>chunk_overlap<\/em>=100 (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435)<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>InMemoryVectorStore <\/em>\u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u044b. <\/p>\n<pre><code class=\"python\">from langchain_core.vectorstores import InMemoryVectorStore from langchain_huggingface import HuggingFaceEmbeddings import os   HF_TOKEN = '\u0412\u0410\u0428 \u0422\u041e\u041a\u0415\u041d' os.environ['HF_TOKEN'] = HF_TOKEN embeddings_model_name  = \"cointegrated\/LaBSE-en-ru\"  model_embed = HuggingFaceEmbeddings(     model_name=embeddings_model_name ) vec_store  = InMemoryVectorStore(model_embed)  _ = vec_store.add_documents(chunks) #\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u0440\u0430\u043c\u0433\u0435\u043d\u0442\u044b \u0432 \u0431\u0430\u0437\u0443<\/code><\/pre>\n<h2>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>GigaChat<\/em>.<\/p>\n<pre><code class=\"python\">from langchain_gigachat import GigaChat  llm  = GigaChat(                 verify_ssl_certs=False,                  credentials=\"\u0412\u0410\u0429 \u041a\u041b\u042e\u0427\",                 model=\"GigaChat-2\" )<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430. \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e 3 \u043f\u043e\u043b\u044f: <\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">from typing import TypedDict   class State(TypedDict):   question: str   context: list[str]   answer: str <\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0433\u0440\u0430\u0444\u0430. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u0432\u0443\u0445 \u0443\u0437\u043b\u043e\u0432, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c <em>RAG<\/em>:  <\/p>\n<ol>\n<li>\n<p><strong>Retrieval Node<\/strong>\u00a0&#8212; \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p><strong>Generation Node<\/strong>\u00a0&#8212; \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">from langchain_core.documents import Document from langchain_core.prompts import ChatPromptTemplate   def retrieve(state: State):   retrieved_docs: List[Document]  = vec_store.similarity_search_with_score(state[\"question\"])   retrieved_content  = [doc[0].page_content for doc in retrieved_docs]   return {\"context\": retrieved_content}   def generate_answer(state: State):    system_prompt  = \"\"\"   \u0422\u044b  - \u0443\u043c\u043d\u044b\u0439 \u0430\u0441\u0441\u0438\u0441\u0442\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.   \u0414\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442.    \u041d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442: {context}   \"\"\"    prompt  = ChatPromptTemplate.from_messages(       [           (\"system\", system_prompt),           (\"human\", state[\"question\"])       ]   )    chain  = prompt | llm   answer  = chain.invoke({\"context\": state[\"context\"]})   return {\"answer\": answer}<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END   builder  = StateGraph(State) builder.add_node(\"retrieve\", retrieve) builder.add_node(\"generate_answer\", generate_answer)  builder.add_edge(START, \"retrieve\") builder.add_edge(\"retrieve\", \"generate_answer\") builder.add_edge(\"generate_answer\", END)<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/p>\n<pre><code class=\"python\">from IPython.display import Image, display   graph  = builder.compile() display(Image(graph.get_graph().draw_mermaid_png()))<\/code><\/pre>\n<figure class=\"\"><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"python\">from pprint import pprint   question  = \"\u0432 \u043a\u0430\u043a\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0420\u043e\u0441\u0441\u0438\u044f?\"  answer  = graph.invoke({\"question\": question}) pprint(answer)<\/code><\/pre>\n<pre><code>content='\u0420\u043e\u0441\u0441\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445:\\n- \u041e\u041e\u041d\\n- G20\\n- \u0415\u0410\u042d\u0421\\n- \u0421\u041d\u0413\\n- \u041e\u0414\u041a\u0411\\n- \u0412\u0422\u041e\\n- \u041e\u0411\u0421\u0415\\n- \u0428\u041e\u0421\\n- \u0410\u0422\u042d\u0421\\n- \u0411\u0420\u0418\u041a\u0421\\n- \u041c\u041e\u041a\\n\\n\u0438 \u0434\u0440\u0443\u0433\u0438\u0445.' additional_kwargs={} response_metadata={'token_usage': {'prompt_tokens': 524, 'completion_tokens': 56, 'total_tokens': 580, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': 'c60ef6da-fc1a-424b-83a4-b99d441bcf22', 'x-session-id': '5dc8944f-3355-44e1-a57b-e91728509bca', 'x-client-id': None}, 'finish_reason': 'stop'} id='c60ef6da-fc1a-424b-83a4-b99d441bcf22' usage_metadata={'output_tokens': 56, 'input_tokens': 524, 'total_tokens': 580, 'input_token_details': {'cache_read': 2}}  {'answer': AIMessage(content='\u0420\u043e\u0441\u0441\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f\u0445:\\n- \u041e\u041e\u041d\\n- G20\\n- \u0415\u0410\u042d\u0421\\n- \u0421\u041d\u0413\\n- \u041e\u0414\u041a\u0411\\n- \u0412\u0422\u041e\\n- \u041e\u0411\u0421\u0415\\n- \u0428\u041e\u0421\\n- \u0410\u0422\u042d\u0421\\n- \u0411\u0420\u0418\u041a\u0421\\n- \u041c\u041e\u041a\\n\\n\u0438 \u0434\u0440\u0443\u0433\u0438\u0445.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 524, 'completion_tokens': 56, 'total_tokens': 580, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': 'c60ef6da-fc1a-424b-83a4-b99d441bcf22', 'x-session-id': '5dc8944f-3355-44e1-a57b-e91728509bca', 'x-client-id': None}, 'finish_reason': 'stop'}, id='c60ef6da-fc1a-424b-83a4-b99d441bcf22', usage_metadata={'output_tokens': 56, 'input_tokens': 524, 'total_tokens': 580, 'input_token_details': {'cache_read': 2}}),  'context': ['\u0420\u043e\u0441\u0441\u0438\u044f \u2014 \u043c\u043d\u043e\u0433\u043e\u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u043e \u0441 \u0448\u0438\u0440\u043e\u043a\u0438\u043c \u044d\u0442\u043d\u043e\u043a\u0443\u043b\u044c\u0442\u0443\u0440\u043d\u044b\u043c '              '\u043c\u043d\u043e\u0433\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435\u043c[20]. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0438 \u043d\u0430\u0441\u0435\u043b\u0435\u043d\u0438\u044f '              '\u0420\u043e\u0441\u0441\u0438\u0438 2020\u20142021 \u0433\u043e\u0434\u0430, \u0432 \u0441\u0442\u0440\u0430\u043d\u0435 \u0436\u0438\u0432\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u0438 \u0441\u0432\u044b\u0448\u0435 190 '              ...........................................................<\/code><\/pre>\n<pre><code class=\"python\">question  = \"\u043a\u0430\u043a\u0438\u043c\u0438 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044f\u043c\u0438 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u0418\u043b\u043e\u043d \u041c\u0430\u0441\u043a?\"  answer  = graph.invoke({\"question\": question}) pprint(answer)<\/code><\/pre>\n<pre><code class=\"python\"> content='\u0418\u0437 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441.' additional_kwargs={} response_metadata={'token_usage': {'prompt_tokens': 58, 'completion_tokens': 13, 'total_tokens': 71, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': '3537302b-ae38-4e82-97cb-affde8772260', 'x-session-id': '27a801e4-4f7e-4e20-a4c1-f62a88bcc8c6', 'x-client-id': None}, 'finish_reason': 'stop'} id='3537302b-ae38-4e82-97cb-affde8772260' usage_metadata={'output_tokens': 13, 'input_tokens': 58, 'total_tokens': 71, 'input_token_details': {'cache_read': 2}}  {'answer': AIMessage(content='\u0418\u0437 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 58, 'completion_tokens': 13, 'total_tokens': 71, 'precached_prompt_tokens': 2}, 'model_name': 'GigaChat-2:2.0.28.2', 'x_headers': {'x-request-id': '3537302b-ae38-4e82-97cb-affde8772260', 'x-session-id': '27a801e4-4f7e-4e20-a4c1-f62a88bcc8c6', 'x-client-id': None}, 'finish_reason': 'stop'}, id='3537302b-ae38-4e82-97cb-affde8772260', usage_metadata={'output_tokens': 13, 'input_tokens': 58, 'total_tokens': 71, 'input_token_details': {'cache_read': 2}}),  'context': [],<\/code><\/pre>\n<h2>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438<\/h2>\n<p>\u042f \u0440\u0435\u0448\u0438\u043b \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 <em>TavilySearch<\/em>.<\/p>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 \u0433\u0440\u0430\u0444\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432\u044b\u0448\u0435. \u0415\u0441\u043b\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043d\u0435 \u0431\u044b\u043b\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0439 \u0431\u0430\u0437\u0435, \u0442\u043e \u0430\u0433\u0435\u043d\u0442 \u0441\u043f\u0440\u043e\u0441\u0438\u0442 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435. <\/p>\n<p> \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"python\">!pip install langchain_tavily<\/code><\/pre>\n<pre><code class=\"python\">tavily_api_key  = \"\u0412\u0410\u0428 \u041a\u041b\u042e\u0427. \u0415\u0413\u041e \u041c\u041e\u0416\u041d\u041e \u041f\u041e\u041b\u0423\u0427\u0418\u0422\u042c \u0411\u0415\u0421\u041f\u041b\u0410\u0422\u041d\u041e \u041d\u0410 \u0421\u0410\u0419\u0422\u0415 TAVILY\" os.environ[\"TAVILY_API_KEY\"] = tavily_api_key <\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u044f \u0441\u043e\u0437\u0434\u0430\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044e \u0440\u0430\u0431\u043e\u0442\u0443<\/p>\n<pre><code class=\"python\">from langchain_tavily import TavilySearch  search_tool  = TavilySearch(     max_results = 5 )  answer_tool  = search_tool.invoke(\"\u0420\u043e\u0441\u0441\u0438\u044f\") content  = [ans[\"content\"] for ans in answer_tool[\"results\"]] pprint(content)<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0438 \u0443\u0437\u043b\u044b. \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435<\/p>\n<\/li>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0440\u043e\u0433 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e \u0440\u0430\u0432\u043d\u044b\u043c 0.4 <\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">from typing import TypedDict from langgraph.types import interrupt, Command   class State(TypedDict):   question: str   context: list[str]   answer: str<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438 <\/p>\n<pre><code class=\"python\">builder  = StateGraph(State) builder.add_node(\"retrieve\", retrieve) builder.add_node(\"generate_answer\", generate_answer) builder.add_node(\"web_search\", web_search)  builder.add_edge(START, \"retrieve\") #\u0434\u043b\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430<\/code><\/pre>\n<\/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-463273","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463273","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=463273"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463273\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}