Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14011

Error trying to use SQLDatabaseChain with Aurora MySQL

$
0
0

I'm trying to tinker with Langchain following this example (https://coinsbench.com/chat-with-your-databases-using-langchain-bb7d31ed2e76). Instead of using PostgreSQL I'm trying to connect to a MySQL database. Everything looks accurate however I am getting an attribute error when trying to run the python script. This is the code:

from langchain.llms import OpenAIfrom langchain.utilities import SQLDatabasefrom langchain_experimental.sql import SQLDatabaseChain# Accessing the OpenAI Keyimport environenv = environ.Env()environ.Env.read_env()API_KEY = env('OPENAI_API_KEY')HOST = env('HOST')DBUSER = env('DBUSER')PASSWPORD = env('DBPASS')DATABASE = env('DATABASE')# Create the database connectionmysql_uri = f"mysql+pymysql://{DBUSER}:{PASSWPORD}@{HOST}:3306/{DATABASE}"db = SQLDatabase.from_uri(mysql_uri)# Setup llmllm = OpenAI(temperature=0, openai_api_key=API_KEY)# Create db chainQUERY = """Given an input question, first create a syntactically correct mysql query to run, then look at the results of the query and return the answer.Use the following format:Question: Question hereSQLQuery: SQL Query to runSQLResult: Result of the SQLQueryAnswer: Final answer here{question}"""# Setup the database chaindb_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)def get_prompt():    print("Type 'exit' to quit")    while True:        prompt = input("Enter a prompt: ")        if prompt.lower() == 'exit':            print('Exiting...')            break        else:            try:                question = QUERY.format(question=prompt)                print(db_chain.run(question))            except Exception as e:                print(e)get_prompt()

This is the error I am getting:

Traceback (most recent call last):  File "/Users/StarScr3am77/Documents/Workspace/Hustle/tracking-bot/app.py", line 18, in <module>    db = SQLDatabase.from_uri(mysql_uri)         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/langchain_community/utilities/sql_database.py", line 124, in from_uri    return cls(create_engine(database_uri, **_engine_args), **kwargs)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/langchain_community/utilities/sql_database.py", line 65, in __init__    self._inspector.get_table_names(schema=schema)  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py", line 396, in get_table_names    return self.dialect.get_table_names(           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "<string>", line 2, in get_table_names  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py", line 97, in cache    ret = fn(self, con, *args, **kw)          ^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/base.py", line 2924, in get_table_names    % self.identifier_preparer.quote_identifier(current_schema)      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/sqlalchemy/sql/compiler.py", line 7356, in quote_identifier+ self._escape_identifier(value)      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/Users/StarScr3am77/Documents/tracking-bot/venv/lib/python3.11/site-packages/sqlalchemy/sql/compiler.py", line 7315, in _escape_identifier    value = value.replace(self.escape_quote, self.escape_to_quote)            ^^^^^^^^^^^^^AttributeError: 'NoneType' object has no attribute 'replace'

Any help would be greatly appreciated!


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>