diff --git a/backend/modules/brain/entity/brain_entity.py b/backend/modules/brain/entity/brain_entity.py index ddb4b717f..fb4f2cb70 100644 --- a/backend/modules/brain/entity/brain_entity.py +++ b/backend/modules/brain/entity/brain_entity.py @@ -2,9 +2,8 @@ from enum import Enum from typing import List, Optional from uuid import UUID -from pydantic import BaseModel - from modules.brain.entity.api_brain_definition_entity import ApiBrainDefinitionEntity +from pydantic import BaseModel class BrainType(str, Enum): diff --git a/backend/modules/brain/repository/brains.py b/backend/modules/brain/repository/brains.py index 575700884..21f426c83 100644 --- a/backend/modules/brain/repository/brains.py +++ b/backend/modules/brain/repository/brains.py @@ -4,8 +4,7 @@ from logger import get_logger from models.settings import get_embeddings, get_supabase_client from modules.brain.dto.inputs import BrainUpdatableProperties from modules.brain.entity.brain_entity import BrainEntity, PublicBrain -from modules.brain.repository.interfaces.brains_interface import \ - BrainsInterface +from modules.brain.repository.interfaces.brains_interface import BrainsInterface logger = get_logger(__name__) diff --git a/backend/modules/brain/repository/brains_users.py b/backend/modules/brain/repository/brains_users.py index 9a8eab63a..6554a2b37 100644 --- a/backend/modules/brain/repository/brains_users.py +++ b/backend/modules/brain/repository/brains_users.py @@ -1,8 +1,7 @@ from uuid import UUID from logger import get_logger -from models.settings import get_supabase_client - +from models.settings import get_embeddings, get_supabase_client from modules.brain.entity.brain_entity import BrainUser, MinimalUserBrainEntity from modules.brain.repository.interfaces.brains_users_interface import ( BrainsUsersInterface, @@ -16,11 +15,28 @@ class BrainsUsers(BrainsUsersInterface): supabase_client = get_supabase_client() self.db = supabase_client + def update_meaning(self, brain: MinimalUserBrainEntity): + embeddings = get_embeddings() + string_to_embed = f"Name: {brain.name} Description: {brain.description}" + brain_meaning = embeddings.embed_query(string_to_embed) + brain_dict = {"meaning": brain_meaning} + response = ( + self.db.table("brains") + .update(brain_dict) + .match({"brain_id": brain.id}) + .execute() + ).data + + if len(response) == 0: + return False + + return True + def get_user_brains(self, user_id) -> list[MinimalUserBrainEntity]: response = ( self.db.from_("brains_users") .select( - "id:brain_id, rights, brains (brain_id, name, status, brain_type, description)" + "id:brain_id, rights, brains (brain_id, name, status, brain_type, description, meaning)" ) .filter("user_id", "eq", user_id) .execute() @@ -40,6 +56,9 @@ class BrainsUsers(BrainsUsersInterface): ) ) user_brains[-1].rights = item["rights"] + if item["brains"]["meaning"] is None: + self.update_meaning(user_brains[-1]) + return user_brains def get_brain_for_user(self, user_id, brain_id):