{"id":458962,"date":"2025-05-08T09:00:40","date_gmt":"2025-05-08T09:00:40","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=458962"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=458962","title":{"rendered":"<span>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 RAG: \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 LangGraph<\/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 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0433\u043e\u0434\u044b \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 d\u0441\u0442\u0430\u043b\u0438 \u043f\u043e\u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u044b <em>RAG<\/em>(<strong>Retrieval Augmented Generation<\/strong> \u0438\u043b\u0438 &#171;<strong>\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u043e\u0439<\/strong>&#171;). <\/p>\n<p>\u0418\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439\u00a0 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439\/\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a. <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0443\u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0442\u0430\u043a\u0438\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u043b\u0438\u0441\u044c \u0432 \u0441\u043b\u0443\u0436\u0431\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438\u043b\u0438 \u044e\u0440\u0438\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0435\/\u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438. <\/p>\n<p>\u0412 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043e\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b <em>RAG<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e\u00a0<strong><em>LangGraph<\/em><\/strong><em>\u00a0<\/em>\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u043c\u044b \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 <em>RAG<\/em>-\u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044e \u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.  <\/p>\n<p><strong>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \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\">pip install langgraph langchain langchain_core langchain_community<\/code><\/pre>\n<h2>\u041a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:\u00a0<strong>\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u0442\u0430\u043f\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0443\u0437\u043b\u044b<\/strong>\u00a0\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u0422\u0430\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<strong>\u0433\u0440\u0430\u0444\u043e\u043c<\/strong>.<\/p>\n<p>\u041e\u0442\u0441\u044e\u0434\u0430 \u0432\u044b\u0442\u0435\u043a\u0430\u0435\u0442 \u0432\u0430\u0436\u043d\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u0443 \u043b\u044e\u0431\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c\u00a0<strong>\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (<em>State<\/em>)<\/strong>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. <em>State <\/em>\u0441\u043b\u0443\u0436\u0438\u0442\u00a0<strong>\u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u043e\u0439 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432<\/strong>\u00a0\u0433\u0440\u0430\u0444\u0430.<\/p>\n<h2>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0438\u0437 3-\u0445 \u0443\u0437\u043b\u043e\u0432, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435: &#8216;\u042f \u043b\u044e\u0431\u043b\u044e \u041c\u043e\u0441\u043a\u0432\u0443!&#8217;. <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <em>TypedDict<\/em>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0435:  <em>graph_state<\/em>. \u041f\u043e\u043c\u0438\u043c\u043e <em>TypedDict <\/em>\u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>Pydantic<\/em>, <em>dataclass<\/em>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"python\">from typing_extensions import TypedDict  class State(TypedDict):     graph_state: str<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0443\u0437\u043b\u044b:<\/p>\n<pre><code class=\"python\">def node_1(state):     print(\"---Node 1---\")     return {\"graph_state\": state['graph_state'] +\"\u042f\"}  def node_2(state):     print(\"---Node 2---\")     return {\"graph_state\": state['graph_state'] +\"\u043b\u044e\u0431\u043b\u044e\"}  def node_3(state):     print(\"---Node 3---\")     return {\"graph_state\": state['graph_state'] +\"\u041c\u043e\u0441\u043a\u0432\u0443!\"}<\/code><\/pre>\n<p>\u041f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435: \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0443\u0437\u043b\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0432\u044b\u0448\u0435 (\u043d\u043e \u0443\u0437\u043b\u044b \u043f\u043e\u043a\u0430 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u043d\u0430\u044e\u0442). \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <em>graph_state<\/em>. <\/p>\n<p>\u0421\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u043b\u044b \u0432 \u0433\u0440\u0430\u0444 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438:<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END  builder = StateGraph(State) builder.add_node(\"node_1\", node_1) builder.add_node(\"node_2\", node_2) builder.add_node(\"node_3\", node_3)<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0443\u0437\u043b\u044b, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u0430:<\/p>\n<pre><code class=\"python\">builder.add_edge(START, \"node_1\") builder.add_edge(\"node_1\", \"node_2\") builder.add_edge(\"node_2\", \"node_3\") builder.add_edge(\"node_3\", END)<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0434\u0432\u0430 \u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u0430- <em>START<\/em>, <em>END<\/em>. \u041e\u043d\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442 \u0432\u0445\u043e\u0434 \u0438 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0433\u0440\u0430\u0444\u0430. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c <em>node1 <\/em>&#8212; \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u0443\u0437\u0435\u043b,  <em>node3 <\/em>&#8212; \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0442\u0440\u0438\u0445:<\/p>\n<pre><code class=\"python\">graph = builder.compile()  # \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e. \u041f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u0445\u0435\u043c\u0443 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\/1cc\/d4e\/415\/1ccd4e41579294af05244b2dcf00ed63.png\" alt=\"\u0413\u0440\u0430\u0444 \u0441 3 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438\" title=\"\u0413\u0440\u0430\u0444 \u0441 3 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438\" width=\"106\" height=\"432\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/1cc\/d4e\/415\/1ccd4e41579294af05244b2dcf00ed63.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1cc\/d4e\/415\/1ccd4e41579294af05244b2dcf00ed63.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0413\u0440\u0430\u0444 \u0441 3 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0433\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044c\u0435\u043a\u0442\u043e\u043c <em>LangChain<\/em>, \u0442\u043e \u043e\u043d \u0438\u043c\u0435\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <em>invoke <\/em>\u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430. <\/p>\n<pre><code class=\"python\">graph.invoke({\"graph_state\": \"\"})<\/code><\/pre>\n<pre><code>---Node 1--- ---Node 2--- ---Node 3--- {'graph_state': '\u042f \u043b\u044e\u0431\u043b\u044e \u041c\u043e\u0441\u043a\u0432\u0443'}<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u043e\u0436\u0435\u043c \u0438\u0434\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<h2>\u0423\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0440\u0435\u0431\u0440\u0430 <\/h2>\n<p>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u0442\u0435\u043c\u0430  &#8212; \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0440\u0435\u0431\u0440\u0430. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0433\u0440\u0430\u0444\u0430, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0437\u043b\u044b \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u044b\u0445. <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043c\u0435\u0441\u0442\u043e &#171;\u041c\u043e\u0441\u043a\u0432\u0443&#187; \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c &#171;\u041f\u0438\u0442\u0435\u0440&#187;.<\/p>\n<pre><code class=\"python\">def node_4(state):     print(\"---Node 4---\")     return {\"graph_state\": state['graph_state'] +\"\u041f\u0438\u0442\u0435\u0440\"}<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430:<\/p>\n<pre><code class=\"python\">import random from typing import Literal  def decide_mood(state) -&gt; Literal[\"node_3\", \"node_4\"]:      user_input = state['graph_state']      if random.random() &lt; 0.5:         return \"node_3\"      return \"node_4\"<\/code><\/pre>\n<p>\u0421\u043d\u043e\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u043b\u044b \u0438 \u0441\u0432\u044f\u0437\u0438:<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END  # Build graph builder = StateGraph(State) builder.add_node(\"node_1\", node_1) builder.add_node(\"node_2\", node_2) builder.add_node(\"node_3\", node_3) builder.add_node(\"node_4\", node_4)<\/code><\/pre>\n<pre><code class=\"python\">builder.add_edge(START, \"node_1\") builder.add_edge(\"node_1\", \"node_2\") builder.add_conditional_edges(\"node_2\", decide_mood) builder.add_edge(\"node_3\", END) builder.add_edge(\"node_4\", END)<\/code><\/pre>\n<p>\u0423 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0435 \u0440\u0435\u0431\u0440\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <em>add_conditional_edges<\/em>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c: \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b, \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0432\u044b\u0431\u043e\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e. <\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0433\u0440\u0430\u0444:<\/p>\n<pre><code class=\"python\">graph = builder.compile()  # View 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\/2cf\/4f5\/60f\/2cf4f560fa8faba1ee3bf72c5baeac48.png\" width=\"233\" height=\"432\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/2cf\/4f5\/60f\/2cf4f560fa8faba1ee3bf72c5baeac48.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2cf\/4f5\/60f\/2cf4f560fa8faba1ee3bf72c5baeac48.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<pre><code class=\"python\">---Node 1--- ---Node 2--- ---Node 4--- #node 3 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f {'graph_state': '\u042f \u043b\u044e\u0431\u043b\u044e \u041f\u0438\u0442\u0435\u0440'}<\/code><\/pre>\n<h2>\u0412\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0441\u0432\u043e\u0435\u043c <em>RAG<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">from langchain_core.tools import tool @tool def multiply(a: int, b: int) -&gt; int:     \"\"\"Multiply a and b.      Args:         a: first int         b: second int     \"\"\"     return a * b @tool # This will be a tool def add(a: int, b: int) -&gt; int:     \"\"\"Adds a and b.      Args:         a: first int         b: second int     \"\"\"     return a + b @tool def divide(a: int, b: int) -&gt; float:     \"\"\"Divide a and b.      Args:         a: first int         b: second int     \"\"\"     return a \/ b  tools = [add, multiply, divide]<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u044f\u0437\u044b\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 (<em>tool calling<\/em>):<\/p>\n<pre><code class=\"python\">from langchain_gigachat import GigaChat   llm  = GigaChat(verify_ssl_certs=False, credentials=\"api key\", model=\"GigaChat-2\")<\/code><\/pre>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>GigaChat<\/em>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">pip install langchain-gigachat<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0441\u0442\u0443\u043c\u0435\u043d\u0442\u044b \u043a \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">llm_with_tools = llm.bind_tools(tools)<\/code><\/pre>\n<p>\u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"python\">llm_with_tools.invoke(\"what is 2 + 2?\")<\/code><\/pre>\n<p><strong>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"python\">query = \"What is 2 * 2?\" messages  = [HumanMessage(query)]  ai_msg  = llm_with_tools.invoke(messages)  print(ai_msg.tool_calls)  messages.append(ai_msg) print(messages)<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 <em>AIMessage <\/em>\u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0433\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b:<\/p>\n<pre><code class=\"python\">for tool_call in ai_msg.tool_calls:   selected_tool = {\"add\": add, \"multiply\": multiply, \"divide\": divide}[tool_call[\"name\"].lower()]   tool_msg  = selected_tool.invoke(tool_call)   messages.append(tool_msg)<\/code><\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 (\u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f)<\/p>\n<pre><code class=\"python\">llm_with_tools.invoke(messages)<\/code><\/pre>\n<pre><code class=\"python\">AIMessage(content='\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b $2$ \u0438 $2$ \u0440\u0430\u0432\u043d\u043e $4$.'....<\/code><\/pre>\n<p><strong>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0440\u0430\u0444 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439<\/strong><\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/02c\/3ba\/caf\/02c3bacafe02b9290eead71639dfa76f.png\" alt=\"\u0441\u0445\u0435\u043c\u0430 \u0433\u0440\u0430\u0444\u0430\" title=\"\u0441\u0445\u0435\u043c\u0430 \u0433\u0440\u0430\u0444\u0430\" width=\"1287\" height=\"761\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/02c\/3ba\/caf\/02c3bacafe02b9290eead71639dfa76f.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/02c\/3ba\/caf\/02c3bacafe02b9290eead71639dfa76f.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0441\u0445\u0435\u043c\u0430 \u0433\u0440\u0430\u0444\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">from typing import Annotated from langgraph.graph.message import add_messages  class MessagesState(TypedDict):     messages: Annotated[list[AnyMessage], add_messages]<\/code><\/pre>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0433\u0440\u0430\u0444 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c  \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0430 \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c (\u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>add_messages<\/em>. \u042d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043b\u044e\u0431\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0423 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0443\u0437\u043b\u0430: \u0443\u0437\u0435\u043b \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0443\u0437\u0435\u043b \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b:<\/p>\n<pre><code class=\"python\"> def chatbot(state: State):   return {\"messages\": [llm_with_tools.invoke(state[\"messages\"])]}<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u0435\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u043d\u0443\u0436\u0435\u043d \u043b\u0438 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<pre><code class=\"python\">import json from langchain_core.messages import ToolMessage  class ToolNode:   def __init__(self, tools: list) -&gt; None:     self.tools_by_name  = {tool.name:tool for tool in tools}    def __call__(self,inputs: dict):     if messages := inputs.get(\"messages\", []):       mesasage = messages[-1] #\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435     else:       raise ValueError(\"No messages\")      outputs  = []     for tool_call in mesasage.tool_calls:       tool_res = self.tools_by_name[tool_call[\"name\"]].invoke(tool_call[\"args\"])        outputs.append(ToolMessage(content = json.dumps(tool_res), name=tool_call[\"name\"], tool_call_id=tool_call[\"id\"]))     return {\"messages\": outputs}<\/code><\/pre>\n<p>\u0414\u0443\u043c\u0430\u044e, \u0437\u0434\u0435\u0441\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0448\u0438\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">{'multiply': StructuredTool(name='multiply', description='Multiply a and b.\\n\\nArgs:\\n    a: first int\\n    b: second int', args_schema=&lt;class 'langchain_core.utils.pydantic.multiply'&gt;, func=&lt;function multiply at 0x00000170638BB600&gt;)} ....<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0443\u0437\u043b\u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f: \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u0430\u043a\u043e\u0439 \u0442\u043e \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438. \u041d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e: \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043e\u043d \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0435\u0441\u0442\u044c, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438. \u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 <em>ToolMessage <\/em>\u0432 \u043d\u0430\u0448\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439, \u043d\u043e \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0439 \u0448\u0430\u0433: \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443. <\/p>\n<pre><code class=\"python\">def route_tools(state: State):   if isinstance(state, list):     ai_message = state[-1]   elif messages := state.get(\"messages\", []):     ai_message = messages[-1]   else:     raise ValueError(\"No messages\")   if hasattr(ai_message, \"tool_calls\") and len(ai_message.tool_calls) &gt; 0:     return \"tools\"   return END<\/code><\/pre>\n<p>\u041e\u0431\u044c\u044f\u0441\u043d\u0435\u043d\u0438\u0435: \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043e\u043d\u043e \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0443\u0437\u0435\u043b &#171;<em>tools<\/em>&#171;, \u0438\u043d\u0430\u0447\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. <\/p>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0437\u043b\u044b \u0438 \u0441\u0432\u044f\u0437\u0438:<\/p>\n<pre><code class=\"python\">graph_builder = StateGraph(State) graph_builder.add_node(\"chatbot\", chatbot)  tool_node = ToolNode(tools) graph_builder.add_node(\"tools\", tool_node)<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u043e \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c<\/p>\n<pre><code class=\"python\">graph_builder.add_conditional_edges(     \"chatbot\",     route_tools,     {\"tools\": \"tools\", END: END} )<\/code><\/pre>\n<pre><code class=\"python\">graph_builder.add_edge(\"tools\", \"chatbot\") graph_builder.add_edge(START, \"chatbot\")  graph  = graph_builder.compile()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">{'messages': [HumanMessage(content='What is 2 + 2?',   AIMessage(content='', additional_kwargs={'function_call': {'name': 'add', 'arguments': {'a': 2, 'b': 2}},   ToolMessage(content='4', name='add',    AIMessage(content='\u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0438 \u0434\u0432\u0443\u0445 \u0440\u0430\u0432\u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0435\u043c.',  ]}<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e \u043f\u0435\u0440\u0432\u0443\u044e \u0447\u0430\u0441\u0442\u044c. \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u0441\u0443\u0442\u044c <em>LangGraph <\/em>\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 <em>RAG<\/em>-\u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 \u043c\u043e\u0439 Telegram-\u043a\u0430\u043d\u0430\u043b:<\/p>\n<p>t.me\/Viacheslav_Talks<\/p>\n<p>\u0433\u0434\u0435 \u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u044e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u043e \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445 \u0438 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0438\u043d\u0442\u0435\u043b\u043b\u0435\u043a\u0442\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0430\u043d\u043e\u043d\u0441\u0438\u0440\u0443\u044e \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438. \u041f\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 \u043d\u043e\u0432\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\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\/907844\/\"> https:\/\/habr.com\/ru\/articles\/907844\/<\/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 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0433\u043e\u0434\u044b \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 d\u0441\u0442\u0430\u043b\u0438 \u043f\u043e\u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u044b <em>RAG<\/em>(<strong>Retrieval Augmented Generation<\/strong> \u0438\u043b\u0438 &#171;<strong>\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u043e\u0439<\/strong>&#171;). <\/p>\n<p>\u0418\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439\u00a0 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439\/\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a. <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0443\u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0442\u0430\u043a\u0438\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u043b\u0438\u0441\u044c \u0432 \u0441\u043b\u0443\u0436\u0431\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438\u043b\u0438 \u044e\u0440\u0438\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0435\/\u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438. <\/p>\n<p>\u0412 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043e\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b <em>RAG<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e\u00a0<strong><em>LangGraph<\/em><\/strong><em>\u00a0<\/em>\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u043c\u044b \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 <em>RAG<\/em>-\u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044e \u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.  <\/p>\n<p><strong>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \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\">pip install langgraph langchain langchain_core langchain_community<\/code><\/pre>\n<h2>\u041a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:\u00a0<strong>\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u0442\u0430\u043f\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0443\u0437\u043b\u044b<\/strong>\u00a0\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u0422\u0430\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<strong>\u0433\u0440\u0430\u0444\u043e\u043c<\/strong>.<\/p>\n<p>\u041e\u0442\u0441\u044e\u0434\u0430 \u0432\u044b\u0442\u0435\u043a\u0430\u0435\u0442 \u0432\u0430\u0436\u043d\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u0443 \u043b\u044e\u0431\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c\u00a0<strong>\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (<em>State<\/em>)<\/strong>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. <em>State <\/em>\u0441\u043b\u0443\u0436\u0438\u0442\u00a0<strong>\u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u043e\u0439 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432<\/strong>\u00a0\u0433\u0440\u0430\u0444\u0430.<\/p>\n<h2>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0438\u0437 3-\u0445 \u0443\u0437\u043b\u043e\u0432, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435: &#8216;\u042f \u043b\u044e\u0431\u043b\u044e \u041c\u043e\u0441\u043a\u0432\u0443!&#8217;. <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <em>TypedDict<\/em>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0435:  <em>graph_state<\/em>. \u041f\u043e\u043c\u0438\u043c\u043e <em>TypedDict <\/em>\u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>Pydantic<\/em>, <em>dataclass<\/em>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"python\">from typing_extensions import TypedDict  class State(TypedDict):     graph_state: str<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0443\u0437\u043b\u044b:<\/p>\n<pre><code class=\"python\">def node_1(state):     print(\"---Node 1---\")     return {\"graph_state\": state['graph_state'] +\"\u042f\"}  def node_2(state):     print(\"---Node 2---\")     return {\"graph_state\": state['graph_state'] +\"\u043b\u044e\u0431\u043b\u044e\"}  def node_3(state):     print(\"---Node 3---\")     return {\"graph_state\": state['graph_state'] +\"\u041c\u043e\u0441\u043a\u0432\u0443!\"}<\/code><\/pre>\n<p>\u041f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435: \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0443\u0437\u043b\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0432\u044b\u0448\u0435 (\u043d\u043e \u0443\u0437\u043b\u044b \u043f\u043e\u043a\u0430 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u043d\u0430\u044e\u0442). \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <em>graph_state<\/em>. <\/p>\n<p>\u0421\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u043b\u044b \u0432 \u0433\u0440\u0430\u0444 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438:<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END  builder = StateGraph(State) builder.add_node(\"node_1\", node_1) builder.add_node(\"node_2\", node_2) builder.add_node(\"node_3\", node_3)<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0443\u0437\u043b\u044b, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u0430:<\/p>\n<pre><code class=\"python\">builder.add_edge(START, \"node_1\") builder.add_edge(\"node_1\", \"node_2\") builder.add_edge(\"node_2\", \"node_3\") builder.add_edge(\"node_3\", END)<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0434\u0432\u0430 \u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u0430- <em>START<\/em>, <em>END<\/em>. \u041e\u043d\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442 \u0432\u0445\u043e\u0434 \u0438 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0433\u0440\u0430\u0444\u0430. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c <em>node1 <\/em>&#8212; \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u0443\u0437\u0435\u043b,  <em>node3 <\/em>&#8212; \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0442\u0440\u0438\u0445:<\/p>\n<pre><code class=\"python\">graph = builder.compile()  # \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e. \u041f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u0445\u0435\u043c\u0443 display(Image(graph.get_graph().draw_mermaid_png()))<\/code><\/pre>\n<figure class=\"\">\n<div><figcaption>\u0413\u0440\u0430\u0444 \u0441 3 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0433\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044c\u0435\u043a\u0442\u043e\u043c <em>LangChain<\/em>, \u0442\u043e \u043e\u043d \u0438\u043c\u0435\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <em>invoke <\/em>\u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430. <\/p>\n<pre><code class=\"python\">graph.invoke({\"graph_state\": \"\"})<\/code><\/pre>\n<pre><code>---Node 1--- ---Node 2--- ---Node 3--- {'graph_state': '\u042f \u043b\u044e\u0431\u043b\u044e \u041c\u043e\u0441\u043a\u0432\u0443'}<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u043e\u0436\u0435\u043c \u0438\u0434\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<h2>\u0423\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0440\u0435\u0431\u0440\u0430 <\/h2>\n<p>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u0442\u0435\u043c\u0430  &#8212; \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0440\u0435\u0431\u0440\u0430. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0433\u0440\u0430\u0444\u0430, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0437\u043b\u044b \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u044b\u0445. <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043c\u0435\u0441\u0442\u043e &#171;\u041c\u043e\u0441\u043a\u0432\u0443&#187; \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c &#171;\u041f\u0438\u0442\u0435\u0440&#187;.<\/p>\n<pre><code class=\"python\">def node_4(state):     print(\"---Node 4---\")     return {\"graph_state\": state['graph_state'] +\"\u041f\u0438\u0442\u0435\u0440\"}<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430:<\/p>\n<pre><code class=\"python\">import random from typing import Literal  def decide_mood(state) -&gt; Literal[\"node_3\", \"node_4\"]:      user_input = state['graph_state']      if random.random() &lt; 0.5:         return \"node_3\"      return \"node_4\"<\/code><\/pre>\n<p>\u0421\u043d\u043e\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u043b\u044b \u0438 \u0441\u0432\u044f\u0437\u0438:<\/p>\n<pre><code class=\"python\">from langgraph.graph import StateGraph, START, END  # Build graph builder = StateGraph(State) builder.add_node(\"node_1\", node_1) builder.add_node(\"node_2\", node_2) builder.add_node(\"node_3\", node_3) builder.add_node(\"node_4\", node_4)<\/code><\/pre>\n<pre><code class=\"python\">builder.add_edge(START, \"node_1\") builder.add_edge(\"node_1\", \"node_2\") builder.add_conditional_edges(\"node_2\", decide_mood) builder.add_edge(\"node_3\", END) builder.add_edge(\"node_4\", END)<\/code><\/pre>\n<p>\u0423 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0435 \u0440\u0435\u0431\u0440\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <em>add_conditional_edges<\/em>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c: \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b, \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0432\u044b\u0431\u043e\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e. <\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0433\u0440\u0430\u0444:<\/p>\n<pre><code class=\"python\">graph = builder.compile()  # View display(Image(graph.get_graph().draw_mermaid_png()))<\/code><\/pre>\n<figure class=\"\"><\/figure>\n<pre><code class=\"python\">---Node 1--- ---Node 2--- ---Node 4--- #node 3 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f {'graph_state': '\u042f \u043b\u044e\u0431\u043b\u044e \u041f\u0438\u0442\u0435\u0440'}<\/code><\/pre>\n<h2>\u0412\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0441\u0432\u043e\u0435\u043c <em>RAG<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">from langchain_core.tools import tool @tool def multiply(a: int, b: int) -&gt; int:     \"\"\"Multiply a and b.      Args:         a: first int         b: second int     \"\"\"     return a * b @tool # This will be a tool def add(a: int, b: int) -&gt; int:     \"\"\"Adds a and b.      Args:         a: first int         b: second int     \"\"\"     return a + b @tool def divide(a: int, b: int) -&gt; float:     \"\"\"Divide a and b.      Args:         a: first int         b: second int     \"\"\"     return a \/ b  tools = [add, multiply, divide]<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u044f\u0437\u044b\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 (<em>tool calling<\/em>):<\/p>\n<pre><code class=\"python\">from langchain_gigachat import GigaChat   llm  = GigaChat(verify_ssl_certs=False, credentials=\"api key\", model=\"GigaChat-2\")<\/code><\/pre>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>GigaChat<\/em>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">pip install langchain-gigachat<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0441\u0442\u0443\u043c\u0435\u043d\u0442\u044b \u043a \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">llm_with_tools = llm.bind_tools(tools)<\/code><\/pre>\n<p>\u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"python\">llm_with_tools.invoke(\"what is 2 + 2?\")<\/code><\/pre>\n<p><strong>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"python\">query = \"What is 2 * 2?\" messages  = [HumanMessage(query)]  ai_msg  = llm_with_tools.invoke(messages)  print(ai_msg.tool_calls)  messages.append(ai_msg) print(messages)<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 <em>AIMessage <\/em>\u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0433\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b:<\/p>\n<pre><code class=\"python\">for tool_call in ai_msg.tool_calls:   selected_tool = {\"add\": add, \"multiply\": multiply, \"divide\": divide}[tool_call[\"name\"].lower()]   tool_msg  = selected_tool.invoke(tool_call)   messages.append(tool_msg)<\/code><\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 (\u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f)<\/p>\n<pre><code class=\"python\">llm_with_tools.invoke(messages)<\/code><\/pre>\n<pre><code class=\"python\">AIMessage(content='\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b $2$ \u0438 $2$ \u0440\u0430\u0432\u043d\u043e $4$.'....<\/code><\/pre>\n<p><strong>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0440\u0430\u0444 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0441\u0445\u0435\u043c\u0430 \u0433\u0440\u0430\u0444\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">from typing import Annotated from langgraph.graph.message import add_messages  class MessagesState(TypedDict):     messages: Annotated[list[AnyMessage], add_messages]<\/code><\/pre>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0433\u0440\u0430\u0444 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c  \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0430 \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c (\u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>add_messages<\/em>. \u042d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043b\u044e\u0431\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0423 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0443\u0437\u043b\u0430: \u0443\u0437\u0435\u043b \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0443\u0437\u0435\u043b \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b:<\/p>\n<pre><code class=\"python\"> def chatbot(state: State):   return {\"messages\": [llm_with_tools.invoke(state[\"messages\"])]}<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0437\u0435\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u043d\u0443\u0436\u0435\u043d \u043b\u0438 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<pre><code class=\"python\">import json from langchain_core.messages import ToolMessage  class ToolNode:   def __init__(self, tools: list) -&gt; None:     self.tools_by_name  = {tool.name:tool for tool in tools}    def __call__(self,inputs: dict):     if messages := inputs.get(\"messages\", []):       mesasage = messages[-1] #\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435     else:       raise ValueError(\"No messages\")      outputs  = []     for tool_call in mesasage.tool_calls:       tool_res = self.tools_by_name[tool_call[\"name\"]].invoke(tool_call[\"args\"])        outputs.append(ToolMessage(content = json.dumps(tool_res), name=tool_call[\"name\"], tool_call_id=tool_call[\"id\"]))     return {\"messages\": outputs}<\/code><\/pre>\n<p>\u0414\u0443\u043c\u0430\u044e, \u0437\u0434\u0435\u0441\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0448\u0438\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">{'multiply': StructuredTool(name='multiply', description='Multiply a and b.\\n\\nArgs:\\n    a: first int\\n    b: second int', args_schema=&lt;class 'langchain_core.utils.pydantic.multiply'&gt;, func=&lt;function multiply at 0x00000170638BB600&gt;)} ....<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0443\u0437\u043b\u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f: \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u0430\u043a\u043e\u0439 \u0442\u043e \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438. \u041d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e: \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043e\u043d \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0435\u0441\u0442\u044c, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438. \u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 <em>ToolMessage <\/em>\u0432 \u043d\u0430\u0448\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439, \u043d\u043e \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0439 \u0448\u0430\u0433: \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443. <\/p>\n<pre><code class=\"python\">def route_tools(state: State):   if isinstance(state, list):     ai_message = state[-1]   elif messages := state.get(\"messages\", []):     ai_message = messages[-1]   else:     raise ValueError(\"No messages\")   if hasattr(ai_message, \"tool_calls\") and len(ai_message.tool_calls) &gt; 0:     return \"tools\"   return END<\/code><\/pre>\n<p>\u041e\u0431\u044c\u044f\u0441\u043d\u0435\u043d\u0438\u0435: \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043e\u043d\u043e \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0443\u0437\u0435\u043b &#171;<em>tools<\/em>&#171;, \u0438\u043d\u0430\u0447\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. <\/p>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0437\u043b\u044b \u0438 \u0441\u0432\u044f\u0437\u0438:<\/p>\n<pre><code class=\"python\">graph_builder = StateGraph(State) graph_builder.add_node(\"chatbot\", chatbot)  tool_node = ToolNode(tools) graph_builder.add_node(\"tools\", tool_node)<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u043e \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c<\/p>\n<pre><code class=\"python\">graph_builder.add_conditional_edges(     \"chatbot\",     route_tools,     {\"tools\": \"tools\", END: END} )<\/code><\/pre>\n<pre><code class=\"python\">graph_builder.add_edge(\"tools\", \"chatbot\") graph_builder.add_edge(START, \"chatbot\")  graph  = graph_builder.compile()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\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-458962","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/458962","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=458962"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/458962\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=458962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=458962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=458962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}