-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨AI to doc editor #250
Open
AntoLC
wants to merge
6
commits into
main
Choose a base branch
from
feature/ai-blocknote
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
✨AI to doc editor #250
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1df14e
✨(backend) create ai endpoint
AntoLC d303f12
fixup! ✨(backend) create ai endpoint
AntoLC fea99a4
fixup! fixup! ✨(backend) create ai endpoint
sampaccoud 539efe5
✨(frontend) add ai blocknote feature
AntoLC 5ec1372
fixup! ✨(frontend) add ai blocknote feature
AntoLC 978d711
🔧(helm) add ai setting to environments
AntoLC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
decorators, | ||
exceptions, | ||
filters, | ||
metadata, | ||
mixins, | ||
pagination, | ||
status, | ||
|
@@ -31,7 +32,8 @@ | |
response as drf_response, | ||
) | ||
|
||
from core import models | ||
from core import enums, models | ||
from core.services.ai_services import AIService | ||
|
||
from . import permissions, serializers, utils | ||
|
||
|
@@ -303,6 +305,23 @@ def perform_update(self, serializer): | |
serializer.save() | ||
|
||
|
||
class DocumentMetadata(metadata.SimpleMetadata): | ||
"""Custom metadata class to add information""" | ||
|
||
def determine_metadata(self, request, view): | ||
"""Add language choices only for the list endpoint.""" | ||
simple_metadata = super().determine_metadata(request, view) | ||
|
||
if request.path.endswith("/documents/"): | ||
simple_metadata["actions"]["POST"]["language"] = { | ||
"choices": [ | ||
{"value": code, "display_name": name} | ||
for code, name in enums.ALL_LANGUAGES.items() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that true the AI can manage the same set of language as the ones available in the backend? |
||
] | ||
} | ||
return simple_metadata | ||
|
||
|
||
class DocumentViewSet( | ||
ResourceViewsetMixin, | ||
mixins.CreateModelMixin, | ||
|
@@ -320,6 +339,7 @@ class DocumentViewSet( | |
resource_field_name = "document" | ||
queryset = models.Document.objects.all() | ||
ordering = ["-updated_at"] | ||
metadata_class = DocumentMetadata | ||
|
||
def list(self, request, *args, **kwargs): | ||
"""Restrict resources returned by the list endpoint""" | ||
|
@@ -456,10 +476,7 @@ def link_configuration(self, request, *args, **kwargs): | |
serializer = serializers.LinkDocumentSerializer( | ||
document, data=request.data, partial=True | ||
) | ||
if not serializer.is_valid(): | ||
return drf_response.Response( | ||
serializer.errors, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
serializer.is_valid(raise_exception=True) | ||
|
||
serializer.save() | ||
return drf_response.Response(serializer.data, status=status.HTTP_200_OK) | ||
|
@@ -472,10 +489,8 @@ def attachment_upload(self, request, *args, **kwargs): | |
|
||
# Validate metadata in payload | ||
serializer = serializers.FileUploadSerializer(data=request.data) | ||
if not serializer.is_valid(): | ||
return drf_response.Response( | ||
serializer.errors, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
serializer.is_valid(raise_exception=True) | ||
|
||
# Extract the file extension from the original filename | ||
file = serializer.validated_data["file"] | ||
extension = os.path.splitext(file.name)[1] | ||
|
@@ -531,6 +546,63 @@ def retrieve_auth(self, request, *args, **kwargs): | |
request = utils.generate_s3_authorization_headers(f"{pk:s}/{attachment_key:s}") | ||
return drf_response.Response("authorized", headers=request.headers, status=200) | ||
|
||
@decorators.action( | ||
detail=True, | ||
methods=["post"], | ||
name="Apply a transformation action on a piece of text with AI", | ||
url_path="ai-transform", | ||
throttle_classes=[utils.AIDocumentRateThrottle, utils.AIUserRateThrottle], | ||
) | ||
def ai_transform(self, request, *args, **kwargs): | ||
""" | ||
POST /api/v1.0/documents/<resource_id>/ai-transform | ||
with expected data: | ||
- text: str | ||
- action: str [prompt, correct, rephrase, summarize] | ||
Return JSON response with the processed text. | ||
""" | ||
# Check permissions first | ||
self.get_object() | ||
|
||
serializer = serializers.AITransformSerializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
|
||
text = serializer.validated_data["text"] | ||
action = serializer.validated_data["action"] | ||
|
||
response = AIService().transform(text, action) | ||
|
||
return drf_response.Response(response, status=status.HTTP_200_OK) | ||
|
||
@decorators.action( | ||
detail=True, | ||
methods=["post"], | ||
name="Translate a piece of text with AI", | ||
serializer_class=serializers.AITranslateSerializer, | ||
url_path="ai-translate", | ||
throttle_classes=[utils.AIDocumentRateThrottle, utils.AIUserRateThrottle], | ||
) | ||
def ai_translate(self, request, *args, **kwargs): | ||
""" | ||
POST /api/v1.0/documents/<resource_id>/ai-translate | ||
with expected data: | ||
- text: str | ||
- language: str [settings.LANGUAGES] | ||
Return JSON response with the translated text. | ||
""" | ||
# Check permissions first | ||
self.get_object() | ||
|
||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
|
||
text = serializer.validated_data["text"] | ||
language = serializer.validated_data["language"] | ||
|
||
response = AIService().translate(text, language) | ||
|
||
return drf_response.Response(response, status=status.HTTP_200_OK) | ||
|
||
|
||
class DocumentAccessViewSet( | ||
ResourceAccessViewsetMixin, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you want to define a
AIBaseThrottle
to prevent duplicated code here?