Langchain recursive sql query

169 Views Asked by At

langchain-experimental : 0.0.42

langchain : 0.0.340

gpt4all : 2.0.2

PostgreSQL : 15.5

I am trying to query my postgres database using GPT4All in langchain. Below is the code that I am using:

from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_experimental.sql import SQLDatabaseChain
from langchain.memory import ConversationBufferMemory
from langchain import SQLDatabase
from langchain.llms import GPT4All
from langchain.prompts import PromptTemplate
from langchain.globals import set_verbose
import os

username = "postgres" 
password = "password" 
host = "127.0.0.1" # internal IP 
port = "5432"
mydatabase = "reporting_db"

pg_uri = f'postgresql://{username}:{password}@{host}:{port}/{mydatabase}'
my_db = SQLDatabase.from_uri(pg_uri)


_DEFAULT_TEMPLATE = '''Given an input question, first create a syntactically correct 
 {dialect} query to run, 
 then look at the results of the query and return the answer.
 Use the following format:
 Question: "Question here"
 SQLQuery: "SQL Query to run"
 SQLResult: "Result of the SQLQuery"
 Answer: "Final answer here"

 Only use the following tables:

 {table_info}

 If someone asks for the book written, they really mean the work table.
 Question: {input}'''

 PROMPT = PromptTemplate(
    input_variables=["input", "table_info", "dialect"], 
    template=_DEFAULT_TEMPLATE
 )

 path = "/var/lib/postgresql/data/llama-2-7b.Q2_K.gguf"

 callbacks = [StreamingStdOutCallbackHandler()]

 llm = GPT4All(model = path,
              callbacks=callbacks,
              n_threads=8,
              max_tokens=81920,
              verbose=True
             )

 set_verbose(True)
 db_chain = SQLDatabaseChain.from_llm(llm = llm, 
                                     db = my_db,
                                     prompt = PROMPT,
                                     use_query_checker=True,
                                     verbose = True
                                    )

 question = 'Count the rows on table Access'
 answer = db_chain(question)
 print(answer)

but I am getting the following error:

ERROR:  sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at 
or near "```"
LINE 1: ```sql
    ^

[SQL: ```sql
SELECT * FROM (
    SELECT * FROM (
          SELECT * FROM (
               SELECT * FROM ( 
                              ...
                                SELECT * FROM (
                                    SELECT * FROM (
                                        SELECT * FROM (
                                                        ...

                                                        SELECT * FROM (
                                                              SELECT * FROM (
                                                                    SELECT * FROM (
                                                                          SELECT * 
   FROM (
                                                                                    
   SELECT * FROM (
                                                                                                
   SELECT * FROM (
                                                                                                            
         ...
                                                                                                                    
   SELECT * FROM (
                                                                                                                        
   SELECT * FROM (                                                                                                                         
                                                                                                                                            
   SELECT * FROM (]

Were does this error coming from and what does this error means? How can I modify the code to get the correct output from the language model?

0

There are 0 best solutions below