chore: code agent
Browse files
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 |
-
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
# sql_response = agent.run(sql_prompt)
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|