From d1c7b5b56fe4b7ff0203993d511c31e67d25ffcf Mon Sep 17 00:00:00 2001 From: Filipe Moreira Date: Mon, 25 Sep 2023 17:54:23 +0400 Subject: [PATCH] fixing the SQL URL creation using SQLAlchemy --- application/AzureOpenAIUtil/SqlServer.py | 22 +++++++++++++++------- application/requirements.txt | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/application/AzureOpenAIUtil/SqlServer.py b/application/AzureOpenAIUtil/SqlServer.py index acecfa4..2ae6012 100644 --- a/application/AzureOpenAIUtil/SqlServer.py +++ b/application/AzureOpenAIUtil/SqlServer.py @@ -3,20 +3,28 @@ from langchain.callbacks.base import CallbackManager from langchain.llms.openai import AzureOpenAI from langchain.agents import AgentExecutor +from sqlalchemy.engine.url import URL class SqlServer: cb_handler = HtmlCallbackHandler() cb_manager = CallbackManager(handlers=[cb_handler]) def __init__(self, llm, Server, Database, Username, Password, port=1433, odbc_ver=18, topK=10) -> None: + + db_config = { + 'drivername': 'mssql+pyodbc', + 'username': Username, + 'password': Password, + 'host': Server, + 'port': 1433, + 'database': Database, + 'query': {'driver': 'ODBC Driver '+ str(odbc_ver) +' for SQL Server'} + } + #print(db_config) + db_url = URL.create(**db_config) + #print(db_url) - odbc_conn = 'Driver={ODBC Driver '+ str(odbc_ver) + ' for SQL Server};Server=tcp:' + \ - Server + f';Database={Database};Uid={Username};Pwd={Password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;' - params = urllib.parse.quote_plus(odbc_conn) - self.conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params) - - - db = SQLDatabase.from_uri(self.conn_str) + db = SQLDatabase.from_uri(db_url) self.toolkit = SQLDatabaseToolkit(db=db, callback_manager=self.cb_manager) # print(deploy_name) self.agent_executor = create_sql_agent(llm, diff --git a/application/requirements.txt b/application/requirements.txt index aa196db..74ead82 100644 --- a/application/requirements.txt +++ b/application/requirements.txt @@ -27,3 +27,4 @@ tenacity==8.2.2 tiktoken==0.3.3 urllib3==1.26.14 Werkzeug==2.2.3 +sqlalchemy<2.0.0