ivxivx commited on
Commit
6e4167c
·
unverified ·
1 Parent(s): e523149

chore: code agent

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -247,24 +247,30 @@ def predict(message, history):
247
  return analysis_response
248
 
249
  # payment is required to use the SQL agent, error: Subscribe to PRO to get 20x more monthly
250
- # sql_prompt = f"""
251
- # Given the '{transaction_type}', find the corresponding table name in the database, the find a record with given id '{transaction_id}' from the table, return the record as JSON. If there is no record, return null.
252
 
253
- # Example:
254
- # Input: transaction_type: payout, transaction_id: payout-abc456
255
- # Record in payouts: {{"id": "payout-abc456", "amount": 50.0, "created_at": "2021-01-01 00:00:00"}}
256
- # Response: {{"id": "payout-abc456", "amount": 50.0, "created_at": "2021-01-01 00:00:00"}}
257
 
258
- # Input: transaction_type: payout, transaction_id: null
259
- # Record in payouts: None
260
- # Response: null
261
- # """
262
-
263
- # sql_response = agent.run(sql_prompt)
264
 
265
- # Directly use the sql_engine tool without CodeAgent to avoid payment
266
- table = payment_table if transaction_type == "payment" else payout_table
267
- sql_response = table.select().where(table.c.id == '{transaction_id}')
 
 
 
 
 
 
 
 
268
 
269
  # print(f"SQL response: {sql_response}\n")
270
 
 
247
  return analysis_response
248
 
249
  # payment is required to use the SQL agent, error: Subscribe to PRO to get 20x more monthly
250
+ sql_prompt = f"""
251
+ Given the '{transaction_type}', find the corresponding table name in the database, the find a record with given id '{transaction_id}' from the table, return the record as JSON. If there is no record, return null.
252
 
253
+ Example:
254
+ Input: transaction_type: payout, transaction_id: payout-abc456
255
+ Record in payouts: {{"id": "payout-abc456", "amount": 50.0, "created_at": "2021-01-01 00:00:00"}}
256
+ Response: {{"id": "payout-abc456", "amount": 50.0, "created_at": "2021-01-01 00:00:00"}}
257
 
258
+ Input: transaction_type: payout, transaction_id: null
259
+ Record in payouts: None
260
+ Response: null
261
+ """
 
 
262
 
263
+ try:
264
+ sql_response = agent.run(sql_prompt)
265
+ except Exception as e:
266
+ print(f"An error occurred while running the SQL agent: {e}")
267
+ try:
268
+ analysis_json = json.loads(analysis_response)
269
+ analysis_json["database_result"] = "Error running SQL agent: " + str(e)
270
+ return json.dumps(analysis_json, ensure_ascii=False)
271
+ except Exception:
272
+ # If analysis_response is not valid JSON, return both as plain text
273
+ return analysis_response
274
 
275
  # print(f"SQL response: {sql_response}\n")
276