philocifer commited on
Commit
193d7d4
·
1 Parent(s): 4029738

Fixed sql agent

Browse files
Files changed (1) hide show
  1. sql_agent.py +13 -7
sql_agent.py CHANGED
@@ -80,9 +80,12 @@ def load_agent():
80
  model="gpt-4o-mini"
81
  )
82
 
 
83
  engine = sa.create_engine(
84
- "sqlite:///:memory:", # Changed from file-based to in-memory database
85
- poolclass=sa.pool.SingletonThreadPool
 
 
86
  )
87
 
88
  # Add this distance function to SQL
@@ -95,11 +98,14 @@ def load_agent():
95
  table_name = 'competitor_stores'
96
 
97
  df = pd.read_json(json_file)
98
-
99
- with engine.connect() as conn:
100
- if not conn.dialect.has_table(conn, table_name):
101
- df.to_sql(table_name, conn, index=False, if_exists='replace')
102
- conn.commit()
 
 
 
103
 
104
  db = SQLDatabase(engine)
105
  toolkit = SQLDatabaseToolkit(
 
80
  model="gpt-4o-mini"
81
  )
82
 
83
+ # Configure engine for thread-safe in-memory database
84
  engine = sa.create_engine(
85
+ "sqlite:///:memory:?cache=shared",
86
+ poolclass=sa.pool.QueuePool, # Use QueuePool for thread safety
87
+ connect_args={'check_same_thread': False}, # Allow cross-thread connections
88
+ echo=True
89
  )
90
 
91
  # Add this distance function to SQL
 
98
  table_name = 'competitor_stores'
99
 
100
  df = pd.read_json(json_file)
101
+
102
+ with engine.begin() as conn:
103
+ df.to_sql(
104
+ table_name,
105
+ conn,
106
+ index=False,
107
+ if_exists='replace'
108
+ )
109
 
110
  db = SQLDatabase(engine)
111
  toolkit = SQLDatabaseToolkit(