I am trying to use RAG to query Graph data that is persisted in a PostgreSQL 17.0 database.
I have graph data modeled as one node table and one edge table. The edge table contains the node to node relationships.
I can manually query the graph using WITH RECURSIVE CTE, but when I submit an equivalent query in natural language to the llm via langchain, the SQL query generated has several issues such as assuming the node_ids in the edge table are arrays and it seems not to construct a WITH RECURSIVE CTE to query the data.
Is there a way I can give examples to the llm on how the SQL query should be constructed?
The llm model I am using is shown below.
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,
});