-
Notifications
You must be signed in to change notification settings - Fork 2k
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
refactor: Support image_list, document_list while using swagger api #2300
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,13 +222,19 @@ def chat(self, instance: Dict, with_valid=True): | |
client_type = self.data.get('client_type') | ||
chat_id = self.generate_chat(chat_id, application_id, message, client_id) | ||
return ChatMessageSerializer( | ||
data={'chat_id': chat_id, 'message': message, | ||
're_chat': re_chat, | ||
'stream': stream, | ||
'application_id': application_id, | ||
'client_id': client_id, | ||
'client_type': client_type, 'form_data': instance.get('form_data', {})}).chat( | ||
base_to_response=OpenaiToResponse()) | ||
data={ | ||
'chat_id': chat_id, 'message': message, | ||
're_chat': re_chat, | ||
'stream': stream, | ||
'application_id': application_id, | ||
'client_id': client_id, | ||
'client_type': client_type, | ||
'form_data': instance.get('form_data', {}), | ||
'image_list': instance.get('image_list', []), | ||
'document_list': instance.get('document_list', []), | ||
'audio_list': instance.get('audio_list', []), | ||
} | ||
).chat(base_to_response=OpenaiToResponse()) | ||
|
||
|
||
class ChatMessageSerializer(serializers.Serializer): | ||
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. The provided code makes a few adjustments to improve readability and maintainability: Changes Made
Suggested OptimizationThere is no significant performance issue mentioned in the current context; however, ensure that all list comprehensions and loops within this function are efficient and optimized if needed for larger datasets. Potential Issues
By addressing these points, the code can become more robust, concise, and easier to maintain. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ class FileSerializer(serializers.Serializer): | |
def upload(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
meta = self.data.get('meta') | ||
meta = self.data.get('meta', {'debug': True}) | ||
file_id = meta.get('file_id', uuid.uuid1()) | ||
file = File(id=file_id, file_name=self.data.get('file').name, meta=meta) | ||
file.save(self.data.get('file').read()) | ||
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. The code looks generally correct but has a few minor points to consider:
These changes ensure robustness against missing keys and clarity in how ID generation works. 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. The code appears to be mostly correct, but there are a few improvements or notes that can be made:
By making these minor enhancements, you can make the code more robust, maintainable, and future-proof. |
||
|
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.
The provided code has several potential issues and areas for improvement:
Data Structure Changes:
The
instance.get('form_data', {})
is being used multiple times, but changes may have occurred to this property over time (e.g., addition of new keys like 'image_list'). Consider usingself.data
instead since it reflects the final serialized data that includes all fields.Code Duplication:
The last three calls to
instance.get
are repeated. Use a list comprehension to avoid duplication if there are more similar operations planned.Variable Naming:
Using short variable names (
with_valid
,chat_id
, etc.) can make the code harder to read. Consider renaming them to meaningful descriptions, such asinclude_validation
,generated_chat_id
.Optional Parameters:
Instead of explicitly setting the values to an empty dictionary when they aren't present (
{} for k in ['image_list', 'document_list', 'audio_list']
), consider checking for existence before adding each value to the serializer's data dictionary.Here's an optimized version of the function with these improvements:
This refactoring makes the code cleaner and easier to maintain, ensuring that only necessary data attributes are included and avoiding redundant checks for list existentials.