Formulating SQL queries via Retrieval Augmented Generation (RAG).

From: Allan Kamau <kamauallan(at)gmail(dot)com>
To: Postgres General Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Formulating SQL queries via Retrieval Augmented Generation (RAG).
Date: 2024-12-10 12:59:00
Message-ID: CAF3N6oQ4GeF93L34FJ=E5GoYBHXv7H4H0gK4FANgv=fHORoN4Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am not sure if this is the right place to post a PostgreSQL + Retrieval
Augmented Generation (RAG) question, but I have tried other sites but I did
not receive any response.

I have managed to use RAG to query data in a PostgreSQL table using Python
and the lanchain_community module.

But I have been struggling for weeks trying to find a solution to using RAG
to perform WITH RECURSIVE CTE on two tables (modeling node and edge graph
data).
The SQL queries generated by RAG for this graph data model are error prone.
Is there an example somewhere where RAG has been used to successfully
generate WITH RECURSIVE CTE on two table graph data?

Below is the main code section I am using to perform RAG on PostgreSQL.

llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0);

chain = (
RunnablePassthrough.assign(query=sql_chain).assign(
schema=lambda _: db.get_table_info(),
response=lambda vars: db.run(vars["query"]),
)
| prompt
| llm
)
response_obj: langchain_core.messages.ai.AIMessage=chain.invoke({
"question": user_query_str,
"chat_history": chat_history_list,
});

Any pointers are welcome.

-Allan

Browse pgsql-general by date

  From Date Subject
Next Message Felipe Matas 2024-12-10 15:59:43 Clusters and shared permissions using LDAP
Previous Message Adrian Klaver 2024-12-10 05:31:15 Re: Errors when restoring backup created by pg_dumpall