zenaight commited on
Commit
483b968
·
1 Parent(s): 829620f

Update database table references for user personas

Browse files

- Changed references from "personas" to "user_personas" in the `get_user_persona` and `update_user_persona` functions to align with the updated database schema.
- This modification ensures consistency in data handling and improves the accuracy of user persona management.

Files changed (2) hide show
  1. ai_chat.py +12 -2
  2. database.py +4 -4
ai_chat.py CHANGED
@@ -87,11 +87,21 @@ async def extract_and_update_persona(state):
87
  # c. Call the LLM
88
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
89
  import json
 
90
  extracted = {}
91
  try:
92
- extracted = json.loads(response.content)
93
- except:
 
 
 
 
 
 
 
 
94
  print("Failed to parse persona JSON:", response.content)
 
95
 
96
  # d. Update DB and in-memory state for any changed values
97
  for field in persona_fields:
 
87
  # c. Call the LLM
88
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
89
  import json
90
+ import re
91
  extracted = {}
92
  try:
93
+ # Clean up the response content (remove markdown formatting if present)
94
+ content = response.content.strip()
95
+ if content.startswith('```json'):
96
+ content = content[7:] # Remove ```json
97
+ if content.endswith('```'):
98
+ content = content[:-3] # Remove ```
99
+ content = content.strip()
100
+
101
+ extracted = json.loads(content)
102
+ except Exception as e:
103
  print("Failed to parse persona JSON:", response.content)
104
+ print("Error:", e)
105
 
106
  # d. Update DB and in-memory state for any changed values
107
  for field in persona_fields:
database.py CHANGED
@@ -194,7 +194,7 @@ async def get_user_persona(wa_id: str) -> dict:
194
 
195
  try:
196
  resp = supabase\
197
- .table("personas")\
198
  .select("*")\
199
  .eq("wa_id", wa_id)\
200
  .single()\
@@ -216,12 +216,12 @@ async def update_user_persona(wa_id: str, updates: dict):
216
  updates_with_ts = {**updates, "updated_at": datetime.utcnow().isoformat()}
217
 
218
  # First try to get existing persona
219
- existing = supabase.table("personas").select("*").eq("wa_id", wa_id).execute()
220
 
221
  if existing.data:
222
  # Update existing record
223
  supabase\
224
- .table("personas")\
225
  .update(updates_with_ts)\
226
  .eq("wa_id", wa_id)\
227
  .execute()
@@ -232,7 +232,7 @@ async def update_user_persona(wa_id: str, updates: dict):
232
  "created_at": datetime.utcnow().isoformat(),
233
  **updates_with_ts
234
  }
235
- supabase.table("personas").insert(new_persona).execute()
236
 
237
  except Exception as e:
238
  print(f"Error updating user persona: {e}")
 
194
 
195
  try:
196
  resp = supabase\
197
+ .table("user_personas")\
198
  .select("*")\
199
  .eq("wa_id", wa_id)\
200
  .single()\
 
216
  updates_with_ts = {**updates, "updated_at": datetime.utcnow().isoformat()}
217
 
218
  # First try to get existing persona
219
+ existing = supabase.table("user_personas").select("*").eq("wa_id", wa_id).execute()
220
 
221
  if existing.data:
222
  # Update existing record
223
  supabase\
224
+ .table("user_personas")\
225
  .update(updates_with_ts)\
226
  .eq("wa_id", wa_id)\
227
  .execute()
 
232
  "created_at": datetime.utcnow().isoformat(),
233
  **updates_with_ts
234
  }
235
+ supabase.table("user_personas").insert(new_persona).execute()
236
 
237
  except Exception as e:
238
  print(f"Error updating user persona: {e}")