{"id":598,"date":"2025-01-11T15:28:59","date_gmt":"2025-01-11T15:28:59","guid":{"rendered":"https:\/\/smolagents.org\/?post_type=docs&#038;p=598"},"modified":"2025-01-11T15:40:54","modified_gmt":"2025-01-11T15:40:54","password":"","slug":"text-to-sql-example","status":"publish","type":"docs","link":"https:\/\/smolagents.org\/ru\/docs\/text-to-sql-example\/","title":{"rendered":"\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 SQL"},"content":{"rendered":"<p>\u0412 \u044d\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0433\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e SQL \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e&nbsp;<code>smolagents<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0437\u043e\u043b\u043e\u0442\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430: \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 SQL?<\/p>\n<\/blockquote>\n\n\n\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 sql \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u0440\u0443\u043f\u043a\u0438\u043c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u043c. \u0425\u0443\u0436\u0435 \u0442\u043e\u0433\u043e, \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u043c, \u043d\u043e \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u0430 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0435\/\u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u0442\u0440\u0435\u0432\u043e\u0433\u0438.<\/p>\n\n\n\n<p>\ud83d\udc49 \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0430\u0433\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438 \u0440\u0435\u0448\u0438\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u043b\u0438 \u043d\u0435\u0442, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u0435\u0439 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n\n\n\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u0442\u043e\u0433\u043e \u0430\u0433\u0435\u043d\u0442\u0430! \ud83d\udcaa<\/p>\n\n\n\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0441\u0440\u0435\u0434\u0443 SQL:<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u0438\u0437 sqlalchemy import (\n    create_engine,\n    \u041c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435,\n    \u0422\u0430\u0431\u043b\u0438\u0446\u0430,\n    Column,\n    String,\n    Integer,\n    Float,\n    \u0432\u0441\u0442\u0430\u0432\u043a\u0430,\n    \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c,\n    \u0442\u0435\u043a\u0441\u0442,\n)\n\nengine = create_engine(\"sqlite:\/\/\/:memory:\")\nmetadata_obj = MetaData()\n\n<em># \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0433\u043e\u0440\u043e\u0434\u0430 SQL<\/em>\n\u0438\u043c\u044f_\u0442\u0430\u0431\u043b\u0438\u0446\u044b = \"\u043a\u0432\u0438\u0442\u0430\u043d\u0446\u0438\u0438\"\n\u043a\u0432\u0438\u0442\u0430\u043d\u0446\u0438\u0438 = \u0422\u0430\u0431\u043b\u0438\u0446\u0430(\n    \u0438\u043c\u044f_\u0442\u0430\u0431\u043b\u0438\u0446\u044b,\n    metadata_obj,\n    Column(\"receipt_id\", Integer, primary_key=True),\n    Column(\"\u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430\", String(16), primary_key=True),\n    Column(\"price\", Float),\n    Column(\"tip\", Float),\n)\nmetadata_obj.create_all(engine)\n\nrows = [\n    { \"receipt_id\": 1, \"customer_name\": \"\u0410\u043b\u0430\u043d \u041f\u0435\u0439\u043d\", \"\u0446\u0435\u043d\u0430\": 12.06, \"\u0447\u0430\u0435\u0432\u044b\u0435\": 1.20},\n    {\"receipt_id\": 2, \"\u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430\": \"\u0410\u043b\u0435\u043a\u0441 \u041c\u0435\u0439\u0441\u043e\u043d\", \"\u0446\u0435\u043d\u0430\": 23.86, \"\u0447\u0430\u0435\u0432\u044b\u0435\": 0.24},\n    {\"receipt_id\": 3, \"\u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430\": \"\u0412\u0443\u0434\u0440\u043e \u0412\u0438\u043b\u044c\u0441\u043e\u043d\", \"\u0446\u0435\u043d\u0430\": 53.43, \"\u0447\u0430\u0435\u0432\u044b\u0435\": 5.43},\n    {\"receipt_id\": 4, \"\u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430\": \"\u041c\u0430\u0440\u0433\u0430\u0440\u0435\u0442 \u0414\u0436\u0435\u0439\u043c\u0441\", \"\u0446\u0435\u043d\u0430\": 21.11, \"\u0447\u0430\u0435\u0432\u044b\u0435\": 1.00},\n]\nfor row in rows:\n    stmt = insert(receipts).values(**row)\n    with engine.begin() as connection:\n        cursor = connection.execute(stmt)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/examples\/text_to_sql#build-our-agent\" target=\"_blank\" rel=\"noopener\"><\/a>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0430\u0433\u0435\u043d\u0442\u0430<\/h3>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0448\u0443 SQL-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n\n\n\n<p>\u0410\u0442\u0440\u0438\u0431\u0443\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d \u0432 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0443 LLM \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0430\u0433\u0435\u043d\u0442\u0430: \u043e\u043d \u0434\u0430\u0435\u0442 LLM \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 SQL.<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">inspector = inspect(engine)\ncolumns_info = [(col[\"name\"], col[\"type\"]) for col in inspector.get_columns(\"receipts\")]\n\ntable_description = \"Columns:\\n\" + \"\\n\".join([f\" - {name}: {col_type}\" for name, col_type in columns_info])\nprint(table_description)<\/pre>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u0421\u0442\u043e\u043b\u0431\u0446\u044b:\n  - receipt_id: INTEGER\n  - \u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430: VARCHAR(16)\n  - \u0446\u0435\u043d\u0430: FLOAT\n  - \u0447\u0430\u0435\u0432\u044b\u0435: FLOAT<\/pre>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0430\u0448 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: (\u0447\u0438\u0442\u0430\u0442\u044c\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043e\u0431 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0445<\/a>\u00a0\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u00a0<code>Args:<\/code>\u00a0\u0447\u0430\u0441\u0442\u044c, \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u044e\u0449\u0430\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b.<\/li>\n\n\n\n<li>\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u043a\u0430\u043a \u043d\u0430 \u0432\u0445\u043e\u0434\u0435, \u0442\u0430\u043a \u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435.<\/li>\n<\/ul>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u0438\u0437 smolagents \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\n\n@tool\ndef sql_engine(query: str) -&gt; str:\n    \"\"\"\n    \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.\n    \u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u0438\u043c\u044f 'receipts'. \u0415\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:\n        \u0421\u0442\u043e\u043b\u0431\u0446\u044b:\n        - receipt_id: INTEGER\n        - \u0438\u043c\u044f_\u043a\u043b\u0438\u0435\u043d\u0442\u0430: VARCHAR(16)\n        - \u0446\u0435\u043d\u0430: FLOAT\n        - \u0447\u0430\u0435\u0432\u044b\u0435: FLOAT\n\n    Args:\n        query: \u0417\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c. \u042d\u0442\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 SQL.\n    \"\"\"\n    output = \"\"\n    with engine.connect() as con:\n        rows = con.execute(text(query))\n        for row in rows:\n            output += \"\\n\" + str(row)\n    \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u0432\u044b\u0432\u043e\u0434\u0430<\/pre>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0430\u0433\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u044d\u0442\u043e\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442.<\/p>\n\n\n\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c&nbsp;<code>CodeAgent<\/code>\u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0430\u0433\u0435\u043d\u0442\u043e\u0432 smolagents: \u0430\u0433\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0438\u0448\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432 \u043a\u043e\u0434\u0435 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c ReAct.<\/p>\n\n\n\n<p>\u041c\u043e\u0434\u0435\u043b\u044c - \u044d\u0442\u043e LLM, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u0433\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430. HfApiModel \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c LLM \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HF's Inference API, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 Serverless, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 Dedicated endpoint, \u043d\u043e \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u043f\u0440\u043e\u043f\u0440\u0438\u0435\u0442\u0430\u0440\u043d\u044b\u0439 API.<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u0438\u0437 smolagents import CodeAgent, HfApiModel\n\n\u0430\u0433\u0435\u043d\u0442 = CodeAgent(\n    tools=[sql_engine],\n    model=HfApiModel(\"meta-llama\/Meta-Llama-3.1-8B-Instruct\"),\n)\nagent.run(\"\u041c\u043e\u0436\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u043c\u043d\u0435 \u0438\u043c\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0441\u0430\u043c\u044b\u0439 \u0434\u043e\u0440\u043e\u0433\u043e\u0439 \u0447\u0435\u043a?\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/examples\/text_to_sql#level-2-table-joins\" target=\"_blank\" rel=\"noopener\"><\/a>\u0423\u0440\u043e\u0432\u0435\u043d\u044c 2: \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446<\/h3>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443! \u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 \u0430\u0433\u0435\u043d\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438.<\/p>\n\n\n\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432\u0442\u043e\u0440\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0438\u043c\u0435\u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e receipt_id!<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u0438\u043c\u044f_\u0441\u0442\u043e\u043b\u0430 = \"\u043e\u0444\u0438\u0446\u0438\u0430\u043d\u0442\u044b\"\n\u043a\u0432\u0438\u0442\u0430\u043d\u0446\u0438\u0438 = \u0422\u0430\u0431\u043b\u0438\u0446\u0430(\n    \u0438\u043c\u044f_\u0441\u0442\u043e\u043b\u0430,\n    metadata_obj,\n    Column(\"receipt_id\", Integer, primary_key=True),\n    Column(\"waiter_name\", String(16), primary_key=True),\n)\nmetadata_obj.create_all(engine)\n\n\u0441\u0442\u0440\u043e\u043a\u0438 = [\n    { \"receipt_id\": 1, \"waiter_name\": \"\u041a\u043e\u0440\u0438 \u0414\u0436\u043e\u043d\u0441\u043e\u043d\"},\n    { \"receipt_id\": 2, \"\u0438\u043c\u044f_\u043e\u0444\u0438\u0446\u0438\u0430\u043d\u0442\u0430\": \"\u041c\u0430\u0439\u043a\u043b \u0423\u043e\u0442\u0442\u0441\"},\n    {\"receipt_id\": 3, \"waiter_name\": \"\u041c\u0430\u0439\u043a\u043b \u0423\u043e\u0442\u0442\u0441\"},\n    {\"receipt_id\": 4, \"waiter_name\": \"\u041c\u0430\u0440\u0433\u0430\u0440\u0435\u0442 \u0414\u0436\u0435\u0439\u043c\u0441\"},\n]\nfor row in rows:\n    stmt = insert(receipts).values(**row)\n    with engine.begin() as connection:\n        cursor = connection.execute(stmt)<\/pre>\n\n\n\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043c\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c&nbsp;<code>SQLExecutorTool<\/code>&nbsp;\u0441 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0447\u0442\u043e\u0431\u044b LLM \u043c\u043e\u0433 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0438\u0437 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">updated_description = \"\"\"\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0418\u043c\u0435\u0439\u0442\u0435 \u0432 \u0432\u0438\u0434\u0443, \u0447\u0442\u043e \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 - \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.\n\u041e\u043d \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:\"\"\"\n\ninspector = inspect(engine)\nfor table in [\"receipts\", \"waiters\"]:\n    columns_info = [(col[\"name\"], col[\"type\"]) for col in inspector.get_columns(table)]\n\n    table_description = f \"\u0422\u0430\u0431\u043b\u0438\u0446\u0430 '{table}':\\n\"\n\n    table_description += \"Columns:\\n\" + \"\\n\".join([f\" - {name}: {col_type}\" for name, col_type in columns_info])\n    updated_description += \"\\n\\n\" + table_description\n\nprint(updated_description)<\/pre>\n\n\n\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e, \u043c\u044b \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043c \u0434\u0432\u0438\u0436\u043e\u043a LLM \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u043e\u043b\u0435\u0435 \u043c\u043e\u0449\u043d\u043e\u0433\u043e\u00a0<a href=\"https:\/\/huggingface.co\/Qwen\/Qwen2.5-Coder-32B-Instruct\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Qwen\/Qwen2.5-Coder-32B-Instruct<\/a>!<\/p>\n\n\n\n<p>\u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sql_engine.description = updated_description\n\n\u0430\u0433\u0435\u043d\u0442 = CodeAgent(\n    tools=[sql_engine],\n    model=HfApiModel(\"Qwen\/Qwen2.5-Coder-32B-Instruct\"),\n)\n\nagent.run(\"\u041a\u0430\u043a\u043e\u0439 \u043e\u0444\u0438\u0446\u0438\u0430\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0431\u0449\u0438\u0445 \u0434\u0435\u043d\u0435\u0433 \u043e\u0442 \u0447\u0430\u0435\u0432\u044b\u0445?\")<\/pre>\n\n\n\n<p>\u041e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043d\u0430 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u0435 \u043f\u0440\u0430\u0432\u0434\u0430 \u043b\u0438?<\/p>\n\n\n\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d! \u041c\u044b \u0437\u0430\u0442\u0440\u043e\u043d\u0443\u043b\u0438 \u044d\u0442\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/li>\n\n\n\n<li>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430.<\/li>\n\n\n\n<li>\u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0441\u0438\u043b\u044c\u043d\u044b\u0439 LLM \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0430\u0433\u0435\u043d\u0442\u0443 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u0442\u044c.<\/li>\n<\/ul>\n\n\n\n<p>\u2705 \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 SQL, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u0435\u0447\u0442\u0430\u043b\u0438! \u2728<\/p>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we\u2019ll see how to implement an agent that leverages SQL using&nbsp;smolagents. Let\u2019s start with the golden question: why not keep it simple and use a standard text-to-SQL pipeline? A standard text-to-sql pipeline is brittle, since the generated SQL query can be incorrect. Even worse, the query could be incorrect, but not raise&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"doc_category":[10],"doc_tag":[],"class_list":["post-598","docs","type-docs","status-publish","hentry","doc_category-examples"],"year_month":"2026-04","word_count":879,"total_views":"4415","reactions":{"happy":"1","normal":"0","sad":"0"},"author_info":{"name":"smolagents","author_nicename":"wd-gstargmail-com","author_url":"https:\/\/smolagents.org\/ru\/author\/wd-gstargmail-com\/"},"doc_category_info":[{"term_name":"Examples","term_url":"https:\/\/smolagents.org\/ru\/docs-category\/examples\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/docs\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/docs\/598\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/docs\/598\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/doc_category?post=598"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/ru\/wp-json\/wp\/v2\/doc_tag?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}