2024-01-04 15:57:13 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
from langchain.embeddings.ollama import OllamaEmbeddings
|
|
|
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
|
|
from logger import get_logger
|
2023-08-02 00:03:47 +03:00
|
|
|
from models.databases.supabase.supabase import SupabaseDB
|
2024-01-04 15:57:13 +03:00
|
|
|
from posthog import Posthog
|
2024-02-15 01:01:35 +03:00
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
2023-07-10 15:27:49 +03:00
|
|
|
from supabase.client import Client, create_client
|
2023-06-19 23:46:25 +03:00
|
|
|
from vectorstore.supabase import SupabaseVectorStore
|
2023-06-19 22:20:08 +03:00
|
|
|
|
2023-11-29 21:17:16 +03:00
|
|
|
logger = get_logger(__name__)
|
2023-06-19 22:20:08 +03:00
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
|
2023-07-20 19:17:55 +03:00
|
|
|
class BrainRateLimiting(BaseSettings):
|
2024-02-15 01:01:35 +03:00
|
|
|
model_config = SettingsConfigDict(validate_default=False)
|
2023-07-24 15:17:33 +03:00
|
|
|
max_brain_per_user: int = 5
|
2023-07-20 19:17:55 +03:00
|
|
|
|
|
|
|
|
2024-01-04 15:57:13 +03:00
|
|
|
# The `PostHogSettings` class is used to initialize and interact with the PostHog analytics service.
|
|
|
|
class PostHogSettings(BaseSettings):
|
2024-02-15 01:01:35 +03:00
|
|
|
model_config = SettingsConfigDict(validate_default=False)
|
2024-01-04 15:57:13 +03:00
|
|
|
posthog_api_key: str = None
|
|
|
|
posthog_api_url: str = None
|
|
|
|
posthog: Posthog = None
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
The function initializes the "posthog" attribute and calls the "initialize_posthog" method.
|
|
|
|
"""
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.posthog = None
|
|
|
|
self.initialize_posthog()
|
|
|
|
|
|
|
|
def initialize_posthog(self):
|
|
|
|
"""
|
|
|
|
The function initializes a PostHog client with an API key and URL.
|
|
|
|
"""
|
|
|
|
if self.posthog_api_key and self.posthog_api_url:
|
|
|
|
self.posthog = Posthog(
|
|
|
|
api_key=self.posthog_api_key, host=self.posthog_api_url
|
|
|
|
)
|
|
|
|
|
|
|
|
def log_event(self, user_id: UUID, event_name: str, event_properties: dict):
|
|
|
|
"""
|
|
|
|
The function logs an event with a user ID, event name, and event properties using the PostHog
|
|
|
|
analytics tool.
|
|
|
|
|
|
|
|
:param user_id: The user_id parameter is a UUID (Universally Unique Identifier) that uniquely
|
|
|
|
identifies a user. It is typically used to track and identify individual users in an application
|
|
|
|
or system
|
|
|
|
:type user_id: UUID
|
|
|
|
:param event_name: The event_name parameter is a string that represents the name or type of the
|
|
|
|
event that you want to log. It could be something like "user_signed_up", "item_purchased", or
|
|
|
|
"page_viewed"
|
|
|
|
:type event_name: str
|
|
|
|
:param event_properties: The event_properties parameter is a dictionary that contains additional
|
|
|
|
information or properties related to the event being logged. These properties provide more
|
|
|
|
context or details about the event and can be used for analysis or filtering purposes
|
|
|
|
:type event_properties: dict
|
|
|
|
"""
|
|
|
|
if self.posthog:
|
|
|
|
self.posthog.capture(user_id, event_name, event_properties)
|
|
|
|
|
|
|
|
def set_user_properties(self, user_id: UUID, event_name, properties: dict):
|
|
|
|
"""
|
|
|
|
The function sets user properties for a given user ID and event name using the PostHog analytics
|
|
|
|
tool.
|
|
|
|
|
|
|
|
:param user_id: The user_id parameter is a UUID (Universally Unique Identifier) that uniquely
|
|
|
|
identifies a user. It is used to associate the user with the event and properties being captured
|
|
|
|
:type user_id: UUID
|
|
|
|
:param event_name: The `event_name` parameter is a string that represents the name of the event
|
|
|
|
that you want to capture. It could be something like "user_signed_up" or "item_purchased"
|
|
|
|
:param properties: The `properties` parameter is a dictionary that contains the user properties
|
|
|
|
that you want to set. Each key-value pair in the dictionary represents a user property, where
|
|
|
|
the key is the name of the property and the value is the value you want to set for that property
|
|
|
|
:type properties: dict
|
|
|
|
"""
|
|
|
|
if self.posthog:
|
|
|
|
self.posthog.capture(
|
|
|
|
user_id, event=event_name, properties={"$set": properties}
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_once_user_properties(self, user_id: UUID, event_name, properties: dict):
|
|
|
|
"""
|
|
|
|
The function sets user properties for a specific event, ensuring that the properties are only
|
|
|
|
set once.
|
|
|
|
|
|
|
|
:param user_id: The user_id parameter is a UUID (Universally Unique Identifier) that uniquely
|
|
|
|
identifies a user
|
|
|
|
:type user_id: UUID
|
|
|
|
:param event_name: The `event_name` parameter is a string that represents the name of the event
|
|
|
|
that you want to capture. It could be something like "user_signed_up" or "item_purchased"
|
|
|
|
:param properties: The `properties` parameter is a dictionary that contains the user properties
|
|
|
|
that you want to set. Each key-value pair in the dictionary represents a user property, where
|
|
|
|
the key is the property name and the value is the property value
|
|
|
|
:type properties: dict
|
|
|
|
"""
|
|
|
|
if self.posthog:
|
|
|
|
self.posthog.capture(
|
|
|
|
user_id, event=event_name, properties={"$set_once": properties}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-19 22:20:08 +03:00
|
|
|
class BrainSettings(BaseSettings):
|
2024-02-15 01:01:35 +03:00
|
|
|
model_config = SettingsConfigDict(validate_default=False)
|
|
|
|
openai_api_key: str = ""
|
|
|
|
supabase_url: str = ""
|
|
|
|
supabase_service_key: str = ""
|
2023-07-17 16:45:31 +03:00
|
|
|
resend_api_key: str = "null"
|
2023-07-18 19:10:48 +03:00
|
|
|
resend_email_address: str = "brain@mail.quivr.app"
|
2023-11-29 21:17:16 +03:00
|
|
|
ollama_api_base_url: str = None
|
2024-02-15 01:01:35 +03:00
|
|
|
langfuse_public_key: str = None
|
|
|
|
langfuse_secret_key: str = None
|
2023-06-19 23:46:25 +03:00
|
|
|
|
2023-11-19 20:46:12 +03:00
|
|
|
|
2023-10-23 18:58:45 +03:00
|
|
|
class ResendSettings(BaseSettings):
|
2024-02-15 01:01:35 +03:00
|
|
|
model_config = SettingsConfigDict(validate_default=False)
|
2023-10-23 18:58:45 +03:00
|
|
|
resend_api_key: str = "null"
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-11-19 20:46:12 +03:00
|
|
|
|
2023-08-03 21:24:42 +03:00
|
|
|
def get_supabase_client() -> Client:
|
|
|
|
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
|
|
|
supabase_client: Client = create_client(
|
|
|
|
settings.supabase_url, settings.supabase_service_key
|
|
|
|
)
|
|
|
|
return supabase_client
|
|
|
|
|
|
|
|
|
|
|
|
def get_supabase_db() -> SupabaseDB:
|
|
|
|
supabase_client = get_supabase_client()
|
|
|
|
return SupabaseDB(supabase_client)
|
2023-08-03 17:20:36 +03:00
|
|
|
|
|
|
|
|
2023-11-29 21:17:16 +03:00
|
|
|
def get_embeddings():
|
2023-07-10 15:27:49 +03:00
|
|
|
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
2023-11-29 21:17:16 +03:00
|
|
|
if settings.ollama_api_base_url:
|
|
|
|
embeddings = OllamaEmbeddings(
|
|
|
|
base_url=settings.ollama_api_base_url,
|
|
|
|
) # pyright: ignore reportPrivateUsage=none
|
|
|
|
else:
|
|
|
|
embeddings = OpenAIEmbeddings() # pyright: ignore reportPrivateUsage=none
|
2023-08-03 21:24:42 +03:00
|
|
|
return embeddings
|
|
|
|
|
|
|
|
|
|
|
|
def get_documents_vector_store() -> SupabaseVectorStore:
|
|
|
|
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
|
|
|
embeddings = get_embeddings()
|
2023-06-22 18:50:06 +03:00
|
|
|
supabase_client: Client = create_client(
|
|
|
|
settings.supabase_url, settings.supabase_service_key
|
|
|
|
)
|
2023-06-19 23:46:25 +03:00
|
|
|
documents_vector_store = SupabaseVectorStore(
|
2023-06-22 18:50:06 +03:00
|
|
|
supabase_client, embeddings, table_name="vectors"
|
|
|
|
)
|
2023-08-03 21:24:42 +03:00
|
|
|
return documents_vector_store
|