[{"data":1,"prerenderedAt":1461},["ShallowReactive",2],{"tag-openai":3},[4],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"date":11,"layout":12,"subtitle":13,"image":14,"optimized_image":14,"category":15,"tags":16,"author":22,"paginate":7,"body":23,"_type":1455,"_id":1456,"_source":1457,"_file":1458,"_stem":1459,"_extension":1460},"/posts/gpt-sql-integration","posts",false,"","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","post","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png","code",[15,17,18,19,20,21],"gpt","openai","sql","ai","nlp","jaimedearcos",{"type":24,"children":25,"toc":1444},"root",[26,38,45,65,71,76,124,137,153,159,164,390,396,401,737,743,748,1099,1105,1110,1169,1175,1180,1361,1367,1385,1391,1396,1403,1438],{"type":27,"tag":28,"props":29,"children":30},"element","blockquote",{},[31],{"type":27,"tag":32,"props":33,"children":34},"p",{},[35],{"type":36,"value":37},"text","Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":27,"tag":39,"props":40,"children":42},"h2",{"id":41},"requisitos-previos",[43],{"type":36,"value":44},"Requisitos Previos",{"type":27,"tag":46,"props":47,"children":48},"ul",{},[49,55,60],{"type":27,"tag":50,"props":51,"children":52},"li",{},[53],{"type":36,"value":54},"Cuenta y clave API de OpenAI",{"type":27,"tag":50,"props":56,"children":57},{},[58],{"type":36,"value":59},"Python instalado en tu entorno",{"type":27,"tag":50,"props":61,"children":62},{},[63],{"type":36,"value":64},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":27,"tag":39,"props":66,"children":68},{"id":67},"paso-1-configuración-del-entorno",[69],{"type":36,"value":70},"Paso 1: Configuración del entorno",{"type":27,"tag":32,"props":72,"children":73},{},[74],{"type":36,"value":75},"Primero, instala las dependencias necesarias:",{"type":27,"tag":77,"props":78,"children":82},"pre",{"className":79,"code":80,"language":81,"meta":8,"style":8},"language-bash shiki shiki-themes github-dark github-light","pip install openai python-dotenv sqlalchemy psycopg2-binary\n","bash",[83],{"type":27,"tag":15,"props":84,"children":85},{"__ignoreMap":8},[86],{"type":27,"tag":87,"props":88,"children":91},"span",{"class":89,"line":90},"line",1,[92,98,104,109,114,119],{"type":27,"tag":87,"props":93,"children":95},{"style":94},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[96],{"type":36,"value":97},"pip",{"type":27,"tag":87,"props":99,"children":101},{"style":100},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[102],{"type":36,"value":103}," install",{"type":27,"tag":87,"props":105,"children":106},{"style":100},[107],{"type":36,"value":108}," openai",{"type":27,"tag":87,"props":110,"children":111},{"style":100},[112],{"type":36,"value":113}," python-dotenv",{"type":27,"tag":87,"props":115,"children":116},{"style":100},[117],{"type":36,"value":118}," sqlalchemy",{"type":27,"tag":87,"props":120,"children":121},{"style":100},[122],{"type":36,"value":123}," psycopg2-binary\n",{"type":27,"tag":32,"props":125,"children":126},{},[127,129,135],{"type":36,"value":128},"Guarda tu clave API en un archivo ",{"type":27,"tag":15,"props":130,"children":132},{"className":131},[],[133],{"type":36,"value":134},".env",{"type":36,"value":136},":",{"type":27,"tag":77,"props":138,"children":142},{"className":139,"code":140,"language":141,"meta":8,"style":8},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[143],{"type":27,"tag":15,"props":144,"children":145},{"__ignoreMap":8},[146],{"type":27,"tag":87,"props":147,"children":148},{"class":89,"line":90},[149],{"type":27,"tag":87,"props":150,"children":151},{},[152],{"type":36,"value":140},{"type":27,"tag":39,"props":154,"children":156},{"id":155},"paso-2-conexión-a-la-base-de-datos",[157],{"type":36,"value":158},"Paso 2: Conexión a la base de datos",{"type":27,"tag":32,"props":160,"children":161},{},[162],{"type":36,"value":163},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":27,"tag":77,"props":165,"children":169},{"className":166,"code":167,"language":168,"meta":8,"style":8},"language-python shiki shiki-themes github-dark github-light","from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n","python",[170],{"type":27,"tag":15,"props":171,"children":172},{"__ignoreMap":8},[173,198,220,233,255,265,274,282,312,339,372],{"type":27,"tag":87,"props":174,"children":175},{"class":89,"line":90},[176,182,188,193],{"type":27,"tag":87,"props":177,"children":179},{"style":178},"--shiki-default:#F97583;--shiki-light:#D73A49",[180],{"type":36,"value":181},"from",{"type":27,"tag":87,"props":183,"children":185},{"style":184},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[186],{"type":36,"value":187}," sqlalchemy ",{"type":27,"tag":87,"props":189,"children":190},{"style":178},[191],{"type":36,"value":192},"import",{"type":27,"tag":87,"props":194,"children":195},{"style":184},[196],{"type":36,"value":197}," create_engine\n",{"type":27,"tag":87,"props":199,"children":201},{"class":89,"line":200},2,[202,206,211,215],{"type":27,"tag":87,"props":203,"children":204},{"style":178},[205],{"type":36,"value":181},{"type":27,"tag":87,"props":207,"children":208},{"style":184},[209],{"type":36,"value":210}," sqlalchemy.orm ",{"type":27,"tag":87,"props":212,"children":213},{"style":178},[214],{"type":36,"value":192},{"type":27,"tag":87,"props":216,"children":217},{"style":184},[218],{"type":36,"value":219}," sessionmaker\n",{"type":27,"tag":87,"props":221,"children":223},{"class":89,"line":222},3,[224,228],{"type":27,"tag":87,"props":225,"children":226},{"style":178},[227],{"type":36,"value":192},{"type":27,"tag":87,"props":229,"children":230},{"style":184},[231],{"type":36,"value":232}," os\n",{"type":27,"tag":87,"props":234,"children":236},{"class":89,"line":235},4,[237,241,246,250],{"type":27,"tag":87,"props":238,"children":239},{"style":178},[240],{"type":36,"value":181},{"type":27,"tag":87,"props":242,"children":243},{"style":184},[244],{"type":36,"value":245}," dotenv ",{"type":27,"tag":87,"props":247,"children":248},{"style":178},[249],{"type":36,"value":192},{"type":27,"tag":87,"props":251,"children":252},{"style":184},[253],{"type":36,"value":254}," load_dotenv\n",{"type":27,"tag":87,"props":256,"children":258},{"class":89,"line":257},5,[259],{"type":27,"tag":87,"props":260,"children":262},{"emptyLinePlaceholder":261},true,[263],{"type":36,"value":264},"\n",{"type":27,"tag":87,"props":266,"children":268},{"class":89,"line":267},6,[269],{"type":27,"tag":87,"props":270,"children":271},{"style":184},[272],{"type":36,"value":273},"load_dotenv()\n",{"type":27,"tag":87,"props":275,"children":277},{"class":89,"line":276},7,[278],{"type":27,"tag":87,"props":279,"children":280},{"emptyLinePlaceholder":261},[281],{"type":36,"value":264},{"type":27,"tag":87,"props":283,"children":285},{"class":89,"line":284},8,[286,292,297,302,307],{"type":27,"tag":87,"props":287,"children":289},{"style":288},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[290],{"type":36,"value":291},"DATABASE_URL",{"type":27,"tag":87,"props":293,"children":294},{"style":178},[295],{"type":36,"value":296}," =",{"type":27,"tag":87,"props":298,"children":299},{"style":184},[300],{"type":36,"value":301}," os.getenv(",{"type":27,"tag":87,"props":303,"children":304},{"style":100},[305],{"type":36,"value":306},"\"DATABASE_URL\"",{"type":27,"tag":87,"props":308,"children":309},{"style":184},[310],{"type":36,"value":311},")\n",{"type":27,"tag":87,"props":313,"children":315},{"class":89,"line":314},9,[316,321,326,331,335],{"type":27,"tag":87,"props":317,"children":318},{"style":184},[319],{"type":36,"value":320},"engine ",{"type":27,"tag":87,"props":322,"children":323},{"style":178},[324],{"type":36,"value":325},"=",{"type":27,"tag":87,"props":327,"children":328},{"style":184},[329],{"type":36,"value":330}," create_engine(",{"type":27,"tag":87,"props":332,"children":333},{"style":288},[334],{"type":36,"value":291},{"type":27,"tag":87,"props":336,"children":337},{"style":184},[338],{"type":36,"value":311},{"type":27,"tag":87,"props":340,"children":342},{"class":89,"line":341},10,[343,348,352,357,363,367],{"type":27,"tag":87,"props":344,"children":345},{"style":184},[346],{"type":36,"value":347},"Session ",{"type":27,"tag":87,"props":349,"children":350},{"style":178},[351],{"type":36,"value":325},{"type":27,"tag":87,"props":353,"children":354},{"style":184},[355],{"type":36,"value":356}," sessionmaker(",{"type":27,"tag":87,"props":358,"children":360},{"style":359},"--shiki-default:#FFAB70;--shiki-light:#E36209",[361],{"type":36,"value":362},"bind",{"type":27,"tag":87,"props":364,"children":365},{"style":178},[366],{"type":36,"value":325},{"type":27,"tag":87,"props":368,"children":369},{"style":184},[370],{"type":36,"value":371},"engine)\n",{"type":27,"tag":87,"props":373,"children":375},{"class":89,"line":374},11,[376,381,385],{"type":27,"tag":87,"props":377,"children":378},{"style":184},[379],{"type":36,"value":380},"session ",{"type":27,"tag":87,"props":382,"children":383},{"style":178},[384],{"type":36,"value":325},{"type":27,"tag":87,"props":386,"children":387},{"style":184},[388],{"type":36,"value":389}," Session()\n",{"type":27,"tag":39,"props":391,"children":393},{"id":392},"paso-3-obtener-esquema-de-la-base-de-datos",[394],{"type":36,"value":395},"Paso 3: Obtener esquema de la base de datos",{"type":27,"tag":32,"props":397,"children":398},{},[399],{"type":36,"value":400},"Genera una descripción del esquema que será utilizada por GPT:",{"type":27,"tag":77,"props":402,"children":404},{"className":166,"code":403,"language":168,"meta":8,"style":8},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[405],{"type":27,"tag":15,"props":406,"children":407},{"__ignoreMap":8},[408,428,435,453,470,487,510,553,575,660,684,697,705,723],{"type":27,"tag":87,"props":409,"children":410},{"class":89,"line":90},[411,415,419,423],{"type":27,"tag":87,"props":412,"children":413},{"style":178},[414],{"type":36,"value":181},{"type":27,"tag":87,"props":416,"children":417},{"style":184},[418],{"type":36,"value":187},{"type":27,"tag":87,"props":420,"children":421},{"style":178},[422],{"type":36,"value":192},{"type":27,"tag":87,"props":424,"children":425},{"style":184},[426],{"type":36,"value":427}," inspect\n",{"type":27,"tag":87,"props":429,"children":430},{"class":89,"line":200},[431],{"type":27,"tag":87,"props":432,"children":433},{"emptyLinePlaceholder":261},[434],{"type":36,"value":264},{"type":27,"tag":87,"props":436,"children":437},{"class":89,"line":222},[438,443,448],{"type":27,"tag":87,"props":439,"children":440},{"style":178},[441],{"type":36,"value":442},"def",{"type":27,"tag":87,"props":444,"children":445},{"style":94},[446],{"type":36,"value":447}," get_database_schema",{"type":27,"tag":87,"props":449,"children":450},{"style":184},[451],{"type":36,"value":452},"(engine):\n",{"type":27,"tag":87,"props":454,"children":455},{"class":89,"line":235},[456,461,465],{"type":27,"tag":87,"props":457,"children":458},{"style":184},[459],{"type":36,"value":460},"    inspector ",{"type":27,"tag":87,"props":462,"children":463},{"style":178},[464],{"type":36,"value":325},{"type":27,"tag":87,"props":466,"children":467},{"style":184},[468],{"type":36,"value":469}," inspect(engine)\n",{"type":27,"tag":87,"props":471,"children":472},{"class":89,"line":257},[473,478,482],{"type":27,"tag":87,"props":474,"children":475},{"style":184},[476],{"type":36,"value":477},"    schema_description ",{"type":27,"tag":87,"props":479,"children":480},{"style":178},[481],{"type":36,"value":325},{"type":27,"tag":87,"props":483,"children":484},{"style":100},[485],{"type":36,"value":486}," \"\"\n",{"type":27,"tag":87,"props":488,"children":489},{"class":89,"line":267},[490,495,500,505],{"type":27,"tag":87,"props":491,"children":492},{"style":178},[493],{"type":36,"value":494},"    for",{"type":27,"tag":87,"props":496,"children":497},{"style":184},[498],{"type":36,"value":499}," table_name ",{"type":27,"tag":87,"props":501,"children":502},{"style":178},[503],{"type":36,"value":504},"in",{"type":27,"tag":87,"props":506,"children":507},{"style":184},[508],{"type":36,"value":509}," inspector.get_table_names():\n",{"type":27,"tag":87,"props":511,"children":512},{"class":89,"line":276},[513,518,523,528,533,538,543,548],{"type":27,"tag":87,"props":514,"children":515},{"style":184},[516],{"type":36,"value":517},"        schema_description ",{"type":27,"tag":87,"props":519,"children":520},{"style":178},[521],{"type":36,"value":522},"+=",{"type":27,"tag":87,"props":524,"children":525},{"style":178},[526],{"type":36,"value":527}," f",{"type":27,"tag":87,"props":529,"children":530},{"style":100},[531],{"type":36,"value":532},"\"Table: ",{"type":27,"tag":87,"props":534,"children":535},{"style":288},[536],{"type":36,"value":537},"{",{"type":27,"tag":87,"props":539,"children":540},{"style":184},[541],{"type":36,"value":542},"table_name",{"type":27,"tag":87,"props":544,"children":545},{"style":288},[546],{"type":36,"value":547},"}\\n",{"type":27,"tag":87,"props":549,"children":550},{"style":100},[551],{"type":36,"value":552},"\"\n",{"type":27,"tag":87,"props":554,"children":555},{"class":89,"line":284},[556,561,566,570],{"type":27,"tag":87,"props":557,"children":558},{"style":178},[559],{"type":36,"value":560},"        for",{"type":27,"tag":87,"props":562,"children":563},{"style":184},[564],{"type":36,"value":565}," column ",{"type":27,"tag":87,"props":567,"children":568},{"style":178},[569],{"type":36,"value":504},{"type":27,"tag":87,"props":571,"children":572},{"style":184},[573],{"type":36,"value":574}," inspector.get_columns(table_name):\n",{"type":27,"tag":87,"props":576,"children":577},{"class":89,"line":314},[578,583,587,591,596,600,605,610,615,620,625,629,633,638,642,646,651,656],{"type":27,"tag":87,"props":579,"children":580},{"style":184},[581],{"type":36,"value":582},"            schema_description ",{"type":27,"tag":87,"props":584,"children":585},{"style":178},[586],{"type":36,"value":522},{"type":27,"tag":87,"props":588,"children":589},{"style":178},[590],{"type":36,"value":527},{"type":27,"tag":87,"props":592,"children":593},{"style":100},[594],{"type":36,"value":595},"\"- ",{"type":27,"tag":87,"props":597,"children":598},{"style":288},[599],{"type":36,"value":537},{"type":27,"tag":87,"props":601,"children":602},{"style":184},[603],{"type":36,"value":604},"column[",{"type":27,"tag":87,"props":606,"children":607},{"style":100},[608],{"type":36,"value":609},"'name'",{"type":27,"tag":87,"props":611,"children":612},{"style":184},[613],{"type":36,"value":614},"]",{"type":27,"tag":87,"props":616,"children":617},{"style":288},[618],{"type":36,"value":619},"}",{"type":27,"tag":87,"props":621,"children":622},{"style":100},[623],{"type":36,"value":624}," (",{"type":27,"tag":87,"props":626,"children":627},{"style":288},[628],{"type":36,"value":537},{"type":27,"tag":87,"props":630,"children":631},{"style":184},[632],{"type":36,"value":604},{"type":27,"tag":87,"props":634,"children":635},{"style":100},[636],{"type":36,"value":637},"'type'",{"type":27,"tag":87,"props":639,"children":640},{"style":184},[641],{"type":36,"value":614},{"type":27,"tag":87,"props":643,"children":644},{"style":288},[645],{"type":36,"value":619},{"type":27,"tag":87,"props":647,"children":648},{"style":100},[649],{"type":36,"value":650},")",{"type":27,"tag":87,"props":652,"children":653},{"style":288},[654],{"type":36,"value":655},"\\n",{"type":27,"tag":87,"props":657,"children":658},{"style":100},[659],{"type":36,"value":552},{"type":27,"tag":87,"props":661,"children":662},{"class":89,"line":341},[663,667,671,676,680],{"type":27,"tag":87,"props":664,"children":665},{"style":184},[666],{"type":36,"value":517},{"type":27,"tag":87,"props":668,"children":669},{"style":178},[670],{"type":36,"value":522},{"type":27,"tag":87,"props":672,"children":673},{"style":100},[674],{"type":36,"value":675}," \"",{"type":27,"tag":87,"props":677,"children":678},{"style":288},[679],{"type":36,"value":655},{"type":27,"tag":87,"props":681,"children":682},{"style":100},[683],{"type":36,"value":552},{"type":27,"tag":87,"props":685,"children":686},{"class":89,"line":374},[687,692],{"type":27,"tag":87,"props":688,"children":689},{"style":178},[690],{"type":36,"value":691},"    return",{"type":27,"tag":87,"props":693,"children":694},{"style":184},[695],{"type":36,"value":696}," schema_description\n",{"type":27,"tag":87,"props":698,"children":700},{"class":89,"line":699},12,[701],{"type":27,"tag":87,"props":702,"children":703},{"emptyLinePlaceholder":261},[704],{"type":36,"value":264},{"type":27,"tag":87,"props":706,"children":708},{"class":89,"line":707},13,[709,714,718],{"type":27,"tag":87,"props":710,"children":711},{"style":184},[712],{"type":36,"value":713},"schema_description ",{"type":27,"tag":87,"props":715,"children":716},{"style":178},[717],{"type":36,"value":325},{"type":27,"tag":87,"props":719,"children":720},{"style":184},[721],{"type":36,"value":722}," get_database_schema(engine)\n",{"type":27,"tag":87,"props":724,"children":726},{"class":89,"line":725},14,[727,732],{"type":27,"tag":87,"props":728,"children":729},{"style":288},[730],{"type":36,"value":731},"print",{"type":27,"tag":87,"props":733,"children":734},{"style":184},[735],{"type":36,"value":736},"(schema_description)\n",{"type":27,"tag":39,"props":738,"children":740},{"id":739},"paso-4-preparar-la-consulta-con-gpt",[741],{"type":36,"value":742},"Paso 4: Preparar la consulta con GPT",{"type":27,"tag":32,"props":744,"children":745},{},[746],{"type":36,"value":747},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":27,"tag":77,"props":749,"children":751},{"className":166,"code":750,"language":168,"meta":8,"style":8},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[752],{"type":27,"tag":15,"props":753,"children":754},{"__ignoreMap":8},[755,767,778,785,810,817,834,851,873,890,961,994,1002,1010,1037,1050,1058,1086],{"type":27,"tag":87,"props":756,"children":757},{"class":89,"line":90},[758,762],{"type":27,"tag":87,"props":759,"children":760},{"style":178},[761],{"type":36,"value":192},{"type":27,"tag":87,"props":763,"children":764},{"style":184},[765],{"type":36,"value":766}," openai\n",{"type":27,"tag":87,"props":768,"children":769},{"class":89,"line":200},[770,774],{"type":27,"tag":87,"props":771,"children":772},{"style":178},[773],{"type":36,"value":192},{"type":27,"tag":87,"props":775,"children":776},{"style":184},[777],{"type":36,"value":232},{"type":27,"tag":87,"props":779,"children":780},{"class":89,"line":222},[781],{"type":27,"tag":87,"props":782,"children":783},{"emptyLinePlaceholder":261},[784],{"type":36,"value":264},{"type":27,"tag":87,"props":786,"children":787},{"class":89,"line":235},[788,793,797,801,806],{"type":27,"tag":87,"props":789,"children":790},{"style":184},[791],{"type":36,"value":792},"openai.api_key ",{"type":27,"tag":87,"props":794,"children":795},{"style":178},[796],{"type":36,"value":325},{"type":27,"tag":87,"props":798,"children":799},{"style":184},[800],{"type":36,"value":301},{"type":27,"tag":87,"props":802,"children":803},{"style":100},[804],{"type":36,"value":805},"\"OPENAI_API_KEY\"",{"type":27,"tag":87,"props":807,"children":808},{"style":184},[809],{"type":36,"value":311},{"type":27,"tag":87,"props":811,"children":812},{"class":89,"line":257},[813],{"type":27,"tag":87,"props":814,"children":815},{"emptyLinePlaceholder":261},[816],{"type":36,"value":264},{"type":27,"tag":87,"props":818,"children":819},{"class":89,"line":267},[820,824,829],{"type":27,"tag":87,"props":821,"children":822},{"style":178},[823],{"type":36,"value":442},{"type":27,"tag":87,"props":825,"children":826},{"style":94},[827],{"type":36,"value":828}," generate_sql",{"type":27,"tag":87,"props":830,"children":831},{"style":184},[832],{"type":36,"value":833},"(question, schema):\n",{"type":27,"tag":87,"props":835,"children":836},{"class":89,"line":276},[837,842,846],{"type":27,"tag":87,"props":838,"children":839},{"style":184},[840],{"type":36,"value":841},"    response ",{"type":27,"tag":87,"props":843,"children":844},{"style":178},[845],{"type":36,"value":325},{"type":27,"tag":87,"props":847,"children":848},{"style":184},[849],{"type":36,"value":850}," openai.ChatCompletion.create(\n",{"type":27,"tag":87,"props":852,"children":853},{"class":89,"line":284},[854,859,863,868],{"type":27,"tag":87,"props":855,"children":856},{"style":359},[857],{"type":36,"value":858},"        model",{"type":27,"tag":87,"props":860,"children":861},{"style":178},[862],{"type":36,"value":325},{"type":27,"tag":87,"props":864,"children":865},{"style":100},[866],{"type":36,"value":867},"\"gpt-4\"",{"type":27,"tag":87,"props":869,"children":870},{"style":184},[871],{"type":36,"value":872},",\n",{"type":27,"tag":87,"props":874,"children":875},{"class":89,"line":314},[876,881,885],{"type":27,"tag":87,"props":877,"children":878},{"style":359},[879],{"type":36,"value":880},"        messages",{"type":27,"tag":87,"props":882,"children":883},{"style":178},[884],{"type":36,"value":325},{"type":27,"tag":87,"props":886,"children":887},{"style":184},[888],{"type":36,"value":889},"[\n",{"type":27,"tag":87,"props":891,"children":892},{"class":89,"line":341},[893,898,903,908,913,918,923,927,932,937,942,947,951,956],{"type":27,"tag":87,"props":894,"children":895},{"style":184},[896],{"type":36,"value":897},"            {",{"type":27,"tag":87,"props":899,"children":900},{"style":100},[901],{"type":36,"value":902},"\"role\"",{"type":27,"tag":87,"props":904,"children":905},{"style":184},[906],{"type":36,"value":907},": ",{"type":27,"tag":87,"props":909,"children":910},{"style":100},[911],{"type":36,"value":912},"\"system\"",{"type":27,"tag":87,"props":914,"children":915},{"style":184},[916],{"type":36,"value":917},", ",{"type":27,"tag":87,"props":919,"children":920},{"style":100},[921],{"type":36,"value":922},"\"content\"",{"type":27,"tag":87,"props":924,"children":925},{"style":184},[926],{"type":36,"value":907},{"type":27,"tag":87,"props":928,"children":929},{"style":178},[930],{"type":36,"value":931},"f",{"type":27,"tag":87,"props":933,"children":934},{"style":100},[935],{"type":36,"value":936},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":27,"tag":87,"props":938,"children":939},{"style":288},[940],{"type":36,"value":941},"\\n{",{"type":27,"tag":87,"props":943,"children":944},{"style":184},[945],{"type":36,"value":946},"schema",{"type":27,"tag":87,"props":948,"children":949},{"style":288},[950],{"type":36,"value":619},{"type":27,"tag":87,"props":952,"children":953},{"style":100},[954],{"type":36,"value":955},"\"",{"type":27,"tag":87,"props":957,"children":958},{"style":184},[959],{"type":36,"value":960},"},\n",{"type":27,"tag":87,"props":962,"children":963},{"class":89,"line":374},[964,968,972,976,981,985,989],{"type":27,"tag":87,"props":965,"children":966},{"style":184},[967],{"type":36,"value":897},{"type":27,"tag":87,"props":969,"children":970},{"style":100},[971],{"type":36,"value":902},{"type":27,"tag":87,"props":973,"children":974},{"style":184},[975],{"type":36,"value":907},{"type":27,"tag":87,"props":977,"children":978},{"style":100},[979],{"type":36,"value":980},"\"user\"",{"type":27,"tag":87,"props":982,"children":983},{"style":184},[984],{"type":36,"value":917},{"type":27,"tag":87,"props":986,"children":987},{"style":100},[988],{"type":36,"value":922},{"type":27,"tag":87,"props":990,"children":991},{"style":184},[992],{"type":36,"value":993},": question}\n",{"type":27,"tag":87,"props":995,"children":996},{"class":89,"line":699},[997],{"type":27,"tag":87,"props":998,"children":999},{"style":184},[1000],{"type":36,"value":1001},"        ]\n",{"type":27,"tag":87,"props":1003,"children":1004},{"class":89,"line":707},[1005],{"type":27,"tag":87,"props":1006,"children":1007},{"style":184},[1008],{"type":36,"value":1009},"    )\n",{"type":27,"tag":87,"props":1011,"children":1012},{"class":89,"line":725},[1013,1018,1022,1027,1032],{"type":27,"tag":87,"props":1014,"children":1015},{"style":184},[1016],{"type":36,"value":1017},"    sql_query ",{"type":27,"tag":87,"props":1019,"children":1020},{"style":178},[1021],{"type":36,"value":325},{"type":27,"tag":87,"props":1023,"children":1024},{"style":184},[1025],{"type":36,"value":1026}," response.choices[",{"type":27,"tag":87,"props":1028,"children":1029},{"style":288},[1030],{"type":36,"value":1031},"0",{"type":27,"tag":87,"props":1033,"children":1034},{"style":184},[1035],{"type":36,"value":1036},"].message.content.strip()\n",{"type":27,"tag":87,"props":1038,"children":1040},{"class":89,"line":1039},15,[1041,1045],{"type":27,"tag":87,"props":1042,"children":1043},{"style":178},[1044],{"type":36,"value":691},{"type":27,"tag":87,"props":1046,"children":1047},{"style":184},[1048],{"type":36,"value":1049}," sql_query\n",{"type":27,"tag":87,"props":1051,"children":1053},{"class":89,"line":1052},16,[1054],{"type":27,"tag":87,"props":1055,"children":1056},{"emptyLinePlaceholder":261},[1057],{"type":36,"value":264},{"type":27,"tag":87,"props":1059,"children":1061},{"class":89,"line":1060},17,[1062,1067,1071,1076,1081],{"type":27,"tag":87,"props":1063,"children":1064},{"style":184},[1065],{"type":36,"value":1066},"query ",{"type":27,"tag":87,"props":1068,"children":1069},{"style":178},[1070],{"type":36,"value":325},{"type":27,"tag":87,"props":1072,"children":1073},{"style":184},[1074],{"type":36,"value":1075}," generate_sql(",{"type":27,"tag":87,"props":1077,"children":1078},{"style":100},[1079],{"type":36,"value":1080},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":27,"tag":87,"props":1082,"children":1083},{"style":184},[1084],{"type":36,"value":1085},", schema_description)\n",{"type":27,"tag":87,"props":1087,"children":1089},{"class":89,"line":1088},18,[1090,1094],{"type":27,"tag":87,"props":1091,"children":1092},{"style":288},[1093],{"type":36,"value":731},{"type":27,"tag":87,"props":1095,"children":1096},{"style":184},[1097],{"type":36,"value":1098},"(query)\n",{"type":27,"tag":39,"props":1100,"children":1102},{"id":1101},"paso-5-ejecutar-consultas-generadas",[1103],{"type":36,"value":1104},"Paso 5: Ejecutar consultas generadas",{"type":27,"tag":32,"props":1106,"children":1107},{},[1108],{"type":36,"value":1109},"Ejecuta la consulta generada y obtiene resultados:",{"type":27,"tag":77,"props":1111,"children":1113},{"className":166,"code":1112,"language":168,"meta":8,"style":8},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[1114],{"type":27,"tag":15,"props":1115,"children":1116},{"__ignoreMap":8},[1117,1134,1156],{"type":27,"tag":87,"props":1118,"children":1119},{"class":89,"line":90},[1120,1125,1129],{"type":27,"tag":87,"props":1121,"children":1122},{"style":184},[1123],{"type":36,"value":1124},"result ",{"type":27,"tag":87,"props":1126,"children":1127},{"style":178},[1128],{"type":36,"value":325},{"type":27,"tag":87,"props":1130,"children":1131},{"style":184},[1132],{"type":36,"value":1133}," session.execute(query)\n",{"type":27,"tag":87,"props":1135,"children":1136},{"class":89,"line":200},[1137,1142,1147,1151],{"type":27,"tag":87,"props":1138,"children":1139},{"style":178},[1140],{"type":36,"value":1141},"for",{"type":27,"tag":87,"props":1143,"children":1144},{"style":184},[1145],{"type":36,"value":1146}," row ",{"type":27,"tag":87,"props":1148,"children":1149},{"style":178},[1150],{"type":36,"value":504},{"type":27,"tag":87,"props":1152,"children":1153},{"style":184},[1154],{"type":36,"value":1155}," result:\n",{"type":27,"tag":87,"props":1157,"children":1158},{"class":89,"line":222},[1159,1164],{"type":27,"tag":87,"props":1160,"children":1161},{"style":288},[1162],{"type":36,"value":1163},"    print",{"type":27,"tag":87,"props":1165,"children":1166},{"style":184},[1167],{"type":36,"value":1168},"(row)\n",{"type":27,"tag":39,"props":1170,"children":1172},{"id":1171},"paso-6-manejo-de-errores-y-seguridad",[1173],{"type":36,"value":1174},"Paso 6: Manejo de errores y seguridad",{"type":27,"tag":32,"props":1176,"children":1177},{},[1178],{"type":36,"value":1179},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":27,"tag":77,"props":1181,"children":1183},{"className":166,"code":1182,"language":168,"meta":8,"style":8},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[1184],{"type":27,"tag":15,"props":1185,"children":1186},{"__ignoreMap":8},[1187,1204,1217,1234,1247,1270,1313,1325,1332,1349],{"type":27,"tag":87,"props":1188,"children":1189},{"class":89,"line":90},[1190,1194,1199],{"type":27,"tag":87,"props":1191,"children":1192},{"style":178},[1193],{"type":36,"value":442},{"type":27,"tag":87,"props":1195,"children":1196},{"style":94},[1197],{"type":36,"value":1198}," execute_safe_query",{"type":27,"tag":87,"props":1200,"children":1201},{"style":184},[1202],{"type":36,"value":1203},"(sql_query):\n",{"type":27,"tag":87,"props":1205,"children":1206},{"class":89,"line":200},[1207,1212],{"type":27,"tag":87,"props":1208,"children":1209},{"style":178},[1210],{"type":36,"value":1211},"    try",{"type":27,"tag":87,"props":1213,"children":1214},{"style":184},[1215],{"type":36,"value":1216},":\n",{"type":27,"tag":87,"props":1218,"children":1219},{"class":89,"line":222},[1220,1225,1229],{"type":27,"tag":87,"props":1221,"children":1222},{"style":184},[1223],{"type":36,"value":1224},"        result ",{"type":27,"tag":87,"props":1226,"children":1227},{"style":178},[1228],{"type":36,"value":325},{"type":27,"tag":87,"props":1230,"children":1231},{"style":184},[1232],{"type":36,"value":1233}," session.execute(sql_query)\n",{"type":27,"tag":87,"props":1235,"children":1236},{"class":89,"line":235},[1237,1242],{"type":27,"tag":87,"props":1238,"children":1239},{"style":178},[1240],{"type":36,"value":1241},"        return",{"type":27,"tag":87,"props":1243,"children":1244},{"style":184},[1245],{"type":36,"value":1246}," result.fetchall()\n",{"type":27,"tag":87,"props":1248,"children":1249},{"class":89,"line":257},[1250,1255,1260,1265],{"type":27,"tag":87,"props":1251,"children":1252},{"style":178},[1253],{"type":36,"value":1254},"    except",{"type":27,"tag":87,"props":1256,"children":1257},{"style":288},[1258],{"type":36,"value":1259}," Exception",{"type":27,"tag":87,"props":1261,"children":1262},{"style":178},[1263],{"type":36,"value":1264}," as",{"type":27,"tag":87,"props":1266,"children":1267},{"style":184},[1268],{"type":36,"value":1269}," e:\n",{"type":27,"tag":87,"props":1271,"children":1272},{"class":89,"line":267},[1273,1278,1283,1287,1292,1296,1301,1305,1309],{"type":27,"tag":87,"props":1274,"children":1275},{"style":288},[1276],{"type":36,"value":1277},"        print",{"type":27,"tag":87,"props":1279,"children":1280},{"style":184},[1281],{"type":36,"value":1282},"(",{"type":27,"tag":87,"props":1284,"children":1285},{"style":178},[1286],{"type":36,"value":931},{"type":27,"tag":87,"props":1288,"children":1289},{"style":100},[1290],{"type":36,"value":1291},"\"Error al ejecutar consulta: ",{"type":27,"tag":87,"props":1293,"children":1294},{"style":288},[1295],{"type":36,"value":537},{"type":27,"tag":87,"props":1297,"children":1298},{"style":184},[1299],{"type":36,"value":1300},"e",{"type":27,"tag":87,"props":1302,"children":1303},{"style":288},[1304],{"type":36,"value":619},{"type":27,"tag":87,"props":1306,"children":1307},{"style":100},[1308],{"type":36,"value":955},{"type":27,"tag":87,"props":1310,"children":1311},{"style":184},[1312],{"type":36,"value":311},{"type":27,"tag":87,"props":1314,"children":1315},{"class":89,"line":276},[1316,1320],{"type":27,"tag":87,"props":1317,"children":1318},{"style":178},[1319],{"type":36,"value":1241},{"type":27,"tag":87,"props":1321,"children":1322},{"style":288},[1323],{"type":36,"value":1324}," None\n",{"type":27,"tag":87,"props":1326,"children":1327},{"class":89,"line":284},[1328],{"type":27,"tag":87,"props":1329,"children":1330},{"emptyLinePlaceholder":261},[1331],{"type":36,"value":264},{"type":27,"tag":87,"props":1333,"children":1334},{"class":89,"line":314},[1335,1340,1344],{"type":27,"tag":87,"props":1336,"children":1337},{"style":184},[1338],{"type":36,"value":1339},"results ",{"type":27,"tag":87,"props":1341,"children":1342},{"style":178},[1343],{"type":36,"value":325},{"type":27,"tag":87,"props":1345,"children":1346},{"style":184},[1347],{"type":36,"value":1348}," execute_safe_query(query)\n",{"type":27,"tag":87,"props":1350,"children":1351},{"class":89,"line":341},[1352,1356],{"type":27,"tag":87,"props":1353,"children":1354},{"style":288},[1355],{"type":36,"value":731},{"type":27,"tag":87,"props":1357,"children":1358},{"style":184},[1359],{"type":36,"value":1360},"(results)\n",{"type":27,"tag":39,"props":1362,"children":1364},{"id":1363},"buenas-prácticas",[1365],{"type":36,"value":1366},"Buenas prácticas",{"type":27,"tag":46,"props":1368,"children":1369},{},[1370,1375,1380],{"type":27,"tag":50,"props":1371,"children":1372},{},[1373],{"type":36,"value":1374},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":27,"tag":50,"props":1376,"children":1377},{},[1378],{"type":36,"value":1379},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":27,"tag":50,"props":1381,"children":1382},{},[1383],{"type":36,"value":1384},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":27,"tag":39,"props":1386,"children":1388},{"id":1387},"conclusión",[1389],{"type":36,"value":1390},"Conclusión",{"type":27,"tag":32,"props":1392,"children":1393},{},[1394],{"type":36,"value":1395},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":27,"tag":1397,"props":1398,"children":1400},"h1",{"id":1399},"recursos-adicionales",[1401],{"type":36,"value":1402},"Recursos adicionales",{"type":27,"tag":46,"props":1404,"children":1405},{},[1406,1418,1428],{"type":27,"tag":50,"props":1407,"children":1408},{},[1409],{"type":27,"tag":1410,"props":1411,"children":1415},"a",{"href":1412,"rel":1413},"https://platform.openai.com/docs/api-reference",[1414],"nofollow",[1416],{"type":36,"value":1417},"Documentación API OpenAI",{"type":27,"tag":50,"props":1419,"children":1420},{},[1421],{"type":27,"tag":1410,"props":1422,"children":1425},{"href":1423,"rel":1424},"https://docs.sqlalchemy.org/en/20/",[1414],[1426],{"type":36,"value":1427},"SQLAlchemy ORM Documentation",{"type":27,"tag":50,"props":1429,"children":1430},{},[1431],{"type":27,"tag":1410,"props":1432,"children":1435},{"href":1433,"rel":1434},"https://pypi.org/project/python-dotenv/",[1414],[1436],{"type":36,"value":1437},"Python Dotenv",{"type":27,"tag":1439,"props":1440,"children":1441},"style",{},[1442],{"type":36,"value":1443},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":8,"searchDepth":200,"depth":200,"links":1445},[1446,1447,1448,1449,1450,1451,1452,1453,1454],{"id":41,"depth":200,"text":44},{"id":67,"depth":200,"text":70},{"id":155,"depth":200,"text":158},{"id":392,"depth":200,"text":395},{"id":739,"depth":200,"text":742},{"id":1101,"depth":200,"text":1104},{"id":1171,"depth":200,"text":1174},{"id":1363,"depth":200,"text":1366},{"id":1387,"depth":200,"text":1390},"markdown","content:posts:gpt-sql-integration.md","content","posts/gpt-sql-integration.md","posts/gpt-sql-integration","md",1790510065094]