47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
from .models import BotInteraction, FacebookPageAssistant
|
|
from pxy_neo4j.neo4j_connector import Neo4jDatabase
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class InteractionStorageBackend:
|
|
"""Interface: implement `.store(...)`."""
|
|
def store(self, *, page_id, object_id, parent_object_id,
|
|
prompt, bot_response, platform):
|
|
raise NotImplementedError
|
|
|
|
|
|
class Neo4jStorage(InteractionStorageBackend):
|
|
def __init__(self):
|
|
self.neo4j_db = Neo4jDatabase()
|
|
|
|
def store(self, *, page_id, object_id, parent_object_id,
|
|
prompt, bot_response, platform):
|
|
try:
|
|
self.neo4j_db.store_interaction(
|
|
user_id = f"fb_bot_{page_id}",
|
|
bot_id = f"fb_bot_{page_id}",
|
|
user_message = prompt,
|
|
bot_response = bot_response,
|
|
platform = platform
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"[Neo4j] Error saving interaction: {e}")
|
|
|
|
|
|
class PostgresStorage(InteractionStorageBackend):
|
|
def store(self, *, page_id, object_id, parent_object_id,
|
|
prompt, bot_response, platform):
|
|
try:
|
|
page = FacebookPageAssistant.objects.get(page_id=page_id)
|
|
BotInteraction.objects.create(
|
|
page = page,
|
|
object_id = object_id or "",
|
|
parent_object_id = parent_object_id,
|
|
prompt = prompt,
|
|
bot_response = bot_response,
|
|
platform = platform
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"[Postgres] Error saving BotInteraction: {e}")
|