-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Chat log add to knowledge permission #3966
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
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 |
|---|---|---|
|
|
@@ -129,7 +129,7 @@ class ApplicationChatRecordAddKnowledge(APIView): | |
| RoleConstants.WORKSPACE_MANAGE.get_workspace_role()) | ||
| def post(self, request: Request, workspace_id: str, application_id: str): | ||
| return result.success(ApplicationChatRecordAddKnowledgeSerializer().post_improve( | ||
| {'workspace_id': workspace_id, 'application_id': application_id, **request.data})) | ||
| {'workspace_id': workspace_id, 'application_id': application_id, **request.data}, request=request)) | ||
|
|
||
|
|
||
| class ApplicationChatRecordImprove(APIView): | ||
|
|
@@ -186,7 +186,7 @@ def put(self, request: Request, | |
| return result.success(ApplicationChatRecordImproveSerializer( | ||
| data={'workspace_id': workspace_id, 'application_id': application_id, 'chat_id': chat_id, | ||
| 'chat_record_id': chat_record_id, | ||
| 'knowledge_id': knowledge_id, 'document_id': document_id}).improve(request.data)) | ||
| 'knowledge_id': knowledge_id, 'document_id': document_id}).improve(request.data, request=request)) | ||
|
|
||
| class Operate(APIView): | ||
| authentication_classes = [TokenAuth] | ||
|
|
@@ -214,4 +214,4 @@ def delete(self, request: Request, workspace_id: str, application_id: str, chat_ | |
| data={'chat_id': chat_id, 'chat_record_id': chat_record_id, 'workspace_id': workspace_id, | ||
| 'application_id': application_id, | ||
| 'knowledge_id': knowledge_id, 'document_id': document_id, | ||
| 'paragraph_id': paragraph_id}).delete()) | ||
| 'paragraph_id': paragraph_id}).delete(request=request)) | ||
|
Contributor
Author
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 for an API view class to handle adding, improving, updating, and deleting knowledge records in the
Here's a revised version of the code incorporating these suggestions: from typing import Dict
class ApplicationChatRecordAddKnowledge(APIView):
def post(self, request: Request, workspace_id: str, application_id: str) -> JsonResponse:
serializer = ApplicationChatRecordAddKnowledgeSerializer()
try:
response_data = serializer.post_improve({'workspace_id': workspace_id, 'application_id': application_id, **request.data})
return Response(result.success(response_data), status=status.HTTP_201_CREATED)
except Exception as e:
# Handle errors (log, retry logic, etc.)
return Response({"error": "Something went wrong"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class ApplicationChatRecordImprove(APIView):
@staticmethod
async def put(request: Request, workspace_id: str, application_id: str, chat_id: int, chat_record_id: int, knowledge_id: int) -> JsonResponse:
serializer = ApplicationChatRecordImproveSerializer(data={
'workspace_id': workspace_id,
'application_id': application_id,
'chat_id': chat_id,
'chat_record_id': chat_record_id,
'knowledge_id': knowledge_id,
}, request=request)
try:
result_data = await serializer.improve(request.data)
return Response(result.success(result_data), status=status.HTTP_200_OK)
except Exception as e:
return Response({"error": "Improvement failed"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class ApplicationChatRecordOperate(OperateAPIView): # Assuming OperateAPIView is defined elsewhere
authentication_classes = [TokenAuth]
@staticmethod
async def delete(request: Request, workspace_id: str, application_id: str, chat_id: int, chat_record_id: int, knowledge_id: int, paragraph_id: int) -> JsonResponse:
data = {
'chat_id': chat_id,
'chat_record_id': chat_record_id,
'workspace_id': workspace_id,
'application_id': application_id,
'knowledge_id': knowledge_id,
'paragraph_id': paragraph_id,
}
try:
response_data = await self.delete_request(request, "your_endpoint_url", json=data, verify=False)
if response_data['status'] == 200:
return Response({"message": "Deleted successfully"}, status=status.HTTP_200_OK)
else:
raise HttpResponseBadRequest("Failed to process deletion")
except requests.RequestException as e:
logger.error(f"Deletion failed due to {e}")
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)Key Changes:
These changes should improve the robustness and readability of the code. |
||
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 improvements and corrections:
Import Statements: Ensure that all necessary modules are imported at the beginning of the file.
Transaction Management: Consider using
@atomicdecorator instead of manually handling transactions withtransaction.atomic()for better readability.Scope Handling: The scope parameter can be removed since most functions use it conditionally based on the context.
Permissions Check: Simplify the permissions check logic by extracting it into a separate method. This makes the main methods cleaner.
Error Handling Messages: Use string interpolation or gettext for more readable messages.
Here's an optimized version of the code:
Key changes:
_check_permissionsstatic method.with_validfrom the methods.