feat: optimization calls (#2417)

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
This commit is contained in:
Stan Girard 2024-04-10 02:45:16 -07:00 committed by GitHub
parent 43a2775f3c
commit 82e0dd9342
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 41 deletions

View File

@ -10,6 +10,7 @@ class IntegrationType(str, Enum):
SYNC = "sync"
DOC = "doc"
class IntegrationBrainTag(str, Enum):
NEW = "new"
RECOMMENDED = "recommended"

View File

@ -59,9 +59,7 @@ class BrainService:
return self.brain_repository.get_brain_by_id(brain_id)
def get_integration_brain(self, brain_id) -> IntegrationEntity | None:
return self.integration_brains_repository.get_integration_brain(
brain_id
)
return self.integration_brains_repository.get_integration_brain(brain_id)
def find_brain_from_question(
self,
@ -105,22 +103,23 @@ class BrainService:
if brain_id_to_use and not brain_to_use:
brain_to_use = self.get_brain_by_id(brain_id_to_use)
# Calculate the closest brains to the question
list_brains = vector_store.find_brain_closest_query(user.id, question)
else:
# Calculate the closest brains to the question
list_brains = vector_store.find_brain_closest_query(user.id, question)
unique_list_brains = []
seen_brain_ids = set()
unique_list_brains = []
seen_brain_ids = set()
for brain in list_brains:
if brain["id"] not in seen_brain_ids:
unique_list_brains.append(brain)
seen_brain_ids.add(brain["id"])
for brain in list_brains:
if brain["id"] not in seen_brain_ids:
unique_list_brains.append(brain)
seen_brain_ids.add(brain["id"])
metadata["close_brains"] = unique_list_brains[:5]
metadata["close_brains"] = unique_list_brains[:5]
if list_brains and not brain_to_use:
brain_id_to_use = list_brains[0]["id"]
brain_to_use = self.get_brain_by_id(brain_id_to_use)
if list_brains and not brain_to_use:
brain_id_to_use = list_brains[0]["id"]
brain_to_use = self.get_brain_by_id(brain_id_to_use)
return brain_to_use, metadata
@ -323,7 +322,9 @@ class BrainService:
def update_brain_last_update_time(self, brain_id: UUID):
self.brain_repository.update_brain_last_update_time(brain_id)
def get_brain_details(self, brain_id: UUID, user_id: UUID = None) -> BrainEntity | None:
def get_brain_details(
self, brain_id: UUID, user_id: UUID = None
) -> BrainEntity | None:
brain = self.brain_repository.get_brain_details(brain_id)
if brain == None:
return None
@ -334,14 +335,14 @@ class BrainService:
brain_id, user_id
)
)
if (brain.integration):
if brain.integration:
brain.integration_description = (
self.integration_description_repository.get_integration_description(
brain.integration.integration_id
)
)
return brain
def get_connected_brains(self, brain_id: UUID) -> list[BrainEntity]:

View File

@ -61,7 +61,6 @@ def get_answer_generator(
current_user: UserIdentity,
):
chat_instance = BrainfulChat()
chat_instance.validate_authorization(user_id=current_user.id, brain_id=brain_id)
user_usage = UserUsage(
id=current_user.id,
@ -247,11 +246,6 @@ async def create_stream_question_handler(
chat_instance = BrainfulChat()
chat_instance.validate_authorization(user_id=current_user.id, brain_id=brain_id)
user_usage = UserUsage(
id=current_user.id,
email=current_user.email,
)
logger.info(
f"Creating question for chat {chat_id} with brain {brain_id} of type {type(brain_id)}"
)

View File

@ -1,10 +1,7 @@
import time
from fastapi import APIRouter, Depends, Request
from middlewares.auth import AuthBearer, get_current_user
from models import UserUsage
from modules.brain.service.brain_user_service import BrainUserService
from modules.brain.service.brain_vector_service import BrainVectorService
from modules.user.dto.inputs import UserUpdatableProperties
from modules.user.entity.user_identity import UserIdentity
from modules.user.repository.users import Users
@ -37,26 +34,16 @@ async def get_user_endpoint(
user_settings = user_daily_usage.get_user_settings()
max_brain_size = user_settings.get("max_brain_size", 1000000000)
date = time.strftime("%Y%m%d")
monthly_chat_credit = user_settings.get("monthly_chat_credit", 10)
user_daily_usage = UserUsage(id=current_user.id)
requests_stats = user_daily_usage.get_user_usage()
default_brain = brain_user_service.get_user_default_brain(current_user.id)
if default_brain:
defaul_brain_size = BrainVectorService(default_brain.brain_id).brain_size
else:
defaul_brain_size = 0
return {
"email": current_user.email,
"max_brain_size": max_brain_size,
"current_brain_size": defaul_brain_size,
"current_brain_size": 0,
"monthly_chat_credit": monthly_chat_credit,
"requests_stats": requests_stats,
"models": user_settings.get("models", []),
"date": date,
"id": current_user.id,
"is_premium": user_settings["is_premium"],
}

View File

@ -39,7 +39,7 @@ if (
// This wrapper is used to make effect calls at a high level in app rendering.
const App = ({ children }: PropsWithChildren): JSX.Element => {
const { fetchAllBrains, fetchPublicPrompts } = useBrainContext();
const { fetchAllBrains } = useBrainContext();
const { onClickOutside } = useOutsideClickListener();
const { session } = useSupabase();
@ -49,7 +49,6 @@ const App = ({ children }: PropsWithChildren): JSX.Element => {
if (session?.user) {
void fetchAllBrains();
void fetchPublicPrompts();
posthog.identify(session.user.id, { email: session.user.email });
posthog.startSessionRecording();
}