Skip to content

Commit

Permalink
Merge pull request #381 from weni-ai/feature/improve-commerce-router
Browse files Browse the repository at this point in the history
Feature/improve commerce router
  • Loading branch information
zMardone authored Dec 26, 2024
2 parents a801116 + 10cb4a6 commit 8ae48a8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 38 deletions.
28 changes: 27 additions & 1 deletion nexus/intelligences/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,34 @@ def test_list(self):
self.assertEqual(response.status_code, 200)

response_json = json.loads(response.content)
instructions = response_json[0].get('instructions', [])

instructions = response_json.get('personalization').get('instructions')

self.assertEqual(len(instructions), 2)
self.assertEqual(instructions[0]['instruction'], 'Try to use emojis')
self.assertEqual(instructions[1]['instruction'], 'Dont change the subject')

@mock.patch('django.conf.settings.DEFAULT_RETAIL_INSTRUCTIONS', ['Try to use emojis', 'Dont change the subject'])
def test_without_links(self):

data = {
"agent": {
"name": "test",
"role": "Doubt analyst",
"personality": "Friendly",
"goal": "Answer user questions"
}
}

request = self.factory.post(self.url, data=data, format='json')
force_authenticate(request, user=self.user)
response = self.view(request, project_uuid=str(self.project.uuid))
response.render()

self.assertEqual(response.status_code, 200)

response_json = json.loads(response.content)
instructions = response_json.get('personalization').get('instructions')

self.assertEqual(len(instructions), 2)
self.assertEqual(response_json.get('links'), None)
129 changes: 93 additions & 36 deletions nexus/intelligences/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,42 @@ def get(self, request, project_uuid):

class RouterRetailViewSet(views.APIView):

def _create_links(
self,
links: list,
user: User,
content_base: ContentBase,
project: Project
) -> list:
created_links = []
if links:
for link in links:

link_serializer = ContentBaseLinkSerializer(data={"link": link})
link_serializer.is_valid(raise_exception=True)
link_dto = intelligences.ContentBaseLinkDTO(
link=link,
user_email=user.email,
content_base_uuid=str(content_base.uuid)
)
content_base_link = intelligences.CreateContentBaseLinkUseCase().create_content_base_link(link_dto)

if project.indexer_database == Project.BEDROCK:
bedrock_send_link.delay(
link=link,
user_email=user.email,
content_base_link_uuid=str(content_base_link.uuid)
)
else:
send_link.delay(
link=link,
user_email=user.email,
content_base_link_uuid=str(content_base_link.uuid)
)

link_serializer = CreatedContentBaseLinkSerializer(content_base_link).data
created_links.append(link_serializer)

# TODO - Refactor this view to have only one searializer and no dependencies
def post(self, request, project_uuid):
user: User = request.user
Expand All @@ -871,44 +907,21 @@ def post(self, request, project_uuid):

project = ProjectsUseCase().get_project_by_content_base_uuid(content_base.uuid)

for link in links:

link_serializer = ContentBaseLinkSerializer(data={"link": link})
link_serializer.is_valid(raise_exception=True)
link_dto = intelligences.ContentBaseLinkDTO(
link=link,
user_email=user.email,
content_base_uuid=str(content_base.uuid)
)
content_base_link = intelligences.CreateContentBaseLinkUseCase().create_content_base_link(link_dto)

if project.indexer_database == Project.BEDROCK:
bedrock_send_link.delay(
link=link,
user_email=user.email,
content_base_link_uuid=str(content_base_link.uuid)
)
else:
send_link.delay(
link=link,
user_email=user.email,
content_base_link_uuid=str(content_base_link.uuid)
)

link_serializer = CreatedContentBaseLinkSerializer(content_base_link).data
created_links = self._create_links(links, user, content_base, project)

# ContentBasePersonalization

agent_data = request.data.get("agent")
default_instructions: list = settings.DEFAULT_RETAIL_INSTRUCTIONS

instructions_objects = []
for instruction in default_instructions:
instructions_objects.append(
{
"instruction": instruction,
}
)
if not content_base.instructions.exists():
default_instructions: list = settings.DEFAULT_RETAIL_INSTRUCTIONS
for instruction in default_instructions:
instructions_objects.append(
{
"instruction": instruction,
}
)

agent = {
"agent": agent_data,
Expand All @@ -929,13 +942,57 @@ def post(self, request, project_uuid):
if personalization_serializer.is_valid():
personalization_serializer.save()

response = [
personalization_serializer.data,
link_serializer
]
response = {
"personalization": personalization_serializer.data,
"links": created_links
}

return Response(response, status=200)

def delete(self, request, project_uuid):

user: User = request.user
module_permission = user.has_perm("users.can_communicate_internally")

if not module_permission:
return Response(status=status.HTTP_401_UNAUTHORIZED)

use_case = intelligences.RetrieveContentBaseUseCase()
content_base = use_case.get_default_by_project(
project_uuid,
user.email,
is_superuser=module_permission
)
content_base_uuid: str = content_base.uuid

use_case = intelligences.RetrieveContentBaseLinkUseCase()
existing_links = use_case.get_content_base_link_by_link(
content_base_uuid=content_base_uuid,
link=request.data.get("link")
)

for link in existing_links:
project_use_case = ProjectsUseCase()
project = project_use_case.get_project_by_content_base_uuid(content_base_uuid)
indexer = project_use_case.get_indexer_database_by_project(project)

use_case = intelligences.DeleteContentBaseLinkUseCase(indexer)
use_case.delete_by_object(
link,
)

if project.indexer_database == Project.BEDROCK:
start_ingestion_job.delay("", post_delete=True)

event_manager.notify(
event="contentbase_link_activity",
action_type="D",
content_base_link=link,
user=user
)

return Response(status=status.HTTP_204_NO_CONTENT)


class LLMViewset(views.APIView):
permission_classes = [IsAuthenticated]
Expand Down
2 changes: 1 addition & 1 deletion nexus/usecases/intelligences/get_by_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_by_content_base_file_uuid(content_base_uuid: str) -> ContentBaseFile:
raise (f"[ ContentBaseFile ] - ContentBaseFile error to get - error: `{exception}`")


def get_by_content_base_link_uuid(content_base_uuid: str) -> ContentBaseFile:
def get_by_content_base_link_uuid(content_base_uuid: str) -> ContentBaseLink:
try:
return ContentBaseLink.objects.get(uuid=content_base_uuid)
except ContentBaseLink.DoesNotExist:
Expand Down
13 changes: 13 additions & 0 deletions nexus/usecases/intelligences/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ContentBaseLinkDoesNotExist,
)
from nexus.projects.permissions import has_project_permission
from nexus.intelligences.models import ContentBaseLink


class RetrieveIntelligenceUseCase():
Expand Down Expand Up @@ -123,6 +124,18 @@ def get_contentbaselink(self, contentbaselink_uuid: str, user_email: str):
raise IntelligencePermissionDenied()
return get_by_content_base_link_uuid(contentbaselink_uuid)

def get_content_base_link_by_link(self, link: str, content_base_uuid: str):

links = ContentBaseLink.objects.filter(
link=link,
content_base__uuid=content_base_uuid
)

if not links.exists():
raise ContentBaseLink.DoesNotExist()

return links


def get_file_info(file_uuid: str) -> Dict:
try:
Expand Down

0 comments on commit 8ae48a8

Please sign in to comment.