add storage option wit neo4j and postgress DB
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b77133fe83
commit
0c55724caa
@ -4,6 +4,7 @@ import logging
|
|||||||
from .models import FacebookPageAssistant
|
from .models import FacebookPageAssistant
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from pxy_neo4j.neo4j_connector import Neo4jDatabase
|
from pxy_neo4j.neo4j_connector import Neo4jDatabase
|
||||||
|
from .storage_backends import Neo4jStorage, PostgresStorage
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -18,6 +19,10 @@ class FacebookService:
|
|||||||
self.facebook_api_version = facebook_api_version
|
self.facebook_api_version = facebook_api_version
|
||||||
self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}"
|
self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}"
|
||||||
self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection
|
self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection
|
||||||
|
self._storages = [
|
||||||
|
Neo4jStorage(), # always log to Neo4j
|
||||||
|
PostgresStorage(), # always log to Postgres
|
||||||
|
]
|
||||||
|
|
||||||
def reply_to_comment(self, page_id, comment_id, message):
|
def reply_to_comment(self, page_id, comment_id, message):
|
||||||
"""
|
"""
|
||||||
@ -106,8 +111,10 @@ class FacebookService:
|
|||||||
)
|
)
|
||||||
if shared:
|
if shared:
|
||||||
self._store_interaction(
|
self._store_interaction(
|
||||||
page_id,
|
page_id = page_id,
|
||||||
user_message=description or "Shared post comment",
|
object_id = post_id,
|
||||||
|
parent_object_id = parent_id,
|
||||||
|
prompt = prompt,
|
||||||
bot_response = bot_response,
|
bot_response = bot_response,
|
||||||
platform = "Facebook"
|
platform = "Facebook"
|
||||||
)
|
)
|
||||||
@ -117,10 +124,12 @@ class FacebookService:
|
|||||||
orig = self._post_facebook_comment(parent_id, bot_response, page_access_token)
|
orig = self._post_facebook_comment(parent_id, bot_response, page_access_token)
|
||||||
if orig:
|
if orig:
|
||||||
self._store_interaction(
|
self._store_interaction(
|
||||||
page_id,
|
page_id = page_id,
|
||||||
user_message=description or "Original post comment",
|
object_id = post_id,
|
||||||
|
parent_object_id = parent_id,
|
||||||
|
prompt = prompt,
|
||||||
bot_response = bot_response,
|
bot_response = bot_response,
|
||||||
platform="Facebook (Original Post)"
|
platform = "Facebook"
|
||||||
)
|
)
|
||||||
|
|
||||||
return shared
|
return shared
|
||||||
@ -153,23 +162,24 @@ class FacebookService:
|
|||||||
def _store_interaction(
|
def _store_interaction(
|
||||||
self,
|
self,
|
||||||
page_id: str,
|
page_id: str,
|
||||||
user_message: str,
|
object_id: str,
|
||||||
|
parent_object_id: str | None,
|
||||||
|
prompt: str,
|
||||||
bot_response: str,
|
bot_response: str,
|
||||||
platform: str
|
platform: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Encapsulates the repeated Neo4j store_interaction calls.
|
Dispatch this interaction to all registered storage backends.
|
||||||
"""
|
"""
|
||||||
try:
|
for backend in self._storages:
|
||||||
self.neo4j_db.store_interaction(
|
backend.store(
|
||||||
user_id = f"fb_bot_{page_id}",
|
page_id=page_id,
|
||||||
bot_id = f"fb_bot_{page_id}",
|
object_id=object_id,
|
||||||
user_message = user_message,
|
parent_object_id=parent_object_id,
|
||||||
|
prompt=prompt,
|
||||||
bot_response=bot_response,
|
bot_response=bot_response,
|
||||||
platform=platform
|
platform=platform
|
||||||
)
|
)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error logging interaction for page {page_id}: {e}")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
46
pxy_meta_pages/storage_backends.py
Normal file
46
pxy_meta_pages/storage_backends.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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}")
|
Loading…
x
Reference in New Issue
Block a user