Write PostgreSQL query from natural language using on-prem gen AI -- with IBM Granite Code LLM, Ollama on Juypter Notebook.
Inspired by a use case mentioned by Anthropic -- but is a local implementation with a minimal setup.
Jupyter Notebook https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
LangChain https://python.langchain.com/docs/how_to/installation/
Ollama https://ollama.com/download
Pull IBM Granite Code
model in Terminal:
ollama pull granite-code:8b-instruct
- Open
sqlgen.ipynb
- Enter your database schema in
schema =
- Enter your question in
user_prompt =
- Click
Run all
to run all the cells in the Juypter Notebook - Scroll to the bottom for the SQL query generated
Gen AI is non-deterministic. With the same user prompt, it generates slightly different completions every time. Verify before use.
As this involves interacting LLM with database. The codes could be reused in database agent in multiagent workflow.