{"id":18581511,"url":"https://github.com/tonyh0401/langchain-mysql-gen-cli","last_synced_at":"2026-05-05T13:38:09.077Z","repository":{"id":251925278,"uuid":"837486066","full_name":"TonyH0401/langchain-mysql-gen-cli","owner":"TonyH0401","description":"Generate (optimized) MySQL/SQLite Query using LangChain running on CLI (Version 01)","archived":false,"fork":false,"pushed_at":"2024-08-06T12:45:38.000Z","size":611,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-16T04:07:21.465Z","etag":null,"topics":["gemini","langchain","langchain-python","mysql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TonyH0401.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-03T05:52:04.000Z","updated_at":"2024-08-06T12:50:03.000Z","dependencies_parsed_at":"2024-08-06T16:09:55.974Z","dependency_job_id":null,"html_url":"https://github.com/TonyH0401/langchain-mysql-gen-cli","commit_stats":null,"previous_names":["tonyh0401/langchain-mysql-gen-cli"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TonyH0401/langchain-mysql-gen-cli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TonyH0401%2Flangchain-mysql-gen-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TonyH0401%2Flangchain-mysql-gen-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TonyH0401%2Flangchain-mysql-gen-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TonyH0401%2Flangchain-mysql-gen-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TonyH0401","download_url":"https://codeload.github.com/TonyH0401/langchain-mysql-gen-cli/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TonyH0401%2Flangchain-mysql-gen-cli/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261995783,"owners_count":23242204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gemini","langchain","langchain-python","mysql","sqlite"],"created_at":"2024-11-07T00:05:48.722Z","updated_at":"2026-05-05T13:38:04.055Z","avatar_url":"https://github.com/TonyH0401.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Generate (Optimized) MySQL Query using LangChain(Python) on CLI (Version 01)\n\nWelcome MySQL Query Generation using Langchain(Python) Project! This project is designed to be a guide, testing and implementation for generating MySQL Query using LangChain.\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Getting Started](#getting-started)\n  - [Virtual Environment](#Virtual-Environment)\n  - [Installation](#installation)\n  - [Environment Variable](#Environment-Variable)\n- [Quick Start](#Quick-Start)\n- [Development Documentation](#development-documentation)\n\n## Introduction\n\nThis is my implementation of LangChain - An LLM Framework. There are 2 implementations using SQLite and MySQL (with MySQL there are 2 different versions). Each implementation has notes, synopsis, hints and further research included.\n\nThe original tutorial by LangChain that I based on is linked [here](https://python.langchain.com/v0.2/docs/tutorials/sql_qa/). I will also have the tutorial displayed in case the hyperlink is not accessible https://python.langchain.com/v0.2/docs/tutorials/sql_qa/. However, I don't totally follow the tutorial and there are variation within the implementation.\n\n## Getting Started\n\nI recommend running this project on **Python 3.10+**. This project was originally running on **Python 3.9.19**.\n\n### Virtual Environment\n\nA virtual environment should be setup for this project. You can use any of yours preferable virtual environment, I will use Anaconda/Miniconda as the Virtual Environment for this project.\n\n### Installation\n\nTo get started, you need to download this project from Github and navigate to the project's folder.\n\n```sh\ncd langchain-mysql-gen-cli/\n```\n\nDowloading the project's dependencies from `requirements.txt` file.\n\n```sh\npip install -r requirements.txt\n```\n\nI also have a [link](https://chatgpt.com/share/757c50b4-f574-48d0-a04d-c955d100aeab) to support you in this process.\n\n### Environment Variable\n\nThis step is **important**! Create an `.env` file to store your API KEY(s). These are the API KEY(s) you will need. Currently, in this project, I am using Google's Gemini API but you can change it to any LLM(s) you prefer.\n\n```sh\nGOOGLE_API_KEY=\"\"\n```\n\n## Quick Start\n\nMost of the functions are defined in `main.py`. Run the project using the following command(s).\n\n```sh\npython main.py\n```\n\n## Development Documentation\n\nOrder from newest to oldest.\n\n### 06/08/2024\n\n- Problem: Currently, we are using raw schemas to generate queries. I learnt that markdown and json format is best used for LLM (in this\nparitcular case), so I want to convert the raw schemas into json then into markdown. I have several method of approach.\n    - Original method: Code the json formatter and markdown by yourself. **This I implemented**.\n    - Structured Output Parser method: Using LLM + output_parser_structure, the LLM will read the raw schemas and the output parser will return structured result. However, with this method, we need to pre-defined the structure and the properties we want to take out first (not automatically generated). \n        - https://python.langchain.com/v0.2/docs/how_to/structured_output/ (gotta read it all, I stopped at Few-shot)\n        - https://python.langchain.com/v0.2/docs/how_to/#output-parsers.\n    - Fully LLM method: Another approach is to ask the LLM (fully) to extract the columns inside the raw schemas, basically letting the LLM do  all the work (reading and extracting from raw schemas). This has a high change of hallucination. **This I implemented**, it is pretty accurate but it is pretty slow as well.\n\n### 03/08/2024\n\n- In LangChain, there is a function (and toolkits) for SQL, this function already has a default prompt but you can define your own prompt (I did implement this method in the project).\n\n- LangChain's SQL tutorial:\n  - https://python.langchain.com/v0.2/docs/tutorials/sql_qa/.\n  - https://api.python.langchain.com/en/latest/chains/langchain.chains.sql_database.query.create_sql_query_chain.html, definition of a function used.\n  - https://python.langchain.com/v0.2/docs/how_to/#use-cases, list of use cases for SQL RAG.\n\n- Here is an old result from a deleted implementation:\n```mysql\n    SELECT\n        tt.ticket_type_id,\n        tt.ticket_type_name,\n        SUM(bd.quantity) AS total_sold\n    FROM TicketType AS tt\n    JOIN BookingDetail AS bd\n    ON tt.ticket_type_id = bd.ticket_type_id\n    JOIN Booking AS b\n    ON bd.booking_id = b.booking_id\n    WHERE\n        b.created_at \u003e= DATE_FORMAT(NOW(), '%Y-01-01')\n    GROUP BY\n        tt.ticket_type_id,\n        tt.ticket_type_name\n    ORDER BY\n        total_sold DESC\n    LIMIT 50;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonyh0401%2Flangchain-mysql-gen-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftonyh0401%2Flangchain-mysql-gen-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonyh0401%2Flangchain-mysql-gen-cli/lists"}