Nitesh Addagatla
Nitesh Addagatla

Reputation: 21

Langchain SQLDatabaseChain error due to SQL formatting

import os
from dotenv import load_dotenv
load_dotenv()

import google.generativeai as genai
from langchain_google_genai import GoogleGenerativeAI

# Ensure your GOOGLE_API_KEY is set in the .env file
api_key = os.getenv("GEMINI_API_KEY") 

# Use GoogleGenerativeAI instead of GooglePalm
llm = GoogleGenerativeAI(model="models/gemini-1.0-pro", 
                         google_api_key=api_key, 
                         temperature=0.1)
from langchain_experimental.sql import SQLDatabaseChain
db_chain = SQLDatabaseChain.from_llm(
                                      llm=llm, 
                                      db=db,
                                      verbose=True,
                                      use_query_checker=True)
result = db_chain.run("What is the min date?")

OUTPUT:

Entering new SQLDatabaseChain chain... What is the minimum date? SQLQuery:'''sql SELECT MIN(date) AS min_date FROM gcs_data; '''

Due to this ''' sql I'm getting the error from Google Big Query as: Syntax error: Unexpected identifier ``` at [1:1]

How to solve this problem...?

I tried to find some solutions using blackbox.ai and chatgpt as well, but couldn't get the issue resolved. Can anyone help me out to remove this error.

Upvotes: 1

Views: 294

Answers (1)

Nitesh Addagatla
Nitesh Addagatla

Reputation: 21

I have solved this problem, just add "Generate SQL without pre-amble" at the end of every prompt. This will create queries without '''sql SELECT * FROM gcs_table '''

Now you'll only get: SELECT * FROM gcs_table

as an output which will easily run the query in DataBase

Upvotes: 1

Related Questions