Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tKzph-00BzYw-OI for pgsql-general@arkaria.postgresql.org; Tue, 10 Dec 2024 12:59:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tKzpf-0043ci-92 for pgsql-general@arkaria.postgresql.org; Tue, 10 Dec 2024 12:59:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tKzpe-0043cZ-U8 for pgsql-general@lists.postgresql.org; Tue, 10 Dec 2024 12:59:20 +0000 Received: from mail-yb1-xb36.google.com ([2607:f8b0:4864:20::b36]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tKzpd-0025AU-Lv for pgsql-general@postgresql.org; Tue, 10 Dec 2024 12:59:18 +0000 Received: by mail-yb1-xb36.google.com with SMTP id 3f1490d57ef6-e3a58827e25so1127418276.3 for ; Tue, 10 Dec 2024 04:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733835557; x=1734440357; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=divIWZ5tPrH92UaYYFFumWs8PEzF1gq2QwfbqxXeA2U=; b=PfMYPAGRge6WJjcEdPRiMKyN1SQr1jkNyM4yCz6AGEqXR9KTXB5zmMZBowbAmohEN/ qKLAr9G26DrFwvMaHmr8wt/HUKwYGMldRBz4w3uU8yppbi5ZIHyfxCpLcYSVyZ7CfcL9 wi8VuU78oXs1J6YwlyMRviiN+33Jj3510EHTjNuZzaMAlN7UtWuA3pDjEg0QbPYMMJk9 Talpzd8i2RUA340og0YGYaAaJKWpLHaabhIda2+ewpVYI2f9paf6RmFZUbjZ+a/a75hX fhLZKecfwMoTFYu64kGxewkwk+VqVDMDwSmdJcwl70uB5wHEvbAFnQqiE4kIbuxZNTlX w4uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733835557; x=1734440357; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=divIWZ5tPrH92UaYYFFumWs8PEzF1gq2QwfbqxXeA2U=; b=gmIGHoOR3Y/fWXuTmi3OlC5YmuSGSUnEKw3zkIGkGnDgeQ0bC88iexsELSDshawSAn Rfiei1tsau5WsDfiPruR3Fv74D2rPGP9tgdYLh9arlt9lJRQT+ANxN/EM68VsD7TZyaA h3F6J5T17IXUQ0GszN7cBUZlm8pydDPCqizYodyuiZCxadDLyw6F6RlmlotQw1aWW+rM PR8VSUQ3zfEibLAXkwIgTWoX+Xxz6Tv/yTP11Cfm5+bXusw83Wjp3oPqA3XF4LoucP3b DCxUSdNmpukkCHsnc2fsY6xAhpZdRs9yy+5Ag8vTOjPO6RykCqOH3rD4jkmVO+jMf1T+ OkYg== X-Gm-Message-State: AOJu0YwqVL2IyxbE0/LWpYZz18omaMGqOvkODEe/Zx1w+gp8UxDTQsOZ lwVUooWErWL5YHdXfSQ8zfonCAkiLfnHGnyaqk4xbXo79bIPPtTOpPw4UW6wJaE74UFMrfUNRZ/ +ZeuP23yiFaBnLwbaV+TJhHLMDbfg5tkHD5k= X-Gm-Gg: ASbGncsTB1D1bKHb/MUwI7tHce6YRdX90+OUAWa9aV+REzJLeX7F3yNCMpcrtGfRtHt 3XRe2wmZYCFPSw3LBvrSCJ86FEIbo6C4nMo9F X-Google-Smtp-Source: AGHT+IFOsps+N4CiPOOjeYIE6nJ2ziJPjXIOuhHb0UxtxogsAOO/+DKvRWH2ZIze0lmusyg+e+ix+6892w7yMCKcc6c= X-Received: by 2002:a05:6902:3402:b0:e39:7170:c2ac with SMTP id 3f1490d57ef6-e3a0b485e23mr11945062276.53.1733835556649; Tue, 10 Dec 2024 04:59:16 -0800 (PST) MIME-Version: 1.0 From: Allan Kamau Date: Tue, 10 Dec 2024 15:59:00 +0300 Message-ID: Subject: Formulating SQL queries via Retrieval Augmented Generation (RAG). To: Postgres General Postgres General Content-Type: multipart/alternative; boundary="0000000000007405030628ea0c53" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000007405030628ea0c53 Content-Type: text/plain; charset="UTF-8" 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 --0000000000007405030628ea0c53 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I am not sure if th= is is the right=C2=A0place to post a PostgreSQL + Retrieval Augmented Gener= ation (RAG) question, but I have tried other=C2=A0sites but I did not recei= ve any response.

I have managed to use RAG to query dat= a 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 e= dge graph data).
The SQL queries generated by RAG for this graph = data model are error prone.
Is there an example somewhere=C2=A0wh= ere RAG has been used to successfully generate WITH RECURSIVE CTE on two ta= ble graph data?

Below is the main code section I am usin= g to perform RAG on PostgreSQL.

    llm =3D ChatGroq(model=3D&q=
uot;mixtral-8x7b-32768", temperature=3D0);
   =20
    chain =3D (
        RunnablePassthrough.assign(query=3Dsql_chain).assign(
            schema=3Dlambda _: db.get_table_info(),
            response=3Dlambda vars: db.run(vars["query"]),
        )
        | prompt
        | llm
    )
    response_obj: langchain_core.messages.ai.AIMessage=3Dchain.invoke({
        "question": user_query_str,
        "chat_history": chat_history_list,
    });


Any pointers are welcome.=

-Allan
--0000000000007405030628ea0c53--