PixDiet / db_client.py
blanchon's picture
Initial commit
41039aa
raw
history blame
1.11 kB
from supabase import create_client, Client
import os
import json
from dotenv import load_dotenv
load_dotenv()
# Initialize Supabase client
supabase: Client = create_client(os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_KEY"))
def get_user_history(user_id):
response = supabase.table("history").select("messages").eq("user", user_id).order("created_at", desc=True).limit(1).execute()
if response.data:
return response.data[0]['messages']
return []
def update_user_history(user_id, new_history):
# Filter out messages with image type
filtered_history = [
message for message in new_history
if not (message["role"] == "user" and
isinstance(message["content"], list) and
any(item.get("type") == "image" for item in message["content"]))
]
# Insert the filtered history into the database
supabase.table("history").insert({"user": user_id, "messages": filtered_history}).execute()
def delete_user_history(user_id):
response = supabase.table("history").delete().eq("user", user_id).execute()
return response.data