Commit
·
e5e3372
1
Parent(s):
8a155b4
erase duplicate code
Browse files
app.py
CHANGED
|
@@ -147,55 +147,6 @@ def create_agent():
|
|
| 147 |
|
| 148 |
logger.info("Starting agent creation process...")
|
| 149 |
|
| 150 |
-
# Step 1: Set up database connection
|
| 151 |
-
logger.info("Setting up database connection...")
|
| 152 |
-
db, db_error = setup_database_connection()
|
| 153 |
-
if not db:
|
| 154 |
-
error_msg = f"Failed to connect to database: {db_error}"
|
| 155 |
-
logger.error(error_msg)
|
| 156 |
-
else:
|
| 157 |
-
logger.info("Database connection successful")
|
| 158 |
-
|
| 159 |
-
# Step 2: Initialize LLM
|
| 160 |
-
logger.info("Initializing language model...")
|
| 161 |
-
llm, llm_error = initialize_llm()
|
| 162 |
-
if not llm:
|
| 163 |
-
error_msg = f"Failed to initialize language model: {llm_error}"
|
| 164 |
-
logger.error(error_msg)
|
| 165 |
-
else:
|
| 166 |
-
logger.info("Language model initialized successfully")
|
| 167 |
-
|
| 168 |
-
# Check if both components are available
|
| 169 |
-
if not db or not llm:
|
| 170 |
-
error_msg = f"Cannot create agent. {db_error if not db else ''} {llm_error if not llm else ''}"
|
| 171 |
-
logger.error(error_msg)
|
| 172 |
-
return None, error_msg
|
| 173 |
-
|
| 174 |
-
try:
|
| 175 |
-
logger.info("Creating SQL agent...")
|
| 176 |
-
agent = create_sql_agent(
|
| 177 |
-
llm=llm,
|
| 178 |
-
db=db,
|
| 179 |
-
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 180 |
-
verbose=True
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
# Test the agent with a simple query
|
| 184 |
-
try:
|
| 185 |
-
logger.info("Testing agent with a simple query...")
|
| 186 |
-
test_result = agent.invoke({"input": "What tables are available?"})
|
| 187 |
-
logger.info(f"Agent test response: {str(test_result)[:200]}...") # Log first 200 chars
|
| 188 |
-
except Exception as test_error:
|
| 189 |
-
logger.warning(f"Agent test query failed (this might be expected): {str(test_error)}")
|
| 190 |
-
|
| 191 |
-
logger.info("SQL agent created and tested successfully")
|
| 192 |
-
return agent, ""
|
| 193 |
-
|
| 194 |
-
except Exception as e:
|
| 195 |
-
error_msg = f"Error creating SQL agent: {str(e)}"
|
| 196 |
-
logger.error(error_msg, exc_info=True) # Include full stack trace
|
| 197 |
-
return None, error_msg
|
| 198 |
-
|
| 199 |
def create_agent(llm, db_connection):
|
| 200 |
"""Create and return a SQL database agent."""
|
| 201 |
if not llm:
|
|
|
|
| 147 |
|
| 148 |
logger.info("Starting agent creation process...")
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def create_agent(llm, db_connection):
|
| 151 |
"""Create and return a SQL database agent."""
|
| 152 |
if not llm:
|